gpt4 book ai didi

python - Pygame KEYDOWN 名称错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:14 26 4
gpt4 key购买 nike

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()

错误是这样的: 文件“/home/fenton/Desktop/main.py”,第 27 行,位于 如果 event.type==KEYDOWN:NameError: 名称 'KEYDOWN' 未定义【4.9秒完成】

最佳答案

你需要做的:

if event.type==pygame.KEYDOWN:

否则,Python 不知道 KEYDOWN 在哪里定义。


此外,您可能希望将其更改为:

elif event.type==pygame.KEYDOWN:

因为 event.type 不可能是 pygame.QUIT pygame.KEYDOWN

关于python - Pygame KEYDOWN 名称错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19894782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com