Spaces:
Runtime error
Runtime error
import streamlit as st | |
import random | |
class Adventure: | |
def __init__(self, name): | |
self.name = name | |
self.health = 100 | |
self.gold = 0 | |
self.victory = False | |
def fight(self, monster_name, monster_attack): | |
self.health -= monster_attack | |
if self.health <= 0: | |
st.write("You have been defeated by the {}.".format(monster_name)) | |
st.write("Your adventure has come to an end.") | |
self.victory = False | |
else: | |
st.write("You fought the {} and took {} damage. Your health is now {}.".format( | |
monster_name, monster_attack, self.health)) | |
self.victory = True | |
def move(self, direction): | |
if direction == "North": | |
st.write("You move North.") | |
elif direction == "South": | |
st.write("You move South.") | |
elif direction == "East": | |
st.write("You move East.") | |
elif direction == "West": | |
st.write("You move West.") | |
else: | |
st.write("Invalid direction. Please choose North, South, East, or West.") | |
def play_game(): | |
name = st.text_input("Enter your name:") | |
adventure = Adventure(name) | |
st.write("Welcome to the dungeon, {}.".format(adventure.name)) | |
while adventure.victory==False: | |
count=1 | |
#direction = st.selectbox('Choose your next move:', ('North', 'South', 'East', 'West'), key = count) | |
#direction = st.selectbox("Choose your next move:", ("North", "South", "East", "West"), key = count) | |
count = count + 1 | |
adventure.move("North") | |
if random.random() > 0.7: | |
monster_name = "Goblin" | |
monster_attack = random.randint(10, 25) | |
st.write("A {} appears and attacks you.".format(monster_name)) | |
adventure.fight(monster_name, monster_attack) | |
if __name__ == "__main__": | |
play_game() |