-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakout.py
136 lines (109 loc) · 5.11 KB
/
breakout.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
import pygame
from status import *
from rectangle import *
from constants import *
pygame.init()
bricks = []
size = (SCREEN_WIDTH, SCREEN_HEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Breakout - PyGame Edition - 2023-11-30")
FONT = pygame.font.Font('assets/PressStart2P.ttf', 44)
# player 1
player_1 = Player("assets/player.png", "h", PLAYER_WIDTH, PLAYER_HEIGHT, 300, SCREEN_HEIGHT - 100, 8)
ball = Ball("assets/player.png", BALL_SIZE, BALL_START_X, BALL_START_Y, 8, -30)
# brick
color = COLOR_BLUE
row_y = 70
for i in range(0, 6):
bricks.append([])
brick_x = FIRST_BRICK_X
for j in range(0, 6):
if i == 2: # Change color of bricks
color = COLOR_GREEN
elif i == 4:
color = COLOR_ORANGE
bricks[i].append(Brick("assets/player.png", "h", BRICK_WIDTH, BRICK_HEIGHT, brick_x, row_y, color))
brick_x += BRICK_WIDTH + BRICK_OFFSET
row_y += 30
Game_Status.bricks_alive = 36
# victory
victory = False
# game loop
game_loop = True
game_clock = pygame.time.Clock()
while game_loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_loop = False
# keystroke events
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
player_1.move_left = True
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
player_1.move_right = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
player_1.move_left = False
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
player_1.move_right = False
# checking the victory condition
if not Game_Status.bricks_alive == 0 and Game_Status.player_lives > 0:
# clear screen
screen.fill(COLOR_BLACK)
player_1.move(0, SCREEN_WIDTH - PLAYER_WIDTH)
ball.move(0, SCREEN_WIDTH - BALL_SIZE, 0, SCREEN_HEIGHT - BALL_SIZE)
lives_text = FONT.render(str(Game_Status.player_lives), True, COLOR_WHITE, COLOR_BLACK)
lives_text_rect = lives_text.get_rect()
lives_text_rect.center = (lives_text_rect.width,
lives_text_rect.height)
score_text = FONT.render(str(Game_Status.player_score), True, COLOR_WHITE, COLOR_BLACK)
score_text_rect = score_text.get_rect()
score_text_rect.center = (SCREEN_WIDTH - score_text_rect.width,
score_text_rect.height)
# drawing objects
screen.blit(score_text, score_text_rect)
screen.blit(lives_text, lives_text_rect)
screen.blit(ball.game_object, (ball.x, ball.y))
screen.blit(player_1.game_object, (player_1.x, player_1.y)) # SCREEN_HEIGHT - 100))
for x in range(0, 6):
for y in range(0, 6):
if (not bricks[x][y].destroyed):
screen.blit(bricks[x][y].game_object, (bricks[x][y].x, bricks[x][y].y))
elif Game_Status.player_lives > 0:
# clear screen
screen.fill(COLOR_BLACK)
victory_text = FONT.render("YOU WIN", True, COLOR_WHITE, COLOR_BLACK)
victory_text_rect = victory_text.get_rect()
victory_text_rect.center = (SCREEN_WIDTH / 2,
SCREEN_HEIGHT / 2 - victory_text_rect.height * 4)
screen.blit(victory_text, victory_text_rect)
score_text = FONT.render("SCORE:" + str(Game_Status.player_score), True, COLOR_WHITE, COLOR_BLACK)
score_text_rect = score_text.get_rect()
score_text_rect.center = (score_text_rect.width / 2 + 30,
victory_text_rect.bottom + victory_text_rect.height * 2)
screen.blit(score_text, score_text_rect)
extra_score_text = FONT.render("EXTRA LIVES: " + str(Game_Status.player_lives - 1), True, COLOR_WHITE,
COLOR_BLACK)
extra_score_text_rect = extra_score_text.get_rect()
extra_score_text_rect.center = (extra_score_text_rect.width / 2 + 30,
score_text_rect.bottom + score_text_rect.height)
screen.blit(extra_score_text, extra_score_text_rect)
total_score_text = FONT.render(
"TOTAL SCORE:" + str(Game_Status.player_score + (Game_Status.player_lives - 1) * 50), True, COLOR_WHITE,
COLOR_BLACK)
total_score_text_rect = total_score_text.get_rect()
total_score_text_rect.center = (total_score_text_rect.width / 2 + 30,
extra_score_text_rect.bottom + extra_score_text_rect.height * 2)
screen.blit(total_score_text, total_score_text_rect)
else:
# clear screen
screen.fill(COLOR_BLACK)
defeat_text = FONT.render('YOU LOSE', True, COLOR_WHITE, COLOR_BLACK)
defeat_text_rect = defeat_text.get_rect()
defeat_text_rect.center = (SCREEN_WIDTH / 2,
SCREEN_HEIGHT / 2)
screen.blit(defeat_text, defeat_text_rect)
# update screen
pygame.display.flip()
game_clock.tick(60)
pygame.quit()