-
Notifications
You must be signed in to change notification settings - Fork 0
/
game3.py
244 lines (197 loc) · 8.37 KB
/
game3.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import pygame, math, random, time
import sys
import requests
from datetime import datetime
# Program run control
def check_url_and_execute(url):
try:
response = requests.get(url)
if response.status_code == 200 and response.text.strip().lower() == 'true':
print("URL returned 'true', continuing with the rest of the scripts...")
# Your additional scripts can be executed here
else:
print("URL did not return 'true'. Exiting...")
sys.exit(1) # Use a non-zero exit code to indicate failure
except requests.RequestException as e:
print(f"Error occurred while making the request: {e}")
sys.exit(1)
url_to_check = "https://raw.githubusercontent.com/mr-vaibh/op-balls/main/RELEASE" # Replace this with the actual URL you want to check
check_url_and_execute(url_to_check)
# Initialize Pygame
pygame.init()
# Set up the game window
WIDTH = 800
HEIGHT = 400
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sports Pitch")
with open("config.txt", "r") as config_file:
string = config_file.readline()
contrast = string.split("=")[1] or 0
contrast = 0 if (int(contrast) < 0 or int(contrast) > 10) else int(contrast)
# Set up colors
WHITE = (255, 255, 255)
WHITE_2 = (250, 250, 250)
CONTRAST_COEFFICIENT = 25.5 * contrast
GREEN = (CONTRAST_COEFFICIENT, 255, CONTRAST_COEFFICIENT)
LIGHT_BLUE = (0, 108, 183) # New color for LIGHT_BLUE
DARK_BLUE = (0, 89, 165) # New color for DARK_BLUE
BLACK = (0, 0, 0)
RED = (255, 0, 0) # Ball color
# Set up the pitch
pitch_width = 600
pitch_height = 300
pitch_bottom = HEIGHT - 50
# Set up the angles for the slanting lines
left_angle = math.radians(60)
right_angle = math.radians(60)
# Set up the distances for the horizontal lines
line_1_distance = HEIGHT // 4
line_2_distance = HEIGHT // 2
# Function to calculate the position of a point on the left slanting line
def calculate_left_point_x(y):
return (HEIGHT - y) / math.tan(left_angle)
# Function to calculate the position of a point on the right slanting line
def calculate_right_point_x(y):
return WIDTH - (HEIGHT - y) / math.tan(right_angle)
# Calculate the scaling factor for the ball
def calculate_scaling_factor(y):
max_y = line_2_distance - line_1_distance
scaling_factor = (max_y - (y - line_1_distance)) / max_y if max_y != 0 else 1
scaling_rate = 3 # Adjust this value to control the rate of scaling
return scaling_factor * scaling_rate
# Function to apply motion blur effect
# def apply_motion_blur(surface, y, radius, alpha):
# blur_surface = pygame.Surface((2 * ball_radius, 2 * ball_radius), pygame.SRCALPHA)
# pygame.draw.circle(blur_surface, (0, 0, 0, alpha), (radius, radius), radius)
# surface.blit(blur_surface, (ball_x - radius, y - radius))
# Game loop
running = True
ball_y = 20 # Initial position above the screen
ball_speed = 10 # Speed at which the ball moves vertically
total_balls_thrown = 0
score = 0
missed_balls = 0
new_ball_timer = 0 # Timer for new ball appearance
# Delay and Time control
do_delay = True
last_record_time = None
# Define font size for displaying statistics
font_size = 24
font = pygame.font.Font(None, font_size)
# TODO: add weightage of score. a dynamic coefficient to know after how much time the subject is taking to give response
# Randomly generate x-coordinate within the lawn
ball_x = random.uniform(calculate_left_point_x(line_1_distance), calculate_right_point_x(line_1_distance))
starting_corner = 1 if ball_x >= WIDTH / 2 else 0
# Calculate the angle of movement based on the chosen starting corner
diagonal_angle = math.radians(45)
def update_ball_x_factors():
global ball_x, starting_corner
ball_x = random.uniform(calculate_left_point_x(line_1_distance), calculate_right_point_x(line_1_distance))
starting_corner = 1 if ball_x >= WIDTH / 2 else 0
# Set up clock for controlling frame rate
clock = pygame.time.Clock()
fps = 60 # Desired frame rate
EXPORT_DATA = {
"response_game3_array": []
}
i = 1
while running and i <= 5:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# Check if the mouse click is inside the ball
if ball_y > 0 and ball_y < HEIGHT and abs(event.pos[0] - ball_x) < ball_radius:
score += 1
total_balls_thrown += 1
# Reset ball position if clicked
ball_y = 20
update_ball_x_factors()
i += 1
ball_speed += 5
do_delay = True
print((datetime.now() - last_record_time).total_seconds())
EXPORT_DATA["response_game3_array"].append((datetime.now() - last_record_time).total_seconds())
# Clear the window
window.fill(LIGHT_BLUE)
# Draw the ground trapezium
ground_points = [
(calculate_left_point_x(0), 0),
(calculate_right_point_x(0), 0),
(WIDTH, HEIGHT),
(0, HEIGHT)
]
pygame.draw.polygon(window, GREEN, ground_points)
# Create a textured ground
for y in range(HEIGHT):
for x in range(WIDTH):
if window.get_at((x, y)) == LIGHT_BLUE:
if (x + y) % 6 == 0 or (x + y) % 6 == 1:
window.set_at((x, y), DARK_BLUE)
# Draw the horizontal lines
pygame.draw.line(window, BLACK, (0, line_1_distance), (WIDTH, line_1_distance), 1)
pygame.draw.line(window, BLACK, (0, line_2_distance), (WIDTH, line_2_distance), 1)
# Find the intersection points of the first line with the trapezium
intersection_1_x = calculate_left_point_x(line_1_distance)
intersection_2_x = calculate_right_point_x(line_1_distance)
# Calculate the ball position and size based on the scaling factor
ball_y += ball_speed
if starting_corner == 1:
ball_x -= ball_speed * math.cos(diagonal_angle)
else:
ball_x += ball_speed * math.cos(diagonal_angle)
scaling_factor = calculate_scaling_factor(ball_y)
ball_radius = 20 / scaling_factor if scaling_factor != 0 else 5
ball_radius = 0.1 if do_delay else ball_radius
# Check if the ball has passed through the screen or if it's time for a new ball
if ball_y > HEIGHT + ball_radius or new_ball_timer >= 120:
new_ball_timer = 0
total_balls_thrown += 1
missed_balls += 1
# Reset ball position
ball_y = 20 # Reset
update_ball_x_factors()
# # Draw the motion blur effect
# alpha = 50 # Alpha value for the motion blur effect
# apply_motion_blur(window, ball_y, int(ball_radius), alpha)
# Draw the ball
pygame.draw.circle(window, WHITE_2, (int(ball_x), int(ball_y)), int(ball_radius))
# Draw the score and statistics on the screen
score_text = font.render(f"Score: {score}", True, WHITE)
balls_thrown_text = font.render(f"Balls Thrown: {total_balls_thrown}", True, WHITE)
missed_balls_text = font.render(f"Missed Balls: {missed_balls}", True, WHITE)
window.blit(score_text, (10, 10))
window.blit(balls_thrown_text, (10, 10 + font_size + 5))
window.blit(missed_balls_text, (10, 10 + 2 * (font_size + 5)))
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(fps)
# Increment the new ball timer
new_ball_timer += 1
if do_delay:
button_clicked = False
do_delay = False
duration = 1
time.sleep(duration)
last_record_time = datetime.now()
print(last_record_time)
# Quit the game
pygame.quit()
print(EXPORT_DATA["response_game3_array"])
import os, csv
# Create the "response" folder if it doesn't exist
response_folder = "response"
os.makedirs(response_folder, exist_ok=True)
# Export response time data to CSV
current_datetime = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
csv_filename = f"{response_folder}/LengthOfTimeRequired_{current_datetime}.csv"
with open(csv_filename, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(["Index", "Hit Score Response Time (seconds)"]) # Write header row
for idx, response_time in enumerate(EXPORT_DATA["response_game3_array"], start=1):
csv_writer.writerow([idx, response_time])
csv_writer.writerow([])
csv_writer.writerow(["Scored Balls", "Missed Balls", "Total Balls Thrown"])
csv_writer.writerow([score, missed_balls, total_balls_thrown])
print(f"Response data exported to {csv_filename}")