Spaces:
Sleeping
Sleeping
import streamlit as st | |
import random | |
import time | |
from datetime import datetime | |
# Constants | |
EMOJIS = ["π―", "π", "π", "π", "π₯"] | |
OBJECTS_MONSTERS = ["Sword", "Shield", "Potion", "Dragon", "Goblin", "Elf", "Orc"] | |
CHAT_HISTORY_LENGTH = 5 | |
ACTION_HISTORY_LENGTH = 5 | |
# Ensure data files exist | |
def ensure_data_files(): | |
files = ['players.txt', 'chat.txt', 'actions.txt', 'objects.txt'] | |
for f in files: | |
try: | |
with open(f, 'r') as file: | |
pass | |
except FileNotFoundError: | |
with open(f, 'w') as file: | |
file.write('') | |
# Add player | |
def add_player(name): | |
with open('players.txt', 'a') as file: | |
file.write(name + '\n') | |
# Get all players | |
def get_all_players(): | |
with open('players.txt', 'r') as file: | |
return [line.strip() for line in file.readlines()] | |
# Add chat message | |
def add_chat_message(name, message): | |
with open('chat.txt', 'a') as file: | |
file.write(name + ': ' + message + '\n') | |
# Get recent chat messages | |
def get_recent_chat_messages(): | |
with open('chat.txt', 'r') as file: | |
return file.readlines()[-CHAT_HISTORY_LENGTH:] | |
# Add action | |
def add_action(name, action): | |
with open('actions.txt', 'a') as file: | |
file.write(name + ': ' + action + '\n') | |
# Get recent actions | |
def get_recent_actions(): | |
with open('actions.txt', 'r') as file: | |
return file.readlines()[-ACTION_HISTORY_LENGTH:] | |
# Streamlit interface | |
def app(): | |
st.title("Emoji Battle Game!") | |
# Ensure data files exist | |
ensure_data_files() | |
# Player name input | |
player_name = st.text_input("Enter your name:") | |
if player_name and player_name not in get_all_players(): | |
add_player(player_name) | |
players = get_all_players() | |
st.write(f"Players: {', '.join(players)}") | |
# Display timer and emoji buttons | |
st.write("Click on the emoji when the timer reaches 0!") | |
timer = st.empty() | |
for i in range(3, 0, -1): | |
timer.write(f"Timer: {i}") | |
time.sleep(1) | |
timer.write("Timer: 0") | |
emoji_clicked = st.button(random.choice(EMOJIS)) | |
if emoji_clicked and player_name: | |
add_action(player_name, "Clicked the emoji!") | |
# Display recent actions | |
st.write("Recent Actions:") | |
for action in get_recent_actions(): | |
st.write(action.strip()) | |
# Interactions with objects and monsters | |
interaction = st.selectbox("Choose an interaction:", OBJECTS_MONSTERS) | |
if st.button("Interact") and player_name: | |
add_action(player_name, f"Interacted with {interaction}") | |
# Chat | |
chat_message = st.text_input("Send a message:") | |
if st.button("Send") and player_name: | |
add_chat_message(player_name, chat_message) | |
st.write("Recent chat messages:") | |
for message in get_recent_chat_messages(): | |
st.write(message.strip()) | |
# Refresh every 3 seconds | |
st.write("Refreshing in 3 seconds...") | |
time.sleep(3) | |
st.experimental_rerun() | |
# Run the Streamlit app | |
app() | |