【问题标题】:How to add a back button in pygame如何在pygame中添加后退按钮
【发布时间】: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:点击按钮时要执行的函数(这应该是一个函数)

提前致谢!

【问题讨论】:

  • 后退按钮的功能与其他按钮有何不同?后退按钮有什么作用?

标签: python button pygame


【解决方案1】:

我真正推荐的是用于创建菜单的pygame-menu 包。它有各种各样的小部件和工作流程。可能基于他们的 github,您可能会找到所需的内容。

pygame-menu github

我不明白你的“后退按钮”和“普通按钮”有什么区别,但这个页面可能会有所帮助。

【讨论】:

    【解决方案2】:

    如果您的意思是让您返回主菜单的后退按钮,那么您只需引用主菜单功能作为操作:

    backButton = button(x, y, w, h, inactive, active, mainMenu()):
    

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2013-09-16
      相关资源
      最近更新 更多