Mercurial > pygame
annotate examples/oldalien.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 | 5f2fa62e5aec |
| children |
| rev | line source |
|---|---|
|
1620
5f2fa62e5aec
remove svn:executable tags and add setmods script to set executable permissions
Lenard Lindstrom <len-l@telus.net>
parents:
1578
diff
changeset
|
1 #!/usr/bin/env python |
| 7 | 2 |
| 10 | 3 """This is a much simpler version of the aliens.py |
| 4 example. It makes a good place for beginners to get | |
| 5 used to the way pygame works. Gameplay is pretty similar, | |
| 6 but there are a lot less object types to worry about, | |
| 7 and it makes no attempt at using the optional pygame | |
| 8 modules. | |
| 9 It does provide a good method for using the updaterects | |
| 10 to only update the changed parts of the screen, instead of | |
| 11 the entire screen surface. This has large speed benefits | |
| 12 and should be used whenever the fullscreen isn't being changed.""" | |
| 13 | |
| 14 | |
| 7 | 15 #import |
| 300 | 16 import random, os.path, sys |
| 17 import pygame | |
| 7 | 18 from pygame.locals import * |
| 19 | |
| 175 | 20 if not pygame.image.get_extended(): |
|
1578
c0d170c1e636
all standard modules build with Python 3.1. Optional movie module adapted. setup.py skips unsupported files for Python 3
Lenard Lindstrom <len-l@telus.net>
parents:
1427
diff
changeset
|
21 raise SystemExit("Requires the extended image loading from SDL_image") |
| 175 | 22 |
| 7 | 23 |
| 24 #constants | |
| 300 | 25 FRAMES_PER_SEC = 40 |
| 26 PLAYER_SPEED = 12 | |
| 7 | 27 MAX_SHOTS = 2 |
| 300 | 28 SHOT_SPEED = 10 |
| 29 ALIEN_SPEED = 12 | |
| 7 | 30 ALIEN_ODDS = 45 |
| 31 EXPLODE_TIME = 6 | |
| 31 | 32 SCREENRECT = Rect(0, 0, 640, 480) |
| 7 | 33 |
| 34 | |
| 35 #some globals for friendly access | |
| 36 dirtyrects = [] # list of update_rects | |
| 37 next_tick = 0 # used for timing | |
| 38 class Img: pass # container for images | |
|
1427
a6e94e964a69
examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents:
300
diff
changeset
|
39 main_dir = os.path.split(os.path.abspath(__file__))[0] # Program's diretory |
| 7 | 40 |
| 41 | |
| 42 #first, we define some utility functions | |
| 43 | |
| 44 def load_image(file, transparent): | |
| 45 "loads an image, prepares it for play" | |
|
1427
a6e94e964a69
examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents:
300
diff
changeset
|
46 file = os.path.join(main_dir, 'data', file) |
| 7 | 47 try: |
| 48 surface = pygame.image.load(file) | |
| 49 except pygame.error: | |
|
1578
c0d170c1e636
all standard modules build with Python 3.1. Optional movie module adapted. setup.py skips unsupported files for Python 3
Lenard Lindstrom <len-l@telus.net>
parents:
1427
diff
changeset
|
50 raise SystemExit('Could not load image "%s" %s' % |
|
c0d170c1e636
all standard modules build with Python 3.1. Optional movie module adapted. setup.py skips unsupported files for Python 3
Lenard Lindstrom <len-l@telus.net>
parents:
1427
diff
changeset
|
51 (file, pygame.get_error())) |
| 7 | 52 if transparent: |
| 53 corner = surface.get_at((0, 0)) | |
| 54 surface.set_colorkey(corner, RLEACCEL) | |
| 55 return surface.convert() | |
| 56 | |
| 57 | |
| 58 | |
| 59 # The logic for all the different sprite types | |
| 60 | |
| 61 class Actor: | |
| 62 "An enhanced sort of sprite class" | |
| 63 def __init__(self, image): | |
| 64 self.image = image | |
| 21 | 65 self.rect = image.get_rect() |
| 7 | 66 |
| 67 def update(self): | |
| 68 "update the sprite state for this frame" | |
| 69 pass | |
| 70 | |
| 71 def draw(self, screen): | |
| 72 "draws the sprite into the screen" | |
| 55 | 73 r = screen.blit(self.image, self.rect) |
| 7 | 74 dirtyrects.append(r) |
| 75 | |
| 76 def erase(self, screen, background): | |
| 77 "gets the sprite off of the screen" | |
| 55 | 78 r = screen.blit(background, self.rect, self.rect) |
| 7 | 79 dirtyrects.append(r) |
| 80 | |
| 81 | |
| 82 class Player(Actor): | |
| 83 "Cheer for our hero" | |
| 84 def __init__(self): | |
| 85 Actor.__init__(self, Img.player) | |
| 86 self.alive = 1 | |
| 87 self.reloading = 0 | |
| 88 self.rect.centerx = SCREENRECT.centerx | |
| 89 self.rect.bottom = SCREENRECT.bottom - 10 | |
| 90 | |
| 91 def move(self, direction): | |
| 92 self.rect = self.rect.move(direction*PLAYER_SPEED, 0).clamp(SCREENRECT) | |
| 93 | |
| 94 | |
| 95 class Alien(Actor): | |
| 96 "Destroy him or suffer" | |
| 97 def __init__(self): | |
| 98 Actor.__init__(self, Img.alien) | |
| 300 | 99 self.facing = random.choice((-1,1)) * ALIEN_SPEED |
| 7 | 100 if self.facing < 0: |
| 101 self.rect.right = SCREENRECT.right | |
| 102 | |
| 103 def update(self): | |
| 104 global SCREENRECT | |
| 217 | 105 self.rect[0] = self.rect[0] + self.facing |
| 7 | 106 if not SCREENRECT.contains(self.rect): |
| 217 | 107 self.facing = -self.facing; |
| 7 | 108 self.rect.top = self.rect.bottom + 3 |
| 109 self.rect = self.rect.clamp(SCREENRECT) | |
| 110 | |
| 111 | |
| 112 class Explosion(Actor): | |
| 113 "Beware the fury" | |
| 114 def __init__(self, actor): | |
| 115 Actor.__init__(self, Img.explosion) | |
| 116 self.life = EXPLODE_TIME | |
| 117 self.rect.center = actor.rect.center | |
| 118 | |
| 119 def update(self): | |
| 217 | 120 self.life = self.life - 1 |
| 7 | 121 |
| 122 | |
| 123 class Shot(Actor): | |
| 124 "The big payload" | |
| 125 def __init__(self, player): | |
| 126 Actor.__init__(self, Img.shot) | |
| 127 self.rect.centerx = player.rect.centerx | |
| 128 self.rect.top = player.rect.top - 10 | |
| 129 | |
| 130 def update(self): | |
| 217 | 131 self.rect.top = self.rect.top - SHOT_SPEED |
| 7 | 132 |
| 133 | |
| 134 | |
| 135 | |
| 136 def main(): | |
| 137 "Run me for adrenaline" | |
| 138 global dirtyrects | |
| 139 | |
| 140 # Initialize SDL components | |
| 141 pygame.init() | |
| 142 screen = pygame.display.set_mode(SCREENRECT.size, 0) | |
| 300 | 143 clock = pygame.time.Clock() |
| 7 | 144 |
| 145 # Load the Resources | |
| 146 Img.background = load_image('background.gif', 0) | |
| 147 Img.shot = load_image('shot.gif', 1) | |
| 148 Img.bomb = load_image('bomb.gif', 1) | |
| 149 Img.danger = load_image('danger.gif', 1) | |
| 150 Img.alien = load_image('alien1.gif', 1) | |
| 151 Img.player = load_image('oldplayer.gif', 1) | |
| 152 Img.explosion = load_image('explosion1.gif', 1) | |
| 153 | |
| 154 # Create the background | |
| 31 | 155 background = pygame.Surface(SCREENRECT.size) |
| 21 | 156 for x in range(0, SCREENRECT.width, Img.background.get_width()): |
| 7 | 157 background.blit(Img.background, (x, 0)) |
| 158 screen.blit(background, (0,0)) | |
| 159 pygame.display.flip() | |
| 160 | |
| 161 # Initialize Game Actors | |
| 162 player = Player() | |
| 163 aliens = [Alien()] | |
| 164 shots = [] | |
| 165 explosions = [] | |
| 166 | |
| 167 # Main loop | |
| 168 while player.alive or explosions: | |
| 300 | 169 clock.tick(FRAMES_PER_SEC) |
| 7 | 170 |
| 171 # Gather Events | |
| 172 pygame.event.pump() | |
| 173 keystate = pygame.key.get_pressed() | |
| 174 if keystate[K_ESCAPE] or pygame.event.peek(QUIT): | |
| 175 break | |
| 176 | |
| 177 # Clear screen and update actors | |
| 178 for actor in [player] + aliens + shots + explosions: | |
| 179 actor.erase(screen, background) | |
| 180 actor.update() | |
| 181 | |
| 182 # Clean Dead Explosions and Bullets | |
| 183 for e in explosions: | |
| 184 if e.life <= 0: | |
| 185 explosions.remove(e) | |
| 186 for s in shots: | |
| 187 if s.rect.top <= 0: | |
| 188 shots.remove(s) | |
| 189 | |
| 190 # Move the player | |
| 191 direction = keystate[K_RIGHT] - keystate[K_LEFT] | |
| 192 player.move(direction) | |
| 193 | |
| 194 # Create new shots | |
| 195 if not player.reloading and keystate[K_SPACE] and len(shots) < MAX_SHOTS: | |
| 196 shots.append(Shot(player)) | |
| 197 player.reloading = keystate[K_SPACE] | |
| 198 | |
| 199 # Create new alien | |
| 300 | 200 if not int(random.random() * ALIEN_ODDS): |
| 7 | 201 aliens.append(Alien()) |
| 202 | |
| 203 # Detect collisions | |
| 217 | 204 alienrects = [] |
| 205 for a in aliens: | |
| 206 alienrects.append(a.rect) | |
| 207 | |
| 7 | 208 hit = player.rect.collidelist(alienrects) |
| 209 if hit != -1: | |
| 210 alien = aliens[hit] | |
| 211 explosions.append(Explosion(alien)) | |
| 212 explosions.append(Explosion(player)) | |
| 213 aliens.remove(alien) | |
| 214 player.alive = 0 | |
| 215 for shot in shots: | |
| 216 hit = shot.rect.collidelist(alienrects) | |
| 217 if hit != -1: | |
| 218 alien = aliens[hit] | |
| 219 explosions.append(Explosion(alien)) | |
| 220 shots.remove(shot) | |
| 221 aliens.remove(alien) | |
| 222 break | |
| 223 | |
| 224 # Draw everybody | |
| 225 for actor in [player] + aliens + shots + explosions: | |
| 226 actor.draw(screen) | |
| 227 | |
| 228 pygame.display.update(dirtyrects) | |
| 229 dirtyrects = [] | |
| 230 | |
| 300 | 231 pygame.time.wait(50) |
| 7 | 232 |
| 233 | |
| 234 #if python says run, let's run! | |
| 235 if __name__ == '__main__': | |
| 236 main() | |
|
1427
a6e94e964a69
examples can be run from outside the examples directory
Lenard Lindstrom <len-l@telus.net>
parents:
300
diff
changeset
|
237 |
