【问题标题】:pygame time.sleep prioritised over the rest of the functionpygame time.sleep 优先于函数的其余部分
【发布时间】:2019-04-14 17:59:15
【问题描述】:

我正在尝试创建一个测验应用程序,您可以在其中按两个按钮之一。这是 MVC 示例。主要问题在于“正确”功能。我打算让它设置问题为假(这并没有解决问题),填充游戏显示,然后在屏幕上打印一条消息,并更新显示,让它等待 3 秒再做其他事情。但相反,它会等待 3 秒,在预期的屏幕上闪烁一个勾号,然后立即返回问题功能。 python不应该从上到下逐行读取函数吗?因为现在它将 time.sleep 优先于函数中的其余指令

import time
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))

def question():
    global question
    question = True
    while question:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.fill(cyan)
        button('text', 150, 500, 100, 50, darkYellow, yellow, action = 'correct')
        pygame.display.update()
        clock.tick(100)


def button(text, x, y, width, height, inactive_color, active_color, action = None):
    cur = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x + width > cur[0] > x and y + height > cur[1] > y:
        pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
        pygame.display.update()
        if click[0] == 1 and action != None:
            if action == 'correct':
                question = False
                correct()
    pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
    pygame.display.update()

def correct():
    question = False
    gameDisplay.fill(cyan)
    message_to_screen('Both of these licenses allow for distribution of software', black)
    pygame.display.update()
    time.sleep(3)

question()

【问题讨论】:

    标签: python python-3.x time pygame


    【解决方案1】:

    因为现在它将 time.sleep 优先于函数中的其余指令

    我不知道你对time.sleep 的作用有什么期望。它只会停止您的游戏,在此期间,基本上没有任何反应。 “什么都没有发生”包括绘图和事件处理。没有什么是“优先”的。

    记住这些简单的规则:

    • 无论何时在游戏中使用time.sleep,都可能是错误的
    • 无论何时多次调用pygame.display.update,都可能是错误的

    您必须跟踪游戏的状态,并且每一帧都根据该状态决定要做什么。在最简单的形式中,这种状态是简单的一个或多个变量。

    这是一个简单、可运行且通用的示例:

    import time
    import pygame
    import pygame.freetype
    pygame.init()
    clock = pygame.time.Clock()
    display_width = 800
    display_height = 600
    gameDisplay = pygame.display.set_mode((display_width, display_height))
    font = pygame.freetype.SysFont(None, 20)
    
    def question():
        global question
        question = True
        state = None
        while question:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
            gameDisplay.fill(pygame.Color('cyan'))
            result = button('text', 150, 500, 100, 50, pygame.Color('grey'), pygame.Color('yellow'), action = correct)
            if result: state = result
            if state: question = state()
            pygame.display.update()
            clock.tick(100)
    
    
    def button(text, x, y, width, height, inactive_color, active_color, action = None):
        cur = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if x + width > cur[0] > x and y + height > cur[1] > y:
            pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
            if click[0] == 1 and action != None:
                return action()
            return None
        pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
    
    def message_to_screen(text, color):
        font.render_to(gameDisplay, (100, 100), text, color)
    
    def correct():
        start = pygame.time.get_ticks()
        def func():
            cur = pygame.time.get_ticks()
            message_to_screen('Both of these licenses allow for distribution of software', pygame.Color('black'))
            return cur - start < 3000
    
        return func
    
    question()
    

    我们将button 函数传递给另一个函数(在本例中为correct),如果玩家点击按钮,它将被调用。

    这个结果也是一个函数,然后存储在变量state 中,并且应该返回True 以保持游戏运行或False 以结束游戏。这是通过将question 设置为该函数调用的结果来完成的。

    当然,这样的事情有无数种方法,但是这种双函数方法的优点是保持button 函数的通用性并将状态(“我们要显示多长时间”)封装在correct 函数。你会明白的。

    【讨论】:

    • 这听起来不对。我有一个不同的项目,我在一个函数中有多个 time.sleep 语句,它完全按预期工作。我希望 python 逐行阅读指令,并让 time.sleep 函数简单地停止游戏,然后继续下面的指令
    猜你喜欢
    • 2018-04-17
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多