我为简单的游戏编写了一个程序(两个战士之间的战斗)。每个战士都有名字,100点生命值和伤害点列表。
目前,程序运行良好,但我编写它时,每次一架战斗机攻击另一架(“悟空”攻击"Konan")。名为startTheFight的函数就是这样做的。
悟空袭击Konnan..。悟空袭击Konnan..。悟空袭击Konnan..。...and等
如何使这个功能交替执行,这样我就可以看到谁赢了(“悟空”还是“科南”)?此外,当我调用函数upgradeAttack时,它不工作,我不知道为什么我想要这样的输出:
悟空袭击Konnan..。孔南袭击悟空..。悟空袭击Konnan..。孔南袭击悟空..。
这是代码:
import random
class Fighters:
attackUp = 0
def __init__(self, name):
self.name = name
self.health = 100
self.damage = [5, 10, 15, 20]
def upgradeAttack(self, attUp):
self.attackUp = self.attackUp + attUp
print(self.attackUp)
return self.attackUp
def startTheFight(self, otherFighter):
while otherFighter.health > 0:
AttactDamage = random.choice(self.damage) + self.attackUp
otherFighter.health = otherFighter.health - AttactDamage
if otherFighter.health > 0:
print("{} attacts {}! Health -{}hp | {}'s Health: {}hp\n".format(self.name, otherFighter.name,
AttactDamage,otherFighter.name, otherFighter.health))
else:
otherFighter.health = 0
print("{} attacts {}! Health -{}hp | {}'s Health: {}hp\n".format(self.name, otherFighter.name,
AttactDamage,otherFighter.name, otherFighter.health))
else:
if otherFighter.health <= 0:
otherFighter.health = 0
print("{} killed {}! | {}'s Health: {}hp\n{} wins!\n\n".format(self.name, otherFighter.name,
otherFighter.name, otherFighter.health, self.name))
def __str__(self):
return "Fighters name: {}\nFighters health: {}hp\n".format(self.name, self.health)
Goku = Fighters("Goku")
Konan = Fighters("Konan")
#print(Goku)
#print(Konan)
Goku.startTheFight(Konan)发布于 2018-11-18 23:01:12
我已经对下面的代码进行了修改。这里的工作示例。一些注意事项:
__str__方法过于复杂,使用不当(实际上不应该直接使用print )。Fighters.hit()方法)是,您可以为不同的战斗人员分配不同的伤害数组,理论上,代码仍然应该像预期的那样工作。startTheFight方法被用来在两者之间进行斗争(这本身并不是错误的,但我个人更希望startFight是一个类方法),这仍然让我感到尴尬。f"{var_name}" (仅在3.6+中),并且应该确保字符串方法是有意义的--通常更容易/更明智/更容易阅读以“构建”最终输出(字符串用于打印、列表、dict等),而不是让大量随机语句在整个函数中提供输出。代码:
import random
class Fighters:
def __init__(self, name):
self.name = name
self.health = 100
self.damage = [5, 10, 15, 20]
def __str__(self):
return self.name
def hit(attacking_fighter, defending_fighter):
attack_damage = random.choice(attacking_fighter.damage)
defending_fighter.health -= attack_damage
if defending_fighter.health > 0:
print_str = f"{attacking_fighter} attacks {defending_fighter}!"
else:
print_str = f"{attacking_fighter} killed {defending_fighter}! {attacking_fighter} wins the fight!"
print_str += f" Hit for {attack_damage} damage | {defending_fighter}'s Health: {defending_fighter.health}hp"
print(print_str, end="\n\n")
return
def startTheFight(self, otherFighter):
while otherFighter.health > 0 and self.health > 0:
self.hit(otherFighter)
if otherFighter.health <= 0 or self.health <= 0:
break
otherFighter.hit(self)
Goku = Fighters("Goku")
Konan = Fighters("Konan")
#print(Goku)
#print(Konan)
Goku.startTheFight(Konan)示例输出:
Goku attacks Konan! Hit for 10 damage | Konan's Health: 90hp
Konan attacks Goku! Hit for 10 damage | Goku's Health: 90hp
Goku attacks Konan! Hit for 5 damage | Konan's Health: 85hp
Konan attacks Goku! Hit for 10 damage | Goku's Health: 80hp
Goku attacks Konan! Hit for 5 damage | Konan's Health: 80hp
Konan attacks Goku! Hit for 20 damage | Goku's Health: 60hp
Goku attacks Konan! Hit for 10 damage | Konan's Health: 70hp
Konan attacks Goku! Hit for 15 damage | Goku's Health: 45hp
Goku attacks Konan! Hit for 10 damage | Konan's Health: 60hp
Konan attacks Goku! Hit for 20 damage | Goku's Health: 25hp
Goku attacks Konan! Hit for 5 damage | Konan's Health: 55hp
Konan attacks Goku! Hit for 10 damage | Goku's Health: 15hp
Goku attacks Konan! Hit for 10 damage | Konan's Health: 45hp
Konan killed Goku! Konan wins the fight! Hit for 15 damage | Goku's Health: 0hp发布于 2018-11-19 02:27:07
还有几个进一步的建议:一个模拟战士的类被称为Fighters是没有意义的。应该是单数的,Fighter。战士没有名字,一个战士有名字。虽然一个战士可以开始战斗,但奇怪的是,有一个战士指挥它;我宁愿有一个静态方法(Fighter.fight(goku, conan)),或者有一个不同的类,Fight,它代表了两个人战斗的事件的一个实例。它还将负责报道这场战斗。对于一个实例来说,对另一个实例进行更改也不是很好;Conan可以命中,但是只有小悟空才能对自己的健康进行治理。while循环比它所需要的要复杂得多;战斗的结构是攻击者击中防御者,然后他们交换位置。这使得流控制变得非常容易。最后,根据Python命名约定,实例变量应该是小写;人们可能会被骗以为Goku和Conan是类名。(这只是一个例子,还有其他案例问题。)因此,考虑到这一点,稍微重写一次重写:
import random
class Fighter:
def __init__(self, name):
self.name = name
self.health = 100
self.damage = [5, 10, 15, 20]
self.strength = 0
def __str__(self):
return self.name
def hit(self, defender):
attack_damage = random.choice(self.damage) + self.strength
defender.suffer(attack_damage)
return attack_damage
def suffer(self, attack_damage):
self.health -= attack_damage
def is_dead(self):
return self.health <= 0
def fight(self, other):
return Fight(self, other)
class Fight:
def __init__(self, attacker, defender):
self.attacker = attacker
self.defender = defender
def round(self):
damage = self.attacker.hit(self.defender)
if self.defender.is_dead():
print_str = f"{self.attacker} killed {self.defender}! {self.attacker} wins the fight!"
else:
print_str = f"{self.attacker} attacks {self.defender}!"
print_str += f" Hit for {damage} damage | {self.defender}'s Health: {self.defender.health}hp"
print(print_str, end="\n\n")
self.attacker, self.defender = self.defender, self.attacker
def run(self):
while not self.attacker.is_dead():
self.round()
goku = Fighter("Goku")
konan = Fighter("Konan")
goku.strength = 3
goku.fight(konan).run()从这里开始,一个高级的练习就是把战斗变成一个发电机,这样你就可以更容易地一轮一轮地运行它。
https://stackoverflow.com/questions/53366069
复制相似问题