Spaces:
Sleeping
Sleeping
| 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() |