-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
508 lines (391 loc) · 16.6 KB
/
main.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
# import sys
# import os
# username = os.environ['LOGNAME']
# new_path = '/Users/' + username + '/Library/Python/3.6/lib/python/site-packages'
# sys.path.insert(0, new_path)
import math
import pygame
import random
import time
from screeninfo import get_monitors
pygame.init()
black = (0, 0, 0)
white = (255, 255, 255)
red = (200, 0, 0)
orange = (255, 115, 49)
yellow = (231, 198, 0)
blue = (50, 150, 255)
dark_blue = (40, 120, 215)
green = (0, 200, 0)
bright_red = (255, 0, 0)
bright_green = (0, 255, 0)
dim_white = (200, 200, 200)
purple = (75, 0, 130)
pink = (225, 0, 225)
res = str(get_monitors()[0])
disp_width = int(res[8:12])
disp_height = int(res[13:17]) if disp_width >= 1920 else int(res[13:16])
disp_x = disp_width / 1920 # creates a scale for width of game (customizable)
disp_y = disp_height / 1080 # creates a scale for height of game (customizable)
screen = pygame.display.set_mode((disp_width, disp_height), pygame.FULLSCREEN)
screen.fill(black)
pygame.display.set_caption('Atari Breakout')
clock = pygame.time.Clock()
sound = True
music = True
if music:
pygame.mixer.music.load('assets/8-bit-music.mp3')
pygame.mixer.music.play(-1)
if not sound and not music:
pygame.mixer.music.stop()
breakout_icon = pygame.image.load('assets/breakout_icon.png')
breakout_icon = pygame.transform.scale(breakout_icon, (32, 32))
pygame.display.set_icon(breakout_icon)
class Paddle(pygame.sprite.Sprite):
def __init__(self, color, width, height):
self.color = color
self.dimensions = [width, height]
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = ((1920 / 2) * disp_x) - (150 * disp_x)
self.rect.y = (930 * disp_y)
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x = pos[0]
if self.rect.x > (1920 * disp_x) - self.dimensions[0]:
self.rect.x = (1920 * disp_x) - self.dimensions[0]
def handle_keys(self):
key = pygame.key.get_pressed()
if key[pygame.K_ESCAPE]:
return game_intro()
class Ball(pygame.sprite.Sprite):
def __init__(self, color, width, speed):
self.color = color
self.width = width
self.speed = 10
self.direction = random.randint(-50, 50) # direction of ball in degrees
self.x = ((1920 / 2) * disp_x)
self.y = (900 * disp_y)
super().__init__()
self.image = pygame.Surface((width, width))
self.image.fill(color)
self.rect = self.image.get_rect()
def bounce(self, difference):
"""Bounces the ball off horizontal surfaces only."""
self.direction = (180 - self.direction) % 360
self.direction -= difference
def set_speed(self, speed):
self.speed = speed
def change_speed(self, multiplier):
"""Lets you update the speed from the main loop"""
self.speed *= multiplier
def update(self, lives):
# ######### #
# This is from some stuff about ball physics about how it bounces
direction_radians = math.radians(self.direction)
self.x += self.speed * math.sin(direction_radians)
self.y -= self.speed * math.cos(direction_radians)
# move the image to x and y
self.rect.x = self.x
self.rect.y = self.y
# ########## #
if self.y <= 0:
self.bounce(0)
self.y = 1
if self.x <= 0:
self.direction = (360 - self.direction) % 360
self.x = 1
if self.x > (1920 * disp_x) - self.width:
self.direction = (360 - self.direction) % 360
self.x = (1920 * disp_x) - self.width - 1
if self.y > 1080 * disp_y:
self.x = ((1920 / 2) * disp_x)
self.y = (700 * disp_y)
self.direction = random.randint(-50, 50)
return lives - 1
return lives
class Block(pygame.sprite.Sprite):
def __init__(self, color, width, height, x, y):
self.color = color
self.width = width
self.height = height
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def text_objects(text, font): # renders text
text_surface = font.render(text, True, white)
return text_surface, text_surface.get_rect()
def text_box(msg, x, y, size=35): # makes a text box
small_text = pygame.font.Font("assets/PressStart2P.ttf", int(size * disp_x))
text_surf, text_rect = text_objects(msg, small_text)
text_rect.center = ((x), (y))
screen.blit(text_surf, text_rect)
def QUIT(): # makes the quit button work
pygame.quit()
quit()
def button(msg, x, y, w, h, ic, ac, action=None): # creates a button
# msg = text, x/y = pos of button, w/h = width height, ic = color when mouse not hover, ac = color when mouse hover, action = function to execute
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, ac, (x, y, w, h))
if click[0] == 1 and action:
if sound: # triggers sound effect
pygame.mixer.music.load('assets/button_click.mp3')
pygame.mixer.music.play(0)
if music:
pygame.mixer.music.load('assets/8-bit-music.mp3')
pygame.mixer.music.play(-1)
if not sound and not music:
pygame.mixer.music.stop()
action()
else:
pygame.draw.rect(screen, ic, (x, y, w, h))
small_text = pygame.font.Font('assets/PressStart2P.ttf', int(50 * disp_x))
text_surf, text_rect = text_objects(msg, small_text)
text_rect.center = ((x + (w / 2)), (y + (h / 2)))
screen.blit(text_surf, text_rect)
def icon(img, x, y, w, h, ic, ac, action=None): # loads the icon/button
# x/y = pos of button, w/h = width height, ic = color when mouse not hover, ac = color when mouse hover, action = function to execute
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, white, (x, y, w, h), 10)
pygame.draw.rect(screen, ac, (x, y, w, h))
if click[0] == 1 and action:
action()
if sound: # triggers sound effect
pygame.mixer.music.load('assets/button_click.mp3')
pygame.mixer.music.play(0)
if music:
pygame.mixer.music.load('assets/8-bit-music.mp3')
pygame.mixer.music.play(-1)
if not sound and not music:
pygame.mixer.music.stop()
else:
pygame.draw.rect(screen, dim_white, (x, y, w, h), 10)
pygame.draw.rect(screen, ic, (x, y, w, h))
# draws image into the surface
screen.blit(img, (x, y))
def s_on():
global sound
sound = False
def s_off(): # music config
global sound
sound = True
def m_on():
global music
music = False
def m_off(): # sound config
global music
music = True
def options(): # pops up the resolution options
oprunning = True
while oprunning:
clock.tick(60)
screen.fill(black)
paddle.handle_keys()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
soundImg = pygame.image.load('assets/sound.png')
soundImg = pygame.transform.scale(soundImg, (int(400 * disp_x), int(400 * disp_x)))
musicImg = pygame.image.load('assets/music.png')
musicImg = pygame.transform.scale(musicImg, (int(400 * disp_x), int(400 * disp_x)))
nsoundImg = pygame.image.load('assets/sound_mute.png')
nsoundImg = pygame.transform.scale(nsoundImg, (int(400 * disp_x), int(400 * disp_x)))
nmusicImg = pygame.image.load('assets/music_mute.png')
nmusicImg = pygame.transform.scale(nmusicImg, (int(400 * disp_x), int(400 * disp_x)))
s_ind = 'On' if sound else 'Off'
m_ind = 'On' if music else 'Off'
icon(nsoundImg, (1400 * disp_x), (((1080 / 4) - 200) * disp_y), 400 * disp_x, 400 * disp_y, dark_blue, blue, s_on)
icon(soundImg, (925 * disp_x), (((1080 / 4) - 200) * disp_y), 400 * disp_x, 400 * disp_y, dark_blue, blue, s_off)
icon(nmusicImg, (1400 * disp_x), (((1080 / 4) + 275) * disp_y), 400 * disp_x, 400 * disp_y, dark_blue, blue, m_on)
icon(musicImg, (925 * disp_x), (((1080 / 4) + 275) * disp_y), 400 * disp_x, 400 * disp_y, dark_blue, blue, m_off)
text_box('Sound : ' + s_ind, 400 * disp_x, 500 * disp_y, 60)
text_box('Music : ' + m_ind, 400 * disp_x, 600 * disp_y, 60)
button('X', 25 * disp_x, 25 * disp_y, 100 * disp_x, 100 * disp_y, red, bright_red, game_intro) # button is slow rn, so use esc
text_box('Press ESC or click X to leave this menu', 700 * disp_x, 1050 * disp_y)
pygame.display.flip() # allows options windows to actually stay
def logo_screen():
intro = True
clock.tick(60)
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(black)
text_box('BROUGHT TO YOU BY:', 960 * disp_x, 150 * disp_y, 75)
shrug_icon = pygame.image.load('assets/shrug_logo.png')
shrug_icon = pygame.transform.scale(shrug_icon, (int(1312 * disp_x), int(610 * disp_y)))
screen.blit(shrug_icon, (int(304 * disp_x), int(325 * disp_y)))
pygame.display.flip()
time.sleep(2)
screen.fill(black)
text_box('INSPIRED BY:', 960 * disp_x, 150 * disp_y, 75)
atari_icon = pygame.image.load('assets/atari_logo.png')
atari_icon = pygame.transform.scale(atari_icon, (int(800 * disp_x), int(800 * disp_y)))
screen.blit(atari_icon, (int(550 * disp_x), int(250 * disp_y)))
pygame.display.flip()
time.sleep(2)
screen.fill(black)
music_overlay = pygame.image.load('assets/music_overlay.png')
music_overlay = pygame.transform.scale(music_overlay, (int(1920 * disp_x), int(1080 * disp_y)))
screen.blit(music_overlay, (0, 0))
pygame.display.flip()
time.sleep(2)
game_intro()
def game_intro(): # title screen for breakout
gearImg = pygame.image.load('assets/gear.png')
gearImg = pygame.transform.scale(gearImg, (int(100 * disp_x), int(100 * disp_y)))
intro = True
pygame.mouse.set_visible(1) # makes the mouse appear
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(black)
# button overlays
rbut_overlay = pygame.image.load('assets/rbut_overlay.png')
rbut_overlay = pygame.transform.scale(rbut_overlay, (int(320 * disp_x), int(170 * disp_y)))
screen.blit(rbut_overlay, (int(1030 * disp_x), int(640 * disp_y)))
gbut_overlay = pygame.image.load('assets/gbut_overlay.png')
gbut_overlay = pygame.transform.scale(gbut_overlay, (int(320 * disp_x), int(170 * disp_y)))
screen.blit(gbut_overlay, (int(570 * disp_x), int(640 * disp_y)))
# title
banner_overlay = pygame.image.load('assets/banner_overlay.png')
banner_overlay = pygame.transform.scale(banner_overlay, (int(1920 * disp_x), int(1080 * disp_y)))
screen.blit(banner_overlay, (int(0 * disp_x), int(0 * disp_y)))
# filler??
breakout_icon = pygame.image.load('assets/breakout_icon.png')
breakout_icon = pygame.transform.scale(breakout_icon, (int(150 * disp_x), int(150 * disp_y)))
screen.blit(breakout_icon, (int(1760 * disp_x), int(10 * disp_y)))
# start/quit buttons
button('START', 580 * disp_x, 650 * disp_y, 300 * disp_x, 150 * disp_y, green, bright_green, main_game)
button('QUIT', 1040 * disp_x, 650 * disp_y, 300 * disp_x, 150 * disp_y, red, bright_red, QUIT)
# options gear button
icon(gearImg, 25 * disp_x, 25 * disp_y, 100 * disp_x, 100 * disp_y, dim_white, white, options)
pygame.display.update()
clock.tick(60)
# Initialize all sprites
blocks = pygame.sprite.Group()
balls = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
paddle = Paddle(blue, (300 * disp_x), (30 * disp_y))
all_sprites.add(paddle)
ball = Ball(white, 30 * disp_x, 30 * disp_y)
balls.add(ball)
all_sprites.add(ball)
def setup_blocks(level):
global disp_y
top = 80 # y of top layer of blocks
# Five rows of blocks
colors = [red, orange, yellow, green, blue, purple, pink]
for row in range(5 + level - 1):
for column in range(8):
block = Block(colors[row], (240 * disp_x), (54 * disp_y), column * (240 * disp_x + 2), top)
blocks.add(block)
all_sprites.add(block)
# Move the top of the next row down
top += 54 * disp_y
def game_done(win: bool):
"""Loads the game over/you win screen"""
pygame.mouse.set_visible(1)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
paddle.handle_keys()
screen.fill(black)
clock.tick(60)
cont_overlay = pygame.image.load('assets/cont_overlay.png')
cont_overlay = pygame.transform.scale(cont_overlay, (int(570 * disp_x), int(170 * disp_y)))
screen.blit(cont_overlay, (int(700 * disp_x), int(640 * disp_y)))
if win:
text_box('YOU WIN', 960 * disp_x, 400 * disp_y, 150)
else:
text_box('GAME OVER', 960 * disp_x, 400 * disp_y, 150)
button('CONTINUE', (960 - 250) * disp_x, 650 * disp_y, 550 * disp_x, 150 * disp_y, green, bright_green, game_intro)
pygame.display.flip()
def main_game(level=1):
running = True
sped_up = False
lives = 10
pygame.mixer.music.stop()
screen.fill(black)
setup_blocks(level)
text_box('LIVES: ' + str(lives), (175 * disp_x), (40 * disp_x))
text_box('LEVEL ' + str(level), (disp_width - (145 * disp_x)), (40 * disp_x))
text_box('LEVEL ' + str(level), 960 * disp_x, 500 * disp_y, 150)
pygame.display.flip()
time.sleep(3)
ball.set_speed(10)
if level == 1:
pass
elif level == 2:
ball.set_speed(12)
elif level == 3:
ball.set_speed(15)
else:
running = False # only up to level 3 for now
while running:
pygame.mouse.set_visible(0) # makes mouse disappear
clock.tick(60) # 60 fps
screen.fill(black)
paddle.handle_keys() # checks for ESC button (then goes back to main menu)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if lives > 0:
paddle.update()
lives = ball.update(lives)
if lives == 0:
game_done(False)
text_box('LIVES: ' + str(lives), (175 * disp_x), (40 * disp_x))
text_box('LEVEL ' + str(level), (disp_width - (145 * disp_x)), (40 * disp_x))
if pygame.sprite.spritecollide(paddle, balls, False):
# diff lets you try to bounce the ball in a certain direction depending on
# where on the paddle you hit the ball
diff = (paddle.rect.x + paddle.dimensions[0] / 2) - (ball.rect.x + ball.rect.height / 2)
# set the ball's y if the edge of the paddle
ball.rect.y = screen.get_height() - paddle.dimensions[1] - ball.rect.height
ball.bounce(diff)
if sound:
pygame.mixer.music.load('assets/paddle_sound.mp3')
pygame.mixer.music.play(0)
all_sprites.draw(screen)
pygame.display.flip()
# Check for collisions between the ball and the blocks
deadblocks = pygame.sprite.spritecollide(ball, blocks, True)
# makes the game more fast paced with 15 blocks left
if len(blocks) < 15 and not sped_up:
ball.change_speed(1.5)
sped_up = True
# If we actually hit a block, bounce the ball
if len(deadblocks) > 0:
ball.bounce(0)
if sound:
pygame.mixer.music.load('assets/brick_break.mp3')
pygame.mixer.music.play(0)
# Game ends if all the blocks are gone
if len(blocks) == 0:
if level == 3:
game_done(True)
# resets ball position
ball.direction = random.randint(-50, 50)
ball.x = ((1920 / 2) * disp_x)
ball.y = (900 * disp_y)
# loads the next level (faster ball speed)
main_game(level + 1)
pygame.quit()
quit()
logo_screen()