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.

84 lines
3.5 KiB

6 years ago
  1. import pygame, logging
  2. from random import randint
  3. from game import generate_random_level
  4. class Target(pygame.sprite.Sprite):
  5. def __init__(self, x, y, textures, color=(30,0,150), width=23, height=23):
  6. super().__init__()
  7. self.width = width
  8. self.height = height
  9. self.textures = textures
  10. self.default_texture = textures.get_target_texture()
  11. self.image = pygame.transform.scale(self.default_texture[0], (self.width, self.height))
  12. self.rect = self.image.get_rect()
  13. self.speed = 5
  14. self.rect.x, self.rect.y = (x+(self.width/2)),(y+(self.width/2)) # centres co-ordinates
  15. self.type = "NORMAL"
  16. self.lives = 1
  17. def move(self):
  18. self.rect.x += self.speed
  19. def drop(self):
  20. self.rect.y += 13
  21. self.speed *= -1
  22. def set_position(self, x, y, center=False):
  23. if center:
  24. self.rect.x, self.rect.y = (x+(self.width/2)),(y+(self.width/2))
  25. else:
  26. self.rect.x, self.rect.y = x, y
  27. def generate_targets(player, window_size, Levels):
  28. sprite_list = []
  29. group = pygame.sprite.Group()
  30. if player.level > len(Levels)-1:
  31. level = generate_random_level()
  32. else: level = Levels[player.level]
  33. logging.debug("Generating Level: " + str(level))
  34. for i in range(level.rows):
  35. i *= level.padding + 8
  36. for j in range(75, window_size[0] - 75, level.padding + 10):
  37. temp = Target(x=j,y=i, textures=player.options["Textures"])
  38. sprite_list.append(temp)
  39. del temp
  40. if len(sprite_list) < level.firebacks:
  41. firebacks = len(sprite_list)
  42. else: firebacks = level.firebacks
  43. for i in range(firebacks):
  44. loop = 0
  45. changed = False
  46. while not changed:
  47. if loop == firebacks: changed = True
  48. index = randint(0, len(sprite_list)-1) if (len(sprite_list) - 1 != 0) else 0
  49. if sprite_list[index].type != "SHOOTER":
  50. sprite_list[index].type = "SHOOTER"
  51. sprite_list[index].lives = 2
  52. sprite_list[index].image = pygame.transform.scale(player.options["Textures"].get_texture("SHOOTER"), (sprite_list[index].width, sprite_list[index].height))
  53. x,y = sprite_list[index].rect.x, sprite_list[index].rect.y
  54. sprite_list[index].rect = sprite_list[index].image.get_rect()
  55. sprite_list[index].set_position(x,y, center=False) #Already Centered!
  56. changed = True
  57. loop += 1
  58. if len(sprite_list) < level.powerups:
  59. powerups = len(sprite_list)
  60. else: powerups = level.powerups
  61. for i in range(powerups):
  62. changed = False
  63. while not changed:
  64. index = randint(0, len(sprite_list)-1) if (len(sprite_list) - 1 != 0) else 0
  65. if sprite_list[index].type != "POWERUP" and sprite_list[index].type != "SHOOTER" :
  66. sprite_list[index].type = "POWERUP"
  67. sprite_list[index].image = pygame.transform.scale(player.options["Textures"].get_texture("POWERUP"), (sprite_list[index].width, sprite_list[index].height))
  68. x,y = sprite_list[index].rect.x, sprite_list[index].rect.y
  69. sprite_list[index].rect = sprite_list[index].image.get_rect()
  70. sprite_list[index].set_position(x,y, center=False) #Already Centered!
  71. changed = True
  72. for sprite in sprite_list: #Because sprite groups dont support indexing!
  73. group.add(sprite)
  74. return group