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
To: pygame-users@seul.org 
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 <killer...@hotmail.com> wrote:

  Hey Lee,

  That was actually alot more helpful than what I have been able to find over 
my past days of searching! Thank you very much. I may actually be able to move 
forward with my ambitious school project now 

  Now all I have to figure out is how to find what angle that the Thumbstick is 
pointing so that I can rotate my player sprite according to which way that the 
player is moving. I talked to my math teacher about it, and she agreed that I 
should try using the Inverse of Sin to find the angle. Hopefully that works, 
because if it doesn’t; I'll be back at square one again.

  ... But if anyone has had any experience with getting the angle for a 
rotation based off of a Joystick Thumbstick, or Mouse Position, it would be 
awesome if you could share how you found it out so I could use it as a 
reference.

  From: Lee Buckingham 
  Sent: Tuesday, December 06, 2011 12:53 AM
  To: pygame-users@seul.org 
  Subject: Re: [pygame] [Pygame] Joystick Inputs

  Hi Andrew,

  For buttons I use this:

  if event.type == pygame.JOYBUTTONDOWN:
      if event.button == "whatever...":
          etc....

  if event.type == pygame.JOYBUTTONUP:
      and so on...


  For analog control and d-pads you have to check for axis position each time 
through the game loop.  I do it all at once before the player objects update 
themselves

  Like this:

  horizontal_axis = self.joysticks[player_number].get_axis(0)
  vertical_axis = self.joysticks[player_number].get_axis(1)

  Then change the x and y speed for the player with "player_number" to whatever 
the axis positions are (times a constant, if necessary).  Of course, the 
player_number has to correspond to the joystick collection indices pretty well 
for this to work.  You can eliminate that part of it by replacing 
"player_number" with "0" and just make sure to check that the joystick count is 
>0 before you call .get_axis

  Hope this helps a little!

  -Lee-


  On Thu, Dec 1, 2011 at 5:51 PM, Andrew Godfroy <killer...@hotmail.com> wrote:

    Alright, I just checked over my question and I noticed I wasn’t exactly 
clear what I was looking for...

    What I am asking is how do I work the event handler for checking joystick 
commands such as if the Left Thumbstick-[0] or [1]- moves, or if the A 
button-[0]- is pressed
    Does anyone know how that works?

Reply via email to