import pygame, sys
from pygame.locals import *
CHARSPEED = 3
pygame.mixer.init()
pygame.mixer.music.load("nq-rotmg.mp3")
pygame.mixer.music.play(-1)
FPS = 30
fpsClock = pygame.time.Clock()
pywindow = pygame.display.set_mode((500, 500), 0, 32)
pygame.display.set_caption('God of the Mad Realm')
background = pygame.image.load("Background.jpg")
background = background.convert()
pywindow.blit(background, (0, 0))
pygame.display.flip()
Assassin = pygame.image.load("Assassin_L.png")
Assassin_R.set_colorkey((255, 255, 255))
Assassin_L.set_colorkey((255, 255, 255))
assassinx = 10
assassiny = 10
pygame.key.set_repeat(10, 10)
while True:
pywindow.blit(background, (0, 0))
pywindow.blit(Assassin, (assassinx, assassiny))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if (event.key == K_LEFT):
Assassin=pygame.image.load('Assassin_L.png')
elif (event.key == K_RIGHT): <<<-------------------------------------
Assassin=pygame.image.load('Assassin_R.png')
elif (event.key == K_UP):
Assassin=pygame.image.load('Assassin_L.png')
elif (event.key == K_DOWN):
Assassin=pygame.image.load('Assassin_L.png')
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_LEFT]:
assassinx -= CHARSPEED
elif keys_pressed[K_RIGHT]:
assassinx += CHARSPEED
elif keys_pressed[K_UP]:
assassiny -= CHARSPEED
elif keys_pressed[K_DOWN]:
assassiny += CHARSPEED
pygame.display.update()
fpsClock.tick(FPS)
在我放箭头的地方,我不断收到错误消息:
Traceback (most recent call last):
File "Character.py", line 42, in <module>
elif (event.key == K_RIGHT):
AttributeError: event member not defined
我确定这是一个愚蠢的错误,但它真的开始给我带来麻烦。
答案 0 :(得分:2)
你还有不正确的缩进。由于AttributeError
变量未在正确范围内定义,因此event
出现。你的代码......
for event in pygame.event.get():# <<<--------
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN: # <<<--------
if (event.key == K_LEFT):
要if event.type == KEYDOWN
循环for
下的event
,才能正确定义for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN: # one more indented
if (event.key == K_LEFT): # corresponding appropriate indent
Assassin=pygame.image.load('Assassin_L.png')
elif (event.key == K_RIGHT):
Assassin=pygame.image.load('Assassin_R.png')
elif ...
。因此,正确的代码应该是,
while True
此外,所有游戏执行代码都应归入while True:
pywindow.blit(background, (0, 0))
pywindow.blit(Assassin, (assassinx, assassiny))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if (event.key == K_LEFT):
Assassin=pygame.image.load('Assassin_L.png')
elif (event.key == K_RIGHT):
Assassin=pygame.image.load('Assassin_R.png')
elif (event.key == K_UP):
Assassin=pygame.image.load('Assassin_L.png')
elif (event.key == K_DOWN):
Assassin=pygame.image.load('Assassin_L.png')
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_LEFT]:
assassinx -= CHARSPEED
elif keys_pressed[K_RIGHT]:
assassinx += CHARSPEED
elif keys_pressed[K_UP]:
assassiny -= CHARSPEED
elif keys_pressed[K_DOWN]:
assassiny += CHARSPEED
pygame.display.update()
fpsClock.tick(FPS)
区块。
{{1}}
答案 1 :(得分:0)
KEYDOWN
后,您要检查它是K_LEFT
,K_RIGHT
,K_UP
还是K_DOWN
,因此if和elif语句应该位于同样的缩进。
if event.type == KEYDOWN:
if (event.key == K_LEFT):
Assassin=pygame.image.load('Assassin_L.png')
elif (event.key == K_RIGHT):
Assassin=pygame.image.load('Assassin_R.png')
elif (event.key == K_UP):
Assassin=pygame.image.load('Assassin_L.png')
elif (event.key == K_DOWN):
Assassin=pygame.image.load('Assassin_L.png')
答案 2 :(得分:0)
看起来您已使用TAB缩进这两行(至少在此处发布的版本中)
找到TAB,删除它们并用空格替换。
某些编辑器会让您设置首选项以使用TAB键插入空格而不是TAB字符。这可以帮助您避免此类问题