2015-11-06 42 views
0

這個節目,我想提出在Python 2.7使用pygame的。這是我第一次嘗試使用類創建遊戲。我正在嘗試初始化一個英雄的統計數據,比如hp,速度等,命名爲'archer'。當我嘗試運行代碼,它給了我這個錯誤:錯誤而調用的類實例

Traceback (most recent call last): File "C:/Python27/rpg.py", line 85, in <module> archer.walk() NameError: name 'archer' is not defined

這裏是我的代碼。

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和其餘的變量,並且它們打印完美。請有人幫我弄清楚爲什麼這不起作用! (附註:我是一個初學編程的,所以請原諒如果是一些簡單的,簡單的辦法是我太愚蠢實現) 謝謝!

回答

0

archer是一個局部變量,因此不是在全球範圍內,你的程序的最後一行是經營上是可見的。你可能想使heroSelect()返回所創建的英雄對象,然後有preStuff()方法回報爲好。

+0

好了,我將如何做呢?我之前從未使用過返回函數,並且在研究之後,我並不真正瞭解如何使用它。我會在heroSelect()和preStuff()的末尾添加返回射手嗎? 'heroSelect'末尾的 –

+0

只需添加'return heroC'這一行。在'preStuff'當調用'heroSelect',輸出存儲到一些變量,即'英雄= heroSelect()',這將給'hero'您從'heroSelect' reterned的值,並在preStuff的'結束'返回這個英雄變量,即'返回英雄' – George