1
Fork 0

Moved target generation to targets file

This commit is contained in:
Jake Howard 2015-05-12 22:37:33 +01:00
parent 55f3bdae15
commit 701f722f10
2 changed files with 36 additions and 38 deletions

40
game.py
View file

@ -41,42 +41,6 @@ def initialise(menu, options):
menu.deiconify() menu.deiconify()
return exit_code 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): def play(window, options):
window_rect = window.get_rect() window_rect = window.get_rect()
options["Textures"].load_texture_pack(options["Textures"].pack) options["Textures"].load_texture_pack(options["Textures"].pack)
@ -86,7 +50,7 @@ def play(window, options):
player_group = pygame.sprite.Group() player_group = pygame.sprite.Group()
player_group.add(player) player_group.add(player)
target_group = generate_targets(player) target_group = generate_targets(player, WINDOW_SIZE, Levels)
bullet_group = pygame.sprite.Group() bullet_group = pygame.sprite.Group()
clock = pygame.time.Clock() clock = pygame.time.Clock()
@ -190,7 +154,7 @@ def play(window, options):
if len(target_group) == 0: #If all current players are gone. if len(target_group) == 0: #If all current players are gone.
player.level += 1 player.level += 1
target_group = generate_targets(player) target_group = generate_targets(player, WINDOW_SIZE, Levels)
target_group.draw(window) target_group.draw(window)
bullet_group.empty() bullet_group.empty()
pygame.display.update() pygame.display.update()

View file

@ -27,3 +27,37 @@ class Target(pygame.sprite.Sprite):
self.rect.x, self.rect.y = x, y 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