% Pygame % % Joe Naegele
- Background
- API
- Demo
Python library designed for writing games
- Written by Pete Shinners
- Can be used for more than just games
- Old and not very "Pythonic"
- Cross-platform
- Does not require OpenGL
- Runs on Android but not iOS
- Small codebase
- Easy to learn
- Simple to use
- Uses optimized C and assembly code for its core functions
- Significantly faster than pure Python code
- Relatively slower than most libraries/languages
-
Well-established (since 1998)
-
Cross-platform C library
-
Provides low-level access to hardware:
- audio
- graphics
- keyboard, mouse, joystick
- Built on OpenGL
- More "Pythonic" API
- Smaller community
- Young, modern, well-designed
- Much larger UI library, more than just games
- Runs on iOS
- Extensive but easy to use
- Slow but not too slow
- Great for prototyping!
You can make great games with Pygame!
<iframe width="640" height="360" src="proxy.php?url=https%3A%2F%2Fwww.github.com%2F%3Ca+href%3D"https://www.youtube.com/embed/chZZpo85b-g?rel=0" rel="nofollow">https://www.youtube.com/embed/chZZpo85b-g?rel=0" frameborder="0" allowfullscreen></iframe>- http://pygame.org/docs/
- Read the Newbie Guide
- "Learn" section of the website - books and tutorials
Pygame ships with a number of small examples
python -mpygame.examples.chimpAt a very basic level, we need the ability to:
- draw on the screen
- handle user input
- maybe play sounds
You must write the "main" loop
- manually process events
- update game logic
- render frame
Simplest approach: do it all in one loop
def main():
while True:
for event in events:
handle(event)
for entity in game:
entity.update()
for entity in game:
entity.render()Contrast this with Pyglet:
def main():
win = pyglet.window.Window()
@win.event
def on_draw():
win.clear()
entity.render()
def update(dt):
entity.update()
pyglet.clock.schedule_interval(update, 0.2)
pyglet.app.run()pygame.init()
pygame.display.init()pygame.display.set_mode(resolution, flags=0, ...)- flags: FULLSCREEN, OPENGL, RESIZABLE, etc.
pygame.display.update()python -mpygame.examples.glcube
- object for representing images, including display
- like a "blank piece of paper"
pygame.Surface.blit- draw one image onto anotherpygame.Surface.fill- fill with solid colorpygame.Surface.set_alpha- set transparency levelpython -mpygame.examples.scroll
- key presses
- mouse motion and clicks
- joystick manipulation
- window resize
- Event queue must be flushed continuously:
for event in pygame.event.get(): ... python -mpygame.examples.eventlist
pygame.sprite.Spritepygame.sprite.Grouppygame.sprite.collide_{rect, circle, mask}python -mpygame.examples.testsprite
pygame.font.initpygame.font.Font(filename)python -mpygame.examples.fontypython -mpygame.examples.freetype_misc
pygame.image.load(filename) -> Surfacepygame.image.load(fileobj) -> Surfacepygame.image.save(Surface, filename)python -mpygame.examples.moveit
pygame.mixer.init(frequency, size, channels, buffer)pygame.mixer.Channelpygame.mixer.Soundpython -mpygame.examples.sound
pygame.time.wait(sleep)pygame.time.Clock
- "Rects are your friends"
- Lowly rectangle object
- Used by much of Pygame's API
pygame.Rect(left, top, width, height)pygame.Rect((left, top), (width, height))- etc.
(borrowed from the Newbie Guide)
Use surface.convert() after loading an image for up to 6x increase in blitting speed
background = pygame.image.load("background.png")
background.convert()This changes the surface's pixel format to match that of the display
Only update "dirty" rects
pygame.display.update() updates the entire screen, which isn't always what you want
Pass a Rect or list of Rects to pygame.display.update()
- Not useful for things like side-scrollers
- Not needed for low-framerate games/apps
- Don't bother with per-pixel collision detection
- Python is too slow
- Just use "sub-rect" collision
2 methods of determining state...
-
Directly check state of the device:
pygame.key.get_pressed()pygame.mouse.get_pressed()pygame.mouse.get_pos()
-
Monitor event queue:
for event in pygame.event.get(): if event.type == ...
KEYDOWN,KEYUPMOUSEMOTION,MOUSEBUTTONDOWN,MOUSEBUTTONUP
QANAT - Galaxians-like SHMUP
by Paul Patterson
<iframe width="640" height="360" src="proxy.php?url=https%3A%2F%2Fwww.github.com%2F%3Ca+href%3D"https://www.youtube.com/embed/wxebbXCEQko?rel=0" rel="nofollow">https://www.youtube.com/embed/wxebbXCEQko?rel=0" frameborder="0" allowfullscreen></iframe>by Paul Patterson
<iframe width="640" height="360" src="proxy.php?url=https%3A%2F%2Fwww.github.com%2F%3Ca+href%3D"https://www.youtube.com/embed/LwbVzT7aGAE?rel=0" rel="nofollow">https://www.youtube.com/embed/LwbVzT7aGAE?rel=0" frameborder="0" allowfullscreen></iframe>by Phil Hassey
<iframe width="640" height="360" src="proxy.php?url=https%3A%2F%2Fwww.github.com%2F%3Ca+href%3D"https://www.youtube.com/embed/chZZpo85b-g?rel=0" rel="nofollow">https://www.youtube.com/embed/chZZpo85b-g?rel=0" frameborder="0" allowfullscreen></iframe>Ardentryst - RPG
by Jordan Trudgett
<iframe width="640" height="360" src="proxy.php?url=https%3A%2F%2Fwww.github.com%2F%3Ca+href%3D"https://www.youtube.com/embed/uBGtm_r6_mY?rel=0" rel="nofollow">https://www.youtube.com/embed/uBGtm_r6_mY?rel=0" frameborder="0" allowfullscreen></iframe>Frets on Fire - Guitar Hero clone
by Sami Kyöstilä
<iframe width="640" height="360" src="proxy.php?url=https%3A%2F%2Fwww.github.com%2F%3Ca+href%3D"https://www.youtube.com/embed/c5i6SxSAY4Q?rel=0" rel="nofollow">https://www.youtube.com/embed/c5i6SxSAY4Q?rel=0" frameborder="0" allowfullscreen></iframe>Void Infinity - Space RTS
by Jeremy Gagnier
<iframe width="640" height="360" src="proxy.php?url=https%3A%2F%2Fwww.github.com%2F%3Ca+href%3D"https://www.youtube.com/embed/Pino_AhZI_Y?t=9m10s&rel=0" rel="nofollow">https://www.youtube.com/embed/Pino_AhZI_Y?t=9m10s&rel=0" frameborder="0" allowfullscreen></iframe>- StarPusher - Puzzle game by Al Sweigart
- Ulmo's Adventure - Zelda-like adventure game by Sam Eldred
- SolarWolf - Arcade shooter
- Albow - GUI library by Gregory Ewing
- Import/Initialize
- Load resources
- Main loop
- event loop
- update
- draw