PythonInvaders.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/python2.7
  2. import pygame
  3. from pygame.locals import *
  4. from random import randint, uniform
  5. import time
  6. import sys
  7. # Settings, enter whatever values you like
  8. jump_power = 7 #How high can you jump?
  9. shot_power = 7 #How hard can you shoot?
  10. ammo_on_kill = 2 #Ammo awarded on kill
  11. score_on_kill = 10 #Score awarded on kill
  12. enemy_damage = 30 #Score lost on enemy collision
  13. score = 0 #Starting score
  14. ammo = 20 #Starting ammo
  15. font_name = "Inconsolata" #Font family for text
  16. font_size = 25 #Font size for text
  17. #Init lists
  18. shots = []
  19. enemies = []
  20. #Init game
  21. screen = pygame.display.set_mode((500, 500), DOUBLEBUF)
  22. pygame.init()
  23. # initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
  24. font = pygame.font.SysFont(font_name, font_size)
  25. #Classes
  26. class Entity(object): #The main class for objects on screen
  27. def __init__(self, x, y, filename, movement=None, gravity=0.1):
  28. self.alive = True
  29. self.x = x
  30. self.y = y
  31. self.image = pygame.image.load(filename)
  32. if movement is None:
  33. self.movement = [0, 0]
  34. else:
  35. self.movement = movement
  36. self.gravity = gravity
  37. def update(self):
  38. self.movement[1] += self.gravity
  39. self.x += self.movement[0]
  40. self.y += self.movement[1]
  41. if self.y > 500:
  42. self.alive = False
  43. def draw(self):
  44. screen.blit(self.image, (self.x, self.y))
  45. def rect(self):
  46. return pygame.rect.Rect(self.x, self.y, self.image.get_rect().width,
  47. self.image.get_rect().height)
  48. class Shot(Entity):
  49. def __init__(self, x, y, movement=None):
  50. global shot_power
  51. super(Shot, self).__init__(x, y, 'shot.png', movement)
  52. class Car(Entity):
  53. def __init__(self):
  54. super(Car, self).__init__(200, 350, 'car.png')
  55. def update(self):
  56. super(Car, self).update()
  57. if self.y < 0:
  58. self.y = 0
  59. elif self.y > (500 - self.image.get_rect().height):
  60. self.y = 500 - self.image.get_rect().height
  61. self.movement[1] = 0
  62. if self.x < 0:
  63. self.x = 0
  64. elif self.x > (500 - self.image.get_rect().width):
  65. self.x = 500 - self.image.get_rect().width
  66. def jump(self):
  67. if self.y >= (500 - self.image.get_rect().height):
  68. global jump_power
  69. self.movement[1] = -jump_power
  70. def shoot(self):
  71. global ammo
  72. if ammo > 0:
  73. ammo -= 1
  74. shots.append(Shot(self.x + self.image.get_rect().width/2, car.y, [0, -shot_power]))
  75. class Enemy(Entity):
  76. justCollided = False
  77. def __init__(self):
  78. self.image = pygame.image.load('enemy.png')
  79. super(Enemy, self).__init__(randint(0, 500 - self.image.get_rect().width), randint(0, 200), 'enemy.png', [uniform(1.0, 5.0), 0], 0)
  80. def update(self):
  81. super(Enemy, self).update()
  82. if self.x < 0 or self.x > 500 - self.image.get_rect().width:
  83. self.movement[0] = -self.movement[0];
  84. if self.y >= 500:
  85. global score
  86. score += score_on_kill
  87. global ammo
  88. ammo += ammo_on_kill
  89. self.alive = False
  90. if (not self.rect().colliderect(car.rect())) and self.justCollided:
  91. self.justCollided = False
  92. if self.rect().colliderect(car.rect()) and not self.justCollided:
  93. self.justCollided = True
  94. global score
  95. if score - enemy_damage < 0:
  96. score = 0
  97. else:
  98. score -= enemy_damage
  99. class Button(Entity):
  100. def __init__(self, x, y):
  101. super(Button, self).__init__(x, y, 'button.png', gravity=0)
  102. def update(self):
  103. if self.rect().colliderect(car.rect()):
  104. enemies.append(Enemy())
  105. else:
  106. self.y = 500 - 8
  107. enemies = [Enemy(), Enemy(), Enemy()]
  108. car = Car()
  109. button = Button(0, 500-8)
  110. while True:
  111. #Check input
  112. for event in pygame.event.get():
  113. if event.type == QUIT:
  114. pygame.quit()
  115. print 'Exiting...'
  116. sys.exit()
  117. elif event.type == KEYDOWN:
  118. if event.key == K_RIGHT:
  119. car.movement[0] += 10
  120. elif event.key == K_LEFT:
  121. car.movement[0] += -10
  122. elif event.key == K_DOWN:
  123. car.gravity = 0.5
  124. elif event.key == K_SPACE:
  125. car.jump()
  126. elif event.key == K_z:
  127. car.shoot()
  128. elif event.type == KEYUP:
  129. if event.key == K_RIGHT:
  130. car.movement[0] -= 10
  131. elif event.key == K_LEFT:
  132. car.movement[0] -= -10
  133. elif event.key == K_DOWN:
  134. car.gravity = 0.1
  135. #Update stuff
  136. for entity in shots + enemies:
  137. entity.update()
  138. for shot in shots:
  139. for enemy in enemies:
  140. if shot.rect().colliderect(enemy.rect()):
  141. enemy.gravity = 0.1
  142. shot.alive = False
  143. enemies = [enemy for enemy in enemies if enemy.alive]
  144. shots = [shot for shot in shots if shot.alive]
  145. if len(enemies) == 0:
  146. enemies.append(Enemy())
  147. enemies.append(Enemy())
  148. enemies.append(Enemy())
  149. car.update()
  150. button.update()
  151. #Draw everything to screen
  152. screen.fill((255, 255, 255))
  153. for entity in enemies + shots:
  154. entity.draw()
  155. car.draw()
  156. button.draw()
  157. #Render text
  158. textlines = []
  159. linenumber = 0
  160. textlines.append(font.render("Score: " + str(score), 1, (0,0,0)))
  161. textlines.append(font.render("Ammo: " + str(ammo), 1, (0,0,0)))
  162. if car.gravity == 0.5:
  163. textlines.append(font.render("GRAVITY", 1, (0,0,0)))
  164. if score == 420:
  165. textlines.append(font.render("Blaze it!", 1, (0,0,0)))
  166. for line in textlines:
  167. screen.blit(line, (0, linenumber * (font_size + 5)))
  168. linenumber += 1
  169. pygame.display.flip()
  170. time.sleep(0.01)