pygame / examples /

#!/usr/bin/env python

This is the full and final example from the Pygame Tutorial,
"How Do I Make It Move". It creates 10 objects and animates
them on the screen.

Note it's a bit scant on error checking, but it's easy to read. :]
Fortunately, this is python, and we needn't wrestle with a pile of
error codes.

__docformat__ = 'restructuredtext'
__version__ = '$Id: $'

from copy import copy
import os
import sys

from SDL import *
from SDL.image import *

#our game object class
class GameObject:
    def __init__(self, image, height, speed):
        self.speed = speed
        self.image = image
        self.pos = SDL_Rect(0, height, image.w, image.h)
    def move(self):
        self.pos.x += self.speed
        if self.pos.x + self.pos.w > 600:
            self.pos.x = 0

#quick function to load an image
def load_image(name):
    path = os.path.join(os.path.dirname(sys.argv[0]), 
                        os.path.join('data', name))
    tmp = IMG_Load(path)
    img = SDL_DisplayFormat(tmp)
    return img

#here's the full code
def main():
    screen = SDL_SetVideoMode(640, 480, 0, 0)

    player = load_image('player1.gif')
    background = load_image('liquid.bmp')

    # scale the background image so that it fills the window and
    #   successfully overwrites the old sprite position.
    scaled = SDL_CreateRGBSurface(SDL_SWSURFACE, 
                                  background.w * 4,
                                  background.h * 4,
    scaled_buf = scaled.pixels
    scaled_pitch = scaled.pitch / scaled.format.BytesPerPixel
    background_buf = background.pixels
    background_pitch = background.pitch / background.format.BytesPerPixel
    for y in range(scaled.h):
        for x in range(scaled.w):
            scaled_buf[y * scaled_pitch + x] = \
                background_buf[(y/4) * background_pitch  + x / 4]
    background = scaled
    #background = pygame.transform.scale2x(background)
    #background = pygame.transform.scale2x(background)

    SDL_BlitSurface(background, None, screen, None)

    objects = []
    for x in range(10):
        o = GameObject(player, x*40, x)

    frames = 0
    then = SDL_GetTicks()
    while 1:
        frames += 1
        event = SDL_PollEventAndReturn()
        while event:
            if event.type in (SDL_QUIT, SDL_KEYDOWN):
                time = SDL_GetTicks() - then
                print '%d frames in %.2f seconds' % (frames, time / 1000.0)
                print '  %f milliseconds per frame (%d FPS)' % \
                    (float(time) / frames, frames * 1000 / time)
            event = SDL_PollEventAndReturn()

        for o in objects:
            SDL_BlitSurface(background, copy(o.pos), screen, copy(o.pos))
        for o in objects:
            SDL_BlitSurface(o.image, None, screen, copy(o.pos))

        SDL_UpdateRect(screen, 0, 0, 0, 0)

if __name__ == '__main__': main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.