所以我制作了一个创造问题的游戏,用户必须猜出正确的游戏。每次用户得到一个问题都正确“正确!”出现,但每次他们弄错了“错!”出现。然后该文本在0.5秒后消失。
当用户得到正确或错误的问题时,由于time.sleep(0.5)
,游戏等待0.5秒。用户可以继续发送鼠标点击垃圾邮件,并将程序延迟几秒钟,因为他们可以继续解决问题。
这是我需要帮助的代码的一部分:
while answer != correct_answer:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
for index, rect in enumerate(answers_rect):
if rect.collidepoint(event.pos):
answer = index
if answer == correct_answer:
display_text("correct!",green,(start_x-150),(start_y-650),medText)
pygame.display.update()
time.sleep(0.5)
display_text("correct!",white,(start_x-150),(start_y-650),medText)
pygame.display.update()
elif answer != correct_answer:
display_text("wrong!",red,(start_x-140),(start_y-650),medText)
pygame.display.update()
time.sleep(0.5)
display_text("wrong!",white,(start_x-140),(start_y-650),medText)
pygame.display.update()
如何阻止此垃圾邮件,0.5秒内不允许用户点击任何答案?
答案 0 :(得分:3)
您可以添加waiting_for_sleep_to_over
全局布尔变量。
使用此var,您可以禁用click事件,如下所示:
waiting_for_sleep_to_over = False
while answer != correct_answer:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and waiting_for_sleep_to_over is not True:
for index, rect in enumerate(answers_rect):
if rect.collidepoint(event.pos):
answer = index
if answer == correct_answer:
display_text("correct!",green,(start_x-150),(start_y-650),medText)
pygame.display.update()
waiting_for_sleep_to_over = True
time.sleep(0.5)
waiting_for_sleep_to_over = False
display_text("correct!",white,(start_x-150),(start_y-650),medText)
pygame.display.update()
elif answer != correct_answer:
display_text("wrong!",red,(start_x-140),(start_y-650),medText)
pygame.display.update()
waiting_for_sleep_to_over = True
time.sleep(0.5)
waiting_for_sleep_to_over = False
display_text("wrong!",white,(start_x-140),(start_y-650),medText)
pygame.display.update()
修改强> 在评论建议之后,使用以下函数代替布尔标志: pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN) pygame.event.set_allowed(pygame.MOUSEBUTTONDOWN)
while answer != correct_answer:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
for index, rect in enumerate(answers_rect):
if rect.collidepoint(event.pos):
answer = index
if answer == correct_answer:
display_text("correct!",green,(start_x-150),(start_y-650),medText)
pygame.display.update()
pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN)
time.sleep(0.5)
pygame.event.set_allowed(pygame.MOUSEBUTTONDOWN)
display_text("correct!",white,(start_x-150),(start_y-650),medText)
pygame.display.update()
elif answer != correct_answer:
display_text("wrong!",red,(start_x-140),(start_y-650),medText)
pygame.display.update()
pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN)
time.sleep(0.5)
pygame.event.set_allowed(pygame.MOUSEBUTTONDOWN)
display_text("wrong!",white,(start_x-140),(start_y-650),medText)
pygame.display.update()