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

[pygame] [Pygame] Joystick Inputs



Hey,

I have been trying to experiment with Joysticks, hoping that I will be
able to use on in my project. I'm having a little trouble with
actually figuring out how to get stated as the only code I have found
online has been to just get the values for what all the buttons,
axis's, and hats are. I have been kicking myself for the past few
hours just on the internet trying to figure out how to get started
with it all. Considering that I haven't been able to find any
information on how to actually work it; I have decided to take the
last resort and ask everyone here.\

What I want to be able to do is have my players be sprites, and then
have the ability to control them with the joystick. (I'll post the
class at the bottom)

If there is anyone that can help me it will be GREATLY appreciated as
I am completely lost on how to do this.
Thanks

~killerrin


Pygame Version: 1.9.1
Python Version: 3.1.1
- Hide quoted text -


Heres is my Joysticks Information, Class, and Player Image
Image: 
http://dl.dropbox.com/u/28710254/Photos/Programming/Player.png
"""
ID: js.get_id()
  0
Name: js.get_name()
  Controller (Xbox 360 Wireless Receiver for Windows)

Buttons: js.get_button(button)
  0 == A
  1 == B
  2 == X
  3 == Y
  4 == LB
  5 == RB
  6 == Back
  7 == Start
  8 == LS
  9 == RS

Axis: js.get_axis(axis_num)
  0: LS X Axis
Left: -1.0  Right: 1.0
  1: LS Y Axis
Up: -1.0    Down: 1.0
  2: LT, RT
LT: 1.0    RT: -1.0
  3: RS Y Axis
Up: -1.0    Down: 1.0
  4: RS X Axis
Left: -1.0  Right: 1.0

Hats: js.get_hat(hat_number)
  0: D-Pad
Left-(-1,0)     Upleft-(-1,1)
Right-(1,0)     Upright-(1,1)
Up-(0,1)        Downleft-(-1,-1)
Down-(0,-1)     Downright-(1,-1)

"""
class Player(pygame.sprite.Sprite):
  # Set Speed of Player
  speed_x=0
  speed_y=0

  # -- Functions -- #
  def __init__(self,x,y, filename):
      pygame.sprite.Sprite.__init__(self)

      # Import the Player Image
      self.image = pygame.image.load(filename).convert()
      self.image.set_colorkey(BLACK)

      # Make our top-left corner the passed-in location.
      self.rect = self.image.get_rect()
      self.rect.topleft = [x,y]

  def movement(self,x,y): # Change the Speed of the character then
move it
      self.speed_x+=x
      self.speed_y+=y


  def update(self,walls): # Find, and Update the Player location
      # Get the old position, in case we need to go back to it
      old_x=self.rect.left
      new_x=old_x+self.speed_x
      self.rect.left = new_x

      # Check to see if the player is in a wall
      collide = pygame.sprite.spritecollide(self, walls, False)
      if collide:
          self.rect.left=old_x # If it hits a wall, we go back to
before/wont update it.

      old_y=self.rect.top
      new_y=old_y+self.speed_y
      self.rect.top = new_y

      # Check to see if the player is in a wall
      collide = pygame.sprite.spritecollide(self, walls, False)
      if collide: # If it hits a wall, we go back to before/wont
update it.
          self.rect.top=old_y
player = Player( 0,0, "Player.png" )
player_sprite = pygame.sprite.RenderPlain((player))
player_clicker.add(player)





Once again, If anyone could help me in just trying to figure out what
has to be done, or can atleast point me in the right direction, I will
be forever in debt.