~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to examples/moveit.py

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""
 
4
This is the full and final example from the Pygame Tutorial,
 
5
"How Do I Make It Move". It creates 10 objects and animates
 
6
them on the screen.
 
7
 
 
8
Note it's a bit scant on error checking, but it's easy to read. :]
 
9
Fortunately, this is python, and we needn't wrestle with a pile of
 
10
error codes.
 
11
"""
 
12
 
 
13
 
 
14
#import everything
 
15
import os, pygame
 
16
from pygame.locals import *
 
17
 
 
18
#our game object class
 
19
class GameObject:
 
20
    def __init__(self, image, height, speed):
 
21
        self.speed = speed
 
22
        self.image = image
 
23
        self.pos = image.get_rect().move(0, height)
 
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
 
31
def load_image(name):
 
32
    path = os.path.join('data', name)
 
33
    return pygame.image.load(path).convert()
 
34
 
 
35
 
 
36
#here's the full code
 
37
def main():
 
38
    pygame.init()
 
39
    screen = pygame.display.set_mode((640, 480))
 
40
 
 
41
    player = load_image('player1.gif')
 
42
    background = load_image('liquid.bmp')
 
43
    screen.blit(background, (0, 0))
 
44
 
 
45
    objects = []
 
46
    for x in range(10):
 
47
        o = GameObject(player, x*40, x)
 
48
        objects.append(o)
 
49
 
 
50
    while 1:
 
51
        for event in pygame.event.get():
 
52
            if event.type in (QUIT, KEYDOWN):
 
53
                return
 
54
 
 
55
        for o in objects:
 
56
            screen.blit(background, o.pos, o.pos)
 
57
        for o in objects:
 
58
            o.move()
 
59
            screen.blit(o.image, o.pos)
 
60
 
 
61
        pygame.display.update()
 
62
 
 
63
 
 
64
 
 
65
if __name__ == '__main__': main()