PythonInvaders.py 5.9 KB

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