在游戏代码中存在缩进问题

时间:2014-03-05 04:20:20

标签: python animation pygame

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

我确定这是一个愚蠢的错误,但它真的开始给我带来麻烦。

3 个答案:

答案 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_LEFTK_RIGHTK_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字符。这可以帮助您避免此类问题