Spaces:
Sleeping
Sleeping
import streamlit as st | |
import random | |
# Constants | |
GRID_WIDTH = 10 | |
GRID_HEIGHT = 10 | |
# Game state variables | |
st.title("Shooting Game") | |
st.subheader("Control the player and shoot bullets!") | |
# Initialize game state | |
if "player_x" not in st.session_state: | |
st.session_state["player_x"] = 3 | |
if "player_y" not in st.session_state: | |
st.session_state["player_y"] = 9 # Player starts at the bottom row | |
if "bullets" not in st.session_state: | |
st.session_state["bullets"] = [] | |
if "enemy" not in st.session_state: | |
st.session_state["enemy"] = {"x": random.randint(0, GRID_WIDTH - 1), "y": 0} | |
# Render the grid | |
def render_grid(): | |
grid = [["β¬" for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)] | |
# Draw player | |
grid[st.session_state["player_y"]][st.session_state["player_x"]] = "π¦" | |
# Draw bullets | |
for bullet in st.session_state["bullets"]: | |
grid[bullet["y"]][bullet["x"]] = "π©" | |
# Draw enemy | |
if st.session_state["enemy"]["x"] >= 0 and st.session_state["enemy"]["y"] >= 0: | |
grid[st.session_state["enemy"]["y"]][st.session_state["enemy"]["x"]] = "π₯" | |
# Render the grid as a string | |
for row in grid: | |
st.write("".join(row)) | |
# Update game state | |
def update_game(action): | |
# Move player | |
if action == "left" and st.session_state["player_x"] > 0: | |
st.session_state["player_x"] -= 1 | |
elif action == "right" and st.session_state["player_x"] < GRID_WIDTH - 1: | |
st.session_state["player_x"] += 1 | |
elif action == "up" and st.session_state["player_y"] > 0: | |
st.session_state["player_y"] -= 1 | |
elif action == "down" and st.session_state["player_y"] < GRID_HEIGHT - 1: | |
st.session_state["player_y"] += 1 | |
elif action == "shoot_up": | |
st.session_state["bullets"].append({"x": st.session_state["player_x"], "y": st.session_state["player_y"] - 1, "direction": "up"}) | |
elif action == "shoot_down": | |
st.session_state["bullets"].append({"x": st.session_state["player_x"], "y": st.session_state["player_y"] + 1, "direction": "down"}) | |
elif action == "shoot_left": | |
st.session_state["bullets"].append({"x": st.session_state["player_x"] - 1, "y": st.session_state["player_y"], "direction": "left"}) | |
elif action == "shoot_right": | |
st.session_state["bullets"].append({"x": st.session_state["player_x"] + 1, "y": st.session_state["player_y"], "direction": "right"}) | |
# Move bullets | |
new_bullets = [] | |
for bullet in st.session_state["bullets"]: | |
if bullet["direction"] == "up": | |
bullet["y"] -= 1 | |
elif bullet["direction"] == "down": | |
bullet["y"] += 1 | |
elif bullet["direction"] == "left": | |
bullet["x"] -= 1 | |
elif bullet["direction"] == "right": | |
bullet["x"] += 1 | |
# Remove bullet if it goes off the grid | |
if 0 <= bullet["x"] < GRID_WIDTH and 0 <= bullet["y"] < GRID_HEIGHT: | |
new_bullets.append(bullet) | |
st.session_state["bullets"] = new_bullets | |
# Check for collisions | |
for bullet in st.session_state["bullets"]: | |
if bullet["x"] == st.session_state["enemy"]["x"] and bullet["y"] == st.session_state["enemy"]["y"]: | |
# Reset enemy position | |
st.session_state["enemy"]["x"] = random.randint(0, GRID_WIDTH - 1) | |
st.session_state["enemy"]["y"] = 0 | |
# Move enemy | |
if st.session_state["enemy"]["y"] < GRID_HEIGHT - 1: | |
st.session_state["enemy"]["y"] += 1 | |
# Buttons for controlling the player | |
col1, col2, col3 = st.columns(3) | |
if col1.button("Move Left"): | |
update_game("left") | |
if col2.button("Move Right"): | |
update_game("right") | |
if col3.button("Move Down"): | |
update_game("down") | |
col4, col5, col6 = st.columns(3) | |
if col4.button("Move Up"): | |
update_game("up") | |
if col5.button("Shoot Up"): | |
update_game("shoot_up") | |
if col6.button("Shoot Down"): | |
update_game("shoot_down") | |
col7, col8, col9 = st.columns(3) | |
if col7.button("Shoot Left"): | |
update_game("shoot_left") | |
if col8.button("Shoot Right"): | |
update_game("shoot_right") | |
# Render the grid | |
render_grid() | |