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
#1
Exact code with commenting can be found here, or here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
while done==False:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        done=True
 
    if event.type == pygame.JOYBUTTONDOWN:
        print("Joystick button pressed.")
    if event.type == pygame.JOYBUTTONUP:
        print("Joystick button released.")
 
screen.fill(darkgrey)
textPrint.reset()
 
joystick_count = pygame.joystick.get_count()
 
textPrint.print(screen, "Number of joysticks: {}".format(joystick_count) )
textPrint.indent()
 
for i in range(joystick_count):
    joystick = pygame.joystick.Joystick(i)
    joystick.init()
 
    textPrint.print(screen, "Joystick {}".format(i) )
    textPrint.indent()
 
    name = joystick.get_name()
    textPrint.print(screen, "Joystick name: {}".format(name) )
 
    axes = joystick.get_numaxes()
    textPrint.print(screen, "Number of axes: {}".format(axes) )
    textPrint.indent()
 
    for i in range( axes ):
        axis = joystick.get_axis( i )
        textPrint.print(screen, "Axis {} value: {:>6.0f}".format(i, axis) )
    textPrint.unindent()
 
    buttons = joystick.get_numbuttons()
    textPrint.print(screen, "Number of buttons: {}".format(buttons) )
    textPrint.indent()
 
    for i in range( buttons ):
        button = joystick.get_button( i )
        textPrint.print(screen, "Button {:>2} value: {}".format(i,button) )
    textPrint.unindent()
 
    hats = joystick.get_numhats()
    textPrint.print(screen, "Number of hats: {}".format(hats) )
    textPrint.indent()
 
    for i in range( hats ):
        hat = joystick.get_hat( i )
        textPrint.print(screen, "Hat {} value: {}".format(i, str(hat)) )
    textPrint.unindent()
 
pygame.display.flip()
clock.tick(20)
With the help of the online PyGame documentation, I was able to produce a screen displaying values for individual Joysticks. Sadly, I'm unsure on how I could translate these values to an event that produces something like, "this button is pressed, now do this".
Reply
#2
not a pygame expert, but it appears that in the code:
1
2
3
4
if event.type == pygame.JOYBUTTONDOWN:
    print("Joystick button pressed.")
if event.type == pygame.JOYBUTTONUP:
    print("Joystick button released.")
instead of just printing, call a function, for example:
1
2
3
4
if event.type == pygame.JOYBUTTONDOWN:
    process_button_down_stuff()
if event.type == pygame.JOYBUTTONUP:
    process_button_up_stuff()
Reply
#3
(Sep-22-2018, 08:16 PM)Larz60+ Wrote: not a pygame expert, but it appears that in the code:
1
2
3
4
if event.type == pygame.JOYBUTTONDOWN:
    print("Joystick button pressed.")
if event.type == pygame.JOYBUTTONUP:
    print("Joystick button released.")
instead of just printing, call a function, for example:
1
2
3
4
if event.type == pygame.JOYBUTTONDOWN:
    process_button_down_stuff()
if event.type == pygame.JOYBUTTONUP:
    process_button_up_stuff()

Hi Larz,
Thanks for the reply. What you have suggested would work, but isn’t specific to the pos and neg values. Is there a way, maybe here,
1
2
3
axes = joystick.get_numaxes()
        textPrint.print(screen, "Number of axes: {}".format(axes) )
        textPrint.indent()
Where I could say “If joystick no.1’s axis no.3 is 1, then print ‘hello!’”?

Cheers.
Reply
#4
Something similar to this,
1
2
3
4
5
6
7
8
9
10
11
for i in range( axes ):
    axis = joystick.get_axis( i )
    textPrint.print(screen, "Axis {} value: {:>6.0f}".format(i, axis) )
 
    if Joystick 2's Axis 3 is equal to 1:
        print("Joystick 2 is pointing to the right.")
 
    if Joystick 2's Axis 3 is equal to -1:
        print("Joystick 2 is pointing to the left.")
 
textPrint.unindent()
Reply
#5
I'd have to have a setup that I could try to say definitively what would and wouldn't work. Some of the moderators here use pygame and will respond sooner or later. metulburr for one.
Reply
#6
Joystick event example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
#7
I'm unsure on how to mark this post as solved, but I managed to work out a solution.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rasp Pi Analog Joystick mmagner2022 1 3,207 Feb-07-2022, 10:19 PM
Last Post: Larz60+
  pygame module not found on Idle after installing on Mac crunchypen 1 4,438 May-09-2020, 11:03 PM
Last Post: metulburr
  [PyGame] Converting PyGame 2 axis joystick float to 360 angle archieab 1 4,031 Sep-26-2018, 05:40 PM
Last Post: archieab
  [PyGame] Joystick Input microphone_head 2 11,292 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 5,646 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