From 701f722f10ca1ca852d5fc1c1fd360f3d6d21c41 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 12 May 2015 22:37:33 +0100 Subject: [PATCH] Moved target generation to targets file --- game.py | 40 ++-------------------------------------- target.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/game.py b/game.py index d2c9d7d..1ad850f 100644 --- a/game.py +++ b/game.py @@ -41,42 +41,6 @@ def initialise(menu, options): menu.deiconify() return exit_code - -def generate_targets(player): - sprite_list = [] - group = pygame.sprite.Group() - if player.level > len(Levels)-1: - level = generate_random_level() - else: level = Levels[player.level] - logging.debug("Generating Level: " + str(level)) - - for i in range(level.rows): - i *= level.padding + 8 - for j in range(40, WINDOW_SIZE[0] - 40, level.padding + 10): - temp = Target(x=j,y=i, textures=player.options["Textures"]) - sprite_list.append(temp) - del temp - - if len(sprite_list) < level.firebacks: - firebacks = len(sprite_list) - else: firebacks = level.firebacks - for i in range(firebacks): - changed = False - while not changed: - index = randint(0, len(sprite_list)-1) if (len(sprite_list) - 1 != 0) else 0 - if sprite_list[index].type != "SHOOTER": - sprite_list[index].type = "SHOOTER" - sprite_list[index].image = pygame.transform.scale(player.options["Textures"].get_texture("SHOOTER"), (sprite_list[index].width, sprite_list[index].height)) - x,y = sprite_list[index].rect.x, sprite_list[index].rect.y - sprite_list[index].rect = sprite_list[index].image.get_rect() - sprite_list[index].set_position(x,y, center=False) #Already Centered! - changed = True - - for sprite in sprite_list: #Because sprite groups dont support indexing! - group.add(sprite) - return group - - def play(window, options): window_rect = window.get_rect() options["Textures"].load_texture_pack(options["Textures"].pack) @@ -86,7 +50,7 @@ def play(window, options): player_group = pygame.sprite.Group() player_group.add(player) - target_group = generate_targets(player) + target_group = generate_targets(player, WINDOW_SIZE, Levels) bullet_group = pygame.sprite.Group() clock = pygame.time.Clock() @@ -190,7 +154,7 @@ def play(window, options): if len(target_group) == 0: #If all current players are gone. player.level += 1 - target_group = generate_targets(player) + target_group = generate_targets(player, WINDOW_SIZE, Levels) target_group.draw(window) bullet_group.empty() pygame.display.update() diff --git a/target.py b/target.py index 46794b7..89fdb04 100644 --- a/target.py +++ b/target.py @@ -27,3 +27,37 @@ class Target(pygame.sprite.Sprite): self.rect.x, self.rect.y = x, y +def generate_targets(player, window_size, Levels): + sprite_list = [] + group = pygame.sprite.Group() + if player.level > len(Levels)-1: + level = generate_random_level() + else: level = Levels[player.level] + logging.debug("Generating Level: " + str(level)) + + for i in range(level.rows): + i *= level.padding + 8 + for j in range(75, window_size[0] - 75, level.padding + 10): + temp = Target(x=j,y=i, textures=player.options["Textures"]) + sprite_list.append(temp) + del temp + + if len(sprite_list) < level.firebacks: + firebacks = len(sprite_list) + else: firebacks = level.firebacks + for i in range(firebacks): + changed = False + while not changed: + index = randint(0, len(sprite_list)-1) if (len(sprite_list) - 1 != 0) else 0 + if sprite_list[index].type != "SHOOTER": + sprite_list[index].type = "SHOOTER" + sprite_list[index].image = pygame.transform.scale(player.options["Textures"].get_texture("SHOOTER"), (sprite_list[index].width, sprite_list[index].height)) + x,y = sprite_list[index].rect.x, sprite_list[index].rect.y + sprite_list[index].rect = sprite_list[index].image.get_rect() + sprite_list[index].set_position(x,y, center=False) #Already Centered! + changed = True + + for sprite in sprite_list: #Because sprite groups dont support indexing! + group.add(sprite) + return group + \ No newline at end of file