| | |
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:
Solved Threads: 0
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:
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
python Syntax (Toggle Plain Text)
while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == KEYDOWN: if (event.key == K_RIGHT): Car.MoveKeyDown('R') if (event.key == K_LEFT): Car.MoveKeyDown('L') if (event.key == K_UP): Car.MoveKeyDown('U') if (event.key == K_DOWN): Car.MoveKeyDown('D') if(event.key == K_a): Car2.MoveKeyDown('L') if(event.key == K_s): Car2.MoveKeyDown('D') if(event.key == K_w): Car2.MoveKeyDown('U') if(event.key == K_d): Car2.MoveKeyDown('R') elif event.type == KEYUP: if (event.key == K_RIGHT): Car.MoveKeyUp('R') if (event.key == K_LEFT): Car.MoveKeyUp('L') if (event.key == K_UP): Car.MoveKeyUp('U') if (event.key == K_DOWN): Car.MoveKeyUp('D') if(event.key == K_a): Car2.MoveKeyUp('L') if(event.key == K_s): Car2.MoveKeyUp('D') if(event.key == K_w): Car2.MoveKeyUp('U') if(event.key == K_d): Car2.MoveKeyUp('R') Car.update() Car2.update() self.screen.blit(self.background, self.backgroundRect) self.screen.blit(Car.image,Car.rect) self.screen.blit(Car2.image,Car2.rect) pygame.display.flip()
THanks in advance
Last edited by missedthepit; Mar 18th, 2007 at 2:19 pm.
![]() |
Similar Threads
- Multiple webcams (USB Devices and other Peripherals)
- KeyPress event with holding down the key (Python)
- Multiple IE starting (Web Browsers)
- Can't close multiple IE windows (Web Browsers)
Other Threads in the Python Forum
- Previous Thread: Help - Frame class in tkinter is playing up
- Next Thread: Can we use tkFileDialog.askdirectory as ListBox?
Views: 1955 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython





