Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i'm trying to make a 2 players racing game with pygame and event.key don't work

Tags:

python

pygame

I'm trying to make a 2-player racing game, and when I try to add the event.key == pygame.K_LEFT it says AttributeError: 'Event' object has no attribute 'key'.

I have tried many things as adding (), but nothing fixed it and I have no clue about it.

Code:

import pygame

pygame.init()

display_width = 1280
display_height = 720

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('U-race multiplayer')
clock = pygame.time.Clock()

car1 = pygame.image.load('car1.png')

def carone(xone, yone):
    gameDisplay.blit(car1,(xone, yone))

xone = (display_width * 0.48)
yone = (display_height * 0.8)

xone_change = 0


crashed = False

while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
               xone_change = -5
        elif event.key == pygame.K_RIGHT:
            xone_change = 5

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                xone_change = 0

error message:

 RESTART: C:\Users\Osamas\Desktop\U-racing multiplayer\U-racing multiplayer.py 
Traceback (most recent call last):
  File "C:\Users\Osamas\Desktop\U-racing multiplayer\U-racing multiplayer.py", line 44, in <module>
    elif event.key == pygame.K_RIGHT:
AttributeError: 'Event' object has no attribute 'key'
like image 229
Ubilab Team Avatar asked Dec 05 '25 03:12

Ubilab Team


2 Answers

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        xone_change = -5
    elif event.key == pygame.K_RIGHT: #indented to be part of the keydown events
        xone_change = 5

You want this. Your indentation was wrong.

You had elif event.key == pygame.K_RIGHT: checking with your event.type which doesn't make sense if you think about it.

like image 127
MooingRawr Avatar answered Dec 07 '25 17:12

MooingRawr


The reason why you're getting an error is because different events have different attributes. You can check which event has which attributes at the official pygame docs.

Let's take an example between two types of events:

  1. QUIT event types has no attributes.
  2. KEYDOWN event types has attributes unicode, key and mod.

This means that we cannot check for the key attribute in an event until we've made sure that the event is of type KEYDOWN (or KEYUP).

In your code you have:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
           xone_change = -5
    elif event.key == pygame.K_RIGHT:
        xone_change = 5

If the event type isn't KEYDOWN then you're checking with the event's key attribute, which will raise an attribute error if the event is of type QUIT, MOUSEBUTTONDOWN or any other event that isn't KEYDOWN or KEYUP.

To correct this you'll do as MooingRawr answered and make sure that you've indented the two last lines inside the first if-statement. Just thought I'd give an answer to why it's wrong.

like image 33
Ted Klein Bergman Avatar answered Dec 07 '25 17:12

Ted Klein Bergman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!