i'm trying to make a 2 players racing game with pygame and event.key don't work
<p>I'm trying to make a 2-player racing game, and when I try to add the <code>event.key == pygame.K_LEFT</code> it says <code>AttributeError: 'Event' object has no attribute 'key'</code>.</p>

<p>I have tried many things as adding <code>()</code>, but nothing fixed it and I have no clue about it.</p>

<p>Code:</p>

<pre><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
</code></pre>

<p>error message:</p>

<pre><code> 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 &lt;module&gt;
    elif event.key == pygame.K_RIGHT:
AttributeError: 'Event' object has no attribute 'key'
</code></pre>

<pre><code>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
</code></pre>

<p>You want this. Your indentation was wrong. </p>

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

python pygame