Multiple keypresses at once in pygame

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2007
Posts: 3
Reputation: missedthepit is an unknown quantity at this point 
Solved Threads: 0
missedthepit missedthepit is offline Offline
Newbie Poster

Multiple keypresses at once in pygame

 
0
  #1
Mar 18th, 2007
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: missedthepit is an unknown quantity at this point 
Solved Threads: 0
missedthepit missedthepit is offline Offline
Newbie Poster

Re: Multiple keypresses at once in pygame

 
0
  #2
Mar 18th, 2007
Nevermind, sorted it. Needed to use pygame.keys.get_pressed()
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 1955 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC