简体   繁体   English

为什么我的重播按钮在pygame中不起作用?

[英]Why is my replay button not working in pygame?

I am making a replay button when my player loses on the game over screen. 当我的玩家在屏幕上输掉游戏时,我正在重播按钮。 However, when I press replay, the screen appears for split second and then the game over screen reappears. 但是,当我按replay时,屏幕出现一秒钟,然后重新出现屏幕上的游戏。 I know I somehow have to make game_over = False, but I can't figure that out. 我知道我必须以某种方式使game_over = False,但我无法弄清楚。 Can anyone fix this? 谁能解决这个问题? NOTE: Not all of the code is included, such as code in the button class " init ", game_over_screen design, etc. 注意:并非包括所有代码,例如按钮类“ init ”,game_over_screen设计等中的代码。

class Button(object):

        mouse = pygame.mouse.get_pos()

        if event.type == pygame.MOUSEMOTION:
            self.hovered = self.rect.collidepoint(mouse)
        if event.type == pygame.MOUSEBUTTONDOWN and command is not None:
            if self.hovered:
                if command == 'play':
                    game_loop()
                elif command == 'quit':
                    pygame.quit()
                    quit()
                elif command == 'instructions':
                    instructions()
                elif command == 'credits':
                    credits()
                elif command == 'back':
                    start_screen()

def game_over_screen():

    b_play = Button("Replay", 55, WHITE, 180, 285, 120, 60, BLUE, LIGHT_BLUE)

    game_over = True

    while game_over:
        clock.tick(FPS)
        for events in pygame.event.get():
            if events.type == pygame.QUIT:
                pygame.quit()
                quit()

            b_play.handle_event(events, 'play')

        b_play.update()

        b_play.draw(screen)

        pygame.display.flip()


def game_loop():  # Game loop

    game_over = False

    running = True

    while running:

        clock.tick(FPS)
        events = pygame.event.get()
        for events in events:
            if events.type == pygame.QUIT:
                pygame.quit()
                quit()

        # shows game over screen and resets stats and graphics
        if game_over:
            game_over_screen()
            game_over = False
            global all_sprites, mobs, bullets, powerups, player, score
            all_sprites = pygame.sprite.Group()
            mobs = pygame.sprite.Group()
            bullets = pygame.sprite.Group()
            powerups = pygame.sprite.Group()
            player = Player()
            all_sprites.add(player)
            for i in range(8):
                newmob()
            score = 0

I'm not sure if this is the problem but I see some problem: game_loop runs game_over_screen which should use return to go back to game_loop and it should continue code inside if game_over: and reset all varaibles. 我不确定这是否是问题,但我game_loop一些问题: game_loop运行game_over_screen ,该游戏应使用return game_loopif game_over: ,它应继续执行内部代码,并重置所有变量。 But instead of returning to previous place it runs again game_loop from beginning and it resets only game_over = False and it starts while running -loop. 但是,它没有返回到先前的位置, game_loop从头开始再次运行game_loop ,并且仅重置game_over = False ,并且while running -loop时开始。 But it didn't reset other variables (ie. Player's energy) so these variables can ends game sooner then you expected. 但是它并没有重置其他变量(即玩家的精力),因此这些变量可以比您预期的更快结束游戏。 And it can run game_over_screen again so fast. 而且它可以再次如此快速地运行game_over_screen

Button should have command return which doesn't run function but returns True 按钮应该具有不运行功能但返回True命令return

    if event.type == pygame.MOUSEBUTTONDOWN and command is not None:
        if self.hovered:
            if command == 'play':
                game_loop()
            elif command == 'quit':
                pygame.quit()
                quit()
            elif command == 'instructions':
                instructions()
            elif command == 'credits':
                credits()
            elif command == 'back':
                start_screen()
            elif command == 'return':
                return True

and then you can use True to return to previous function 然后您可以使用True返回上一个功能

if b_play.handle_event(events, 'return') is True:
     return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM