Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# Constants
|
| 7 |
+
EMOJIS = ["π―", "π", "π", "π", "π₯"]
|
| 8 |
+
OBJECTS_MONSTERS = ["Sword", "Shield", "Potion", "Dragon", "Goblin", "Elf", "Orc"]
|
| 9 |
+
CHAT_HISTORY_LENGTH = 5
|
| 10 |
+
ACTION_HISTORY_LENGTH = 5
|
| 11 |
+
|
| 12 |
+
# Ensure data files exist
|
| 13 |
+
def ensure_data_files():
|
| 14 |
+
files = ['players.txt', 'chat.txt', 'actions.txt', 'objects.txt']
|
| 15 |
+
for f in files:
|
| 16 |
+
try:
|
| 17 |
+
with open(f, 'r') as file:
|
| 18 |
+
pass
|
| 19 |
+
except FileNotFoundError:
|
| 20 |
+
with open(f, 'w') as file:
|
| 21 |
+
file.write('')
|
| 22 |
+
|
| 23 |
+
# Add player
|
| 24 |
+
def add_player(name):
|
| 25 |
+
with open('players.txt', 'a') as file:
|
| 26 |
+
file.write(name + '\n')
|
| 27 |
+
|
| 28 |
+
# Get all players
|
| 29 |
+
def get_all_players():
|
| 30 |
+
with open('players.txt', 'r') as file:
|
| 31 |
+
return [line.strip() for line in file.readlines()]
|
| 32 |
+
|
| 33 |
+
# Add chat message
|
| 34 |
+
def add_chat_message(name, message):
|
| 35 |
+
with open('chat.txt', 'a') as file:
|
| 36 |
+
file.write(name + ': ' + message + '\n')
|
| 37 |
+
|
| 38 |
+
# Get recent chat messages
|
| 39 |
+
def get_recent_chat_messages():
|
| 40 |
+
with open('chat.txt', 'r') as file:
|
| 41 |
+
return file.readlines()[-CHAT_HISTORY_LENGTH:]
|
| 42 |
+
|
| 43 |
+
# Add action
|
| 44 |
+
def add_action(name, action):
|
| 45 |
+
with open('actions.txt', 'a') as file:
|
| 46 |
+
file.write(name + ': ' + action + '\n')
|
| 47 |
+
|
| 48 |
+
# Get recent actions
|
| 49 |
+
def get_recent_actions():
|
| 50 |
+
with open('actions.txt', 'r') as file:
|
| 51 |
+
return file.readlines()[-ACTION_HISTORY_LENGTH:]
|
| 52 |
+
|
| 53 |
+
# Streamlit interface
|
| 54 |
+
def app():
|
| 55 |
+
st.title("Emoji Battle Game!")
|
| 56 |
+
|
| 57 |
+
# Ensure data files exist
|
| 58 |
+
ensure_data_files()
|
| 59 |
+
|
| 60 |
+
# Player name input
|
| 61 |
+
player_name = st.text_input("Enter your name:")
|
| 62 |
+
if player_name and player_name not in get_all_players():
|
| 63 |
+
add_player(player_name)
|
| 64 |
+
|
| 65 |
+
players = get_all_players()
|
| 66 |
+
st.write(f"Players: {', '.join(players)}")
|
| 67 |
+
|
| 68 |
+
# Display timer and emoji buttons
|
| 69 |
+
st.write("Click on the emoji when the timer reaches 0!")
|
| 70 |
+
timer = st.empty()
|
| 71 |
+
for i in range(3, 0, -1):
|
| 72 |
+
timer.write(f"Timer: {i}")
|
| 73 |
+
time.sleep(1)
|
| 74 |
+
timer.write("Timer: 0")
|
| 75 |
+
|
| 76 |
+
emoji_clicked = st.button(random.choice(EMOJIS))
|
| 77 |
+
if emoji_clicked and player_name:
|
| 78 |
+
add_action(player_name, "Clicked the emoji!")
|
| 79 |
+
|
| 80 |
+
# Display recent actions
|
| 81 |
+
st.write("Recent Actions:")
|
| 82 |
+
for action in get_recent_actions():
|
| 83 |
+
st.write(action.strip())
|
| 84 |
+
|
| 85 |
+
# Interactions with objects and monsters
|
| 86 |
+
interaction = st.selectbox("Choose an interaction:", OBJECTS_MONSTERS)
|
| 87 |
+
if st.button("Interact") and player_name:
|
| 88 |
+
add_action(player_name, f"Interacted with {interaction}")
|
| 89 |
+
|
| 90 |
+
# Chat
|
| 91 |
+
chat_message = st.text_input("Send a message:")
|
| 92 |
+
if st.button("Send") and player_name:
|
| 93 |
+
add_chat_message(player_name, chat_message)
|
| 94 |
+
|
| 95 |
+
st.write("Recent chat messages:")
|
| 96 |
+
for message in get_recent_chat_messages():
|
| 97 |
+
st.write(message.strip())
|
| 98 |
+
|
| 99 |
+
# Refresh every 3 seconds
|
| 100 |
+
st.write("Refreshing in 3 seconds...")
|
| 101 |
+
time.sleep(3)
|
| 102 |
+
st.experimental_rerun()
|
| 103 |
+
|
| 104 |
+
# Run the Streamlit app
|
| 105 |
+
app()
|