Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| # Initialize game state | |
| game_state = {"step": 0} | |
| # Game logic | |
| def play_game(user_input): | |
| step = game_state["step"] | |
| user_input = user_input.strip().lower() | |
| if step == 0: | |
| game_state["step"] = 1 | |
| return "You are in a dark forest. Do you go 'left' or 'right'?" | |
| elif step == 1: | |
| if user_input == "left": | |
| game_state["step"] = 2 | |
| return "You see a river. Do you 'swim' across or 'build a raft'?" | |
| elif user_input == "right": | |
| game_state["step"] = 3 | |
| return "You meet a wizard. Do you 'talk' to him or 'run' away?" | |
| else: | |
| return "Please type 'left' or 'right'." | |
| elif step == 2: | |
| if user_input == "swim": | |
| game_state["step"] = 4 | |
| return "You tried to swim and got swept away! Game Over. Type anything to restart." | |
| elif user_input == "build a raft": | |
| game_state["step"] = 4 | |
| return "You safely crossed the river. You win! π Type anything to restart." | |
| else: | |
| return "Please choose 'swim' or 'build a raft'." | |
| elif step == 3: | |
| if user_input == "talk": | |
| game_state["step"] = 4 | |
| return "The wizard gives you a treasure. You win! π§ββοΈ Type anything to restart." | |
| elif user_input == "run": | |
| game_state["step"] = 4 | |
| return "The wizard curses you! Game Over. Type anything to restart." | |
| else: | |
| return "Please choose 'talk' or 'run'." | |
| else: | |
| game_state["step"] = 0 | |
| return "The game is restarted. Do you want to go 'left' or 'right'?" | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=play_game, | |
| inputs=gr.Textbox(placeholder="Type your choice here..."), | |
| outputs="text", | |
| title="πΉοΈ Simple Text Adventure Game", | |
| description="Make your choices and see where the story goes!" | |
| ) | |
| iface.launch() |