一.用python制作一款简易文字游戏
代码主要实现了一个简单的回合制游戏,包括以下几个要点:
-
Player类:
- Player类定义了游戏中的玩家对象,每个玩家有名称、生命值和攻击力这些属性。
- 每个Player对象有三个方法:attack()用于攻击其他玩家,heal()用于治疗自己,skill()用于使用技能。
- 当玩家生命值降到0以下时,会被判断为死亡。
-
Game类:
- Game类负责游戏的整体流程控制。
- 在游戏开始时创建三个不同的Player对象,代表三个不同的游戏角色。
- play()方法负责游戏的主要逻辑,包括循环执行玩家的操作直到有玩家死亡或者游戏结束。
-
游戏执行逻辑:
- 游戏开始后,玩家轮流进行操作,可以选择攻击其他玩家、治疗自己或使用技能。
- 每个玩家的操作结果会被打印输出,展示了攻击、治疗和技能的效果。
- 当有玩家生命值降至0以下时,会被宣布死亡,并在输出中显示。
- 游戏会判断胜利条件,如果有玩家的生命值大于0,该玩家获胜并游戏结束。
总体而言,这段代码展示了一个简单的回合制游戏实现,包括玩家对象的定义、游戏流程的控制和玩家操作的执行,最终确定哪个玩家在游戏结束时获胜
代码如下
import random
class Player:
def __init__(self, name, hp, atk):
self.name = name
self.hp = hp
self.max_hp = hp
self.atk = atk
self.alive = True
def __str__(self):
return "姓名:%s\n血量:%s/%s\n攻击力:%s" % (self.name, self.hp, self.max_hp, self.atk)
def attack(self, enemy):
if not self.alive:
return "%s已经死亡,无法攻击!" % self.name
if enemy.alive:
if enemy.hp > self.atk:
damage = self.atk
else:
damage = enemy.hp
enemy.hp -= damage
result = "%s发动了攻击,%s受到了%s点伤害。" % (self.name, enemy.name, damage)
if enemy.hp <= 0:
enemy.alive = False
result += "%s已经死亡!" % enemy.name
return result
else:
return "%s已经死亡,无法被攻击!" % enemy.name
def heal(self):
if not self.alive:
return "%s已经死亡,无法治疗!" % self.name
self.hp += 50
if self.hp > self.max_hp:
self.hp = self.max_hp
return "%s使用治疗技能,回复了50点血量。" % self.name
class GuanYu(Player):
def __init__(self, name, hp, atk):
super().__init__(name, hp, atk)
self.skill_name = "苍天之怒"
def use_skill(self, enemy):
if not self.alive:
return "%s已经死亡,无法使用技能!" % self.name
if enemy.alive:
damage = self.atk * 2
enemy.hp -= damage
result = "%s发动了技能“%s”,对%s造成了%s点伤害。" % (self.name, self.skill_name, enemy.name, damage)
if enemy.hp <= 0:
enemy.alive = False
result += "%s已经死亡!" % enemy.name
return result
else:
return "%s已经死亡,无法被攻击!" % enemy.name
class Ahri(Player):
def __init__(self, name, hp, atk):
super().__init__(name, hp, atk)
self.skill_name = "魅惑之狐"
def use_skill(self, enemy):
if not self.alive:
return "%s已经死亡,无法使用技能!" % self.name
if enemy.alive:
success_rate = random.randint(1, 100)
if success_rate <= 50:
enemy.alive = False
result = "%s发动了技能“%s”,成功将%s魅惑致死!" % (self.name, self.skill_name, enemy.name)
else:
result = "%s发动了技能“%s”,但%s免疫了魅惑效果。" % (self.name, self.skill_name, enemy.name)
return result
else:
return "%s已经死亡,无法被攻击!" % enemy.name
class MasterYi(Player):
def __init__(self, name, hp, atk):
super().__init__(name, hp, atk)
self.skill_name = "高原之舞"
def use_skill(self):
if not self.alive:
return "%s已经死亡,无法使用技能!" % self.name
self.atk *= 2
self.hp *= 2
self.max_hp *= 2
self.alive = False
return "%s发动了技能“%s”,攻击力和血量翻倍,但因过度疲劳导致瞬间死亡。" % (self.name, self.skill_name)
class Game:
def __init__(self, player1, player2, player3):
self.player1 = player1
self.player2 = player2
self.player3 = player3
self.round = 1
def play(self):
print("游戏开始!")
while self.player1.alive + self.player2.alive + self.player3.alive > 1:
print("第%s回合:" % self.round)
choice = input("请选择操作(1.攻击 2.治疗 3.使用技能):")
if choice == "1":
attacker = input("请选择攻击者(1.%s 2.%s 3.%s):" % (self.player1.name, self.player2.name, self.player3.name))
if attacker == "1":
defender = input("请选择被攻击者(1.%s 2.%s):" % (self.player2.name, self.player3.name))
if defender == "1":
result = self.player1.attack(self.player2)
if not self.player2.alive:
print(result)
else:
result = self.player1.attack(self.player3)
if not self.player3.alive:
print(result)
elif attacker == "2":
defender = input("请选择被攻击者(1.%s 2.%s):" % (self.player1.name, self.player3.name))
if defender == "1":
result = self.player2.attack(self.player1)
if not self.player1.alive:
print(result)
else:
result = self.player2.attack(self.player3)
if not self.player3.alive:
print(result)
else:
defender = input("请选择被攻击者(1.%s 2.%s):" % (self.player1.name, self.player2.name))
if defender == "1":
result = self.player3.attack(self.player1)
if not self.player1.alive:
print(result)
else:
result = self.player3.attack(self.player2)
if not self.player2.alive:
print(result)
print(result)
elif choice == "2":
healer = input("请选择使用治疗技能的玩家(1.%s 2.%s 3.%s):" % (self.player1.name, self.player2.name, self.player3.name))
if healer == "1":
result = self.player1.heal()
elif healer == "2":
result = self.player2.heal()
else:
result = self.player3.heal()
print(result)
elif choice == "3":
attacker = input("请选择使用技能的玩家(1.%s 2.%s 3.%s):" % (self.player1.name, self.player2.name, self.player3.name))
if attacker == "1":
enemy = random.choice([self.player2, self.player3])
result = self.player1.use_skill(enemy)
if not enemy.alive:
print(result)
elif attacker == "2":
enemy = random.choice([self.player1, self.player3])
result = self.player2.use_skill(enemy)
if not enemy.alive:
print(result)
else:
result = self.player3.use_skill()
print(result)
print(result)
else:
print("无效操作!")
continue
if self.player1.alive:
print(self.player1)
if self.player2.alive:
print(self.player2)
if self.player3.alive:
print(self.player3)
self.round += 1
if self.player1.alive:
print("%s获胜!" % self.player1.name)
elif self.player2.alive:
print("%s获胜!" % self.player2.name)
else:
print("%s获胜!" % self.player3.name)
player1 = GuanYu("关羽", 100, 20)
player2 = Ahri("阿狸", 200, 35)
player3 = MasterYi("易大师", 50, random.randint(1, 100))
game = Game(player1, player2, player3)
game.play()