Create python rougelike text based game, with coins, randomly generated items, randomly…
- User
Create python rougelike text based game, with coins, randomly generated items, randomly generated words, etc etc
- Assistant
Create python rougelike text based game, with coins, randomly generated items, randomly generated words, etc etc
Certainly! Let's create a simple text-based roguelike game in Python. This game will feature:
Here's a step-by-step guide to building the game:
We'll use the random and sys modules for generating random events and exiting the game.
import random
import sys
Represent the player with attributes like health, coins, inventory, and position.
class Player:
def __init__(self):
self.name = "Adventurer"
self.health = 100
self.coins = 0
self.inventory = []
self.position = (0, 0) # Start at the origin
def move(self, direction):
x, y = self.position
if direction == "north":
self.position = (x, y + 1)
elif direction == "south":
self.position = (x, y - 1)
elif direction == "east":
self.position = (x + 1, y)
elif direction == "west":
self.position = (x - 1, y)
print(f"You moved {direction} to {self.position}.")
Represent items with a name, description, and value.
class Item:
def __init__(self, name, description, value):
self.name = name
self.description = description
self.value = value
Generate random item names using sample adjectives and nouns.
def generate_random_name():
adjectives = ["Ancient", "Mystical", "Enchanted", "Glowing", "Cursed"]
nouns = ["Sword", "Shield", "Potion", "Amulet", "Ring"]
return f"{random.choice(adjectives)} {random.choice(nouns)}"
Create an item with a random name and value.
def generate_random_item():
name = generate_random_name()
description = f"A {name.lower()} that looks valuable."
value = random.randint(10, 100)
return Item(name, description, value)
Represent the world as a dictionary where each position can have events like finding coins or items.
world = {}
def generate_event(position):
events = []
# 50% chance to find coins
if random.random() < 0.5:
coins_found = random.randint(1, 50)
events.append(('coins', coins_found))
# 30% chance to find an item
if random.random() < 0.3:
item_found = generate_random_item()
events.append(('item', item_found))
world[position] = events
Show the player's current health, coins, inventory, and position.
def display_status(player):
print("\n--- Status ---")
print(f"Health: {player.health}")
print(f"Coins: {player.coins}")
print(f"Position: {player.position}")
if player.inventory:
print("Inventory:")
for item in player.inventory:
print(f"- {item.name}: {item.description} (Value: {item.value})")
else:
print("Inventory: Empty")
print("--------------\n")
Process player input to move, pick up items, or quit the game.
def process_action(player, action):
action = action.lower()
if action in ["north", "south", "east", "west"]:
player.move(action)
position = player.position
if position not in world:
generate_event(position)
events = world.get(position, [])
for event in events:
if event[0] == 'coins':
print(f"You found {event[1]} coins!")
elif event[0] == 'item':
print(f"You see a {event[1].name}.")
elif action == "pick up":
position = player.position
events = world.get(position, [])
for event in events[:]:
if event[0] == 'coins':
player.coins += event[1]
print(f"You picked up {event[1]} coins.")
events.remove(event)
elif event[0] == 'item':
player.inventory.append(event[1])
print(f"You picked up the {event[1].name}.")
events.remove(event)
world[position] = events
elif action == "inventory":
if player.inventory:
print("You are carrying:")
for item in player.inventory:
print(f"- {item.name}: {item.description} (Value: {item.value})")
else:
print("Your inventory is empty.")
elif action == "quit":
print("Thanks for playing!")
sys.exit()
else:
print("Invalid action. Try 'north', 'south', 'east', 'west', 'pick up', 'inventory', or 'quit'.")
Run the game, prompting the player for actions.
def main():
player = Player()
print("Welcome to the Text-Based Roguelike Game!")
while True:
display_status(player)
action = input("What do you want to do? ")
process_action(player, action)
Check if the script is run directly and start the game.
if __name__ == "__main__":
main()
Here's the complete code assembled together:
import random
import sys
class Player:
def __init__(self):
self.name = "Adventurer"
self.health = 100
self.coins = 0
self.inventory = []
self.position = (0, 0)
def move(self, direction):
x, y = self.position
if direction == "north":
self.position = (x, y + 1)
elif direction == "south":
self.position = (x, y - 1)
elif direction == "east":
self.position = (x + 1, y)
elif direction == "west":
self.position = (x - 1, y)
print(f"You moved {direction} to {self.position}.")
class Item:
def __init__(self, name, description, value):
self.name = name
self.description = description
self.value = value
def generate_random_name():
adjectives = ["Ancient", "Mystical", "Enchanted", "Glowing", "Cursed"]
nouns = ["Sword", "Shield", "Potion", "Amulet", "Ring"]
return f"{random.choice(adjectives)} {random.choice(nouns)}"
def generate_random_item():
name = generate_random_name()
description = f"A {name.lower()} that looks valuable."
value = random.randint(10, 100)
return Item(name, description, value)
world = {}
def generate_event(position):
events = []
if random.random() < 0.5:
coins_found = random.randint(1, 50)
events.append(('coins', coins_found))
if random.random() < 0.3:
item_found = generate_random_item()
events.append(('item', item_found))
world[position] = events
def display_status(player):
print("\n--- Status ---")
print(f"Health: {player.health}")
print(f"Coins: {player.coins}")
print(f"Position: {player.position}")
if player.inventory:
print("Inventory:")
for item in player.inventory:
print(f"- {item.name}: {item.description} (Value: {item.value})")
else:
print("Inventory: Empty")
print("--------------\n")
def process_action(player, action):
action = action.lower()
if action in ["north", "south", "east", "west"]:
player.move(action)
position = player.position
if position not in world:
generate_event(position)
events = world.get(position, [])
for event in events:
if event[0] == 'coins':
print(f"You found {event[1]} coins!")
elif event[0] == 'item':
print(f"You see a {event[1].name}.")
elif action == "pick up":
position = player.position
events = world.get(position, [])
for event in events[:]:
if event[0] == 'coins':
player.coins += event[1]
print(f"You picked up {event[1]} coins.")
events.remove(event)
elif event[0] == 'item':
player.inventory.append(event[1])
print(f"You picked up the {event[1].name}.")
events.remove(event)
world[position] = events
elif action == "inventory":
if player.inventory:
print("You are carrying:")
for item in player.inventory:
print(f"- {item.name}: {item.description} (Value: {item.value})")
else:
print("Your inventory is empty.")
elif action == "quit":
print("Thanks for playing!")
sys.exit()
else:
print("Invalid action. Try 'north', 'south', 'east', 'west', 'pick up', 'inventory', or 'quit'.")
def main():
player = Player()
print("Welcome to the Text-Based Roguelike Game!")
while True:
display_status(player)
action = input("What do you want to do? ")
process_action(player, action)
if __name__ == "__main__":
main()
north, south, east, or west to move.pick up to collect items or coins in your current location.inventory to see what you're carrying.quit to exit the game.Now that you have a basic game, you can enhance it by:
curses for terminal control or shift to a graphical interface with tkinter or pygame.Feel free to modify and expand the code to suit your ideas. Happy coding!