~ubuntu-branches/ubuntu/maverick/pygame/maverick

« back to all changes in this revision

Viewing changes to examples/chimp.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
"""
3
3
This simple example is used for the line-by-line tutorial
4
4
that comes with pygame. It is based on a 'popular' web banner.
5
 
Note there are comments here, but for the full explanation, 
 
5
Note there are comments here, but for the full explanation,
6
6
follow along in the tutorial.
7
7
"""
8
8
 
10
10
#Import Modules
11
11
import os, pygame
12
12
from pygame.locals import *
13
 
 
14
 
if not pygame.font: print 'Warning, fonts disabled'
15
 
if not pygame.mixer: print 'Warning, sound disabled'
16
 
 
 
13
from pygame.compat import geterror
 
14
 
 
15
if not pygame.font: print ('Warning, fonts disabled')
 
16
if not pygame.mixer: print ('Warning, sound disabled')
 
17
 
 
18
main_dir = os.path.split(os.path.abspath(__file__))[0]
 
19
data_dir = os.path.join(main_dir, 'data')
17
20
 
18
21
#functions to create our resources
19
22
def load_image(name, colorkey=None):
20
 
    fullname = os.path.join('data', name)
 
23
    fullname = os.path.join(data_dir, name)
21
24
    try:
22
25
        image = pygame.image.load(fullname)
23
 
    except pygame.error, message:
24
 
        print 'Cannot load image:', fullname
25
 
        raise SystemExit, message
 
26
    except pygame.error:
 
27
        print ('Cannot load image:', fullname)
 
28
        raise SystemExit(str(geterror()))
26
29
    image = image.convert()
27
30
    if colorkey is not None:
28
31
        if colorkey is -1:
35
38
        def play(self): pass
36
39
    if not pygame.mixer or not pygame.mixer.get_init():
37
40
        return NoneSound()
38
 
    fullname = os.path.join('data', name)
 
41
    fullname = os.path.join(data_dir, name)
39
42
    try:
40
43
        sound = pygame.mixer.Sound(fullname)
41
 
    except pygame.error, message:
42
 
        print 'Cannot load sound:', fullname
43
 
        raise SystemExit, message
 
44
    except pygame.error:
 
45
        print ('Cannot load sound: %s' % fullname)
 
46
        raise SystemExit(str(geterror()))
44
47
    return sound
45
 
        
 
48
 
46
49
 
47
50
#classes for our game objects
48
51
class Fist(pygame.sprite.Sprite):
117
120
        if not self.dizzy:
118
121
            self.dizzy = 1
119
122
            self.original = self.image
120
 
        
 
123
 
121
124
 
122
125
def main():
123
126
    """this function is called when the program starts.
133
136
    background = pygame.Surface(screen.get_size())
134
137
    background = background.convert()
135
138
    background.fill((250, 250, 250))
136
 
    
 
139
 
137
140
#Put Text On The Background, Centered
138
141
    if pygame.font:
139
142
        font = pygame.font.Font(None, 36)
144
147
#Display The Background
145
148
    screen.blit(background, (0, 0))
146
149
    pygame.display.flip()
147
 
    
 
150
 
148
151
#Prepare Game Objects
149
152
    clock = pygame.time.Clock()
150
153
    whiff_sound = load_sound('whiff.wav')
152
155
    chimp = Chimp()
153
156
    fist = Fist()
154
157
    allsprites = pygame.sprite.RenderPlain((fist, chimp))
155
 
    
 
158
 
 
159
 
156
160
#Main Loop
157
 
    while 1:
 
161
    going = True
 
162
    while going:
158
163
        clock.tick(60)
159
164
 
160
 
    #Handle Input Events
 
165
        #Handle Input Events
161
166
        for event in pygame.event.get():
162
167
            if event.type == QUIT:
163
 
                return
 
168
                going = False
164
169
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
165
 
                return
 
170
                going = False
166
171
            elif event.type == MOUSEBUTTONDOWN:
167
172
                if fist.punch(chimp):
168
173
                    punch_sound.play() #punch
174
179
 
175
180
        allsprites.update()
176
181
 
177
 
    #Draw Everything
 
182
        #Draw Everything
178
183
        screen.blit(background, (0, 0))
179
184
        allsprites.draw(screen)
180
185
        pygame.display.flip()
181
186
 
 
187
    pygame.quit()
 
188
 
182
189
#Game Over
183
190
 
184
191
 
185
192
#this calls the 'main' function when this script is executed
186
 
if __name__ == '__main__': main()
 
193
if __name__ == '__main__':
 
194
    main()