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