Sunday, August 3, 2014

X,Y coordinates in Joystick in Pygame

Co-ordinates can be controlled easily with pygame joystick get_axis function.

joystick.get_axis(0) returns the 'x' axis position, at the same time
joystick.get_axis(1) returns the y axis position of the joystick. You can add to you rectangle image coordinates with the above values to make movement to the image in X,Y axis.

# get the rectangle the image occupies
# rec(x, y, w, h)
start_rect = image.get_rect()
image_rect = start_rect
.....
.....
.....
hor_x   = joystick.get_axis(0)
hor_y   = joystick.get_axis(1)
image_rect.left = image_rect.left+ hor_x * 2
image_rect.top = image_rect.top+ hor_y * 2


No comments: