【发布时间】:2020-07-27 11:44:55
【问题描述】:
所以我决定在 pygame 中为按游戏制作一个后退按钮,因为它有一个主菜单和许多其他菜单(例如帮助菜单)。我已经有了制作按钮的代码,但我所需要的只是某种允许按钮像后退按钮一样的功能。
这是我通常用来制作按钮的按钮功能:
def button(x, y, w, h, inactive, active, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
gameDisplay.blit(active, (x, y))
if click[0] == 1 and action is not None:
action()
else:
gameDisplay.blit(inactive, (x, y))
button() 中每个参数的含义如下:
- x:按钮的 x 坐标
- y:按钮的 y 坐标
- w:按钮图片的宽度
- h:按钮图像的高度
- 活动:鼠标悬停在按钮上时的按钮图片
- inactive:按钮空闲时的图片
- action:点击按钮时要执行的函数(这应该是一个函数)
提前致谢!
【问题讨论】:
-
后退按钮的功能与其他按钮有何不同?后退按钮有什么作用?