首页 > 解决方案 > pygame time.sleep 优先于函数的其余部分

问题描述

我正在尝试创建一个测验应用程序,您可以在其中按下两个按钮之一。这是 MVC 示例。主要问题在于“正确”功能。我打算将问题设置为 false(这并没有解决问题),填充游戏显示,然后在屏幕上打印一条消息,并更新显示,让它等待 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()

标签: pythonpython-3.xtimepygame

解决方案


因为现在它优先于 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函数中。你会明白的。


推荐阅读