Python pygame.JOYHATMOTION Examples

The following are 4 code examples of pygame.JOYHATMOTION(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pygame , or try the search function .
Example #1
Source File: core_controls.py    From Retropie-CRT-Edition with GNU General Public License v3.0 6 votes vote down vote up
def event_wait(self):
        while True:
            for event in pygame.event.get():
                if self.m_bUnload: break
                if event.type == pygame.KEYDOWN:
                    input = self.get_key(event.key)
                    if input: return input
                elif event.type == pygame.JOYBUTTONDOWN:
                    input = self.get_button(event.joy, event.button)
                    if input: return input
                elif event.type == pygame.JOYHATMOTION:
                    input = self.get_hat(event.value)
                    if input: return input
                elif event.type == pygame.JOYAXISMOTION:
                    input = self.get_axis(event.joy, event.axis, event.value)
                    if input: return input
            self.m_oClock.tick(20)
            pygame.time.wait(0) 
Example #2
Source File: event_handling.py    From stuntcat with GNU Lesser General Public License v2.1 6 votes vote down vote up
def check_hat(self, pg_event):
        if pg_event.type == pg.JOYHATMOTION:
            x, y = pg_event.value
            if x == -1:
                self.press(actions.LEFT, value=x * -1)
            elif x == 0:
                self.release(actions.LEFT)
                self.release(actions.RIGHT)
            elif x == 1:
                self.press(actions.RIGHT)

            if y == -1:
                self.press(actions.DOWN, value=y * -1)
            elif y == 0:
                self.release(actions.DOWN)
                self.release(actions.UP)
            elif y == 1:
                self.press(actions.UP) 
Example #3
Source File: _utils.py    From pygame-menu with MIT License 6 votes vote down vote up
def joy_key(key, inlist=True, testmode=True):
        """
        Create a pygame joy controller key event.

        :param key: Key to press
        :type key: bool
        :param inlist: Return event in a list
        :type inlist: bool
        :param testmode: Key event is in test mode
        :type testmode: bool
        :return: Event
        :rtype: :py:class:`pygame.event.Event`
        """
        event_obj = pygame.event.Event(pygame.JOYHATMOTION,
                                       {'value': key,
                                        'test': testmode
                                        })
        if inlist:
            event_obj = [event_obj]
        return event_obj 
Example #4
Source File: selector.py    From pygame-menu with MIT License 5 votes vote down vote up
def update(self, events):
        updated = False
        for event in events:  # type: pygame.event.Event

            if event.type == pygame.KEYDOWN:  # Check key is valid
                if not check_key_pressed_valid(event):
                    continue

            # Events
            keydown = event.type == pygame.KEYDOWN
            joy_hatmotion = self.joystick_enabled and event.type == pygame.JOYHATMOTION
            joy_axismotion = self.joystick_enabled and event.type == pygame.JOYAXISMOTION
            joy_button_down = self.joystick_enabled and event.type == pygame.JOYBUTTONDOWN

            if keydown and event.key == _controls.KEY_LEFT or \
                    joy_hatmotion and event.value == _controls.JOY_LEFT or \
                    joy_axismotion and event.axis == _controls.JOY_AXIS_X and event.value < _controls.JOY_DEADZONE:
                self.sound.play_key_add()
                self.left()
                updated = True

            elif keydown and event.key == _controls.KEY_RIGHT or \
                    joy_hatmotion and event.value == _controls.JOY_RIGHT or \
                    joy_axismotion and event.axis == _controls.JOY_AXIS_X and event.value > -_controls.JOY_DEADZONE:
                self.sound.play_key_add()
                self.right()
                updated = True

            elif keydown and event.key == _controls.KEY_APPLY or \
                    joy_button_down and event.button == _controls.JOY_BUTTON_SELECT:
                self.sound.play_open_menu()
                self.apply(*self._elements[self._index][1:])
                updated = True

            elif self.mouse_enabled and event.type == pygame.MOUSEBUTTONUP:
                if self._rect.collidepoint(*event.pos):
                    # Check if mouse collides left or right as percentage, use only X coordinate
                    mousex, _ = event.pos
                    topleft, _ = self._rect.topleft
                    topright, _ = self._rect.topright
                    dist = mousex - (topleft + self._title_size)  # Distance from label
                    if dist > 0:  # User clicked the options, not label

                        # Position in percentage, if <0.5 user clicked left
                        pos = dist / float(topright - topleft - self._title_size)
                        if pos <= 0.5:
                            self.left()
                        else:
                            self.right()
                        updated = True

        return updated