Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
import time
|
5 |
+
from PIL import Image
|
6 |
+
import json
|
7 |
+
from datetime import datetime
|
8 |
+
from pathlib import Path
|
9 |
+
|
10 |
+
# Initialize session state variables
|
11 |
+
if 'game_state' not in st.session_state:
|
12 |
+
st.session_state.game_state = {
|
13 |
+
'players': {},
|
14 |
+
'chat_messages': [],
|
15 |
+
'tile_map': [],
|
16 |
+
'current_player': None
|
17 |
+
}
|
18 |
+
|
19 |
+
if 'player_name' not in st.session_state:
|
20 |
+
st.session_state.player_name = None
|
21 |
+
|
22 |
+
# Utility functions
|
23 |
+
def load_tiles(tiles_path="tiles"):
|
24 |
+
"""Load tile images from the specified directory"""
|
25 |
+
tiles = {}
|
26 |
+
if not os.path.exists(tiles_path):
|
27 |
+
os.makedirs(tiles_path)
|
28 |
+
# Here you would need to add some default tiles
|
29 |
+
return tiles
|
30 |
+
|
31 |
+
for file in os.listdir(tiles_path):
|
32 |
+
if file.endswith(('.png', '.jpg', '.jpeg')):
|
33 |
+
tile_name = os.path.splitext(file)[0]
|
34 |
+
tile_path = os.path.join(tiles_path, file)
|
35 |
+
tiles[tile_name] = Image.open(tile_path)
|
36 |
+
return tiles
|
37 |
+
|
38 |
+
def generate_map(width=20, height=15):
|
39 |
+
"""Generate a random tile map"""
|
40 |
+
tile_types = ['grass', 'water', 'rock'] # Example tile types
|
41 |
+
return [[random.choice(tile_types) for _ in range(width)] for _ in range(height)]
|
42 |
+
|
43 |
+
def save_game_state():
|
44 |
+
"""Save the current game state to a file"""
|
45 |
+
state_file = Path("game_state.json")
|
46 |
+
state_to_save = {
|
47 |
+
'players': st.session_state.game_state['players'],
|
48 |
+
'chat_messages': st.session_state.game_state['chat_messages'],
|
49 |
+
'tile_map': st.session_state.game_state['tile_map']
|
50 |
+
}
|
51 |
+
with open(state_file, 'w') as f:
|
52 |
+
json.dump(state_to_save, f)
|
53 |
+
|
54 |
+
def load_game_state():
|
55 |
+
"""Load the game state from file"""
|
56 |
+
state_file = Path("game_state.json")
|
57 |
+
if state_file.exists():
|
58 |
+
with open(state_file, 'r') as f:
|
59 |
+
loaded_state = json.load(f)
|
60 |
+
st.session_state.game_state.update(loaded_state)
|
61 |
+
|
62 |
+
def add_chat_message(player_name, message):
|
63 |
+
"""Add a message to the chat history"""
|
64 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
65 |
+
st.session_state.game_state['chat_messages'].append({
|
66 |
+
'player': player_name,
|
67 |
+
'message': message,
|
68 |
+
'timestamp': timestamp
|
69 |
+
})
|
70 |
+
save_game_state()
|
71 |
+
|
72 |
+
# Main game UI
|
73 |
+
def main():
|
74 |
+
st.title("Multiplayer Tile Game")
|
75 |
+
|
76 |
+
# Player login/join game section
|
77 |
+
if st.session_state.player_name is None:
|
78 |
+
with st.form("join_game"):
|
79 |
+
player_name = st.text_input("Enter your name:")
|
80 |
+
submitted = st.form_submit_button("Join Game")
|
81 |
+
if submitted and player_name:
|
82 |
+
st.session_state.player_name = player_name
|
83 |
+
st.session_state.game_state['players'][player_name] = {
|
84 |
+
'position': {'x': 0, 'y': 0},
|
85 |
+
'last_active': time.time()
|
86 |
+
}
|
87 |
+
st.experimental_rerun()
|
88 |
+
|
89 |
+
else:
|
90 |
+
# Game layout with columns
|
91 |
+
col1, col2 = st.columns([2, 1])
|
92 |
+
|
93 |
+
with col1:
|
94 |
+
st.subheader("Game Map")
|
95 |
+
# Load or generate tile map
|
96 |
+
if not st.session_state.game_state['tile_map']:
|
97 |
+
st.session_state.game_state['tile_map'] = generate_map()
|
98 |
+
|
99 |
+
# Display tile map
|
100 |
+
tiles = load_tiles()
|
101 |
+
if tiles:
|
102 |
+
# Create a placeholder for the game map
|
103 |
+
map_placeholder = st.empty()
|
104 |
+
|
105 |
+
# Update player position with arrow keys
|
106 |
+
if st.session_state.player_name:
|
107 |
+
player = st.session_state.game_state['players'][st.session_state.player_name]
|
108 |
+
|
109 |
+
# Movement controls
|
110 |
+
cols = st.columns(4)
|
111 |
+
if cols[0].button("β"):
|
112 |
+
player['position']['x'] = max(0, player['position']['x'] - 1)
|
113 |
+
if cols[1].button("β"):
|
114 |
+
player['position']['y'] = max(0, player['position']['y'] - 1)
|
115 |
+
if cols[2].button("β"):
|
116 |
+
player['position']['y'] = min(14, player['position']['y'] + 1)
|
117 |
+
if cols[3].button("β"):
|
118 |
+
player['position']['x'] = min(19, player['position']['x'] + 1)
|
119 |
+
|
120 |
+
# Display current position
|
121 |
+
st.write(f"Position: ({player['position']['x']}, {player['position']['y']})")
|
122 |
+
else:
|
123 |
+
st.warning("No tile images found. Please add images to the 'tiles' directory.")
|
124 |
+
|
125 |
+
with col2:
|
126 |
+
st.subheader("Chat Room")
|
127 |
+
# Chat message display
|
128 |
+
chat_container = st.container()
|
129 |
+
with chat_container:
|
130 |
+
for message in st.session_state.game_state['chat_messages'][-50:]: # Show last 50 messages
|
131 |
+
st.text(f"[{message['timestamp']}] {message['player']}: {message['message']}")
|
132 |
+
|
133 |
+
# Chat input
|
134 |
+
with st.form("chat_form", clear_on_submit=True):
|
135 |
+
message = st.text_input("Message:")
|
136 |
+
if st.form_submit_button("Send") and message:
|
137 |
+
add_chat_message(st.session_state.player_name, message)
|
138 |
+
st.experimental_rerun()
|
139 |
+
|
140 |
+
# Logout button
|
141 |
+
if st.button("Leave Game"):
|
142 |
+
if st.session_state.player_name in st.session_state.game_state['players']:
|
143 |
+
del st.session_state.game_state['players'][st.session_state.player_name]
|
144 |
+
st.session_state.player_name = None
|
145 |
+
save_game_state()
|
146 |
+
st.experimental_rerun()
|
147 |
+
|
148 |
+
if __name__ == "__main__":
|
149 |
+
load_game_state()
|
150 |
+
main()
|