1#!/usr/bin/env python 2""" pygame.examples.moveit 3 4This is the full and final example from the Pygame Tutorial, 5"How Do I Make It Move". It creates 10 objects and animates 6them on the screen. 7 8Note it's a bit scant on error checking, but it's easy to read. :] 9Fortunately, this is python, and we needn't wrestle with a pile of 10error codes. 11""" 12import os 13import pygame as pg 14 15main_dir = os.path.split(os.path.abspath(__file__))[0] 16 17# our game object class 18class GameObject: 19 def __init__(self, image, height, speed): 20 self.speed = speed 21 self.image = image 22 self.pos = image.get_rect().move(0, height) 23 24 def move(self): 25 self.pos = self.pos.move(self.speed, 0) 26 if self.pos.right > 600: 27 self.pos.left = 0 28 29 30# quick function to load an image 31def load_image(name): 32 path = os.path.join(main_dir, "data", name) 33 return pg.image.load(path).convert() 34 35 36# here's the full code 37def main(): 38 pg.init() 39 screen = pg.display.set_mode((640, 480)) 40 41 player = load_image("player1.gif") 42 background = load_image("liquid.bmp") 43 44 # scale the background image so that it fills the window and 45 # successfully overwrites the old sprite position. 46 background = pg.transform.scale2x(background) 47 background = pg.transform.scale2x(background) 48 49 screen.blit(background, (0, 0)) 50 51 objects = [] 52 for x in range(10): 53 o = GameObject(player, x * 40, x) 54 objects.append(o) 55 56 while 1: 57 for event in pg.event.get(): 58 if event.type in (pg.QUIT, pg.KEYDOWN): 59 return 60 61 for o in objects: 62 screen.blit(background, o.pos, o.pos) 63 for o in objects: 64 o.move() 65 screen.blit(o.image, o.pos) 66 67 pg.display.update() 68 69 70if __name__ == "__main__": 71 main() 72 pg.quit() 73