【发布时间】: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