首页 > 解决方案 > 树莓派和 Pygame 菜单

问题描述

我想在树莓派中使用名为 pygame-menu 的 python 库,因为我想和其他人一起开发游戏。我测试了他们的一个演示,我得到了以下错误:

Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "/home/pi/Pandemic/GameDev.py", line 2, in <module>
    import pygame_menu
  File "/home/pi/.local/lib/python3.7/site-packages/pygame_menu/__init__.py", line 66, in <module>
    import pygame_menu.themes
  File "/home/pi/.local/lib/python3.7/site-packages/pygame_menu/themes.py", line 386, in <module>
    THEME_DEFAULT = Theme()
  File "/home/pi/.local/lib/python3.7/site-packages/pygame_menu/themes.py", line 198, in __init__
    bool, pygame.vernum.major == 2)  # type: bool
AttributeError: 'tuple' object has no attribute 'major'

这也是我的代码:

import pygame
import pygame_menu
pygame.init()
surface = pygame.display.set_mode((600, 400))
def set_difficulty(value, difficulty):
    # Do the job here !
    pass

def start_the_game():
    # Do the job here !
    pass
menu = pygame_menu.Menu(300, 400, 'Welcome',
                       theme=pygame_menu.themes.THEME_SOLARIZED)

menu.add_text_input('Name :', default='John Doe')
menu.add_selector('Difficulty :', [('Hard', 1), ('Easy', 2)], onchange=set_difficulty)
menu.add_button('Play', start_the_game)
menu.add_button('Quit', pygame_menu.events.EXIT)
menu.mainloop(surface)

你能帮我修一下吗?我和 Thonny 一起运行了这个程序

标签: pythonpython-3.xraspberry-pipygame

解决方案


我已经修好了。事实证明,树莓派的 pygame 的默认版本是 1.9.4,而 pygame-menu 需要 1.9.6 及更高版本,所以我使用以下命令对其进行了升级:

python3 -m pip install -U pygame --user

推荐阅读