2017-05-31 53 views
0

我試圖在我的Raspberry Pi Zero W上運行pygame,它有一個PS4控制器連接到它。我found some code that should work但我得到這個錯誤,當我嘗試python3 game.pyRaspberry 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上運行它,所以沒有桌面或類似的東西。

+0

之前有人將這個作爲重複:是的,這個錯誤信息是很常見的,但所有的「重複的帖子」並沒有解決我的問題。 – MortenMoulder

+0

「請注意,如果在使用'pygame.init()'初始化時發生錯誤,它將會自動失敗。當手動初始化這樣的模塊時,任何錯誤都會引發異常。任何必須初始化的模塊都有一個' get_init()函數,如果模塊已經初始化,它將返回true。「 [(源)](http://www.pygame.org/docs/tut/ImportInit.html)。也就是說,顯示器*沒有被初始化*。 –

+0

@AnttiHaapala但據我所知,pygame沒有需要具有'get_init()'函數的模塊。你有什麼建議? – MortenMoulder

回答

1

pygame.initfails silently當一個模塊不能初始化:

No exceptions will be raised if a module fails, but the total number if successful and failed inits will be returned as a tuple. You can always initialize individual modules manually, but pygame.init()initialize all imported pygame modules is a convenient way to get everything started. The init() functions for individual modules will raise exceptions when they fail.

在你的情況下,它沒有初始化顯示。有它失敗大聲,叫pygame.display.init明確:

import pygame.display 
pygame.display.init() 
+0

這實際上做到了。很酷,非常感謝! – MortenMoulder

相關問題