尝试从我的标题屏幕函数调用信用屏幕函数时,信用屏幕不会加载



这是我的信用屏幕功能:

def game_credit():
    credit = True
    timer = 0
    while credit:
        print("In Loop")
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        print(credit)
        gameDisplay.fill(white)
        #gameDisplay.blit(img_screen_title, [0,0,display_width,display_height])
        button("BACK", black, "medium", button_x, 260, 180, 59, title_screen(), img_button, img_button_highlight)
        pygame.display.update()

        clock.tick(FPS)

这是我的标题屏幕功能:

def title_screen():
    title = True
    #pygame.mouse.set_cursor(img_cursor, hotspot, xormasks, andmasks)
    while title:
        for event in pygame.event.get():
            #print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    title = False
                elif event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    quit()
                if event.key == pygame.K_c:
                    game_credit()
        gameDisplay.blit(img_screen_title, [0,0,display_width,display_height])
        button("START", black, "medium", button_x, 260, 180, 59, img_button, img_button_highlight, action="start")
        button("CREDITS", black, "medium", button_x, 322, 180, 59, img_button, img_button_highlight, action="credit")
        button("QUIT", black, "medium", button_x, 384, 180, 59, img_button, img_button_highlight, action="quit")
        message_to_screen("A FANGAME by Scott Kiistner", white, 280, "small2")
        pygame.display.update()
        clock.tick(FPS)

这是前面提到的按钮功能:

def button(text, text_color, text_size, x, y, width, height, inactive_img=img_button, active_img=img_button_highlight, action=None):
    cur = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x + width > cur[0] > x and y + height > cur[1] > y:
        gameDisplay.blit(active_img, [x,y])
        if click[0] == 1 and action != None:
            if action == "credit":
                game_credit()
            elif action == "start":
                gameLoop()
            elif action == "quit":
                gameQuit()

    else:
        gameDisplay.blit(inactive_img, [x,y])

    text_to_button(text, text_color, x, y, width, height, text_size)

当我运行脚本时,不会显示任何错误,它也会运行,但当我试图点击致谢菜单时,它只会停留在标题屏幕上,然而当我点击开始菜单或退出菜单时,它们都能正常工作。

我对Python还比较陌生,所以我可能只是缺少了一些明显的东西,但我已经尝试搜索我的问题并找到了解决方案,但我做不到。

这也是我的游戏循环功能,或者我点击开始按钮也可以得到的功能:

def gameLoop():
    gameExit = False
    FPS = 30
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameQuit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    gameQuit()
        gameDisplay.fill(blue)
        pygame.display.update()
        clock.tick(FPS)

这只是猜测。我无法运行你的代码来检查它。

你的代码看起来还可以。但可能是因为你使用了mouse.get_pressed(),而且计算机对人类来说太快了。

mouse.get_pressed()通知您保持按下按钮。

你一直按下按钮,例如100ms,但计算机同时调用game_credit,它在CREDITS按钮的同一位置看到BACK按钮(但你仍然按下按钮),它认为你点击了这个按钮,所以它返回到标题屏幕。(然后你按下CREDIT按钮,它就会调用game_credit,等等)

您应该使用if event.type == pygame.MOUSEBUTTONDOWN:,因为只有当按钮的状态从UP变为DOWN时才是真的。当您按下按钮时,这是错误的。

最新更新