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:

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()

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 :)

Nevermind, sorted it. Needed to use pygame.keys.get_pressed()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.