为了优化战斗循环以避免不必要的重复,并提升代码的可读性,通常需要关注以下几个方面:
if-else
语句,使用简洁的逻辑表达。我们可以通过一个简单的战斗循环示例来演示这些原则。假设我们有一个玩家与敌人进行回合制战斗的游戏。
1、问题背景
在给定的代码中,玩家可以为两个角色创建一个战斗游戏。游戏包括两个角色: Charone 和 Chartwo,都有力量和技能属性。玩家可以为每个属性输入值,然后游戏将计算他们的修改器,根据角色的差异,然后他们将掷骰子来确定谁赢得了回合。战斗循环将继续进行,直到其中一个角色的强度属性达到0,此时该角色死亡,游戏结束。
然而,在给定的代码中,存在重复代码,导致可读性差,维护困难。代码中,骰子滚动功能运行两次,这似乎是不必要的。此外,还有许多全局变量,导致代码难以阅读和理解。
2、解决方案
为了解决代码中的问题,改进战斗循环,使其变得更简洁和易读,具体改进措施包括:
下面是改进后的代码:
import random
import time
import math
import sys
class Fighter:
def __init__(self, name, strength, skill):
self.name = name
self.strength = strength
self.skill = skill
def is_alive(self):
return self.strength > 0
def health_mod(self, other_fighter):
delta = abs(self.strength - other_fighter.strength)
return int(delta / 5)
def skill_mod(self, other_fighter):
delta = abs(self.skill - other_fighter.skill)
return int(delta / 5)
def attack(self, other_fighter):
# wait_for_enter("Hit Enter to fight!")
# figure out mod values
health_mod = self.health_mod(other_fighter)
skill_mod = self.skill_mod(other_fighter)
self_roll = roll()
other_roll = roll()
# slow_print(
# "Health mod: {} Skill mod: {} {} rolls {} {} rolls {}"
# .format(
# health_mod, skill_mod,
# self.name, self_roll,
# other_fighter.name, other_roll
# )
# )
# figure out who won this round
if self_roll == other_roll:
print("Draw! No damage done.")
else:
winner, loser = (self, other_fighter) if self_roll > other_roll else (other_fighter, self)
print("{} hits {}!".format(winner.name, loser.name))
winner.strength += health_mod
winner.skill += skill_mod
loser.strength -= health_mod
loser.skill -= skill_mod
# show results
print('')
print(self)
print(other_fighter)
print('')
def __str__(self):
return "{}: strength {}, skill {}".format(self.name, max(self.strength, 0), max(self.skill, 0))
def roll(die_sides=6):
return random.randint(1, die_sides)
def fight():
charone = Fighter("Charone", 60, 70)
chartwo = Fighter("Chartwo", 65, 75)
print("\n{} encounters {}\nBattle Initiated!".format(charone.name, chartwo.name))
while charone.is_alive() and chartwo.is_alive():
charone.attack(chartwo)
chartwo.attack(charone)
winner, loser = (charone, chartwo) if charone.is_alive() else (chartwo, charone)
print("{} has died; {} wins!".format(loser.name, winner.name))
def main():
while True:
fight()
if not get_tf("Would you like to play again? (Y/n)"):
print("Goodbye!")
break
if __name__ == "__main__":
main()
pause
来添加延迟,以便用户可以看到正在发生的事情。slow_print
将骰子掷骰的结果缓慢地打印出来,以便更具戏剧性。wait_for_enter
函数来等待用户按回车键,这样他们才能在每个回合中继续前进。charone_strength
和 chartwo_strength
变量初始化为 60 和 65,并将 charone_skill
和 chartwo_skill
变量初始化为 70 和 75。battle
函数中,使用 charone
和 chartwo
对象来跟踪每个角色的属性,而不是使用全局变量。roll
函数:def roll(die_sides=6):
return random.randint(1, die_sides)
Fighter
类:class Fighter:
def __init__(self, name, strength, skill):
self.name = name
self.strength = strength
self.skill = skill
def is_alive(self):
return self.strength > 0
def health_mod(self, other_fighter):
delta = abs(self.strength - other_fighter.strength)
return int(delta / 5)
def skill_mod(self, other_fighter):
delta = abs(self.skill - other_fighter.skill)
return int(delta / 5)
def attack(self, other_fighter):
# wait_for_enter("Hit Enter to fight!")
# figure out mod values
health_mod = self.health_mod(other_fighter)
skill_mod = self.skill_mod(other_fighter)
self_roll = roll()
other_roll = roll()
print(
"Health mod: {} Skill mod: {} {} rolls {} {} rolls {}"
.format(
health_mod, skill_mod,
self.name, self_roll,
other_fighter.name, other_roll
)
)
# figure out who won this round
if self_roll == other_roll:
print("Draw! No damage done.")
else:
winner, loser = (self, other_fighter) if self_roll > other_roll else (other_fighter, self)
print("{} hits {}!".format(winner.name, loser.name))
winner.strength += health_mod
winner.skill += skill_mod
loser.strength -= health_mod
loser.skill -= skill_mod
# show results
print('')
print(self)
print(other_fighter)
print('')
def __str__(self):
return "{}: strength {}, skill {}".format(self.name, max(self.strength, 0), max(self.skill, 0))
battle
函数:def battle(charone, chartwo):
global charone_strength
global charone_skill
global chartwo_strength
global chartwo_skill
if player1 == player2:
print("\nThis round is a draw! No damage done")
elif player1 > player2:
charone_strength = charone_str + str_mod
charone_skill = charone_skl + skl_mod
chartwo_strength = chartwo_skl - str_mod
chartwo_skill = chartwo_skl - skl_mod
print "\n"+charone+" won this round"
print "\n"+"Character 1:",charone
print "Strength:",charone_strength
print "Skill:",charone_skill
time.sleep(1)
print "\nCharacter 2:",chartwo
print "Strength:",chartwo_strength
print "Skill:",chartwo_skill
elif player2 > player1:
chartwo_strength = chartwo_str + str_mod
chartwo_skill = chartwo_skl + skl_mod
charone_strength = charone_str - str_mod
charone_skill = charone_skl - skl_mod
print "\n"+chartwo+" won this round"
print "\nCharacter 2:",chartwo
print "Strength
改进后的战斗循环具有以下优势:
这种编写方式使代码更具可读性和灵活性,是对复杂战斗系统的一个良好开端。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。