oldalien.py 6.6 KB

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