-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
352 lines (259 loc) · 11.4 KB
/
game.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from typing import Union
import pygame
import pygame.gfxdraw
import requests
import io
from math import sin, cos, atan2, pi
import config
import crf
import timer
from renderer.particle import ParticleRenderer
from renderer.settings import SettingsRenderer
from renderer.history import HistoryRenderer
from renderer.times import TimesManagerRenderer
class Game:
def __init__(self):
VERSION = "v1.0a"
_ = pygame.display.set_mode((0, 0)) # weird, windows
del _
self.screen = pygame.display.set_mode((0, 0), pygame.RESIZABLE)
pygame.display.set_caption(f"cubescramble-rewrite {VERSION}")
try:
pygame.display.set_icon(pygame.image.load("assets/images/icon/cubescramble.ico"))
except:
pass
pygame.scrap.init()
pygame.scrap.set_mode(pygame.SCRAP_CLIPBOARD)
self.font1 = pygame.font.Font("assets/fonts/font1.ttf", 25)
self.font2 = pygame.font.Font("assets/fonts/font1.ttf", 100)
initcheck()
self.state = 'main'
self.config = config.Config(1, "example.png", True, 'true', 'clock')
self.load()
self.timer = timer.Timer(self)
self.time_stats = timer.TimeStats()
self.particle_renderer = ParticleRenderer(self.screen)
self.settings_renderer = SettingsRenderer(self.config, self)
self.history_renderer = HistoryRenderer(self.config, self)
self.times_manager_renderer = TimesManagerRenderer(self.config, self)
self.background = None
self.background_image = None
self.background_raw = None
self.refresh_background()
self.timer.refresh_stats()
update_mouse(not self.config.hide_mouse)
def refresh_background(self):
if self.config.background_url is not None and self.background_image is None:
# TODO: if you change the settings this should refresh
if not self.config.background_local:
r = requests.get(self.config.background_url) # TODO: do in background
print(r.status_code)
if r.status_code != 200:
print('background image website did not return 200')
self.background = None
self.background_raw = None
self.background_image = None
else:
try:
self.background_image = pygame.image.load(io.BytesIO(r.content))
except pygame.error:
print('background image brocken')
self.background = None
self.background_raw = None
self.background_image = None
else:
try:
self.background_image = pygame.image.load(self.config.background_url)
except pygame.error:
self.background = None
self.background_raw = None
self.background_image = None
print('background image brocken')
except FileNotFoundError: # TODO: report errors in GUI
self.background = None
self.background_raw = None
self.background_image = None
print('image not found')
else:
self.background = None
self.background_raw = None
if self.background_image is not None:
self.background_raw = pygame.Surface(self.background_image.get_size())
self.background_raw.blit(self.background_image, (0, 0))
if self.background_raw is not None:
if self.config.background_scale == 'aspect': # aspect scaling
scale = self.background_raw.get_width() / self.screen.get_size()[0]
scale = max(scale, self.background_raw.get_height() / self.screen.get_size()[1])
self.background = pygame.transform.smoothscale(self.background_raw,
(self.background_raw.get_width() / scale,
self.background_raw.get_height() / scale))
elif self.config.background_scale: # full scaling
self.background = pygame.transform.smoothscale(self.background_raw, self.screen.get_size())
else: # no scaling
self.background = self.background_raw
def update(self, events: list[pygame.event.Event]):
for event in events:
if event.type == pygame.VIDEORESIZE:
self.refresh_background()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_s and event.mod & pygame.KMOD_CTRL: # switch UI
if self.state == 'settings':
self.background_image = None
self.refresh_background()
self.timer.refresh_stats()
timer.DEVICE_NUM = self.config.device_num
self.timer.refresh_puzzle()
self.timer.rescramble()
self.timer.reset(False) # update DEVICE_NUM of stackmat timer
update_mouse(not self.config.hide_mouse) # update mouse after done with settings
self.state = 'main'
else:
self.state = 'settings'
update_mouse(True) # make mouse visible so you can configure settings
elif event.key == pygame.K_h and event.mod & pygame.KMOD_CTRL:
if self.state == 'main':
# self.timer.refresh_stats()
self.history_renderer.re_render()
self.state = 'history'
else:
self.state = 'main'
elif event.key == pygame.K_t and event.mod & pygame.KMOD_CTRL:
if self.state == 'main':
self.times_manager_renderer.refresh()
self.state = 'times'
update_mouse(True)
else:
self.timer.refresh_stats()
update_mouse(not self.config.hide_mouse)
self.state = 'main'
elif event.key == pygame.K_c and event.mod & pygame.KMOD_CTRL:
pygame.scrap.put(pygame.SCRAP_TEXT, bytes(self.timer.current_scramble, encoding="utf-8"))
if self.state == 'main':
self.timer.update(events)
elif self.state == 'settings':
self.settings_renderer.update(events)
elif self.state == 'history':
self.history_renderer.update(events)
elif self.state == 'times':
self.times_manager_renderer.update(events)
else:
assert False, f"unknown state '{self.state}'"
def draw(self):
screen = self.screen
screen.fill((0, 0, 0))
if self.background is not None:
screen.blit(self.background, (0, 0))
if self.state == 'main':
self.draw_main(screen)
elif self.state == 'settings':
self.settings_renderer.draw(screen)
elif self.state == 'history':
self.history_renderer.draw(screen)
elif self.state == 'times':
self.times_manager_renderer.draw(screen)
else:
assert False, f"unknown state '{self.state}'"
pygame.display.update()
def draw_main(self, screen):
"""
Code below is used to render the overall items on the screen.
Adjust and modify the code however you want.
"""
# particles, update of screen render
if self.config.particles:
self.particle_renderer.update()
"""
Elements that only will be displayed if timer is inactive.
"""
if self.timer.ready <= 0 and not self.timer.running:
# current scramble
self.draw_string(self.font1, self.timer.current_scramble, (
self.screen.get_size()[0] / 2 - self.font1.size(self.timer.current_scramble)[0] / 2 - 5, 20))
# draw scramble
if self.config.draw_scramble:
self.timer.puzzle.renderer.render(self.screen, None)
# time stats
reversed_time_stats_list = self.time_stats.stats[::-1]
tx = 0
for stat in reversed_time_stats_list:
self.draw_string(self.font1, f"{stat.type}{stat.amount}: {time_str(stat.ms, long=True)}", (5, screen.get_size()[1] - 40 - tx))
tx += 25
"""
Elements that will be displayed all the time.
"""
# time
c = (255, 255, 255)
long = not self.timer.running
if self.timer.ready == 1:
c = (255, 0, 0)
long = False
elif self.timer.ready == 2:
c = (0, 255, 0)
long = False
s = time_str(int(self.timer.ms), long)
if self.timer.error is not None:
c = (255, 0, 0)
s = self.timer.error
self.draw_string(self.font2, s, (self.screen.get_size()[0] / 2 - self.font2.size(s)[0] / 2 - 5,
self.screen.get_size()[1] / 2 - self.font2.size(s)[1] / 2), color=c)
"""
End
"""
def on_timer_start(self):
self.particle_renderer.clear()
def on_timer_stop(self):
self.particle_renderer.refresh(70)
def draw_string(self, font: pygame.font.Font, text, coords, color=(255, 255, 255), background_color=None):
self.screen.blit(font.render(text, True, color, background_color), coords)
def save(self):
self.config.save()
def load(self):
try:
self.config.load()
except FileNotFoundError:
pass
timer.DEVICE_NUM = self.config.device_num
def time_str(time: Union[int, str], long: bool = False) -> str:
if type(time) == str:
return time
if time == -1:
return "-"
if time == -2:
return "DNF"
seconds = time // 1000 % 60
out = ''
if time // 60000 > 0:
out += f'{int(time // 60000)}:'
if seconds < 10:
out += '0'
out += f'{int(seconds)}'
if long:
out += '.'
millis = int(time % 1000)
if millis < 100:
out += '0'
if millis < 10:
out += '0'
out += f'{millis}'
return out
def update_mouse(visible):
pygame.mouse.set_visible(visible)
pygame.mouse.set_pos((pygame.mouse.get_pos()[0] - 0.0000000000069420, pygame.mouse.get_pos()[1]))
def draw_antialias_circle(surface, color, x, y, radius):
pygame.gfxdraw.aacircle(surface, round(x), round(y), radius, color)
pygame.gfxdraw.filled_circle(surface, round(x), round(y), radius, color)
def draw_aa_pie(surface, color, from_, to, radius):
draw_antialias_circle(surface, color, from_[0], from_[1], radius)
direction = atan2(from_[0] - to[0], from_[1] - to[1])
args = [
surface,
round(from_[0] + sin(direction + pi / 2) * radius),
round(from_[1] + cos(direction + pi / 2) * radius),
round(from_[0] + sin(direction - pi / 2) * radius),
round(from_[1] + cos(direction - pi / 2) * radius),
round(to[0]), round(to[1]), color
]
pygame.gfxdraw.filled_trigon(*args)
pygame.gfxdraw.aatrigon(*args)
def initcheck():
crf.CrfHandler().check_file()