我正在尝试在我的Raspberry Pi Zero W上运行pygame,它有一个PS4控制器连接到它。我found some code that should work但是当我尝试python3 game.py
时出现此错误:
来自Stackoverflow上的其他人的追踪(最近的呼叫最后):
文件“controller.py”,第74行,在 ps4.listen()
文件“controller.py”,第52行,在听力中 对于pygame.event.get()中的事件:
pygame.error:视频系统未初始化
The same code让它工作(至少这是我的假设),但他有一个不同的问题,但相同的代码。我确实尝试运行该代码,但我得到了同样的错误。我尝试了从Stackoverflow中找到的所有建议,但没有一个能够工作。这是我找到的代码:
import os
import pprint
import pygame
class PS4Controller(object):
"""Class representing the PS4 controller. Pretty straightforward functionality."""
controller = None
axis_data = None
button_data = None
hat_data = None
def init(self):
"""Initialize the joystick components"""
pygame.init()
pygame.joystick.init()
self.controller = pygame.joystick.Joystick(0)
self.controller.init()
def listen(self):
"""Listen for events to happen"""
if not self.axis_data:
self.axis_data = {}
if not self.button_data:
self.button_data = {}
for i in range(self.controller.get_numbuttons()):
self.button_data[i] = False
if not self.hat_data:
self.hat_data = {}
for i in range(self.controller.get_numhats()):
self.hat_data[i] = (0, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.JOYAXISMOTION:
self.axis_data[event.axis] = round(event.value,2)
elif event.type == pygame.JOYBUTTONDOWN:
self.button_data[event.button] = True
elif event.type == pygame.JOYBUTTONUP:
self.button_data[event.button] = False
elif event.type == pygame.JOYHATMOTION:
self.hat_data[event.hat] = event.value
# Insert your code on what you would like to happen for each event here!
# In the current setup, I have the state simply printing out to the screen.
os.system('clear')
pprint.pprint(self.button_data)
pprint.pprint(self.axis_data)
pprint.pprint(self.hat_data)
if __name__ == "__main__":
ps4 = PS4Controller()
ps4.init()
ps4.listen()
任何线索该怎么做以及为什么它不起作用?我在Jessie Lite上运行它,所以没有桌面或类似的东西。
答案 0 :(得分:1)
pygame.init
fails silently:
如果模块失败,则不会引发异常,但如果成功且失败的总数将作为元组返回。您总是可以手动初始化单个模块,但是pygame.init()初始化所有导入的pygame模块是一种方便的方法来启动所有内容。各个模块的init()函数会在失败时引发异常。
在您的情况下,它没有初始化显示。要让它失败,请明确致电pygame.display.init
:
import pygame.display
pygame.display.init()