python修仙养成游戏代码
时间: 2024-04-07 19:26:42 浏览: 241
python文字修仙游戏(半成品)
5星 · 资源好评率100%
Python修仙养成游戏是一种基于Python语言开发的游,玩家可以在游戏中扮演一个修仙者,通过修炼、任务和战斗等方式提升自己的修仙实力。以下是一个简单的Python修仙养成游戏代码示例:
```python
import random
class Player:
def __init__(self, name):
self.name = name
self.level = 1
self.exp = 0
self.hp = 100
self.mp = 100
def level_up(self):
self.level += 1
self.hp += 10
self.mp += 10
def gain_exp(self, exp):
self.exp += exp
if self.exp >= 100:
self.level_up()
self.exp = 0
def attack(self, enemy):
damage = random.randint(10, 20)
enemy.hp -= damage
if enemy.hp <= 0:
print(f"{enemy.name}被击败了!")
self.gain_exp(50)
class Enemy:
def __init__(self, name, hp):
self.name = name
self.hp = hp
def attack(self, player):
damage = random.randint(5, 15)
player.hp -= damage
if player.hp <= 0:
print(f"{player.name}被击败了!")
player_name = input("请输入你的角色名:")
player = Player(player_name)
while True:
enemy_name = input("请输入敌人的名字(输入q退出游戏):")
if enemy_name == "q":
break
enemy_hp = random.randint(50, 100)
enemy = Enemy(enemy_name, enemy_hp)
while True:
print(f"{player.name}的血量:{player.hp}")
print(f"{enemy.name}的血量:{enemy.hp}")
choice = input("请选择行动(1.攻击 2.逃跑):")
if choice == "1":
player.attack(enemy)
if enemy.hp <= 0:
break
enemy.attack(player)
if player.hp <= 0:
break
elif choice == "2":
print("你逃跑了!")
break
else:
print("无效的选择!")
print("游戏结束!")
```
这段代码实现了一个简单的修仙养成游戏,玩家可以输入角色名和敌人名字,然后进行攻击或逃跑的选择。玩家和敌人的血量会根据攻击造成的伤害进行相应的减少,当其中一方血量归零时,游戏结束。
阅读全文