我在网站上发现了这个问题的几个版本,但没有一个答案给我一个我理解的答案(this question是最接近的,但已经回答了' 39;答案似乎朝着不同的方向发展。)
我正在通过学习python这本艰难的书,并且已经达到了我试图为游戏构建一个简单的战斗系统的程度。好消息是,当我把它作为一个独立的程序时,它似乎有用。坏消息是,一旦我尝试将其添加为类,它就会中断。如果它有用,我可以添加完整的代码,但我认为这个问题基本上与代码看起来像这样:
class Room1(Scene):
def kick():
#things happen here
def start():
move = raw_input("> ")
if move == "kick":
kick()
start()
当它只是一组独立的defs时,这很好用,但是现在我已经添加了类,当move == kick时它会引发一个全局名称错误。我错过了什么?
提前致谢,如果有一个明显的答案,我很抱歉。
感谢大家的快速回复!看起来添加整个代码可能对我有所帮助。需要明确的是,这是以example 43 from Learn Python the Hard Way为蓝本的大型游戏的一部分。我非常欣赏有关改进结构的建议,但我现在的感觉是,我想弄清楚为什么在改变更多的东西之前,我几乎无法理解这个结构。当然,我非常愿意接受这样的回答:你想要做的事情并不适合你想要使用的结构。"
当我将下面的代码作为更大结构的一部分运行时(为了空间的利益,我不会粘贴整个事物,但游戏引擎结构与上面相关联)我得到了我描述的错误。我尝试添加' self.start()'或' Room1.start()'我收到的错误是“自我”和“自我”。或姓名' Room1'没有定义。
class Room1(Scene):
gothon_power = 500
move_points = 10
damage = 0
def kick(self):
global gothon_power
global move_points
global damage
damage = randint(10,201)
gothon_power = gothon_power - damage
move_points = move_points - 2
result()
def punch(self):
global gothon_power
global move_points
global damage
damage = randint(1, 101)
gothon_power = gothon_power - damage
move_points = move_points -1
result()
def result(self):
if gothon_power > 0 and move_points > 1:
print "You did %s damage." % damage
print "The Gothon is down to %s health points." % gothon_power
print "You are down to %s move points." % move_points
print "\n"
print "What's your next move?"
move = raw_input("> ")
if move == "kick":
kick()
elif move == "punch":
punch()
else:
print "This isn't going to go anywhere unless you type 'kick' or 'punch'"
print "\n"
result()
elif gothon_power > 0 and move_points == 1:
print "You did %s damage." % damage
print "The Gothon is down to %s health points." % gothon_power
print "You are down to %s move points." % move_points
print "\n"
print "What's your next move? Remember, you only have 1 move point."
move = raw_input("> ")
if move == "kick":
print "You don't have enough move points for a kick."
print "\n"
result()
elif move == "punch":
punch()
else:
print "This isn't going to go anywhere unless you type 'kick' or 'punch'"
print "\n"
result()
elif gothon_power < 1 and move_points > 0:
print "Congratuations, you killed the Gothon!"
return 'room2'
else:
print "The Gothon still has health but you don't have moves."
print "You know what that means."
print "\n"
print "The Gothon killed you."
return 'death'
def start(self):
print "It is time to fight the Gothon"
print "First, let's pause to explain the fighting rules."
print "\n"
print "The Gothon has 500 health points."
print "You have 10 move points."
print "\n"
print "Kicks cost 2 move points and do between 10 and 200 points of damage."
print "Punches cost 1 move opint and do between 1 and 100 points of damage."
print "\n"
print "If you get rid of all 500 Gothon health points before you run out of"
print "move points you win. If you run out of move points before the Gothon"
print "moves out of health points you lose."
print "\n"
print "What's your first move?"
move = raw_input("> ")
if move == "kick":
kick()
elif move == "punch":
punch()
else:
print "This isn't going to go anywhere unless you type 'kick' or 'punch'"
start()
start()
答案 0 :(得分:1)
类上适当的方法集如下:
class Room1(Scene):
def kick(self):
#things happen here
def start(self):
move = raw_input("> ")
if move == "kick":
self.kick()
Room1().start()
但是你可能想重新考虑一下你的设计。对于查询输入的场景来说真的没有意义。您必须在游戏中的每个房间复制该代码。
想一下这种顶级司机:
game = Game()
starting_room = FrontPorch()
game.move_to(starting_room)
while not game.is_over():
move = raw_input("> ")
cmd, args = move.split(None, 1)
game.current_room.do_command(cmd, args)
然后让每个房间处理它特别执行的命令。在基层Room类,您可以实现大多数房间常用的命令,例如&#34; GOTO&#34;,&#34; LOOK&#34;,&#34; WHERE&#34;等等。允许踢的人会覆盖do_command
并包含你现在拥有的逻辑。
Here is a presentation I gave at PyCon '06撰写文字冒险故事。跳过解析内容,然后转到描述游戏/房间/项目设计的部分。您不必逐字遵循此设计,但您可能会了解有关对象和类如何交互的一些想法。在实际潜入之前想一想写下很多代码。
答案 1 :(得分:0)
要调用kick()
作为方法,您需要使用self.<method Name>
语法并添加self
作为第一个参数:
def kick(self):
#things happen here
print('kicking')
#call method
self.kick()
或者通过简单地调用
使kick()
成为静态方法
A.kick()
答案 2 :(得分:0)
如果不创建实例,则无法执行类的实例方法。例如,您可以改为:
class Room1(Scene):
def kick(self):
#things happen here
def start(self):
move = raw_input("> ")
if move == "kick":
self.kick()
room = Room1()
room.start()
但是,在这种情况下,我不建议使用类。类旨在提供一种用状态表示自己的自定义对象的方法。例如,您可以拥有一个Monster
类,其中包含属性damage
,name
等。
对于您展示的这个示例,最好只为Room1使用一个函数。