import pygame, sys, pygame.mixer
pygame.init()
size = width,height = 1256,640
screen = pygame.display.set_mode(size)
player_x = 0
player_y = 0
movex = 0
maintheme = pygame.mixer.Sound("music/mainthemes.ogg")
maintheme.play()
player_w = pygame.image.load("characters/player/character_w.png").convert_alpha()
player_a = pygame.image.load("characters/player/character_a.png").convert_alpha()
player_s = pygame.image.load("characters/player/character_s.png").convert_alpha()
player_d = pygame.image.load("characters/player/character_d.png").convert_alpha()
background1 = pygame.image.load("maps/background1.png").convert_alpha()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_a:
movex=-1
screen.fill((0,0,0))
screen.blit(background1,(0,0))
screen.blit(player_s,(player_x,player_y))
pygame.display.update()
The error is this: File "/home/fenton/Desktop/main.py", line 27, in if event.type==KEYDOWN: NameError: name 'KEYDOWN' is not defined [Finished in 4.9s]
You need to do:
if event.type==pygame.KEYDOWN:
Otherwise, Python has no clue where KEYDOWN
is defined.
Also, you may want to change it to:
elif event.type==pygame.KEYDOWN:
because there is no way event.type
could be pygame.QUIT
and pygame.KEYDOWN
.
from pygame.locals import *
Add this line and you're good to go.
However while this does work it is bad practice and the solution iCodez provided should be favored and used instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.