awacke1 commited on
Commit
9c06b18
Β·
1 Parent(s): dcef6d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -87
app.py CHANGED
@@ -1,97 +1,105 @@
1
- # Import necessary libraries
2
  import streamlit as st
3
  import random
4
  import time
5
  from datetime import datetime
6
 
7
  # Constants
8
- HISTORY_LENGTH = 5 # Number of previous actions to display
9
-
10
- # Initialize game state
11
- def initialize_game(player_name):
12
- timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
13
- with open(f"{timestamp}_{player_name}_state.txt", "w") as f:
14
- f.write(f"{player_name}:100,None,Player 1")
15
- with open(f"{timestamp}_{player_name}_actions.txt", "w") as f:
16
- f.write("")
17
-
18
- # Read game state
19
- def read_game_state(player_name, timestamp):
20
- with open(f"{timestamp}_{player_name}_state.txt", "r") as f:
21
- state = f.readline().split(',')
22
- player_name, hp, last_action, turn = state
23
- return player_name, int(hp), last_action, turn
24
-
25
- # Write player action
26
- def write_action(player_name, action, timestamp):
27
- with open(f"{timestamp}_{player_name}_actions.txt", "a") as f:
28
- f.write(f"{action}\n")
29
-
30
- # Update game state based on actions
31
- def update_game_state(player_name, timestamp):
32
- _, hp, _, turn = read_game_state(player_name, timestamp)
33
-
34
- # Read Player's action
35
- with open(f"{timestamp}_{player_name}_actions.txt", "r") as f:
36
- actions = f.readlines()
37
- last_action = actions[-1].strip() if actions else None
38
-
39
- # Update game based on actions
40
- if turn == player_name and last_action:
41
- if last_action == "Attack":
42
- damage = random.randint(5, 15)
43
- hp -= damage
44
- elif last_action == "Heal":
45
- heal = random.randint(5, 10)
46
- hp += heal
47
-
48
- # Write updated game state
49
- with open(f"{timestamp}_{player_name}_state.txt", "w") as f:
50
- f.write(f"{player_name}:{hp},{last_action},Player 1")
51
-
52
- # Get the last few actions
53
- def get_recent_actions(player_name, timestamp):
54
- with open(f"{timestamp}_{player_name}_actions.txt", "r") as f:
55
- actions = f.readlines()
56
- return actions[-HISTORY_LENGTH:]
57
-
58
- # Streamlit Interface
59
  def app():
60
- st.title("Text Battle!")
61
-
62
- if "player_name" not in st.session_state:
63
- st.session_state.player_name = st.text_input("Enter your name:")
64
- if st.session_state.player_name:
65
- st.session_state.timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
66
- initialize_game(st.session_state.player_name)
67
-
68
- if "player_name" in st.session_state and "timestamp" in st.session_state:
69
- player_name, hp, last_action, turn = read_game_state(st.session_state.player_name, st.session_state.timestamp)
70
-
71
- st.write(f"{player_name} HP: {hp}")
72
- st.write(f"Last Action: {last_action}")
73
-
74
- if hp <= 0:
75
- st.write(f"{player_name} has been defeated!")
76
- del st.session_state.player_name
77
- st.stop()
78
-
79
- # Display recent actions
80
- st.write("Recent Actions:")
81
- for action in get_recent_actions(st.session_state.player_name, st.session_state.timestamp):
82
- st.write(action.strip())
83
-
84
- # Player actions
85
- if turn == st.session_state.player_name:
86
- action = st.selectbox(f"{st.session_state.player_name}, choose your action:", ["", "Attack", "Heal", "Pass"])
87
- if st.button("Submit"):
88
- write_action(st.session_state.player_name, action, st.session_state.timestamp)
89
- update_game_state(st.session_state.player_name, st.session_state.timestamp)
90
-
91
- # Display timer and refresh game state every 10 seconds
92
- st.write("Refreshing in 10 seconds...")
93
- time.sleep(10)
94
- st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  # Run the Streamlit app
97
  app()
 
 
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()