用python写个RGP游戏
时间: 2023-05-13 17:05:06 浏览: 115
使用Python写一个小游戏
好的,以下是一个简单的Python RGP游戏的代码:
```
import random
class Character:
def __init__(self, name, hp, attack):
self.name = name
self.hp = hp
self.attack = attack
def is_alive(self):
return self.hp > 0
def attack_enemy(self, enemy):
damage = random.randint(1, self.attack)
enemy.hp -= damage
print(f"{self.name}攻击了{enemy.name},造成了{damage}点伤害。")
class Player(Character):
def __init__(self, name, hp, attack):
super().__init__(name, hp, attack)
self.potions = 3
def use_potion(self):
if self.potions > 0:
self.hp += 20
self.potions -= 1
print(f"{self.name}使用了一瓶药水,恢复了20点生命值。")
else:
print("你没有药水了!")
class Enemy(Character):
pass
def main():
player = Player("玩家", 100, 20)
enemy = Enemy("魔王", 200, 10)
while player.is_alive() and enemy.is_alive():
print(f"{player.name}的生命值:{player.hp}")
print(f"{enemy.name}的生命值:{enemy.hp}")
print("1. 攻击")
print("2. 使用药水")
choice = input("请选择:")
if choice == "1":
player.attack_enemy(enemy)
elif choice == "2":
player.use_potion()
else:
print("无效的选择!")
if enemy.is_alive():
enemy.attack_enemy(player)
if player.is_alive():
print("你赢了!")
else:
print("你输了!")
if __name__ == "__main__":
main()
```
希望这个代码对你有所帮助!
阅读全文