Python 调用类实例时出错
我正在用Python2.7和Pygame制作这个程序。这是我第一次尝试使用类创建游戏。我试图用英雄的属性初始化一个类,比如生命、速度等等,名为“阿彻”。当我尝试运行代码时,会出现以下错误:Python 调用类实例时出错,python,class,pygame,Python,Class,Pygame,我正在用Python2.7和Pygame制作这个程序。这是我第一次尝试使用类创建游戏。我试图用英雄的属性初始化一个类,比如生命、速度等等,名为“阿彻”。当我尝试运行代码时,会出现以下错误: 回溯(最近一次呼叫最后一次): 文件“C:/Python27/rpg.py”,第85行,在 阿切尔·沃克 名称错误:未定义名称“archer” 这是我的密码 from pygame import * from random import * from time import * import pygame i
回溯(最近一次呼叫最后一次):
文件“C:/Python27/rpg.py”,第85行,在
阿切尔·沃克
名称错误:未定义名称“archer”
这是我的密码
from pygame import *
from random import *
from time import *
import pygame
init()
###############VARIABLES
xmove = 0
ymove = 0
white = ( 255,255,255 )
###############INITIALIZING
def preStuff():
heroSelect()#choose a hero
initiateScreen()#set screen size
def initiateScreen():
screen = display.set_mode ((500,500))
def heroSelect():
print """ Welcome to Boss Fights!
Select a hero:
Archer: Use a bow for long range attacks! Attacks do little damage, but can hit from far away. Has average health.
Warrior: Smite enemies with his powerful sword! Attacks a very powerful, but have very short range and are quite slow. Has high health.
Assasin: Hit your enemy without them even seeing you! Assasin moves very quickly, and attacks quickly Has low health.
Alien: Blast your enemy with fire! Use a staff to shoot strong fireballs at enemies, with moderate damage and range. Has average health.
"""
heroC = raw_input('Which class would you like?')
if heroC == 'Archer' or 'archer' or 'ar':
archer=Hero(50,250,image.load('archer.png'),100,.3,.1)#initialize Hero as: archer
elif heroC == 'Warrior' or 'warrior' or 'w':
warrior=Hero(50,250,image.load('warrior.png'),200,.4,.05)
elif heroC == 'Assasin' or 'assasin' or 'as':
assasin=Hero(50,250,image.load('asassin.png'),125,.1,.2)
elif heroC == 'Alien' or 'alien' or 'al':
alien=Hero(50,250,image.load('alien.png'),150,.3,.1)
class Hero:
def __init__(self,x,y, blitImg,hp,attackspeed,speed):
self.x=x
self.y=y
self.blitImg=blitImg
self.hp = hp
self.attackspeed = attackspeed
self.speed=speed
def walk(self):
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_s:
ymove = self.speed
elif event.key == K_w:
ymove = -self.speed
elif event.key == K_a:
xmove = -self.speed
elif event.key == K_d:
xmove = self.speed
elif event.type == KEYUP:
if event.key == K_a or K_s or K_d or K_w:
xmove = 0
ymove = 0
self.y += ymove
self.x += xmove
screen.fill( white )
screen.blit(self.blitImg,(self.x,self.y))
display.update()
if __name__ == "__main__":
preStuff()
while True:
archer.walk()
我现在只是想让角色走路。(还有弓箭手课)
我试过跑步
archer=Hero(50,250,image.load('archer.png'),100,.3,.1)
在外壳中,它工作正常。我可以调用archer.y和其他变量,它们打印得非常完美。请有人帮我弄清楚为什么这不起作用!。(顺便说一句,我是一个初学者程序员,所以如果这是一个简单、容易的解决方案,请原谅,我太笨了,无法实现)
谢谢
archer
是一个局部变量,因此在程序最后一行操作的全局范围内不可见。您可能希望让heroSelect()
返回所创建的Hero对象,然后让preStuff()方法也返回该对象。好的,那么我该怎么做呢?我以前从未使用过返回函数,在研究它之后,我真的不知道如何使用它。我会在heroSelect()和preStuff()的末尾添加return archer吗?在heroSelect
的末尾添加一行return heroC
。在preStuff
中,当您调用heroSelect
时,将输出存储到某个变量中,即hero=heroSelect()
,该变量将为hero
提供从heroSelect
返回的值,并在preStuff
末尾返回此hero变量,即返回hero