944,047 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2890
  • Python RSS
Mar 18th, 2007
0

Multiple keypresses at once in pygame

Expand Post »
I am designing a 2 player car racing game, and I need to be able to control both cars at the same time from the keyboard (using the arrow keys for one and a,s,d,w for the other) I can get them to work when the other one is not being moved, but I don't know how to get both moving at once. Pygame doesn't seem to accept more than one key to be pressed down at any one time. Below is my while loop in the main game loop:

python Syntax (Toggle Plain Text)
  1. while 1:
  2. for event in pygame.event.get():
  3. if event.type == pygame.QUIT:
  4. sys.exit()
  5. elif event.type == KEYDOWN:
  6. if (event.key == K_RIGHT):
  7. Car.MoveKeyDown('R')
  8. if (event.key == K_LEFT):
  9. Car.MoveKeyDown('L')
  10. if (event.key == K_UP):
  11. Car.MoveKeyDown('U')
  12. if (event.key == K_DOWN):
  13. Car.MoveKeyDown('D')
  14. if(event.key == K_a):
  15. Car2.MoveKeyDown('L')
  16. if(event.key == K_s):
  17. Car2.MoveKeyDown('D')
  18. if(event.key == K_w):
  19. Car2.MoveKeyDown('U')
  20. if(event.key == K_d):
  21. Car2.MoveKeyDown('R')
  22. elif event.type == KEYUP:
  23. if (event.key == K_RIGHT):
  24. Car.MoveKeyUp('R')
  25. if (event.key == K_LEFT):
  26. Car.MoveKeyUp('L')
  27. if (event.key == K_UP):
  28. Car.MoveKeyUp('U')
  29. if (event.key == K_DOWN):
  30. Car.MoveKeyUp('D')
  31. if(event.key == K_a):
  32. Car2.MoveKeyUp('L')
  33. if(event.key == K_s):
  34. Car2.MoveKeyUp('D')
  35. if(event.key == K_w):
  36. Car2.MoveKeyUp('U')
  37. if(event.key == K_d):
  38. Car2.MoveKeyUp('R')
  39.  
  40. Car.update()
  41. Car2.update()
  42. self.screen.blit(self.background, self.backgroundRect)
  43. self.screen.blit(Car.image,Car.rect)
  44. self.screen.blit(Car2.image,Car2.rect)
  45. pygame.display.flip()
Car and Car2 are instances of a custom object based on the sprite class, and have the methods to move tyhe car in them. I can post these if required.

THanks in advance
Last edited by missedthepit; Mar 18th, 2007 at 2:19 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
missedthepit is offline Offline
3 posts
since Mar 2007
Mar 18th, 2007
0

Re: Multiple keypresses at once in pygame

Nevermind, sorted it. Needed to use pygame.keys.get_pressed()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
missedthepit is offline Offline
3 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Help - Frame class in tkinter is playing up
Next Thread in Python Forum Timeline: Can we use tkFileDialog.askdirectory as ListBox?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC