Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,23 @@ import json
|
|
| 3 |
import random
|
| 4 |
import time
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
# Constants
|
| 8 |
GRID_SIZE = 10
|
| 9 |
MAX_PLAYERS = 10
|
| 10 |
UPDATE_INTERVAL = 3
|
| 11 |
GAME_STATE_FILE = "game_state.json"
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Initialize session state
|
| 14 |
if "player_name" not in st.session_state:
|
| 15 |
st.session_state.player_name = f"Player_{random.randint(1000, 9999)}"
|
| 16 |
if "selected_player" not in st.session_state:
|
| 17 |
st.session_state.selected_player = None
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Function to load game state
|
| 20 |
def load_game_state():
|
|
@@ -36,34 +41,76 @@ def update_player_position(player_name, x, y):
|
|
| 36 |
state["players"][player_name]["y"] = y
|
| 37 |
else:
|
| 38 |
if len(state["players"]) < MAX_PLAYERS:
|
| 39 |
-
state["players"][player_name] = {"x": x, "y": y}
|
| 40 |
save_game_state(state)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# Streamlit app
|
| 43 |
-
st.
|
|
|
|
| 44 |
|
| 45 |
# Display player name and allow updates
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
st.session_state.player_name
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Create grid
|
| 52 |
grid = st.empty()
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Main game loop
|
| 55 |
while True:
|
| 56 |
# Load current game state
|
| 57 |
game_state = load_game_state()
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
x, y = pos["x"], pos["y"]
|
| 63 |
-
grid_repr[y][x] = player[0].upper()
|
| 64 |
-
|
| 65 |
-
# Display grid
|
| 66 |
-
grid.text("\n".join(" ".join(row) for row in grid_repr))
|
| 67 |
|
| 68 |
# Handle player movement
|
| 69 |
col1, col2 = st.columns(2)
|
|
@@ -76,6 +123,10 @@ while True:
|
|
| 76 |
target_x = st.number_input("Target X", min_value=0, max_value=GRID_SIZE-1, value=GRID_SIZE//2)
|
| 77 |
target_y = st.number_input("Target Y", min_value=0, max_value=GRID_SIZE-1, value=GRID_SIZE//2)
|
| 78 |
if st.button("Move"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
update_player_position(st.session_state.player_name, target_x, target_y)
|
| 80 |
|
| 81 |
# Wait for update interval
|
|
|
|
| 3 |
import random
|
| 4 |
import time
|
| 5 |
import os
|
| 6 |
+
from streamlit_javascript import st_javascript
|
| 7 |
|
| 8 |
# Constants
|
| 9 |
GRID_SIZE = 10
|
| 10 |
MAX_PLAYERS = 10
|
| 11 |
UPDATE_INTERVAL = 3
|
| 12 |
GAME_STATE_FILE = "game_state.json"
|
| 13 |
+
CELL_SIZE = 50
|
| 14 |
+
PLAYER_SIZE = 40
|
| 15 |
|
| 16 |
# Initialize session state
|
| 17 |
if "player_name" not in st.session_state:
|
| 18 |
st.session_state.player_name = f"Player_{random.randint(1000, 9999)}"
|
| 19 |
if "selected_player" not in st.session_state:
|
| 20 |
st.session_state.selected_player = None
|
| 21 |
+
if "player_color" not in st.session_state:
|
| 22 |
+
st.session_state.player_color = f"#{random.randint(0, 0xFFFFFF):06x}"
|
| 23 |
|
| 24 |
# Function to load game state
|
| 25 |
def load_game_state():
|
|
|
|
| 41 |
state["players"][player_name]["y"] = y
|
| 42 |
else:
|
| 43 |
if len(state["players"]) < MAX_PLAYERS:
|
| 44 |
+
state["players"][player_name] = {"x": x, "y": y, "color": st.session_state.player_color}
|
| 45 |
save_game_state(state)
|
| 46 |
|
| 47 |
+
# Function to generate SVG for the game board
|
| 48 |
+
def generate_svg(game_state):
|
| 49 |
+
svg = f'<svg width="{GRID_SIZE * CELL_SIZE}" height="{GRID_SIZE * CELL_SIZE}" xmlns="http://www.w3.org/2000/svg">'
|
| 50 |
+
|
| 51 |
+
# Draw grid
|
| 52 |
+
for i in range(GRID_SIZE):
|
| 53 |
+
for j in range(GRID_SIZE):
|
| 54 |
+
svg += f'<rect x="{i * CELL_SIZE}" y="{j * CELL_SIZE}" width="{CELL_SIZE}" height="{CELL_SIZE}" fill="white" stroke="gray" />'
|
| 55 |
+
|
| 56 |
+
# Draw players
|
| 57 |
+
for player, data in game_state["players"].items():
|
| 58 |
+
x, y = data["x"] * CELL_SIZE + CELL_SIZE // 2, data["y"] * CELL_SIZE + CELL_SIZE // 2
|
| 59 |
+
color = data.get("color", "#000000")
|
| 60 |
+
svg += f'<circle cx="{x}" cy="{y}" r="{PLAYER_SIZE // 2}" fill="{color}">'
|
| 61 |
+
svg += f'<animate attributeName="cx" from="{x}" to="{x}" dur="0.5s" repeatCount="1" />'
|
| 62 |
+
svg += f'<animate attributeName="cy" from="{y}" to="{y}" dur="0.5s" repeatCount="1" />'
|
| 63 |
+
svg += '</circle>'
|
| 64 |
+
svg += f'<text x="{x}" y="{y + 5}" text-anchor="middle" fill="white" font-size="12">{player[0].upper()}</text>'
|
| 65 |
+
|
| 66 |
+
svg += '</svg>'
|
| 67 |
+
return svg
|
| 68 |
+
|
| 69 |
# Streamlit app
|
| 70 |
+
st.set_page_config(layout="wide")
|
| 71 |
+
st.title("Enhanced Multiplayer Grid Game")
|
| 72 |
|
| 73 |
# Display player name and allow updates
|
| 74 |
+
col1, col2 = st.columns([3, 1])
|
| 75 |
+
with col1:
|
| 76 |
+
player_name = st.text_input("Your name", value=st.session_state.player_name)
|
| 77 |
+
if player_name != st.session_state.player_name:
|
| 78 |
+
st.session_state.player_name = player_name
|
| 79 |
+
update_player_position(player_name, GRID_SIZE // 2, GRID_SIZE // 2)
|
| 80 |
+
|
| 81 |
+
with col2:
|
| 82 |
+
st.color_picker("Choose your color", value=st.session_state.player_color, key="player_color")
|
| 83 |
|
| 84 |
# Create grid
|
| 85 |
grid = st.empty()
|
| 86 |
|
| 87 |
+
# JavaScript for smooth movement
|
| 88 |
+
js_code = """
|
| 89 |
+
function movePlayer(playerId, fromX, fromY, toX, toY) {
|
| 90 |
+
const player = document.getElementById(playerId);
|
| 91 |
+
if (player) {
|
| 92 |
+
const animateX = player.getElementsByTagName('animate')[0];
|
| 93 |
+
const animateY = player.getElementsByTagName('animate')[1];
|
| 94 |
+
animateX.setAttribute('from', fromX);
|
| 95 |
+
animateX.setAttribute('to', toX);
|
| 96 |
+
animateY.setAttribute('from', fromY);
|
| 97 |
+
animateY.setAttribute('to', toY);
|
| 98 |
+
animateX.beginElement();
|
| 99 |
+
animateY.beginElement();
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
st.components.v1.html(f"<script>{js_code}</script>", height=0)
|
| 105 |
+
|
| 106 |
# Main game loop
|
| 107 |
while True:
|
| 108 |
# Load current game state
|
| 109 |
game_state = load_game_state()
|
| 110 |
|
| 111 |
+
# Generate and display SVG
|
| 112 |
+
svg = generate_svg(game_state)
|
| 113 |
+
grid.markdown(svg, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# Handle player movement
|
| 116 |
col1, col2 = st.columns(2)
|
|
|
|
| 123 |
target_x = st.number_input("Target X", min_value=0, max_value=GRID_SIZE-1, value=GRID_SIZE//2)
|
| 124 |
target_y = st.number_input("Target Y", min_value=0, max_value=GRID_SIZE-1, value=GRID_SIZE//2)
|
| 125 |
if st.button("Move"):
|
| 126 |
+
current_pos = game_state["players"].get(st.session_state.player_name, {"x": GRID_SIZE // 2, "y": GRID_SIZE // 2})
|
| 127 |
+
from_x, from_y = current_pos["x"] * CELL_SIZE + CELL_SIZE // 2, current_pos["y"] * CELL_SIZE + CELL_SIZE // 2
|
| 128 |
+
to_x, to_y = target_x * CELL_SIZE + CELL_SIZE // 2, target_y * CELL_SIZE + CELL_SIZE // 2
|
| 129 |
+
st_javascript(f"movePlayer('{st.session_state.player_name}', {from_x}, {from_y}, {to_x}, {to_y})")
|
| 130 |
update_player_position(st.session_state.player_name, target_x, target_y)
|
| 131 |
|
| 132 |
# Wait for update interval
|