Muhaimin60 commited on
Commit
14c4530
·
verified ·
1 Parent(s): 9c026bf

Create App.py

Browse files
Files changed (1) hide show
  1. App.py +79 -0
App.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_column_width=True)
53
+
54
+ # Create a score display
55
+ score_display = st.empty()
56
+ score_display.text(f"Score: {score}")
57
+
58
+ # Create a placeholder for instructions
59
+ instructions = st.empty()
60
+ instructions.text("Click anywhere inside the target to score!")
61
+
62
+ # Capture user click on the target image (simulation)
63
+ click = st.experimental_get_query_params()
64
+ if "click" in click:
65
+ mouse_click_position = (int(click["click"][0]), int(click["click"][1]))
66
+ score = shoot_at_target(mouse_click_position, target_position)
67
+
68
+ # After each click, regenerate target position
69
+ target_position = (random.randint(50, 450), random.randint(50, 450))
70
+ target_image = create_target_image(target_position)
71
+ st.image(target_image, caption="New Target", use_column_width=True)
72
+
73
+ # Update score
74
+ score_display.text(f"Score: {score}")
75
+
76
+ time.sleep(1)
77
+
78
+ if __name__ == "__main__":
79
+ main()