import pygame

from pygame.locals import*

import sys

def pintarCirculo(x,y,color):  

    pygame.draw.circle(screen, color, (x, y), 20, 2)

   

pygame.init()

pygame.joystick.init() # main joystick device system

tam = (600,600)  

#Se crea la ventana con las dimensiones indicadas y se guarda la superficie  

#devuelta en la variable screen  

screen = pygame.display.set_mode((tam))

if pygame.joystick.get_count() <= 0:  

    print "Error no hay joysticks en el sistema" 

    sys.exit()  

else: 

    #Creamos un objeto joystick  

    mando = pygame.joystick.Joystick(0)  

    #Lo iniciamos  

    mando.init()  

    #Comprobamos si al menos tiene dos ejes y una cruceta,si no salimos  

    if mando.get_numaxes() < 2 and mando.get_numhats() < 1:  

        print "Error numero de ejes invalido"  

        sys.exit()

velocidad = 0

posx = 200

posy = 200

posax = 200

posay = 200

while 1:

    screen.fill((0,0,0))  

    for e in pygame.event.get():

            if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):

                sys.exit()

            if e.type ==  pygame.locals.JOYAXISMOTION:# 8

                    posax = posax + int(mando.get_axis(0) * 10)  

                    posay = posay + int(mando.get_axis(1) * 10)

                    print "axis"

                    print "(%d,%d)"%(posax,posay)

            elif e.type == pygame.locals.JOYHATMOTION: # 9

        #print "x %d y %d"%(mando.get_hat(0)[0],mando.get_hat(0)[1])

                    posx += mando.get_hat(0)[0] #captura la posicion en eje x con axis

                    posy -= mando.get_hat(0)[1] #captura posicion en ejey con axis

                    print "(%d,%d)"%(posx,posy)

                         

                    if e.value[1] == -1:

                        print "abajo"

                    if e.value[1] == 1:

                        print "arriba"

                    if e.value[0] == -1:

                        print "izquierda"

                    if e.value[0] == 1:

                        print "derecha"

                                               

            elif e.type == pygame.locals.JOYBUTTONDOWN: #botones al dejar de presionarlo

                pass

                       

            elif e.type == pygame.locals.JOYBUTTONUP: #botones presionados

                if e.button == 9:#boton start

                    sys.exit()

                           

    pintarCirculo(posax,posay,(100,255,0))

    pygame.display.flip()