0

I'm currently making a game with Python using the PyGame module. I have two classes, a game class and a car class. The game class has a game loop method, which I made to loop through different methods, one being the event method in the car class. When I run the program, everything loads up fine. However, when I try to move the object, the keyboard inputs are slow to process and when I spam the movement keys, some of the inputs don't get recognized at all.

Is there something fundamentally wrong with how I'm structuring the game loop?

The Game loop method goes as follows:

def game_loop(self):                                                                       

    running = True
    self.test_car = car()

    while running:

        pygame.display.set_caption("Project G")
        self.event_handler()
        self.screen.blit(self.background, (0,0))
        self.test_car.event_handler()
        self.test_car.update()
        pygame.display.flip()

And this is the car class:

class car(game):

    def __init__(self):

        super(car, self).__init__()
        self.init_x_pos = 100
        self.init_y_pos = 100
        self.x_speed = 0
        self.y_speed = 0
        self.load_img = load()
        self.car_img = pygame.image.load(self.load_img.car_img)

    def event_handler(self):

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            if event.type == KEYDOWN:
                if event.key == K_DOWN:
                    self.y_speed += 1
                    print "DOWN"
                elif event.key == K_UP:
                    self.y_speed -= 1
                    print "UP"
                elif event.key == K_RIGHT:
                    self.x_speed += 1
                    print "RIGHT"
                elif event.key == K_LEFT:
                    self.x_speed -= 1
                    print "LEFT"

    def update(self):

        self.screen.blit(self.car_img, (self.init_x_pos + self.x_speed, self.init_y_pos + self.y_speed))             
reuwacm
  • 3
  • 2
  • Possibly related: http://stackoverflow.com/questions/6348952/handling-keyboard-events-in-python – paddy Jan 20 '13 at 23:29
  • @paddy: No. This question relates to pygame, which has a standard way of accessing the keyboard. The other question relates to getting pressed keys in a terminal. – icktoofay Jan 20 '13 at 23:30

1 Answers1

1

You should probably only be having one loop calling pygame.event.get(); depending on what part of the code is executing when you press the key, the event could be routed to one or the other. If one discards certain events and the other doesn't, then whenever the first one gets the event, it will be ignored, whereas when the second one gets the event, it is processed normally.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • I've fixed my problem. I simply commented out the event_handler method from the game class. I'd like to ask though, how would you suggest I go about handling events during different states of the game? For example, event handling in the menu, event handling during play etc. Would a simple if-elif structure within the game loop suffice? For example, if state == "menu": run the corresponding event loop. – reuwacm Jan 20 '13 at 23:46
  • 1
    You should take a look at state machines. They help you do that more easily than by implementing a giant if-else-statement. – Colin Emonds Jan 20 '13 at 23:49
  • @Raymond: cemper93's suggestion of state machines is good. You may also want to look at how some other games do things. For example, although it uses Pyglet rather than pygame, [spacegame](http://hg.codeflow.org/spacegame/file/tip) seems to deal with states in a rather sane way. – icktoofay Jan 20 '13 at 23:58
  • Thank you for all the advice! – reuwacm Jan 21 '13 at 00:03