我有一个学校项目,用pygame用Python编写游戏。我选择制作一个炮塔防御游戏,我想要有多个级别以及它们之间的菜单,我可以升级炮塔。
游戏的理想功能与炮塔防御2(http://www.crazymonkeygames.com/Turret-Defense-2.html)
类似这是我的主循环:
def gen_enemies(enemies, sprites, lvl):
if lvl < 10:
times = [t for t in range(1, 1000*lvl)]
for j in range(10*lvl):
spawn_time = random.choice(times)
s = Soldier(screen, spawn_time)
enemies.add(s)
sprites.add(s)
done = False
playing = True
level = 1
clock = pygame.time.Clock()
while not done:
gen_enemies(enemies, all_sprites, level)
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
playing = False
'''key functions for shooting and moving the turret'''
'''creating bullets'''
t.move()
screen.fill(WHITE)
'''Here, I move bad guys with some other code'''
for b in bullets:
b.update()
if b.rect.x > screen_width or b.rect.y > screen_height:
b.kill()
enemy_hits = pygame.sprite.spritecollide(b, enemies, False)
if len(enemy_hits) != 0:
enemy_hits[0].health -= 10
b.kill()
if enemy_hits[0].health == 0:
kills.append(enemy_hits[0])
enemy_hits[0].kill()
score += enemy_hits[0].score
if len(kills) == 10*level:
'''Takes me out of playing loop (so I can go to menu)'''
frame = 0
playing = False
all_sprites.draw(screen)
t.draw()
pygame.display.flip()
frame += 1
clock.tick(100)
'''This is where I want to go to a menu'''
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
print("BEGIN")
space_down = False
level += 1
playing = True
pygame.quit()
游戏在第一级完美运行。我杀死了10个坏人,游戏离开了游戏循环。我还没有建立一个菜单,但当我按下返回键以提升一个级别时,游戏突然产生无限的敌人。这是为什么?
我在Mac上使用Python 3.5.1