[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Spam problem with joystick module



Rouiller, check if these work for you.

Teste1.py controls a circle (and a line, if you have 2 or more directional pads) on the window.
Teste2.py outputs button numbers and directional axes information (it's a bit spammy when you're dealing with analog directional buttons, it's just because there's so many different values the axes can take in that case).

-Thiago

On Fri, Nov 13, 2009 at 2:15 AM, rouiller olivier <o.rouiller@xxxxxxxxx> wrote:
ok thank's let's try to build it, should be a good exercise after all.

2009/11/13 Lorenz Quack <don@xxxxxxxxxxxxxxxxx>

Hi,


rouiller olivier wrote:
The calls to get_axis and get_button cause these outputs...
Maybee there is another way of getting the values?..

Not sure if there is another way.
Note that this is already fixed in svn trunk.
So I guess you can either try to build pygame from source from the svn version or
wait for the next release.


regards
//Lorenz



--
Rouiller Olivier
06 79 66 89 63
Résidence Léonard de Vinci
App. A008
59650 Villeneuve d'Ascq

#!/usr/bin/python

import pygame
import math
import random

#Constants
black  = (0,0,0)
color  = (255,255,255)
speed  = 3.0
size   = 7
length = 20

#Pygame initialization
pygame.init()
pygame.display.set_mode((500,500))
s = pygame.display.get_surface()
t = pygame.time.Clock()

#Checking for joysticks
print "Joysticks:", pygame.joystick.get_count()
j = pygame.joystick.Joystick(0)
j.init()

print "Using joystick:",j.get_name(),"With",j.get_numbuttons(),"buttons and",j.get_numaxes(),"axes."
print "Press button 1 to change color (or button 0, if you start counting from 0)"
print "Press button",j.get_numbuttons(),"to quit. (or button", j.get_numbuttons()-1,"if you start counting from 0)"

x = s.get_width()/2.0
y = s.get_height()/2.0
angle = 0.0

def loop():
    global s, x, y, b, j, black, size, length, angle, color
    while pygame.event.get(pygame.QUIT) == []:
        s.fill(black)
        pygame.event.pump()

        x += j.get_axis(0)*speed
        y += j.get_axis(1)*speed
        pygame.draw.circle(s, color, (int(round(x)),int(round(y))), size)

        if(j.get_numaxes() >= 4):
            fx = j.get_axis(2)
            fy = j.get_axis(3)
            if math.fabs(fx)+math.fabs(fy) > 0.5:
                angle = math.atan2(fy, fx)
            pygame.draw.line(s, color, (int(round(x)),int(round(y))), (int(round(x+math.cos(angle)*length)),int(round((y+math.sin(angle)*length)))))

        if j.get_button(j.get_numbuttons()-1):
            print "Quit button pressed!"
            return
        if j.get_button(0):
            color = (random.randint(50,255),random.randint(50,255),random.randint(50,255))

        pygame.display.flip()
        t.tick(80)

loop()
#!/usr/bin/python

import pygame

#Pygame initialization
pygame.init()
#pygame.display.set_mode((500,500))
#s = display.get_surface()
t = pygame.time.Clock()

print "Joysticks:", pygame.joystick.get_count()
j = pygame.joystick.Joystick(0)
j.init()
print "Using joystick:",j.get_name(),"With",j.get_numbuttons(),"buttons and",j.get_numaxes(),"axes."
print "Press joystick button 0 to quit"

def loop():
    global t, j
    while pygame.event.get(pygame.QUIT) == []:
        #Getting joystick motion
        axes = pygame.event.get(pygame.JOYAXISMOTION)
        if axes != []:
            axes_data = []
            for c in range(0, j.get_numaxes()):
                axes_data += [str(round(j.get_axis(c),1))]
            print axes_data

        #Getting button presses
        buttonsdown = pygame.event.get(pygame.JOYBUTTONDOWN)
        for button in buttonsdown:
            print "Pressed button", button.button
            if button.button == 0:
                return

        #Getting button releases
        buttonsup = pygame.event.get(pygame.JOYBUTTONUP)
        for button in buttonsup:
            print "Released button", button.button

        t.tick(80)

loop()