我试图在我的Raspberry Pi Zero W上运行pygame,它有一个PS4控制器连接到它。我found some code that should work但我得到这个错误,当我尝试python3 game.py
:Raspberry Pi和pygame.error:视频系统未初始化
Traceback (most recent call last):
File "controller.py", line 74, in
ps4.listen()
File "controller.py", line 52, in listen
for event in pygame.event.get():
pygame.error: video system not initialized
The same code从别人#2人得到它的工作(至少这是我认为),但他有一个不同的问题,但相同的代码。我确实试图运行该代码,但我得到了同样的错误。我尝试了所有可以从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上运行它,所以没有桌面或类似的东西。
之前有人将这个作为重复:是的,这个错误信息是很常见的,但所有的“重复的帖子”并没有解决我的问题。 – MortenMoulder
“请注意,如果在使用'pygame.init()'初始化时发生错误,它将会自动失败。当手动初始化这样的模块时,任何错误都会引发异常。任何必须初始化的模块都有一个' get_init()函数,如果模块已经初始化,它将返回true。“ [(源)](http://www.pygame.org/docs/tut/ImportInit.html)。也就是说,显示器*没有被初始化*。 –
@AnttiHaapala但据我所知,pygame没有需要具有'get_init()'函数的模块。你有什么建议? – MortenMoulder