Python Forum
[PyGame] Using joystick module from PyGame
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Using joystick module from PyGame
#6
Joystick event example.
import pygame
pygame.init()

def main():
    pygame.display.set_caption('JoyStick Example')
    surface = pygame.display.set_mode((800, 600))
    clock = pygame.time.Clock()
    running = True

    font = pygame.font.Font(None, 20)
    linesize = font.get_linesize()
    joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
    for joy in joysticks:
        joy.init()

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.JOYAXISMOTION:
                print(event)
            elif event.type == pygame.JOYHATMOTION:
                print(event)
            elif event.type == pygame.JOYBUTTONDOWN:
                print(event)

        surface.fill((0,0,0))


        pygame.display.flip()
        clock.tick(20)

    pygame.quit()

main()
joystick states example.
import pygame
pygame.init()

def main():
    pygame.display.set_caption('JoyStick Example')
    surface = pygame.display.set_mode((800, 600))
    clock = pygame.time.Clock()
    running = True

    font = pygame.font.Font(None, 20)
    linesize = font.get_linesize()
    joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
    print(len(joysticks))
    for joy in joysticks:
        joy.init()

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        surface.fill((0,0,0))

        # joystick buttons
        for j in range(len(joysticks)):
            for i in range(joysticks[j].get_numbuttons()):
                if joysticks[j].get_button(i):
                    print("Joystick {}, Button {} pressed".format(j, i))

        pygame.display.flip()
        clock.tick(20)

    pygame.quit()

main()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
Using joystick module from PyGame - by archieab - Sep-22-2018, 05:02 PM
RE: Using joystick module from PyGame - by Larz60+ - Sep-22-2018, 08:16 PM
RE: Using joystick module from PyGame - by archieab - Sep-22-2018, 08:30 PM
RE: Using joystick module from PyGame - by archieab - Sep-22-2018, 10:40 PM
RE: Using joystick module from PyGame - by Larz60+ - Sep-23-2018, 01:03 AM
RE: Using joystick module from PyGame - by Windspar - Sep-23-2018, 01:43 AM
RE: Using joystick module from PyGame - by archieab - Sep-25-2018, 06:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rasp Pi Analog Joystick mmagner2022 1 2,447 Feb-07-2022, 10:19 PM
Last Post: Larz60+
  pygame module not found on Idle after installing on Mac crunchypen 1 3,580 May-09-2020, 11:03 PM
Last Post: metulburr
  [PyGame] Converting PyGame 2 axis joystick float to 360 angle archieab 1 3,396 Sep-26-2018, 05:40 PM
Last Post: archieab
  [PyGame] Joystick Input microphone_head 2 9,485 Sep-16-2018, 06:02 AM
Last Post: microphone_head
  [PyGame] Limiting a Joystick value -1 to +1, to a value between 0 an 127 with 64 being center? japreja 2 4,534 Dec-10-2017, 06:20 AM
Last Post: japreja

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020