Spaces:
Sleeping
Sleeping
File size: 2,493 Bytes
b3945ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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 button to simulate shooting at the target
if st.button("Shoot at Target"):
# Simulate a random click position (you can enhance this logic later)
mouse_click_position = (random.randint(50, 450), random.randint(50, 450))
# Update the score based on the click
score = shoot_at_target(mouse_click_position, target_position)
# Regenerate the target's 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 the score display
score_display.text(f"Score: {score}")
time.sleep(1)
if __name__ == "__main__":
main() |