|
import streamlit as st |
|
import random |
|
import time |
|
from datetime import datetime |
|
|
|
|
|
EMOJIS = ["π―", "π", "π", "π", "π₯"] |
|
OBJECTS_MONSTERS = ["Sword", "Shield", "Potion", "Dragon", "Goblin", "Elf", "Orc"] |
|
CHAT_HISTORY_LENGTH = 5 |
|
ACTION_HISTORY_LENGTH = 5 |
|
|
|
|
|
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('') |
|
|
|
|
|
def add_player(name): |
|
with open('players.txt', 'a') as file: |
|
file.write(name + '\n') |
|
|
|
|
|
def get_all_players(): |
|
with open('players.txt', 'r') as file: |
|
return [line.strip() for line in file.readlines()] |
|
|
|
|
|
def add_chat_message(name, message): |
|
with open('chat.txt', 'a') as file: |
|
file.write(name + ': ' + message + '\n') |
|
|
|
|
|
def get_recent_chat_messages(): |
|
with open('chat.txt', 'r') as file: |
|
return file.readlines()[-CHAT_HISTORY_LENGTH:] |
|
|
|
|
|
def add_action(name, action): |
|
with open('actions.txt', 'a') as file: |
|
file.write(name + ': ' + action + '\n') |
|
|
|
|
|
def get_recent_actions(): |
|
with open('actions.txt', 'r') as file: |
|
return file.readlines()[-ACTION_HISTORY_LENGTH:] |
|
|
|
|
|
def app(): |
|
st.title("Emoji Battle Game!") |
|
|
|
|
|
ensure_data_files() |
|
|
|
|
|
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)}") |
|
|
|
|
|
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!") |
|
|
|
|
|
st.write("Recent Actions:") |
|
for action in get_recent_actions(): |
|
st.write(action.strip()) |
|
|
|
|
|
interaction = st.selectbox("Choose an interaction:", OBJECTS_MONSTERS) |
|
if st.button("Interact") and player_name: |
|
add_action(player_name, f"Interacted with {interaction}") |
|
|
|
|
|
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()) |
|
|
|
|
|
st.write("Refreshing in 3 seconds...") |
|
time.sleep(3) |
|
st.experimental_rerun() |
|
|
|
|
|
app() |
|
|