Write simple rpg game
- User
Write simple rpg game
- Assistant
Write simple rpg game
Certainly! Below is a simple text-based RPG (Role-Playing Game) written in Python. This game allows you to create a character, explore a world, battle monsters, and collect items.
import random
class Character:
def __init__(self, name, char_class):
self.name = name
self.char_class = char_class
self.hp = 100 # Health Points
self.max_hp = 100
self.attack = 10
self.defense = 5
self.inventory = []
# Customize stats based on character class
if char_class == "Warrior":
self.attack += 5
self.defense += 5
elif char_class == "Mage":
self.attack += 10
elif char_class == "Thief":
self.defense += 10
def is_alive(self):
return self.hp > 0
def show_stats(self):
print(f"\n--- {self.name}'s Stats ---")
print(f"Class: {self.char_class}")
print(f"HP: {self.hp}/{self.max_hp}")
print(f"Attack: {self.attack}")
print(f"Defense: {self.defense}")
def show_inventory(self):
print(f"\n--- {self.name}'s Inventory ---")
if self.inventory:
for item in self.inventory:
print(f"- {item}")
else:
print("Inventory is empty.")
class Monster:
def __init__(self):
self.name = random.choice(["Goblin", "Orc", "Skeleton", "Zombie"])
self.hp = random.randint(20, 50)
self.attack = random.randint(5, 15)
self.defense = random.randint(0, 5)
def is_alive(self):
return self.hp > 0
def battle(player, monster):
print(f"\nA wild {monster.name} appears!")
while player.is_alive() and monster.is_alive():
action = input("\nDo you want to (A)ttack or (R)un? ").lower()
if action == 'a':
# Player attacks monster
damage = max(player.attack - monster.defense, 1)
monster.hp -= damage
print(f"\nYou attack the {monster.name} for {damage} damage.")
if not monster.is_alive():
print(f"The {monster.name} has been defeated!")
loot = random.choice(["Gold Coin", "Healing Potion", "Magic Scroll"])
player.inventory.append(loot)
print(f"You found a {loot}!")
break
# Monster attacks player
damage = max(monster.attack - player.defense, 1)
player.hp -= damage
print(f"The {monster.name} attacks you for {damage} damage.")
if not player.is_alive():
print("You have been defeated by the monster...")
elif action == 'r':
print(f"\nYou run away from the {monster.name}!")
break
else:
print("Invalid action. Please choose 'A' to attack or 'R' to run.")
def main():
print("Welcome to the Simple Text RPG!")
name = input("Enter your character's name: ")
print("\nChoose your class:")
print("1. Warrior")
print("2. Mage")
print("3. Thief")
class_choice = input("Enter the number of your choice: ")
classes = {'1': 'Warrior', '2': 'Mage', '3': 'Thief'}
char_class = classes.get(class_choice, 'Warrior')
if char_class != classes.get(class_choice):
print("Invalid choice. Defaulting to Warrior.")
player = Character(name, char_class)
print(f"\nWelcome, {player.name} the {player.char_class}!")
while player.is_alive():
print("\nWhat would you like to do?")
print("1. Explore")
print("2. Check Stats")
print("3. Check Inventory")
print("4. Rest")
print("5. Quit Game")
action = input("Enter the number of your choice: ")
if action == '1':
monster = Monster()
battle(player, monster)
elif action == '2':
player.show_stats()
elif action == '3':
player.show_inventory()
elif action == '4':
player.hp = player.max_hp
print("\nYou take a rest and recover to full health.")
elif action == '5':
print("\nThank you for playing!")
break
else:
print("Invalid choice. Please select a valid option.")
if not player.is_alive():
print("\nGame Over. Better luck next time!")
if __name__ == "__main__":
main()
Save the Code: Copy the code above into a file named simple_rpg.py.
Run the Game: Open a terminal or command prompt, navigate to the directory containing simple_rpg.py, and run:
python simple_rpg.py
Make sure you have Python 3 installed on your system.
Character Creation:
Main Menu Options:
Exploration and Combat:
Resting:
Game Over:
This simple RPG provides a foundation that you can build upon. Here are some ideas for extending the game:
By experimenting and adding new features, you can enhance your programming skills and make the game more engaging.
Enjoy your adventure in this simple RPG game! Feel free to modify and expand the code to create a more complex and personalized gaming experience.
Not yet included in a published release.