Mercurial > pygame
annotate examples/chimp.py @ 3172:d1b367fc08bc tests-returncode
Use a return code of 1 when the tests fail.
| author | Thomas Kluyver <takowl@gmail.com> |
|---|---|
| date | Wed, 09 Jan 2013 10:13:57 +0000 |
| parents | ebd545641c8b |
| children |
| rev | line source |
|---|---|
| 498 | 1 #!/usr/bin/env python |
| 280 | 2 """ |
| 3 This simple example is used for the line-by-line tutorial | |
| 44 | 4 that comes with pygame. It is based on a 'popular' web banner. |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
5 Note there are comments here, but for the full explanation, |
| 280 | 6 follow along in the tutorial. |
| 7 """ | |
| 8 | |
| 9 | |
| 10 #Import Modules | |
| 303 | 11 import os, pygame |
| 280 | 12 from pygame.locals import * |
|
1553
ebd545641c8b
merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents:
1431
diff
changeset
|
13 from pygame.compat import geterror |
| 300 | 14 |
|
1553
ebd545641c8b
merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents:
1431
diff
changeset
|
15 if not pygame.font: print ('Warning, fonts disabled') |
|
ebd545641c8b
merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents:
1431
diff
changeset
|
16 if not pygame.mixer: print ('Warning, sound disabled') |
| 44 | 17 |
|
1427
a6e94e964a69
examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents:
540
diff
changeset
|
18 main_dir = os.path.split(os.path.abspath(__file__))[0] |
|
a6e94e964a69
examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents:
540
diff
changeset
|
19 data_dir = os.path.join(main_dir, 'data') |
| 44 | 20 |
| 280 | 21 #functions to create our resources |
| 22 def load_image(name, colorkey=None): | |
|
1427
a6e94e964a69
examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents:
540
diff
changeset
|
23 fullname = os.path.join(data_dir, name) |
| 280 | 24 try: |
| 25 image = pygame.image.load(fullname) | |
|
1553
ebd545641c8b
merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents:
1431
diff
changeset
|
26 except pygame.error: |
|
ebd545641c8b
merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents:
1431
diff
changeset
|
27 print ('Cannot load image:', fullname) |
|
ebd545641c8b
merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents:
1431
diff
changeset
|
28 raise SystemExit(str(geterror())) |
| 280 | 29 image = image.convert() |
| 30 if colorkey is not None: | |
| 31 if colorkey is -1: | |
| 32 colorkey = image.get_at((0,0)) | |
| 33 image.set_colorkey(colorkey, RLEACCEL) | |
| 34 return image, image.get_rect() | |
| 35 | |
| 36 def load_sound(name): | |
| 37 class NoneSound: | |
| 38 def play(self): pass | |
| 331 | 39 if not pygame.mixer or not pygame.mixer.get_init(): |
| 280 | 40 return NoneSound() |
|
1427
a6e94e964a69
examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents:
540
diff
changeset
|
41 fullname = os.path.join(data_dir, name) |
| 280 | 42 try: |
| 43 sound = pygame.mixer.Sound(fullname) | |
|
1553
ebd545641c8b
merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents:
1431
diff
changeset
|
44 except pygame.error: |
|
ebd545641c8b
merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents:
1431
diff
changeset
|
45 print ('Cannot load sound: %s' % fullname) |
|
ebd545641c8b
merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents:
1431
diff
changeset
|
46 raise SystemExit(str(geterror())) |
| 280 | 47 return sound |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
48 |
| 280 | 49 |
| 50 #classes for our game objects | |
| 51 class Fist(pygame.sprite.Sprite): | |
| 52 """moves a clenched fist on the screen, following the mouse""" | |
| 53 def __init__(self): | |
| 54 pygame.sprite.Sprite.__init__(self) #call Sprite initializer | |
| 55 self.image, self.rect = load_image('fist.bmp', -1) | |
| 56 self.punching = 0 | |
| 57 | |
| 58 def update(self): | |
| 59 "move the fist based on the mouse position" | |
| 60 pos = pygame.mouse.get_pos() | |
| 61 self.rect.midtop = pos | |
| 62 if self.punching: | |
| 63 self.rect.move_ip(5, 10) | |
| 64 | |
| 65 def punch(self, target): | |
| 66 "returns true if the fist collides with the target" | |
| 67 if not self.punching: | |
| 68 self.punching = 1 | |
| 69 hitbox = self.rect.inflate(-5, -5) | |
| 70 return hitbox.colliderect(target.rect) | |
| 71 | |
| 72 def unpunch(self): | |
| 73 "called to pull the fist back" | |
| 74 self.punching = 0 | |
| 44 | 75 |
| 76 | |
| 280 | 77 class Chimp(pygame.sprite.Sprite): |
| 78 """moves a monkey critter across the screen. it can spin the | |
| 79 monkey when it is punched.""" | |
| 80 def __init__(self): | |
| 81 pygame.sprite.Sprite.__init__(self) #call Sprite intializer | |
| 82 self.image, self.rect = load_image('chimp.bmp', -1) | |
| 83 screen = pygame.display.get_surface() | |
| 84 self.area = screen.get_rect() | |
| 399 | 85 self.rect.topleft = 10, 10 |
| 280 | 86 self.move = 9 |
| 87 self.dizzy = 0 | |
| 88 | |
| 89 def update(self): | |
| 90 "walk or spin, depending on the monkeys state" | |
| 91 if self.dizzy: | |
| 92 self._spin() | |
| 93 else: | |
| 94 self._walk() | |
| 44 | 95 |
| 280 | 96 def _walk(self): |
| 97 "move the monkey across the screen, and turn at the ends" | |
| 98 newpos = self.rect.move((self.move, 0)) | |
| 402 | 99 if self.rect.left < self.area.left or \ |
| 100 self.rect.right > self.area.right: | |
| 280 | 101 self.move = -self.move |
| 102 newpos = self.rect.move((self.move, 0)) | |
| 103 self.image = pygame.transform.flip(self.image, 1, 0) | |
| 104 self.rect = newpos | |
| 105 | |
| 106 def _spin(self): | |
| 107 "spin the monkey image" | |
| 108 center = self.rect.center | |
| 388 | 109 self.dizzy = self.dizzy + 12 |
| 280 | 110 if self.dizzy >= 360: |
| 111 self.dizzy = 0 | |
| 112 self.image = self.original | |
| 113 else: | |
| 114 rotate = pygame.transform.rotate | |
| 115 self.image = rotate(self.original, self.dizzy) | |
| 540 | 116 self.rect = self.image.get_rect(center=center) |
| 280 | 117 |
| 118 def punched(self): | |
| 119 "this will cause the monkey to start spinning" | |
| 120 if not self.dizzy: | |
| 121 self.dizzy = 1 | |
| 122 self.original = self.image | |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
123 |
| 44 | 124 |
| 125 def main(): | |
| 280 | 126 """this function is called when the program starts. |
| 127 it initializes everything it needs, then runs in | |
| 128 a loop until the function returns.""" | |
| 44 | 129 #Initialize Everything |
| 130 pygame.init() | |
| 280 | 131 screen = pygame.display.set_mode((468, 60)) |
| 44 | 132 pygame.display.set_caption('Monkey Fever') |
| 133 pygame.mouse.set_visible(0) | |
| 280 | 134 |
| 44 | 135 #Create The Backgound |
| 136 background = pygame.Surface(screen.get_size()) | |
| 309 | 137 background = background.convert() |
| 44 | 138 background.fill((250, 250, 250)) |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
139 |
| 44 | 140 #Put Text On The Background, Centered |
| 280 | 141 if pygame.font: |
| 142 font = pygame.font.Font(None, 36) | |
| 143 text = font.render("Pummel The Chimp, And Win $$$", 1, (10, 10, 10)) | |
| 540 | 144 textpos = text.get_rect(centerx=background.get_width()/2) |
| 280 | 145 background.blit(text, textpos) |
| 44 | 146 |
| 280 | 147 #Display The Background |
| 44 | 148 screen.blit(background, (0, 0)) |
| 149 pygame.display.flip() | |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
150 |
| 280 | 151 #Prepare Game Objects |
| 152 clock = pygame.time.Clock() | |
| 153 whiff_sound = load_sound('whiff.wav') | |
| 154 punch_sound = load_sound('punch.wav') | |
| 155 chimp = Chimp() | |
| 156 fist = Fist() | |
| 157 allsprites = pygame.sprite.RenderPlain((fist, chimp)) | |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
158 |
|
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
159 |
| 44 | 160 #Main Loop |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
161 going = True |
|
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
162 while going: |
| 309 | 163 clock.tick(60) |
| 388 | 164 |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
165 #Handle Input Events |
| 280 | 166 for event in pygame.event.get(): |
| 402 | 167 if event.type == QUIT: |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
168 going = False |
| 402 | 169 elif event.type == KEYDOWN and event.key == K_ESCAPE: |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
170 going = False |
| 402 | 171 elif event.type == MOUSEBUTTONDOWN: |
| 280 | 172 if fist.punch(chimp): |
| 173 punch_sound.play() #punch | |
| 174 chimp.punched() | |
| 175 else: | |
| 176 whiff_sound.play() #miss | |
| 402 | 177 elif event.type == MOUSEBUTTONUP: |
| 280 | 178 fist.unpunch() |
| 44 | 179 |
| 280 | 180 allsprites.update() |
| 181 | |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
182 #Draw Everything |
| 44 | 183 screen.blit(background, (0, 0)) |
| 280 | 184 allsprites.draw(screen) |
| 44 | 185 pygame.display.flip() |
| 186 | |
|
1429
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
187 pygame.quit() |
|
c4537df65ff4
Call pygame.quit to clean up things. closes window when called from
Rene Dudfield <renesd@gmail.com>
parents:
1427
diff
changeset
|
188 |
| 44 | 189 #Game Over |
| 190 | |
| 191 | |
| 280 | 192 #this calls the 'main' function when this script is executed |
|
1431
0eb69a939a65
updated examples so that they all have a main() and use pygame.quit
Rene Dudfield <renesd@gmail.com>
parents:
1429
diff
changeset
|
193 if __name__ == '__main__': |
|
0eb69a939a65
updated examples so that they all have a main() and use pygame.quit
Rene Dudfield <renesd@gmail.com>
parents:
1429
diff
changeset
|
194 main() |
