【发布时间】:2023-03-08 12:00:02
【问题描述】:
我想创建一个提示按钮,可以通过单击按钮或单击“h”键来工作。有没有更简单的方法来做到这一点而不必重复提示按钮的代码?
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_h:
code for hint button
if event.type == pygame.MOUSEBUTTONUP:
mouse_x, mouse_y = pygame.mouse.get_pos()
if mouse_x and mouse_y are over hint button:
code for hint button
但我在“提示按钮的代码”部分下有相当多的代码,所以我宁愿不必重复两次。有没有办法绕过这种重复?
非常感谢您的帮助。谢谢!
【问题讨论】:
-
编写一个包含“提示按钮代码”的函数,并在任一情况下调用该函数。 clean code
标签: python pygame event-handling