主角打怪升级代码python
时间: 2023-10-13 14:15:42 浏览: 199
以下是一个简单的主角打怪升级的代码示例,使用Python语言实现:
```python
import random
player_level = 1
player_exp = 0
player_max_exp = 10
player_hp = 100
player_max_hp = 100
player_attack = 10
monster_level = 1
monster_hp = 50
monster_max_hp = 50
monster_attack = 5
print("你遇到了一个等级为", monster_level, "的怪物,准备战斗!")
while player_hp > 0 and monster_hp > 0:
# 玩家击怪物
damage = random.randint(player_attack - 2, player_attack + 2)
monster_hp -= damage
print("你攻击了怪物,造成了", damage, "点伤害。怪物剩余血量:", monster_hp)
# 怪物攻击玩家
damage = random.randint(monster_attack - 2, monster_attack + 2)
player_hp -= damage
print("怪物攻击了你,造成了", damage, "点伤害。你剩余血量:", player_hp)
# 判断战斗结果
if player_hp <= 0:
print("你被怪物击败了,游戏结束。")
break
if monster_hp <= 0:
print("你击败了怪物,获得了", monster_level * 10, "点经验值。")
player_exp += monster_level * 10
if player_exp >= player_max_exp:
player_level += 1
player_max_exp *= 2
player_hp = player_max_hp
player_attack += 5
print("恭喜你升级了!当前等级为", player_level, ",最大生命值增加到", player_max_hp, ",攻击力增加到", player_attack, "。")
# 生成新的怪物
monster_level += 1
monster_max_hp *= 2
monster_hp = monster_max_hp
monster_attack += 5
print("你遇到了一个等级为", monster_level, "的怪物,准备战斗!")
```
在这个代码中,玩家和怪物都有等级、生命值、攻击力等属性。玩家和怪物每次攻击都会随机产生一定的伤害值,直到一方的生命值降为0为止。如果玩家胜利,就会获得一定的经验值,并有可能升级。升级后,玩家的属性会得到提升,同时会生成一个等级更高的新怪物。如果玩家失败,则游戏结束。
阅读全文