0

I'm trying to run pygame on my Raspberry Pi Zero W, which has a PS4 controller hooked up to it. I found some code that should work but I get this error when I try to python3 game.py:

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 from someone else on Stackoverflow got it to work (at least that's what I assume), but he had a different problem, yet the same code. I did try to run that code instead, but I got the same error. I tried all suggestions I could find from Stackoverflow, but none of them worked. Here's the code I found:

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()

Any clue what to do and why it's not working? I run this on Jessie Lite, so there is no desktop or anything like that.

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
  • Before someone marks this as duplicate: Yes, this error message is pretty common, but all the "duplicated posts" doesn't solve my issue. – MortenMoulder May 31 '17 at 14:56
  • "Note that if there is an error when you initialize with `pygame.init()`, it will silently fail. When hand initializing modules like this, any errors will raise an exception. Any modules that must be initialized also have a `get_init()` function, which will return true if the module has been initialized." [(source)](http://www.pygame.org/docs/tut/ImportInit.html). That is, the display *wasn't initialized*. – Antti Haapala -- Слава Україні May 31 '17 at 15:02
  • @AnttiHaapala But pygame has no modules that needs to have a `get_init()` function... as far as I know. What do you propose? – MortenMoulder May 31 '17 at 15:04

1 Answers1

1

pygame.init fails silently when a module cannot be initialized:

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.

In your case, it didn't initialize the display. To have it fail loudly, call pygame.display.init explicitly:

import pygame.display
pygame.display.init()