python - NameError: global name 'player' is not defined -
well building pygame in sublime text, when run batch file start error:
traceback (most recent call last): file "game.py", line 12, in <module> player = player("default", 1, 1, 1) file "c:\users\*****\desktop\rpg\characters\player.py", line 8, in __init__ character.__init__(self, name, hp) file "c:\users\*****\desktop\rpg\characters\character.py", line 9, in __init__ player.dead = false nameerror: global name 'player' not defined press key continue . . .
now here files:
game.py file:
# main game file characters.player import * commands import * commands = { 'help': help, 'exit': exit } player = player("default", 1, 1, 1) def isvalidcmd(cmd): if cmd in commands: return true return false def runcmd(cmd, args, player): commands[cmd](player, args) def main(player): while(not player.dead): line = raw_input(">> ") input = line.split() input.append("eoi") if isvalidcmd(input[0]): runcmd(input[0], input[1], player) main(player)
player.py file:
# player base file character import * class player(character): def __init__(self, name, hp, str, int): character.__init__(self, name, hp) self.str = str self.int = int
and character.py file:
# character base file class character(object): def __init__(self, name, hp): self.name = name self.hp = hp player.dead = false def attack(self, other): pass def update(self): if self.hp <= 0: player.dead = true self.hp = 0
what do?
1) change
from characters.player import *
to
from player import *
in game.py
2)in player.py, class player
class player
and
3) in character.py
, make change please
player.dead = false
to
self.dead = false
Comments
Post a Comment