python - 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()
解决方案
因为现在它优先于 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
函数中。你会明白的。
推荐阅读
- java - 尝试将发布数据发送到 nodejs 服务器
- php - 正在下载 NGINX 无扩展名 PHP 文件(带有身份验证)
- c++ - 在按键期间不会不断调用来自 GLFW 的 glfwSetKeyCallback()
- amazon-web-services - 使用 AWS GLUE 从 S3 将引用的数据上传到 Redshift 时出现问题。如何插入数据?
- bash - Docker 容器中的管道和 grep 似乎被阻塞了
- matlab - 查找数组最大值附近的数组
- mailchimp - Mailchimp - 重定向到嵌入表单的感谢页面
- botframework - 如何在 node.js 中的 Microsoft azure bot sdk 4.0 中获取会话对象?
- google-cloud-platform - 您可以调用特定版本的 Google Cloud 函数吗?
- symfony-3.2 - Symfony 网站一旦投入生产就崩溃了