That actually works really well for finding the angle. I put it into a test
application that didn’t use classes (just straight load the image, then rotate
and blit) and I found that if I put in “-angle_degrees” in as the angle, it acts
as it would if it was 0-360 degrees.
..Though I found I’m running into a little problem when I take the code and
import it into my game. Whenever I call the function, it shows the character on
the screen for half a second until it gets thrown wildly (towards the lower
right) outside of the window only for it to give me the “error: Out of memory”
error after its x,y pos go somewhere into the 9000’s. I’m thinking it may
have something to do with it not getting the topleft coordinates again, but I’m
not completely sure.
( Current Class) *Portions of it
class Player(pygame.sprite.Sprite):
# Set Speed of Player
joystick_count = 0
# -- Functions -- #
def __init__(self,x,y, filename, player):
""" PlayerX, PlayerY, Filename,
Player Number(0-4)"""
pygame.sprite.Sprite.__init__(self)
# Import the Player Image
self.image =
pygame.image.load(filename).convert()
self.image.set_colorkey(BLACK)
self.image =
pygame.transform.rotate (self.image, 90)
# Make our top-left corner the
passed-in location.
self.rect =
self.image.get_rect()
self.rect.topleft = [x,y]
# Grab the variable for
Player
self.playerNum = player
# Count the joysticks the
computer has
self.joystick_count=pygame.joystick.get_count()
if self.joystick_count ==
0:
# No
joysticks!
print
("Error, I didn't find any joysticks.")
elif self.playerNum == 1:
# Use
joystick #0 and initialize it
self.my_joystick = pygame.joystick.Joystick(0)
self.my_joystick.init()
def updateangle (self):
if self.joystick_count !=
0:
if
self.playerNum == 1:
x_axis= self.my_joystick.get_axis(0) #x_axis is the position of the horizontal
joystick
y_axis= self.my_joystick.get_axis(1) #y_axis is the position of the vertical
joystick
angle_radians = math.atan2(y_axis,x_axis)
angle_degrees = math.degrees(math.atan2(y_axis,x_axis))
self.image = pygame.transform.rotate (self.image, -angle_degrees)
From: Lee Buckingham
Sent: Wednesday, December 07, 2011 12:04 PM
Subject: Re: [pygame] [Pygame] Joystick Inputs Inverse
Tangent might be slightly easier since you don't need the hypotenuse in the
calculation. I've not used it before so if anyone has a slicker way of
doing it, please chime in. #x_axis is the position of the horizontal joystick #y_axis is the position of the vertical joystick angle_radians = math.atan2(y_axis,x_axis) angle_degrees = math.degrees(math.atan2(y_axis,x_axis)) Either way, the output will be from -180 degrees to +180 degrees (or the radian equivalents) rather than 0 to 360, so you'll potentially have to watch out for that in your logic. See the documentation at http://docs.python.org/library/math.html for why atan2(y,x) is probably more useful than just atan(x). -Lee- On Tue, Dec 6, 2011 at 10:44 PM, Andrew Godfroy <killerrin@xxxxxxxxxxx>
wrote:
|