请用python 写一个简单的RPG游戏
时间: 2023-12-19 08:06:42 浏览: 274
好的,我可以为您提供一个简单的 RPG 游戏设计,但是由于篇幅和时间限制,我只能提供一个简单的版本,您可以自行根据需求进行扩展。
游戏规则:
- 玩家可以选择角色类型(战士、法师、刺客)
- 玩家可以选择装备(武器、防具、饰品)
- 玩家可以与怪物战斗,战斗胜利可获得经验值和金币
- 经验值可以升级,升级后可以提升角色的属性值
- 金币可以购买装备和药品
代码实现:
```python
import random
# 定义角色类
class Role:
def __init__(self, name, role_type, health, attack, defense, magic):
self.name = name
self.role_type = role_type
self.health = health
self.attack = attack
self.defense = defense
self.magic = magic
self.level = 1
self.exp = 0
self.max_exp = 100
def show_info(self):
print(f"{self.name} ({self.role_type}) Level: {self.level}")
print(f"Health: {self.health}, Attack: {self.attack}, Defense: {self.defense}, Magic: {self.magic}")
print(f"Exp: {self.exp}/{self.max_exp}")
def attack_target(self, target):
damage = max(0, self.attack - target.defense)
target.health -= damage
print(f"{self.name} attacks {target.name} and deals {damage} damage.")
if target.health <= 0:
print(f"{target.name} is defeated.")
self.gain_exp(30)
def gain_exp(self, exp):
self.exp += exp
if self.exp >= self.max_exp:
self.level_up()
def level_up(self):
self.level += 1
self.exp = 0
self.max_exp = self.max_exp * 2
self.health += random.randint(5, 10)
self.attack += random.randint(1, 3)
self.defense += random.randint(1, 3)
self.magic += random.randint(1, 3)
print(f"{self.name} levels up. Health: {self.health}, Attack: {self.attack}, Defense: {self.defense}, Magic: {self.magic}")
# 定义装备类
class Equipment:
def __init__(self, name, equipment_type, health, attack, defense, magic, price):
self.name = name
self.equipment_type = equipment_type
self.health = health
self.attack = attack
self.defense = defense
self.magic = magic
self.price = price
def show_info(self):
print(f"{self.name} ({self.equipment_type}) Health: {self.health}, Attack: {self.attack}, Defense: {self.defense}, Magic: {self.magic}, Price: {self.price}")
# 定义怪物类
class Monster:
def __init__(self, name, health, attack, defense, gold):
self.name = name
self.health = health
self.attack = attack
self.defense = defense
self.gold = gold
def show_info(self):
print(f"{self.name} Health: {self.health}, Attack: {self.attack}, Defense: {self.defense}, Gold: {self.gold}")
def attack_target(self, target):
damage = max(0, self.attack - target.defense)
target.health -= damage
print(f"{self.name} attacks {target.name} and deals {damage} damage.")
if target.health <= 0:
print(f"{target.name} is defeated.")
target.gold += self.gold
# 定义战斗函数
def battle(player, monster):
while player.health > 0 and monster.health > 0:
player.attack_target(monster)
if monster.health > 0:
monster.attack_target(player)
if player.health <= 0:
print("You are defeated.")
else:
print("You win!")
print(f"Gold: {player.gold}")
player.gain_exp(50)
# 初始化角色、装备和怪物
player = Role("Player", "Warrior", 100, 20, 10, 5)
sword = Equipment("Sword", "Weapon", 0, 5, 0, 0, 20)
armor = Equipment("Armor", "Armor", 20, 0, 5, 0, 30)
ring = Equipment("Ring", "Accessory", 10, 0, 0, 5, 10)
monster1 = Monster("Goblin", 50, 10, 5, 10)
monster2 = Monster("Ogre", 80, 15, 10, 20)
monster3 = Monster("Dragon", 150, 30, 20, 50)
# 游戏开始
print("Welcome to Simple RPG Game!")
while True:
# 显示角色信息和选项
player.show_info()
print("1. Equipments")
print("2. Battle")
print("3. Quit")
choice = input("Enter your choice: ")
if choice == "1":
# 显示装备信息和选项
print("Equipments:")
sword.show_info()
armor.show_info()
ring.show_info()
print("1. Equip Sword")
print("2. Equip Armor")
print("3. Equip Ring")
print("4. Back")
choice = input("Enter your choice: ")
if choice == "1":
player.attack += sword.attack
print("Sword equipped.")
elif choice == "2":
player.defense += armor.defense
player.health = min(player.health + armor.health, 100)
print("Armor equipped.")
elif choice == "3":
player.magic += ring.magic
player.health = min(player.health + ring.health, 100)
print("Ring equipped.")
elif choice == "2":
# 显示怪物信息和选项
print("Monsters:")
monster1.show_info()
monster2.show_info()
monster3.show_info()
print("1. Battle Goblin (Level 1)")
print("2. Battle Ogre (Level 2)")
print("3. Battle Dragon (Level 3)")
print("4. Back")
choice = input("Enter your choice: ")
if choice == "1":
battle(player, monster1)
elif choice == "2" and player.level >= 2:
battle(player, monster2)
elif choice == "3" and player.level >= 3:
battle(player, monster3)
else:
print("You are not strong enough to battle this monster.")
elif choice == "3":
print("Goodbye!")
break
else:
print("Invalid choice.")
```
这是一个简单的 RPG 游戏,其中包含了角色、装备和怪物三个类,以及战斗函数和游戏开始部分的代码。您可以自行根据需求进行扩展和修改。
阅读全文