You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
8.2 KiB

6 years ago
6 years ago
  1. import pygame, logging
  2. from time import time, sleep
  3. from random import randint
  4. import eggs
  5. from bullet import Bullet
  6. from player import Shooter
  7. from assets import *
  8. from target import Target, generate_targets
  9. POWERUPS = ["SPEED", "DOUBLE", "LIVES", "SCORE", "BIGGER"]
  10. PLAYING_GAME = False
  11. WINDOW_SIZE = (680, 790)
  12. HUD_COLOUR = (255,168,72)
  13. def update_score(window, score, colour=HUD_COLOUR):
  14. font = pygame.font.SysFont(None, 30, bold=False)
  15. window.blit(font.render("Score: {}".format(int(score)), True, colour), (25, WINDOW_SIZE[1] - 30))
  16. def update_level(window, level, colour=HUD_COLOUR):
  17. font = pygame.font.SysFont(None, 30, bold=False)
  18. window.blit(font.render("Level: {}".format(int(level)+1), True, colour), (8.5*WINDOW_SIZE[0]/10, WINDOW_SIZE[1] - 30))
  19. def update_lives(window, lives, colour=HUD_COLOUR):
  20. font = pygame.font.SysFont(None, 30, bold=False)
  21. window.blit(font.render("Lives Remaining: {}".format(int(lives)), True, colour if lives != 1 else (255,50,0)), (3.8*WINDOW_SIZE[0]/10, WINDOW_SIZE[1] - 30))
  22. def initialise(menu, options):
  23. pygame.mixer.pre_init(44100, -16, 2, 2048)
  24. pygame.init()
  25. window = pygame.display.set_mode(WINDOW_SIZE)
  26. pygame.display.set_caption("Attack on Blocks")
  27. exit_code = play(window, options) # Run main game loop
  28. for key, value in Sounds.items():
  29. value.fadeout(500)
  30. logging.debug("Game Exited with code {}".format(exit_code))
  31. if exit_code != "QUIT": sleep(1)
  32. pygame.quit()
  33. menu.deiconify()
  34. return exit_code
  35. def play(window, options):
  36. window_rect = window.get_rect()
  37. options["Textures"].load_texture_pack(options["Textures"].pack)
  38. player = Shooter(window=window, texture=options["Textures"])
  39. player.set_position(WINDOW_SIZE[0]/2, WINDOW_SIZE[1]*0.90)
  40. player.options = options
  41. player_group = pygame.sprite.Group()
  42. player_group.add(player)
  43. target_group = generate_targets(player, WINDOW_SIZE, Levels)
  44. bullet_group = pygame.sprite.Group()
  45. clock = pygame.time.Clock()
  46. FPS = player.options["Difficulty"]
  47. timeouts = {
  48. "Target Movement":[FPS*0.65,FPS*0.65],
  49. "Powerup":[FPS*15, FPS*15]
  50. }
  51. init_sounds()
  52. if options["Sounds"]: Sounds["main"].play(loops=-1) #Start background music
  53. logging.info("Game Started.")
  54. PLAYING_GAME = True
  55. while PLAYING_GAME:
  56. window.fill((0,0,0))
  57. fired = False
  58. for event in pygame.event.get():
  59. if event.type == pygame.QUIT:
  60. logging.critical("Exiting Game...")
  61. PLAYING_GAME = False
  62. return "QUIT"
  63. if event.type == pygame.KEYDOWN and event.key in [pygame.K_SPACE, pygame.K_w, pygame.K_UP] and not fired:
  64. if player.options["Sounds"]: Sounds["shot"].play()
  65. temp = Bullet(player, player.options["Textures"], player.powerup == "BIGGER")
  66. bullet_group.add(temp)
  67. if player.powerup == "DOUBLE":
  68. temp = Bullet(player, player.options["Textures"], player.powerup == "BIGGER")
  69. temp.rect.y += 20
  70. bullet_group.add(temp)
  71. fired = True
  72. keys = pygame.key.get_pressed()
  73. if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
  74. if player.powerup == "SPEED": player.move(player.speed * 1.5)
  75. else: player.move(player.speed)
  76. if keys[pygame.K_LEFT] or keys[pygame.K_a]:
  77. if player.powerup == "SPEED": player.move(-player.speed*1.5)
  78. else: player.move(-player.speed)
  79. if keys[pygame.K_KP4] and keys[pygame.K_KP5] and keys[pygame.K_KP6] and player.OP:
  80. temp = Bullet(player, player.options["Textures"], False)
  81. bullet_group.add(temp)
  82. if keys[pygame.K_r] and [pygame.K_9] and [pygame.K_k] and player.OP:
  83. eggs.r9k(window)
  84. if keys[pygame.K_KP4] and keys[pygame.K_KP2] and keys[pygame.K_KP0] and keys[pygame.K_o] and keys[pygame.K_p] and not player.OP:
  85. if player.options["Sounds"]:
  86. Sounds["main"].stop()
  87. Sounds["OP"].play(loops=-1)
  88. player.OP = True
  89. player.change_colour((255,96,0))
  90. logging.warn("OP mode engaged.")
  91. if keys[pygame.K_KP_MINUS] and player.OP:
  92. if player.options["Sounds"]:
  93. Sounds["OP"].stop()
  94. Sounds["main"].play(loops=-1)
  95. player.OP = False
  96. player.reset_image()
  97. logging.warn("OP mode disabled.")
  98. for sprite in bullet_group:
  99. sprite.update()
  100. if sprite.rect.y < 0 or (sprite.rect.y > player.rect.y and sprite.type == "TARGET"):
  101. bullet_group.remove(sprite)
  102. for bullet in bullet_group:
  103. hit_list = pygame.sprite.spritecollide(bullet, target_group, False)
  104. for target in hit_list:
  105. if bullet.type != "TARGET":
  106. target.lives -= 1
  107. if target.lives <= 0:
  108. target_group.remove(target)
  109. player.score += 1
  110. bullet_group.remove(bullet)
  111. if target.lives <= 0 and target.type == "POWERUP":
  112. player.powerup = POWERUPS[randint(0,len(POWERUPS)-1)]
  113. logging.info("Powerup set to {}".format(player.powerup))
  114. hit_list = pygame.sprite.spritecollide(bullet, player_group, False)
  115. for player in hit_list:
  116. if bullet.type == "TARGET":
  117. bullet_group.remove(bullet)
  118. logging.info("You were hit by a target's bullet!")
  119. if not player.OP: player.lives -= 1
  120. if player.lives <= 0:
  121. return "LIVES"
  122. if timeouts["Target Movement"][0] <=0:
  123. drop_targets = False
  124. for target in target_group:
  125. target.move()
  126. if target.rect.x <= 30 or target.rect.x >=WINDOW_SIZE[0] - target.width/2 - 30:
  127. drop_targets = True
  128. if target.rect.y >= player.rect.y + target.height/2:
  129. PLAYING_GAME = False
  130. return "PLAYER COLLISION"
  131. if drop_targets:
  132. logging.debug("The targets are moving down a level!")
  133. for target in target_group:
  134. target.drop()
  135. timeouts["Target Movement"][0] = timeouts["Target Movement"][1]
  136. if timeouts["Powerup"][0] <= 0:
  137. timeouts["Powerup"][0] = timeouts["Powerup"][1]
  138. if player.powerup == "SCORE":
  139. player.score += 10
  140. player.powerup = ""
  141. elif player.powerup == "LIVES":
  142. player.lives += 1
  143. player.powerup = ""
  144. for target in target_group:
  145. if target.type == "SHOOTER":
  146. if randint(0,600) > 1: continue
  147. temp = Bullet(target, player.options["Textures"], False)
  148. temp.type="TARGET"
  149. temp.image = pygame.transform.scale(player.options["Textures"].get_texture("TARGET_BULLET"), (temp.width, temp.height))
  150. x,y = temp.rect.x, temp.rect.y
  151. temp.rect = temp.image.get_rect()
  152. temp.set_position(x,y)
  153. temp.speed = -1 #So it shoots down!
  154. bullet_group.add(temp)
  155. if len(target_group) == 0: #If all current players are gone.
  156. player.level += 1
  157. Sounds["main"].set_volume(0.5)
  158. target_group = generate_targets(player, WINDOW_SIZE, Levels)
  159. target_group.draw(window)
  160. bullet_group.empty()
  161. pygame.display.update()
  162. sleep(0.75)
  163. Sounds["main"].set_volume(1.0)
  164. update_score(window, player.score)
  165. update_level(window, player.level)
  166. update_lives(window, player.lives)
  167. target_group.draw(window)
  168. bullet_group.draw(window)
  169. player_group.draw(window)
  170. for key, value in timeouts.items():
  171. value[0] -= 1
  172. pygame.display.update()
  173. clock.tick(FPS)