Skip to content

Latest commit

 

History

History

README.md

% Pygame % % Joe Naegele

Pygame

http://pygame.org

Pygame

  • Background
  • API
  • Demo

Pygame

Python library designed for writing games

  • Written by Pete Shinners
  • Can be used for more than just games
  • Old and not very "Pythonic"

Pygame

  • Cross-platform
  • Does not require OpenGL
  • Runs on Android but not iOS

Pygame

  • Small codebase
  • Easy to learn
  • Simple to use

Pygame

  • Uses optimized C and assembly code for its core functions
  • Significantly faster than pure Python code
  • Relatively slower than most libraries/languages

SDL

Simple DirectMedia Layer

  • Well-established (since 1998)

  • Cross-platform C library

  • Provides low-level access to hardware:

    • audio
    • graphics
    • keyboard, mouse, joystick

Alternatives

Pyglet

http://pyglet.org

  • Built on OpenGL
  • More "Pythonic" API
  • Smaller community

Kivy

http://kivy.org

  • Young, modern, well-designed
  • Much larger UI library, more than just games
  • Runs on iOS

Recap

  • Extensive but easy to use
  • Slow but not too slow
  • Great for prototyping!

Powerful

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>

Documentation

Examples

Pygame ships with a number of small examples

python -mpygame.examples.chimp

Making games

At a very basic level, we need the ability to:

  • draw on the screen
  • handle user input
  • maybe play sounds

Game Loop

You must write the "main" loop

  • manually process events
  • update game logic
  • render frame

Game Loop

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()

Pyglet

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()

API

Initialize

  • pygame.init()

Display

  • pygame.display.init()
  • pygame.display.set_mode(resolution, flags=0, ...)
  • flags: FULLSCREEN, OPENGL, RESIZABLE, etc.
  • pygame.display.update()
  • python -mpygame.examples.glcube

Surface

  • object for representing images, including display
  • like a "blank piece of paper"
  • pygame.Surface.blit - draw one image onto another
  • pygame.Surface.fill - fill with solid color
  • pygame.Surface.set_alpha - set transparency level
  • python -mpygame.examples.scroll

Events

  • 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

Sprites

  • pygame.sprite.Sprite
  • pygame.sprite.Group
  • pygame.sprite.collide_{rect, circle, mask}
  • python -mpygame.examples.testsprite

Fonts

  • pygame.font.init
  • pygame.font.Font(filename)
  • python -mpygame.examples.fonty
  • python -mpygame.examples.freetype_misc

Images

  • pygame.image.load(filename) -> Surface
  • pygame.image.load(fileobj) -> Surface
  • pygame.image.save(Surface, filename)
  • python -mpygame.examples.moveit

Mixer

  • pygame.mixer.init(frequency, size, channels, buffer)
  • pygame.mixer.Channel
  • pygame.mixer.Sound
  • python -mpygame.examples.sound

Time

  • pygame.time.wait (sleep)
  • pygame.time.Clock

Rect

  • "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.

Tips

(borrowed from the Newbie Guide)

Loading Images

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

"Dirty" Rects

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

Collision Detection

  • Don't bother with per-pixel collision detection
  • Python is too slow
  • Just use "sub-rect" collision

Input Handling

2 methods of determining state...

Input Handling

  1. Directly check state of the device:

    • pygame.key.get_pressed()
    • pygame.mouse.get_pressed()
    • pygame.mouse.get_pos()

Input Handling

  1. Monitor event queue:

    for event in pygame.event.get():
        if event.type == ...
    • KEYDOWN, KEYUP
    • MOUSEMOTION, MOUSEBUTTONDOWN, MOUSEBUTTONUP

Showcase


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>

SubTerrex

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>

Barbie Seahorse Adventures

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>

Others

Others

Demo

  1. Import/Initialize
  2. Load resources
  3. Main loop
    • event loop
    • update
    • draw

Basics

Snake