Collin Burgess
Collin Burgess

Reputation: 11

Why does the event.type function return a name error?

I'm trying to make a simple maze game using PyGame as a beginners project, but when i try to use event.type it says,

 Traceback (most recent call last):
  File "simple_maze.py", line 22, in <module>
    if event.type == pygame.KEYDOWN:
NameError: name 'event' is not defined 

For reference, this is my current code.

import pygame
screendim = (500, 500)
blu = (0, 0, 255)
red = (255, 0, 0)
sc = (0, 0)
pr = 50
pygame.init()
screen = pygame.display.set_mode(screendim)
pygame.draw.circle(screen, blu, (250, 10), 10)
pygame.draw.rect(screen, red, pygame.Rect(0, 25, 450, 15)) 
pygame.display.flip()
play = True
while play:
    if event.type == pygame.KEYDOWN:

Any ideas?

Upvotes: 1

Views: 63

Answers (1)

furas
furas

Reputation: 142641

event is not object which exist all time - you have to create it

for event in pygame.event.get(): 
    if event.type == pygame.KEYDOWN:
        print(event) # do something with this information about key event

You should see this in every tutorial - ie. Program Arcade Games With Python And Pygame.

Upvotes: 1

Related Questions