-
Notifications
You must be signed in to change notification settings - Fork 11
/
game.py
629 lines (569 loc) · 25.3 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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# BallAndBrick
# Copyright (C) 2017 Hitesh Agarwal
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from random import random
import sys
import pygame
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class BallAndBrick:
def __init__(self):
self.green = (34, 139, 34)
self.red = (255, 0, 0)
self.yellow = (255, 255, 153)
self.white = (255, 255, 255)
self.blue = (0, 0, 255)
self.black = (0, 0, 0)
self.back_col = (255, 255, 255)
self.brick_col = (0, 0, 0)
self.scroll_col = (0, 0, 0)
self.ball_still = 0
self.ball_play = 1
self.ball_won = 2
self.ball_game_over = 3
self.clock = pygame.time.Clock()
self.pause = False
self.shake = 0
self.ball_vel = pygame.Vector2(7, -7)
self.theme_keys = (
pygame.K_1,
pygame.K_2,
pygame.K_3,
pygame.K_4,
pygame.K_5,
pygame.K_6,
pygame.K_7,
pygame.K_8,
pygame.K_9,
)
self.back_cols = (
(255, 255, 255),
(228, 47, 12),
(47, 79, 79),
(140, 89, 51),
(0, 255, 255),
(115, 69, 35),
(6, 43, 22),
(25, 25, 112),
(9, 84, 190),
)
self.brick_cols = (
(0, 0, 0),
(245, 226, 226),
(255, 255, 255),
(237, 201, 175),
(34, 139, 34),
(0, 255, 0),
(173, 255, 47),
(255, 255, 49),
(212, 219, 178),
)
self.scroll_cols = (
(0, 0, 0),
(0, 255, 0),
(255, 255, 255),
(237, 201, 175),
(34, 139, 34),
(0, 255, 0),
(173, 255, 47),
(255, 255, 49),
(212, 219, 178),
)
# Called to save the state of the game to the Journal.
def write_file(self, file_path):
pass
# Called to load the state of the game from the Journal.
def read_file(self, file_path):
pass
def run(self):
self.brick_hit_sound = pygame.mixer.Sound("assets/brickhit.ogg")
self.paddle_hit_sound = pygame.mixer.Sound("assets/paddlehit.ogg")
self.brick_hit_sound.set_volume(0.8)
def restart_game():
pygame.mixer.music.load("assets/jazz.ogg")
pygame.mixer.music.play(-1)
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
self.lives = 3
self.score = 0
self.state = self.ball_still
self.paddle = pygame.Rect(
int(screen.get_width() / 2), y_scrol, scroller_w, scroller_h
)
self.ball = pygame.Rect(
int(screen.get_width() / 2),
y_scrol - b_diameter,
b_diameter,
b_diameter,
)
brick_make()
def sx(x):
screen = pygame.display.get_surface()
scale = screen.get_width() / 1200
x *= scale
return x
def brick_make():
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
brick_start_pos_y = 70
self.bricks_arr = []
for i in range(7):
brick_start_pos_x = (screen.get_width() - (brick_w + sx(10)) * 8) / 2
for j in range(8):
self.bricks_arr.append(
pygame.Rect(
brick_start_pos_x, brick_start_pos_y, brick_w, brick_h
)
)
brick_start_pos_x += brick_w + sx(10)
brick_start_pos_y += brick_h + 5
def draw_bricks():
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
if self.shake > pygame.time.get_ticks():
rand = (random() - 0.5) * 6, (random() - 0.5) * 6
else:
rand = 0, 0
for brick in self.bricks_arr:
pygame.draw.rect(
screen,
self.brick_col,
(brick.x + rand[0], brick.y + rand[1], brick.width, brick.height),
)
def check_input():
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
while Gtk.events_pending():
Gtk.main_iteration()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
self.pause = True
pause = True
paused()
if event.key == pygame.K_LEFT:
self.keys_pressed[pygame.K_LEFT] = True
if event.key == pygame.K_RIGHT:
self.keys_pressed[pygame.K_RIGHT] = True
if event.key == pygame.K_c and self.state == self.ball_still:
if self.score > 150:
self.ball_vel = pygame.Vector2(10, -10)
elif self.score > 100:
self.ball_vel = pygame.Vector2(8, -8)
else:
self.ball_vel = pygame.Vector2(7, -7)
self.state = self.ball_play
elif event.key == pygame.K_n and (
self.state == self.ball_game_over or self.state == self.ball_won
):
restart_game()
elif event.key == pygame.K_s:
settings()
elif event.key == pygame.K_q:
pygame.quit()
quit()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
self.keys_pressed[pygame.K_LEFT] = False
if event.key == pygame.K_RIGHT:
self.keys_pressed[pygame.K_RIGHT] = False
if self.keys_pressed[pygame.K_LEFT]:
self.paddle.left -= 12
if self.paddle.left < 0:
self.paddle.left = 0
if self.keys_pressed[pygame.K_RIGHT]:
self.paddle.left += 12
if self.paddle.left > x_max_scrol:
self.paddle.left = x_max_scrol
def move_ball():
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
self.ball.left += self.ball_vel.x
self.ball.top += self.ball_vel.y
if self.ball.left <= 0:
self.ball.left = 0
self.ball_vel.x = -self.ball_vel.x
elif self.ball.left >= x_max_ball:
self.ball.left = x_max_ball
self.ball_vel.x = -self.ball_vel.x
if self.ball.top < 0:
self.ball.top = 0
self.ball_vel.y = -self.ball_vel.y
elif self.ball.top >= y_max_ball:
self.ball.top = y_max_ball
self.ball_vel.y = -self.ball_vel.y
def handle_collisions():
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
for brick in self.bricks_arr:
if self.ball.colliderect(brick):
self.brick_hit_sound.play()
self.score += 3
self.bricks_arr.remove(brick)
self.shake = pygame.time.get_ticks() + 200
dr = abs(self.ball.right - brick.left)
dl = abs(self.ball.left - brick.right)
db = abs(self.ball.bottom - brick.top)
dt = abs(self.ball.top - brick.bottom)
if min(dl, dr) < min(dt, db):
self.ball_vel.x = -self.ball_vel.x
else:
self.ball_vel.y = -self.ball_vel.y
self.ball_vel.rotate_ip(random() * 10 - 5)
break
if len(self.bricks_arr) == 0:
self.state = self.ball_won
if self.ball.colliderect(self.paddle):
self.paddle_hit_sound.play()
self.ball.top = y_scrol - b_diameter
self.ball_vel.y = -self.ball_vel.y
self.ball_vel.rotate_ip(random() * 10 - 5)
elif self.ball.top > self.paddle.top:
self.lives -= 1
if self.lives > 0:
self.state = self.ball_still
else:
self.state = self.ball_game_over
def score_lives():
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
medfont = pygame.font.Font("fonts/comicsansms.ttf", int(sx(30)))
font_surface = medfont.render(
"Score: " + str(self.score), True, self.brick_col
)
live_surface = medfont.render(
"Lives left: " + str(self.lives), True, self.brick_col
)
screen.blit(font_surface, (sx(100), 5))
screen.blit(
live_surface,
(round(screen.get_width() / 2 - live_surface.get_width() / 2), 5),
)
pause_mess = medfont.render("Press P to pause", True, self.brick_col)
screen.blit(
pause_mess,
(round(screen.get_width() - pause_mess.get_width() - sx(100)), 5),
)
def text_size(text, color, size):
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
smallfont = pygame.font.Font("fonts/comicsansms.ttf", int(sx(30)))
medfont = pygame.font.Font("fonts/comicsansms.ttf", int(sx(40)))
largefont = pygame.font.Font("fonts/comicsansms.ttf", int(sx(70)))
if size == "small":
textSurf = smallfont.render(text, True, color)
if size == "medium":
textSurf = medfont.render(text, True, color)
if size == "large":
textSurf = largefont.render(text, True, color)
return textSurf, textSurf.get_rect()
def message_to_screen(msg, color, y_displace=100, size="small"):
screen = pygame.display.get_surface()
scale = screen.get_height() / 900
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
textSur, textRect = text_size(msg, color, size)
textRect.center = ((screen.get_width()) / 2), (
(screen.get_height() - 50) / 2
) + y_displace * scale
screen.blit(textSur, textRect)
def unpause():
global pause
pygame.mixer.music.unpause()
self.pause = False
def draw_pause():
screen = pygame.display.get_surface()
screen.fill(self.back_col)
message_to_screen("Paused", self.brick_col, 0, size="large")
message_to_screen("Press P to continue", self.brick_col, 100, size="medium")
message_to_screen(
"Press N for a new game", self.brick_col, 170, size="medium"
)
message_to_screen("Press Q to quit", self.brick_col, 240, size="medium")
pygame.display.update()
def paused():
draw_pause()
pygame.mixer.music.pause()
self.pause = True
while self.pause:
while Gtk.events_pending():
Gtk.main_iteration()
for event in pygame.event.get():
if event.type == pygame.VIDEORESIZE:
pygame.display.set_mode(event.size, pygame.RESIZABLE)
draw_pause()
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
unpause()
elif event.key == pygame.K_q:
pygame.quit()
quit()
elif event.key == pygame.K_n:
gameLoop()
def draw_settings():
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
screen.fill(self.yellow)
message_to_screen("Settings", self.black, -400, size="large")
message_to_screen("Press N to start a new game", self.black, -300, size="medium")
message_to_screen("Press Q to quit", self.black, -230, size="medium")
message_to_screen("Themes: ", self.black, -150, size="medium")
message_to_screen("1. Classic ", self.black, -80, size="small")
message_to_screen("2. Fresh and Bright ", self.black, -30, size="small")
message_to_screen("3. Professional ", self.black, 20, size="small")
message_to_screen("4. Earthy ", self.black, 70, size="small")
message_to_screen("5. Watery ", self.black, 120, size="small")
message_to_screen("6. Natural ", self.black, 170, size="small")
message_to_screen("7. Energetic ", self.black, 220, size="small")
message_to_screen("8. Day and Night ", self.black, 270, size="small")
message_to_screen("9. Aqua Blues ", self.black, 320, size="small")
message_to_screen(
"Press the theme number to select the theme ",
self.black,
370,
size="medium",
)
pygame.display.update()
def settings():
draw_settings()
while True:
while Gtk.events_pending():
Gtk.main_iteration()
for event in pygame.event.get():
if event.type == pygame.VIDEORESIZE:
pygame.display.set_mode(event.size, pygame.RESIZABLE)
draw_settings()
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_n:
gameLoop()
elif event.key == pygame.K_q:
pygame.quit()
quit()
else:
for i, key in enumerate(self.theme_keys):
if event.key == key:
self.back_col = self.back_cols[i]
self.brick_col = self.brick_cols[i]
self.scroll_col = self.scroll_cols[i]
gameLoop()
break
def gameLoop():
screen = pygame.display.get_surface()
brick_w = round((screen.get_width()) / 10.7)
brick_h = round((screen.get_height()) / 32)
scroller_w = round((screen.get_width()) / 10.7)
scroller_h = round((screen.get_height()) / 40)
b_diameter = round((scroller_w) / 3.75)
x_max_scrol = screen.get_width() - scroller_w
y_scrol = (screen.get_height() - 10) - scroller_h - 70
x_max_ball = (screen.get_width() - sx(10)) - b_diameter
y_max_ball = (screen.get_height() - 10) - b_diameter
pygame.display.update()
restart_game()
self.keys_pressed = {
pygame.K_LEFT: False,
pygame.K_RIGHT: False
}
while 1:
self.clock.tick(50)
screen.fill(self.back_col)
check_input()
draw_bricks()
pygame.draw.rect(screen, self.scroll_col, self.paddle)
pygame.draw.circle(
screen,
self.scroll_col,
(
self.ball.left + int(b_diameter / 2),
self.ball.top + int(b_diameter / 2),
),
int(b_diameter / 2),
)
score_lives()
if self.state == self.ball_play:
move_ball()
handle_collisions()
elif self.state == self.ball_still:
self.ball.left = self.paddle.left + self.paddle.width / 2
self.ball.top = self.paddle.top - self.ball.height
message_to_screen(
"Press C to play", self.brick_col, 0, size="large"
)
elif self.state == self.ball_game_over:
message_to_screen("Game Over", self.brick_col, 50, size="large")
message_to_screen(
"Press N for New Game", self.brick_col, 150, size="medium"
)
message_to_screen(
"Press S for setting the theme",
self.brick_col,
220,
size="medium",
)
message_to_screen(
"Press Q to quit", self.brick_col, 290, size="medium"
)
pygame.mixer.music.stop()
elif self.state == self.ball_won:
message_to_screen("You Won", self.brick_col, 50, size="large")
message_to_screen(
"Press N for New Game", self.brick_col, 150, size="medium"
)
message_to_screen(
"Press S for setting the theme",
self.brick_col,
220,
size="medium",
)
message_to_screen(
"Press Q to quit", self.brick_col, 290, size="medium"
)
pygame.mixer.music.stop()
pygame.display.update()
#####################################################################
pygame.init()
pygame.display.set_mode((0, 0), pygame.RESIZABLE)
screen = pygame.display.get_surface()
def draw_welcome():
screen.fill(self.yellow)
message_to_screen(
"Welcome to Ball and Brick", [0, 0, 0], -100, size="large"
)
message_to_screen("Press C to start the game", self.green, 0, size="medium")
message_to_screen(
"Press S for setting the theme", self.green, 70, size="medium"
)
message_to_screen("Press Q to quit the game", self.red, 140, size="medium")
pygame.display.flip()
self.clock.tick(10)
draw_welcome()
while True:
while Gtk.events_pending():
Gtk.main_iteration()
for event in pygame.event.get():
if event.type == pygame.VIDEORESIZE:
pygame.display.set_mode(event.size, pygame.RESIZABLE)
draw_welcome()
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
gameLoop()
elif event.key == pygame.K_s:
settings()
elif event.key == pygame.K_q:
pygame.quit()
quit()
self.clock.tick(10)
def main():
pygame.init()
pygame.display.set_mode((0, 0), pygame.RESIZABLE)
game = BallAndBrick()
game.run()
if __name__ == "__main__":
main()