Game / app.py
Muhaimin60's picture
Rename app py to app.py
bfde4c5 verified
raw
history blame
2.52 kB
import streamlit as st
import random
import time
from PIL import Image, ImageDraw
# Set up a basic target image
def create_target_image(target_position=(0, 0)):
# Create a blank white canvas
img = Image.new("RGB", (500, 500), color="white")
draw = ImageDraw.Draw(img)
# Draw the target in a random position
target_radius = 30
target_color = (255, 0, 0)
# Draw a circle at the specified target position
draw.ellipse([target_position[0] - target_radius, target_position[1] - target_radius,
target_position[0] + target_radius, target_position[1] + target_radius],
fill=target_color)
return img
# Function to handle the shooting and update the score
score = 0
def shoot_at_target(mouse_click_position, target_position):
global score
# Calculate distance between click and target
target_radius = 30
distance = ((mouse_click_position[0] - target_position[0])**2 +
(mouse_click_position[1] - target_position[1])**2) ** 0.5
# If the click is within the target, increment score
if distance <= target_radius:
score += 1
return score
# Main Streamlit App
def main():
global score
st.title("Shooting Game")
st.markdown("### Click on the target to score points!")
# Set up initial target position and score
target_position = (random.randint(50, 450), random.randint(50, 450))
target_image = create_target_image(target_position)
# Display the target
st.image(target_image, caption="Target", use_container_width=True)
# Create a score display
score_display = st.empty()
score_display.text(f"Score: {score}")
# Create a placeholder for instructions
instructions = st.empty()
instructions.text("Click anywhere inside the target to score!")
# Capture user click on the target image (simulation)
click = st.experimental_get_query_params()
if "click" in click:
mouse_click_position = (int(click["click"][0]), int(click["click"][1]))
score = shoot_at_target(mouse_click_position, target_position)
# After each click, regenerate target position
target_position = (random.randint(50, 450), random.randint(50, 450))
target_image = create_target_image(target_position)
st.image(target_image, caption="New Target", use_container_width=True)
# Update score
score_display.text(f"Score: {score}")
time.sleep(1)
if __name__ == "__main__":
main()