forked from ilaykav/Colorush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorush.py
564 lines (465 loc) · 20.3 KB
/
colorush.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# COLORUSH - a game for kids who want to learn the colors and the relatives between them
import math
import os
import random
from sys import exit
import pygame
from pygame.locals import *
total_score = 0
win_message = "WIN = 25"
set_lose_2 = True
you_win, you_lose = False, False
grey_counter = 0
pause = False
screen = 0
# TODO: delete completed todos
# TODO: seperate files (generally classes / or a bunch of functions)
# TODO: use PyCharm Code -> Reformat code, Auto-Indent lines & optimize imports - V
# TODO: run pycharm inspect project and fix - V
# TODO: this seems like types of colors. name accordingly?
# TODO: what about the other bunch or random vars?
class Variables():
# TODO: ilay, please. In what way is the next line readable?
white, black, grey, red, green, blue, light_blue, yellow, pink = (255, 255, 255), (0, 0, 0), (100, 100, 100), \
(255, 0, 0), (0, 255, 0), \
(0, 0, 255), (0, 255, 255), (255, 255, 0), (
255, 0, 255)
def update_presents():
global total_score, set_lose_2, you_lose, you_win, grey_counter, screen, fill_box_2, fill_box_1, i, \
rush_title_size, space_bar, grey_has_been_found, counter, text, font
global bar1_x, bar1_y, circle_x, circle_y, bar1_move, bar2_move, speed_x, speed_y, clock, clock_presents, \
index_of_color, right, left, presents, balls
global index_of_color2, try1, win_message, pause
if len(presents) != 0:
for ex_present in presents:
# TODO: move (255, 255, 255) to variable - V
if ex_present.color != Variables.white:
if you_lose and set_lose_2:
ex_present.set_lose()
set_lose_2 = False
if ex_present.color[0] == 0 and ex_present.color[1] == 0 and ex_present.color[2] == 0:
presents.remove(ex_present)
ex_present.update()
rt = pygame.Surface((50, 50))
rectangle_present = rt.convert()
rectangle_present.fill(ex_present.color)
# print bar1_y
# TODO: looks lime you can simplify this by using python 'min_limit < x < max_limit' syntax - V
if not you_lose and not you_win and bar1_y - 50 < ex_present.y < bar1_y and \
bar1_x - 50 < ex_present.x < bar1_x + 50:
if grey_has_been_found:
# TODO: is it duplication here? - V
try:
presents.remove(ex_present)
except ValueError:
pass
if not ex_present.is_grey:
if not you_lose:
total_score += 1
if real_color[0] == ex_present.color[0] and real_color[1] == ex_present.color[1] and real_color[2] \
== ex_present.color[2]:
try:
presents.remove(ex_present)
except ValueError:
pass
if not ex_present.is_grey:
if not you_lose:
total_score += 1
if ex_present.is_grey:
try:
presents.remove(ex_present)
except ValueError:
pass
grey_has_been_found = True
grey_counter = 1
screen.blit(rectangle_present, (ex_present.x, ex_present.y + i))
def update_death_balls():
if len(balls) != 0:
for ball in balls:
circle_surface = pygame.Surface((30, 30))
circle_outside = pygame.draw.circle(circle_surface, (220, 0, 20), (15, 15), 15)
circle_inside = pygame.draw.circle(circle_surface, Variables.black, (15, 15), 12)
circle = circle_surface.convert()
circle.set_colorkey(Variables.black)
screen.blit(circle, (ball.x, ball.y + i))
ball.update()
global you_lose
if not you_lose and not you_win and bar1_y - 30 < ball.y < bar1_y and \
bar1_x - 30 < ball.x < bar1_x + 50:
balls.remove(ball)
you_lose = True
def seem_impressive():
bla = 1
bla2 = random.randint(1, 1000000000)
for seem_counter in range(1, 10):
bla += math.pow(10, seem_counter) * random.randint(0, 1)
print bin(bla2 * bla2), bla, bla + bla2
def draw_pause_menu():
pause_box = pygame.Surface((500, 300))
pause_box.set_alpha(20)
load_shape(screen, (50, 50, 50), 75, 100, pause_box)
# load the texts of the pause menu
load_text(screen, Variables.white, font, 327, 320, "+ =")
# load pause menu object
pause_box = pygame.Surface((50, 50))
load_shape(screen, Variables.grey, 400, 245, pause_box)
# load pause menu object
pause_box = pygame.Surface((20, 20))
load_shapes(screen, (Variables.blue, Variables.green, Variables.light_blue), (300, 330, 350, 330, 400, 330),
pause_box)
# draws the circle
circle_surface = pygame.Surface((30, 30))
circle_outside = pygame.draw.circle(circle_surface, (220, 0, 20), (15, 15), 15)
circle_inside = pygame.draw.circle(circle_surface, Variables.black, (15, 15), 13)
circle = circle_surface.convert()
circle.set_colorkey(Variables.black)
screen.blit(circle, (205, 255))
# load the texts of the pause menu
font2 = pygame.font.SysFont("calibri", 25)
colors1 = [(100, 255, 100), (100, 255, 100), (255, 100, 100), (100, 100, 255), (100, 255, 100), (255, 100, 100),
(100, 100, 255), (255, 100, 100), (100, 100, 255), Variables.white]
xys = [80, 225, 80, 355, 270, 100, 80, 125, 80, 150, 80, 175, 80, 200, 80, 305, 80, 330, 80, 375]
texts = ["Hit R to restart", "To change: A D Your color", "Game Rules:",
"Reach to 25 points, earn points by selecting ", "presents- you can only select presents while the",
"present's color and your color are the same!", "Avoid the red loops! Take the gray presents!",
"Your color is the mix of the colors in the", "top-left corner:",
"Hit the space-bar to change your color to white"]
load_texts(screen, colors1, font2, xys, texts)
def load_shapes(load_screen, load_colors, load_xys, load_surface):
converted_surface = load_surface.convert()
for shape_counter, loaded_color in enumerate(load_colors):
converted_surface.fill(loaded_color)
load_screen.blit(converted_surface, (load_xys[shape_counter * 2], load_xys[shape_counter * 2 + 1]))
def load_shape(load_screen, load_color, load_x, load_y, load_surface):
converted_surface = load_surface.convert()
converted_surface.fill(load_color)
load_screen.blit(converted_surface, (load_x, load_y))
def restart():
global total_score, set_lose_2, you_lose, you_win, grey_counter, screen, fill_box_2, fill_box_1, i, \
rush_title_size, space_bar, grey_has_been_found, counter, text, font
global bar1_x, bar1_y, circle_x, circle_y, bar1_move, bar2_move, speed_x, speed_y, clock, clock_presents, \
index_of_color, right, left, presents, balls
global index_of_color2, try1, win_message, pause
win_message = "WIN = 25"
total_score = 0
set_lose_2 = True
you_win, you_lose = False, False
grey_counter = 0
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("COLORUSH")
fill_box_1 = Variables.red
fill_box_2 = Variables.red
rush_title_size = 20
space_bar = False
grey_has_been_found = False
counter, text = 60, '60'.rjust(3)
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
back = pygame.Surface((640, 480))
background = back.convert()
background.fill(Variables.black)
bar = pygame.Surface((50, 10))
bar1 = bar.convert()
bar1.fill(Variables.blue)
bar1_x = 300
bar1_y = 440
circle_x, circle_y = 307.5, 232.5
bar1_move, bar2_move = 0, 0
speed_x, speed_y = 250, 250
clock = pygame.time.Clock()
clock_presents = pygame.time.Clock()
font = pygame.font.SysFont("calibri", 40)
index_of_color = 0
right = False
left = False
del presents[:]
del balls[:]
index_of_color2 = 0
try1 = 0
def load_text(load_screen, load_color, load_font, load_x, load_y, load_text):
loaded_text = load_font.render(load_text, True, load_color)
load_screen.blit(loaded_text, (load_x, load_y))
def load_texts(load_screen, load_colors, load_font, load_xys, loaded_texts):
# TODO: use enumerate instead (google how to use it!) - V
for load_texts_counter, text in enumerate(loaded_texts):
loaded_text = load_font.render(text, True, load_colors[load_texts_counter])
load_screen.blit(loaded_text, (load_xys[load_texts_counter * 2], load_xys[load_texts_counter * 2 + 1]))
def game_handler():
# TODO: you can create a game state class with all of the globals, its better then all of the globals. It should be in a seperate file.
global total_score, set_lose_2, you_lose, you_win, grey_counter, screen, fill_box_2, fill_box_1, i, \
rush_title_size, space_bar, grey_has_been_found, counter, text, font
global bar1_x, bar1_y, circle_x, circle_y, bar1_move, bar2_move, speed_x, speed_y, clock, clock_presents, \
index_of_color, right, left, presents, balls
global index_of_color2, try1, win_message, pause
while True:
# TODO: the event handeler below has some balagan. try to structure it a clearer way. One possebility is - creating a dict mapping (event_type -> handeling_function) then just call that. Also the keyboard keys.
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
exit()
if event.key == K_r:
restart()
if event.key == K_p:
pause = True
if not you_win and not you_lose:
if event.type == pygame.USEREVENT:
counter -= 1
clock.tick(60)
if event.type == KEYDOWN:
if event.key == K_SPACE:
space_bar = True
if event.key == K_r:
restart()
elif event.key == K_d:
index_of_color2 = (index_of_color2 + 1) % 3
if index_of_color2 == 0:
fill_box_2 = Variables.red
elif index_of_color2 == 1:
fill_box_2 = Variables.green
elif index_of_color2 == 2:
fill_box_2 = Variables.blue
if event.key == K_a:
index_of_color = (index_of_color + 1) % 3
if index_of_color == 0:
fill_box_1 = Variables.red
elif index_of_color == 1:
fill_box_1 = Variables.green
elif index_of_color == 2:
fill_box_1 = Variables.blue
if event.key == K_LEFT:
try:
bar1_move = -ai_speed
except UnboundLocalError:
print("except")
left = True
elif event.key == K_RIGHT:
try:
bar1_move = ai_speed
except UnboundLocalError:
print("except")
right = True
elif event.type == KEYUP:
if event.key == K_SPACE:
space_bar = False
if event.key == K_LEFT:
bar1_move = 0.
if right:
try:
bar1_move = ai_speed
except UnboundLocalError:
pass
left = False
elif event.key == K_RIGHT:
bar1_move = 0.
if left:
try:
bar1_move = -ai_speed
except UnboundLocalError:
pass
right = False
# TODO: structure werid game logic below?
if bar1_x < 0:
if bar1_move < 0:
bar1_move = 0
if bar1_x > 585:
if bar1_move > 0:
bar1_move = 0
bar1_x += bar1_move
# movement
time_passed = clock.tick(30)
# seem_impressive()
mom = (random.randint(0, 1000))
chance = 33
if grey_has_been_found:
chance = 100
if mom < chance:
presents.append(Present())
if not grey_has_been_found and mom < chance - 28: # take the chance whether to make a death ball or noy
# including if there is color rush
i += 1
balls.append(DeathBall())
ai_speed = 15
if total_score == 25: # checks win-case
you_win = True
if counter == 0: # checks lose-case: if the timer reaches the 0
you_lose = True
update_screen()
while pause: # makes the pause menu and freezes the game
draw_pause_menu() # I think it's pretty clear...
for event in pygame.event.get(): # gets events from the user in the pause 'mode'
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
if event.key == K_p:
pause = False
if event.key == K_ESCAPE:
exit()
if event.key == K_r:
restart()
pygame.display.update()
pygame.display.update()
def prepare_game_workspace():
global screen
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.init()
pygame.display.set_caption("COLORUSH")
main_dir = os.path.split(os.path.abspath(__file__))[0]
file_of_icon = os.path.join(main_dir, 'icon.png')
surface_icon = pygame.image.load(file_of_icon)
icon = pygame.transform.scale(surface_icon, (32, 32))
pygame.display.set_icon(icon)
class DeathBall:
speed = random.randint(5, 15)
x = 0
y = 20
rectangle_present = 0
def __init__(self):
self.speed = random.randint(5, 12) # random.randint(5, 20)
self.x = random.randint(0, 600)
def update(self):
self.y += self.speed
class Present:
is_grey = False
speed = random.randint(3, 10)
x = 0
y = 20
rectangle_present = 0
def __init__(self):
global grey_has_been_found
self.color = [0, 0, 0]
self.speed = random.randint(3, 10) # random.randint(5, 20)
self.x = random.randint(0, 600)
grey_chance = 25
if grey_has_been_found:
grey_chance = 70
if 6 == random.randint(0, grey_chance):
self.is_grey = True
self.color = [100, 100, 100]
if not self.is_grey:
if 5 > (random.randint(1, 10)):
self.color[2] = 255
if 5 > (random.randint(1, 10)):
self.color[1] = 255
if 5 > (random.randint(1, 10)):
self.color[0] = 255
def reset_random(self):
self.speed = random.randint(0, 5)
self.x = random.randint(0, 600)
if bool(random.getrandbits(1)):
self.color[2] = 255
if bool(random.getrandbits(1)):
self.color[1] = 0
if bool(random.getrandbits(1)):
self.color[0] = 0
def update(self):
self.y += self.speed
def set_lose(self):
self.speed = random.randint(-10, -1)
self.y = 500
prepare_game_workspace()
fill_box_1 = Variables.red
fill_box_2 = Variables.red
i = 0
rush_title_size = 20
space_bar = False
grey_has_been_found = False
counter, text = 60, '60'.rjust(3)
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
def update_screen():
global rush_title_size
global grey_counter
global total_score
global grey_has_been_found
global bar1_move
global win_message
screen.blit(background, (0, 0))
screen.blit(bar1, (bar1_x, bar1_y))
global real_color
real_color = [0, 0, 0]
if 0 < (fill_box_2[0] + fill_box_1[0]):
real_color[0] = 255
if 0 < (fill_box_2[1] + fill_box_1[1]):
real_color[1] = 255
if 0 < (fill_box_2[2] + fill_box_1[2]):
real_color[2] = 255
bar1.fill(real_color)
if space_bar:
real_color = Variables.white
bar1.fill(Variables.white)
if grey_has_been_found:
real_color = [100, 100, 100]
bar1.fill([100, 100, 100])
grey_counter += 1
if grey_counter == 150:
grey_counter = False
grey_has_been_found = False
if 0 < (fill_box_2[0] + fill_box_1[0]):
real_color[0] = 255
if 0 < (fill_box_2[1] + fill_box_1[1]):
real_color[1] = 255
if 0 < (fill_box_2[2] + fill_box_1[2]):
real_color[2] = 255
bar1.fill(real_color)
if space_bar:
real_color = Variables.white
bar1.fill(Variables.white)
global set_lose_2
update_presents()
update_death_balls()
if you_lose:
win_message = ":'("
bar1_move = 0
load_text(screen, Variables.red, font, 250, 100, "YOU LOSE")
lose_present = Present()
lose_present.set_lose()
presents.append(lose_present)
if you_win:
bar1_move = 0
load_text(screen, Variables.yellow, font, 250, 100, "YOU WIN")
presents.append(Present())
if grey_counter == 0:
rush_title_size = 10
grey_has_been_found = False
index_box = pygame.Surface((20, 20))
load_texts(screen, [Variables.white, Variables.white], font, [40, 5, 10, 28], ["+ =", "A D "])
font2 = pygame.font.SysFont("calibri", 20)
load_text(screen, Variables.white, font2, 10, 62, "P = Instructions/Pause")
# Loads the texts of the timer
load_texts(screen, [(100, 255, 255), Variables.white], font, [580, 10, 484, 10], [str(counter).rjust(3), "Timer:"])
if grey_counter:
font2 = pygame.font.SysFont("calibri", rush_title_size)
load_text(screen, (100, 100, 255), font2, 310 - 2.5 * rush_title_size, 120 - rush_title_size / 2, "COLORUSH!")
if grey_counter % 2 == 0:
rush_title_size += 1
colors = [fill_box_1, fill_box_2, real_color]
xys = [10, 10, 70, 10, 130, 10]
load_shapes(screen, colors, xys, index_box)
score1 = font.render(str(total_score), True, Variables.light_blue)
screen.blit(score1, (280, 10))
score6 = font.render(win_message, True, Variables.pink)
screen.blit(score6, (320, 10))
back = pygame.Surface((640, 480))
background = back.convert()
background.fill(Variables.black)
bar = pygame.Surface((50, 10))
bar1 = bar.convert()
bar1.fill(Variables.blue)
bar1_x = 300
bar1_y = 440
circle_x, circle_y = 307.5, 232.5
bar1_move, bar2_move = 0., 0.
speed_x, speed_y = 250., 250.
clock = pygame.time.Clock()
clock_presents = pygame.time.Clock()
font = pygame.font.SysFont("calibri", 40)
index_of_color = 0
right = False
left = False
presents, balls = [], []
index_of_color2 = 0
try1 = 0
game_handler()