NameError:name ...未定义

时间:2015-10-03 05:20:06

标签: python python-2.7

我正在尝试在python 2.7中制作RPG游戏但是,我遇到了这个问题。我尝试让pygame在屏幕上绘制starting_money和s​​tam。(我将它们放在Player_1.py中的 init 函数中。)当我运行它时,它表示starting_money和s​​tam没有定义。 (是的,我已经从Player_1导入*导入了Player_1。)以下是我的代码示例:

import pygame
from pygame.locals import *
from Player_1 import *
from monster import *

background_colour = (0,0,0)
(width, height) = (800, 800)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('RPG Game')
screen.fill(background_colour)

pygame.display(starting_money(500), stam(100))
pygame.display.flip()

running = True
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = False`

以下是Player_1.py

的示例
import pygame

pygame.image.load('player1.png')

class Hero:
    def __init__(self, hp, alive, starting_money, stamina, player1_sprite,     player1, run):

    self.starting_money = starting_money
    self.money = 500
    self.hp = 1
    self.hp = hp
    self.alive = True
    self.stam = stamina
    self.stamina = 100
    self.ps = player1_sprite
    self.run = run

    player1_sprite = 'player1.png'

def attacked(self, hp):
    if self.hp >= 1:
        self.alive = True
    elif self.hp < 1:
        self.alive = False

def stam(self, stamina):
    if self.stam > 0:
        self.run = True
    elif self.stam <= 0:
        self.run = False

def money(self, money):
    pass

我想通了,感谢回应的家伙们。

1 个答案:

答案 0 :(得分:1)

你需要从你写的Hero课程中实例化你的英雄。因此,为了创造一个500首发金,100耐力和100马力的英雄,你会打电话:

galavant = Hero(100, 'placeholder', 500, 100, 'placeholder', 'placeholder', 'placeholder')

然后打电话:

galavant.starting_moneygalavant.stam应分别产生500和100。