-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaim-trainer.py
166 lines (122 loc) · 4.89 KB
/
aim-trainer.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
import math
import random
import time
import pygame
pygame.init()
WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Aim Trainer")
TARGET_INCREMENT = 400
TARGET_EVENT = pygame.USEREVENT
TARGET_PADDING = 30
BG_COLOR = (0, 25, 40)
LIVES = 3
TOP_BAR_HEIGHT = 50
LABEL_FONT = pygame.font.SysFont("comicsans", 24)
class Target:
MAX_SIZE = 30
GROWTH_RATE = 0.2
COLOR = "red"
SECOND_COLOR = "white"
def __init__(self, x, y):
self.x = x
self.y = y
self.size = 0
self.grow = True
def update(self):
if self.size + self.GROWTH_RATE >= self.MAX_SIZE:
self.grow = False
if self.grow:
self.size += self.GROWTH_RATE
else:
self.size -= self.GROWTH_RATE
def draw(self, win):
pygame.draw.circle(win, self.COLOR, (self.x, self.y), self.size)
pygame.draw.circle(win, self.SECOND_COLOR, (self.x, self.y), self.size * 0.8)
pygame.draw.circle(win, self.COLOR, (self.x, self.y), self.size * 0.6)
pygame.draw.circle(win, self.SECOND_COLOR, (self.x, self.y), self.size * 0.4)
def collide (self, x, y):
dis = math.sqrt((self.x - x)**2 + (self.y - y)**2)
return dis <= self.size
def draw(win, targets):
win.fill(BG_COLOR)
for target in targets:
target.draw(win)
def format_time(secs):
milli = math.floor(int(secs * 100 % 1000) / 100)
seconds = int(round(secs % 60, 1))
minutes = int(secs // 60)
return f"{minutes:02d}:{seconds:02d}.{milli}" # pads with 0 if it's not already two digits
def draw_top_bar(win, elapsed_time, targets_pressed, misses):
pygame.draw.rect(win, "grey", (0, 0, WIDTH, TOP_BAR_HEIGHT))
time_label = LABEL_FONT.render(f"Time: {format_time(elapsed_time)}", 1, "black")
speed = round(targets_pressed / elapsed_time, 1)
speed_label = LABEL_FONT.render(f"Speed: {speed} t/s", 1, "black")
hits_label = LABEL_FONT.render(f"Hits: {targets_pressed}", 1, "black")
lives_label = LABEL_FONT.render(f"Lives: {LIVES - misses}", 1, "black")
win.blit(time_label, (5, 5))
win.blit(speed_label, (200, 5))
win.blit(hits_label, (450, 5))
win.blit(lives_label, (650, 5))
def end_screen(win, elapsed_time, targets_pressed, clicks):
win.fill(BG_COLOR)
time_label = LABEL_FONT.render(f"Time: {format_time(elapsed_time)}", 1, "white")
speed = round(targets_pressed / elapsed_time, 1)
speed_label = LABEL_FONT.render(f"Speed: {speed} t/s", 1, "white")
hits_label = LABEL_FONT.render(f"Hits: {targets_pressed}", 1, "white")
accuracy = round(targets_pressed / clicks * 100, 1)
accuracy_label = LABEL_FONT.render(f"Accuracy: {accuracy}%", 1, "white")
win.blit(time_label, (get_middle(time_label), 100))
win.blit(speed_label, (get_middle(speed_label), 200))
win.blit(hits_label, (get_middle(hits_label), 300))
win.blit(accuracy_label, (get_middle(accuracy_label), 400))
pygame.display.update()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
quit()
def get_middle(surface):
return WIDTH / 2 - surface.get_width()/2
def main():
run = True
targets = []
clock = pygame.time.Clock()
targets_pressed = 0
clicks = 0
misses = 0
start_time = time.time()
pygame.time.set_timer(TARGET_EVENT, TARGET_INCREMENT)
while run:
clock.tick(60)
click = False
mouse_pos = pygame.mouse.get_pos()
elapsed_time = time.time() - start_time
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
if event.type == TARGET_EVENT:
x = random.randint(TARGET_PADDING, WIDTH - TARGET_PADDING)
y = random.randint(TARGET_PADDING, HEIGHT - TARGET_PADDING)
target = Target(x, y)
targets.append(target)
if event.type == pygame.MOUSEBUTTONDOWN:
click = True
clicks += 1
for target in targets:
target.update()
if target.size <= 0:
targets.remove(target)
misses += 1
if click and target.collide(*mouse_pos): # splat: the asterisk passes each element in the tuple as an individual operator
targets.remove(target)
targets_pressed += 1
if misses >= LIVES:
end_screen(WIN, elapsed_time, targets_pressed, clicks)
draw(WIN, targets)
draw_top_bar(WIN, elapsed_time, targets_pressed, misses)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()