c++简单小游戏代码打怪
时间: 2024-02-05 09:01:24 浏览: 28
下面是一个简单的用Python编写的小游戏代码,实现了一个打怪游戏:
```python
import random
# 定义玩家的初始血量和攻击力
player_hp = 100
player_attack = 20
# 定义怪物的初始血量和攻击力
monster_hp = 80
monster_attack = 15
# 游戏开始
print("游戏开始,玩家的初始血量为100,攻击力为20")
print("玩家需要在怪物的攻击力剩下零之前击败怪物")
while player_hp > 0 and monster_hp > 0:
# 玩家攻击怪物
damage = random.randint(player_attack - 10, player_attack + 10)
monster_hp -= damage
print("玩家对怪物造成了{}点伤害".format(damage))
# 怪物攻击玩家
damage = random.randint(monster_attack - 5, monster_attack + 5)
player_hp -= damage
print("怪物对玩家造成了{}点伤害".format(damage))
# 打印玩家和怪物的剩余血量
print("玩家剩余血量:{}".format(player_hp))
print("怪物剩余血量:{}".format(monster_hp))
print()
# 游戏结束,根据玩家和怪物的血量判断胜负
if player_hp > 0 and monster_hp <= 0:
print("玩家胜利!")
elif player_hp <= 0 and monster_hp > 0:
print("怪物胜利!")
else:
print("平局!")
```
这个游戏中,玩家和怪物分别有初始的血量和攻击力。玩家和怪物每回合轮流攻击对方,攻击力在一个范围内随机产生。当玩家或怪物的血量降为0或以下时,游戏结束,并根据剩余的血量判断胜负。