-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_timer_test.py
More file actions
executable file
·119 lines (105 loc) · 2.73 KB
/
background_timer_test.py
File metadata and controls
executable file
·119 lines (105 loc) · 2.73 KB
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
import pygame as pg
import time
import os
pg.init()
WIDTH, HEIGHT = 900, 600
WIN = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption('BG TIMER TEST')
GREY = (20, 20, 20)
WHITE = (236, 236, 236)
FONT = pg.font.Font(os.path.join('Assets', 'Retro Font.ttf'), 60)
FPS_CAP = pg.time.Clock()
FPS = 60
def draw_text(text, location=(450, 300), color=WHITE, font=FONT, center='both'):
if center == 'both':
text_surface = font.render(text, True, color)
text_location = text_surface.get_rect()
text_location.center = location
elif center == 'x':
text_surface = font.render(text, True, color)
text_location = text_surface.get_rect()
text_location.centerx = location[0]
text_location[1] = location[1]
elif center == 'y':
text_surface = font.render(text, True, color)
text_location = text_surface.get_rect()
text_location.centery = location[1]
text_location[0] = location[0]
WIN.blit(text_surface, text_location)
class bgtimer:
def __init__(self, time, kill, cframe=False, fps=60):
self.time = time
self.fps = fps
self.fcount = 0
self.scount = 0
self.kill = kill
self.cframe = cframe#count frames instead of seconds
def count(self):
if not self.kill:
self.fcount += 1
#scaling frame to second
if self.fcount == self.fps:
self.scount += 1
if not self.cframe:
self.fcount = 0
#finishing timer
if self.cframe:
if self.fcount == self.time:
self.kill = True
else:
if self.scount == self.time:
self.kill = True
def check_status(self):
#time left
if self.cframe:
time_left = self.time - self.fcount
else:
time_left = self.time - self.scount
status = not self.kill
if self.cframe:
return self.fcount, time_left, status
else:
return self.scount, time_left, status
def restart(self):
self.fcount = 0
self.scount = 0
self.kill = False
def force_finish(self):
if self.cframe:
self.fcount = self.time
else:
self.scount = self.time
self.kill = True
timer3 = bgtimer(3, False)
kcooldown = bgtimer(30, True, True)
def draw():
WIN.fill(GREY)
draw_text('time ellapsed: ' + str(data[0]), (20, 230), center='y')
draw_text('time left: ' + str(data[1]), (20, 300), center='y')
draw_text('status: ' + str(data[2]), (20, 370), center='y')
#print(data)
pg.display.update()
num = 0
jnum = 0
while True:
FPS_CAP.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
quit()
#detecting keypress
keys = pg.key.get_pressed()
if keys[pg.K_k]:
if not kcooldown.check_status()[2]:
print('K Detected', num)
num += 1
timer3.restart()
kcooldown.restart()
if keys[pg.K_j]:
print('J Detected', jnum)
jnum += 1
timer3.force_finish()
timer3.count()
kcooldown.count()
#print(kcooldown.check_status())
data = timer3.check_status()
draw()