在简单的Python游戏中创建战斗系统
这是代码:
# TEST.PY
import sys
import random
class Fight(object):
def enter(self):
print "\n", "-" * 10
print "There are two muchachos:"
print "MUCHACHO1"
print "MUCHACHO2"
print "One of them looks wounded or something."
your_hit_points = 20
muchacho1_hit_points = 6
muchacho2_hit_points = 11
muchacho1 = True
muchacho2 = True
while your_hit_points > 0 or (not muchacho1 and not muchacho2):
print "\n", "-" * 10
your_attack = random.randint(4,12)
muchacho1_attack = random.randint(1,4)
muchacho2_attack = random.randint(4,8)
attack = int(raw_input("Type 1 to attack MUCHACHO1, 2 to attack MUCHACHO2 >"))
if attack == 1:
muchacho1_hit_points - your_attack
print "You hit MUCHACHO1 for %d hit points." % your_attack
if muchacho1_hit_points <= 0 and muchacho1:
muchacho1 = False
print "MUCHACHO1 killed!"
else:
pass
elif attack == 2:
muchacho2_hit_points - your_attack
print "You hit MUCHACHO2 for %d hit points." % your_attack
if muchacho2_hit_points <= 0 and muchacho2:
muchacho2 = False
print "MUCHACHO2 killed!"
else:
pass
else:
print "DOES NOT COMPUTE"
pass
your_hit_points - muchacho1_attack
print "MUCHACHO1 hit you for %d points, you have %d hit points left." % (muchacho1_attack, your_hit_points)
your_hit_points - muchacho2_attack
print "MUCHACHO2 hit you for %d points, you have %d hit points left." % (muchacho2_attack, your_hit_points)
exit(1)
a_fight = Fight()
a_fight.enter()
我遇到了一点小困难。基本上,WHILE循环一直没有结束,似乎每个人的生命值都没有减少。我感觉我可能漏掉了一些非常非常基础的东西,因为我已经编程几个小时了,可能看不到简单的部分。
我知道用更多的类或函数可以做得更好,但现在我想这样做(另外,抱歉行长超过80个字符)。
2 个回答
1
我觉得你想做的事情是把 muchacho1_hit_points
减去 your_attack
的值,muchacho2
也是这样。现在你只是把减法的结果给丢掉了。
0
我不是说我做得最好或者我的代码风格最棒,但我对你的代码做了一些改进(顺便说一句,游戏做得不错)。
我发现了一些问题:
你的循环有问题(你应该在你自己或者至少有一个小伙伴还活着的时候继续循环)。
每次受到伤害时,你需要更新muchacho1_hit_points和muchacho2_hit_points。
在每一轮之前,你需要检查一下muchacho是否还活着。
这个类没有必要,你可以直接用一个函数来代替。
在Python中,pass
是个空操作,可以忽略;通常是在声明后面会用到的容器类时使用。
你还可以做一些改进:捕捉异常,以防用户输入的不是整数(现在会崩溃)。
import random
class Fight(object):
def enter(self):
print "\n", "-" * 10
print "There are two muchachos:"
print "MUCHACHO1"
print "MUCHACHO2"
print "One of them looks wounded or something."
your_hit_points = 20
muchacho1_hit_points = 6
muchacho2_hit_points = 11
muchacho1 = True
muchacho2 = True
while your_hit_points > 0 and (muchacho1 or muchacho2):
print "\n", "-" * 10
your_attack = random.randint(4,12)
muchacho1_attack = random.randint(1,4)
muchacho2_attack = random.randint(4,8)
attack = int(raw_input("Type 1 to attack MUCHACHO1, 2 to attack MUCHACHO2 >"))
if attack == 1:
if muchacho1:
muchacho1_hit_points = muchacho1_hit_points - your_attack
print "You hit MUCHACHO1 for %d hit points." % your_attack
if muchacho1_hit_points <= 0:
muchacho1 = False
print "MUCHACHO1 killed!"
else:
print "MUCHACHO 1 is already dead!"
elif attack == 2:
if muchacho2:
muchacho2_hit_points = muchacho2_hit_points - your_attack
print "You hit MUCHACHO2 for %d hit points." % your_attack
if muchacho2_hit_points <= 0:
muchacho2 = False
print "MUCHACHO2 killed!"
else:
print "MUCHACHO 2 is already dead!"
else:
print "DOES NOT COMPUTE"
if muchacho1:
your_hit_points = your_hit_points - muchacho1_attack
print ("MUCHACHO1 hits you for %d points, you have %d"
" hit points left." %(muchacho1_attack, your_hit_points))
if your_hit_points <= 0:
print 'You are dead'
break
if muchacho2:
your_hit_points = your_hit_points - muchacho2_attack
print ("MUCHACHO2 hits you for %d points, you have %d"
" hit points left." % (muchacho2_attack, your_hit_points))
if your_hit_points <= 0:
print 'You are dead'
a_fight = Fight()
a_fight.enter()