torsdag 22 september 2022

Python | Getting steering wheel to work in Arcade

Steering wheels in pygame and arcade go under the name "joysticks". I was able to get my Logitech G27 Racing Wheel to work with pygame but not with Arcade. I made a project using Arcade and used the joystick handling from pygame. 


General idea of the code, important parts:

joysticks = None

def init():
    pygame.init()
    pygame.joystick.init()

    global joysticks
    joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]


class MyGame(arcade.Window):

    def __init__(self):
    ...

    '''
    Use Logitech G27 Racing Wheel if found, else the first connected, if
    any.
    '''

    self.joy_in_use = None

    if joysticks:  
            for joystick in joysticks:  
                if "Logitech G27 Racing Wheel USB" in joystick.get_name():
                    self.joy_in_use = joystick  
               
            if not self.joy_in_use:
                self.joy_in_use = joysticks[0]
       
        if self.joy_in_use:
            self.joy_in_use.init()


    def arcade_joystick_events(self):
        for event in pygame.event.get(): # User did something.
               
            if event.type == JOYBUTTONDOWN:
                input = event.button
                if (input == 0 or input == 6):
                    # code for player jump
           
            if event.type == pygame.JOYAXISMOTION:
                if joysticks:
                    if (axis_val := self.joy_in_use.get_axis(0)) > 0.2:
                        # code for player moving right
                        # axis_val can be used to affect moving speed.
                        
                    elif axis_val < - 0.2:
                        # code for player moving left
                    else:
                        # code for player standing still
       
        pygame.time.wait(10)


    def on_update(self, delta_time):
        self.arcade_joystick_events()
        ...

Inga kommentarer:

Skicka en kommentar