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>

<p>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 <a class='doc-link' href="http://stackoverflow.com/documentation/pygame/5110/event-handling/18046/event-loop#t=201609262052092301037">Stackoverflow pygame documentation</a> or at the official <a href="http://www.pygame.org/docs/ref/event.html" rel="nofollow">pygame docs</a>.</p>

<p>Let's take an example between two types of events:</p>

<ol>
<li>QUIT event types has no attributes.</li>
<li>KEYDOWN event types has attributes <em>unicode</em>, <em>key</em> and <em>mod</em>.</li>
</ol>

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

<p>In your code you have:</p>

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

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

<p>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.</p>

python pygame