-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbreaker.py
281 lines (224 loc) · 8.68 KB
/
breaker.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
import pygame
import random
from button import Button
pygame.init()
scr_width = 800
scr_height = 600
screen = pygame.display.set_mode((scr_width, scr_height))
pygame.display.set_caption("Brick Breaker")
# COLORS IN GAME
bkgrd_color = (200, 200, 200)
ball_color = (50, 40, 255)
paddle_color = (80, 50, 30)
brick_color = [(200, 50, 80), (45, 80, 240), (46, 235, 70),
(150, 50, 180), (230, 170, 80), (60, 230, 220), (180, 210, 80)]
class Bricks():
global screen
global scr_width
global scr_height
global brick_color
def __init__(self):
self.rows = 7
self.rows_bricks = 9
self.length = int(scr_width*0.8)//self.rows_bricks
self.width = 40
self.spacing = 4
self.cordinates = []
self.random_color = []
for i in range(10, self.rows*(self.width), self.width):
for j in range(int(scr_width*0.1), int(scr_width*0.9 - self.length)+1, self.length):
self.cordinates.append([j, i])
self.random_color.append(random.choice(brick_color))
def show(self):
num = 1
color_index = 1
for item in self.cordinates:
pygame.draw.rect(screen, brick_color[color_index-1], ((
item[0], item[1]), (self.length-self.spacing, self.width-self.spacing)))
num += 1
if num > color_index * self.rows_bricks:
color_index += 1
# self.random_color[num]
def clone(self):
brick_list = []
for item in self.cordinates:
brick_list.append(item)
return brick_list
def update(self, cordinate):
pygame.draw.rect(screen, bkgrd_color, (cordinate,
(self.length-self.spacing, self.width-self.spacing)))
class Paddle():
global screen
global scr_height
global scr_width
global paddle_color
def __init__(self, paddleX):
self.paddleX = paddleX
self.paddleY = int(scr_height*0.95)
self.length = 120
def show(self):
thickness = 10
pygame.draw.rect(screen, paddle_color, ((
self.paddleX, self.paddleY), (self.length, thickness)))
def move_left(self):
self.velocity = 15
self.paddleX += -self.velocity
def move_right(self):
self.velocity = 15
self.paddleX += self.velocity
def stop(self):
self.velocity = 0
def boundries(self):
if self.paddleX >= (scr_width - self.length):
self.paddleX = scr_width - self.length
elif self.paddleX <= 0:
self.paddleX = 0
# have to initial here because there is a reference of paddle in Ball object
paddle = Paddle(int(scr_width*0.45))
class Ball():
global screen
global ball_color
global paddle
def __init__(self, ballX, ballY):
self.ballX = ballX
self.ballY = ballY
self.x_vel = 8
self.y_vel = -8
self.ball_radius = 10
self.max_x_vel = 10
def show(self):
pygame.draw.circle(screen, ball_color,
(self.ballX, self.ballY), self.ball_radius)
def move(self):
self.ballX += self.x_vel
self.ballY += self.y_vel
def collision_change(self):
center = paddle.paddleX + paddle.length//2
left_end = paddle.paddleX
right_end = paddle.paddleX + paddle.length
self.y_vel = -self.y_vel
if left_end < self.ballX < center:
ratio = (center - self.ballX)//(paddle.length//2)
self.x_vel += -self.max_x_vel * ratio
elif center < self.ballX < right_end:
ratio = (self.ballX - center)//(paddle.length//2)
self.x_vel += self.max_x_vel * ratio
def boundries(self):
if self.ballY <= (0 + self.ball_radius):
self.y_vel = -self.y_vel
if self.ballX <= (0 + self.ball_radius):
self.x_vel = -self.x_vel
if self.ballX >= (scr_width - self.ball_radius):
self.x_vel = -self.x_vel
def limit_vel(self):
if -self.max_x_vel > self.x_vel:
self.x_vel = -self.max_x_vel
elif self.x_vel > self.max_x_vel:
self.x_vel = self.max_x_vel
def brick_collision(brick, brick_list, brick_breaked, ball):
for item in brick_list:
x = item[0]
y = item[1]
index = brick_list.index(item)
if x < ball.ballX and ball.ballX < (x + brick.length) and (ball.ballY + ball.ball_radius) > y and ball.ballY < y:
ball.y_vel = -ball.y_vel
brick_breaked.append(item)
brick_list.pop(index)
elif y < ball.ballY and ball.ballY < (y + brick.width) and (ball.ballX + ball.ball_radius) > x and ball.ballX < x:
ball.x_vel = -ball.x_vel
brick_breaked.append(item)
brick_list.pop(index)
elif y < ball.ballY and ball.ballY < (y + brick.width) and (ball.ballX - ball.ball_radius) < (x + brick.length) and ball.ballX > (x + brick.length):
ball.x_vel = -ball.x_vel
brick_breaked.append(item)
brick_list.pop(index)
elif x < ball.ballX and ball.ballX < (x + brick.length) and (ball.ballY - ball.ball_radius) < (y + brick.width) and ball.ballY > (y + brick.width):
ball.y_vel = -ball.y_vel
brick_breaked.append(item)
brick_list.pop(index)
def show_gameover():
global scr_height
global scr_width
text = pygame.font.Font("freesansbold.ttf", int(scr_height*0.1))
gameover = text.render("GAME OVER", True, (255, 23, 20))
screen.blit(gameover, (int(scr_width*0.25), int(scr_height*0.4)))
clock = pygame.time.Clock()
while True:
# initial positions
ball = Ball(int(scr_width/2), int(scr_height*0.8))
brick = Bricks()
brick_list = brick.clone()
brick_breaked = []
over = False
clicked_replay = False
# paddle movement switches
key_left = False
key_right = False
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
key_left = True
if event.key == pygame.K_RIGHT:
key_right = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
paddle.stop()
key_left = False
if event.key == pygame.K_RIGHT:
paddle.stop()
key_right = False
# GAME LOGIC
# paddle movement switches
if key_left == True:
paddle.move_left()
if key_right == True:
paddle.move_right()
# ball machanics
ball.move()
ball.boundries()
ball.limit_vel()
# must review code in the first inquality statement
if paddle.paddleY + 10 > (ball.ballY + ball.ball_radius) > paddle.paddleY and ball.ballX > paddle.paddleX and ball.ballX < (paddle.paddleX + paddle.length):
ball.collision_change()
# brick collision
brick_collision(brick, brick_list, brick_breaked, ball)
# paddle boundries
paddle.boundries()
if ball.ballY > scr_height:
show_gameover()
over = True
# REPLAY BUTTON
b = Button(screen, (80, 45, 200), (200, 250, 255),
(260, 350), (150, 60), "REPLAY", 30)
state = 'original'
while True:
b.show()
for event in pygame.event.get():
if b.isOverMouse() == True:
if event.type == pygame.MOUSEBUTTONUP:
clicked_replay = True
state = 'changed'
elif b.isOverMouse() == False:
state = 'original'
if event.type == pygame.QUIT:
pygame.quit()
if state == 'changed':
b.changeColor((80, 240, 80), (14, 37, 100))
if clicked_replay == True:
break
pygame.display.update()
# DISPLAY THINGS
screen.fill(bkgrd_color)
paddle.show()
brick.show()
for brk in brick_breaked:
brick.update(brk)
# brick_collision(brick, brick_list, ball)
ball.show()
if over == True:
break
pygame.display.update()