Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
|
| 6 |
+
# Set up a basic target image
|
| 7 |
+
def create_target_image(target_position=(0, 0)):
|
| 8 |
+
# Create a blank white canvas
|
| 9 |
+
img = Image.new("RGB", (500, 500), color="white")
|
| 10 |
+
draw = ImageDraw.Draw(img)
|
| 11 |
+
|
| 12 |
+
# Draw the target in a random position
|
| 13 |
+
target_radius = 30
|
| 14 |
+
target_color = (255, 0, 0)
|
| 15 |
+
|
| 16 |
+
# Draw a circle at the specified target position
|
| 17 |
+
draw.ellipse([target_position[0] - target_radius, target_position[1] - target_radius,
|
| 18 |
+
target_position[0] + target_radius, target_position[1] + target_radius],
|
| 19 |
+
fill=target_color)
|
| 20 |
+
|
| 21 |
+
return img
|
| 22 |
+
|
| 23 |
+
# Function to handle the shooting and update the score
|
| 24 |
+
score = 0
|
| 25 |
+
|
| 26 |
+
def shoot_at_target(mouse_click_position, target_position):
|
| 27 |
+
global score
|
| 28 |
+
|
| 29 |
+
# Calculate distance between click and target
|
| 30 |
+
target_radius = 30
|
| 31 |
+
distance = ((mouse_click_position[0] - target_position[0])**2 +
|
| 32 |
+
(mouse_click_position[1] - target_position[1])**2) ** 0.5
|
| 33 |
+
|
| 34 |
+
# If the click is within the target, increment score
|
| 35 |
+
if distance <= target_radius:
|
| 36 |
+
score += 1
|
| 37 |
+
|
| 38 |
+
return score
|
| 39 |
+
|
| 40 |
+
# Main Streamlit App
|
| 41 |
+
def main():
|
| 42 |
+
global score
|
| 43 |
+
|
| 44 |
+
st.title("Shooting Game")
|
| 45 |
+
st.markdown("### Click on the target to score points!")
|
| 46 |
+
|
| 47 |
+
# Set up initial target position and score
|
| 48 |
+
target_position = (random.randint(50, 450), random.randint(50, 450))
|
| 49 |
+
target_image = create_target_image(target_position)
|
| 50 |
+
|
| 51 |
+
# Display the target
|
| 52 |
+
st.image(target_image, caption="Target", use_container_width=True)
|
| 53 |
+
|
| 54 |
+
# Create a score display
|
| 55 |
+
score_display = st.empty()
|
| 56 |
+
score_display.text(f"Score: {score}")
|
| 57 |
+
|
| 58 |
+
# Create a button to simulate shooting at the target
|
| 59 |
+
if st.button("Shoot at Target"):
|
| 60 |
+
# Simulate a random click position (you can enhance this logic later)
|
| 61 |
+
mouse_click_position = (random.randint(50, 450), random.randint(50, 450))
|
| 62 |
+
|
| 63 |
+
# Update the score based on the click
|
| 64 |
+
score = shoot_at_target(mouse_click_position, target_position)
|
| 65 |
+
|
| 66 |
+
# Regenerate the target's position
|
| 67 |
+
target_position = (random.randint(50, 450), random.randint(50, 450))
|
| 68 |
+
target_image = create_target_image(target_position)
|
| 69 |
+
st.image(target_image, caption="New Target", use_container_width=True)
|
| 70 |
+
|
| 71 |
+
# Update the score display
|
| 72 |
+
score_display.text(f"Score: {score}")
|
| 73 |
+
|
| 74 |
+
time.sleep(1)
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
main()
|