Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit da0e6ed

Browse files
Merge pull request avinashkranjan#854 from Yuvraj-kadale/brick_game
Brick game
2 parents 1abec10 + 15facfe commit da0e6ed

File tree

2 files changed

+299
-0
lines changed

2 files changed

+299
-0
lines changed

‎Brick Breaker game/Readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Brick Breaker Game
2+
3+
Brick Breaker (The game) is a Breakout clonewhich the player must smash a wall of bricks by deflecting a bouncing ball with a paddle. The paddle may move horizontally and is controlled with the side arrow keys.
4+
5+
## Setup instructions
6+
7+
Run `python/python3 brick_breaker.py`
8+
9+
## Output
10+
11+
![image](https://user-images.githubusercontent.com/43489758/114232380-68122e00-9999-11eb-93c2-6de9af43804a.png)
12+
13+
## Author
14+
15+
[Yuvraj kadale](https://github.com/Yuvraj-kadale) with ❤

‎Brick Breaker game/brick_breaker.py

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
2+
import pygame
3+
from pygame.locals import *
4+
5+
pygame.init()
6+
7+
'''
8+
Defining gaming window size and font
9+
'''
10+
Window_width = 500
11+
Window_height = 500
12+
13+
window = pygame.display.set_mode((Window_width, Window_height))
14+
pygame.display.set_caption('Brickstroy')
15+
16+
17+
font = pygame.font.SysFont('Arial', 30)
18+
19+
'''
20+
Defining Bricks colour
21+
'''
22+
O_brick = (255, 100, 10)
23+
w_brick = (255, 255, 255)
24+
g_brick = (0, 255, 0)
25+
black = (0, 0, 0)
26+
27+
28+
game_rows = 6
29+
game_coloumns = 6
30+
clock = pygame.time.Clock()
31+
frame_rate = 60
32+
my_ball = False
33+
game_over = 0
34+
score = 0
35+
36+
37+
class Ball():
38+
'''
39+
Creating ball for the game
40+
'''
41+
42+
def __init__(self, x, y):
43+
44+
self.radius = 10
45+
self.x = x - self.radius
46+
self.y = y - 50
47+
self.rect = Rect(self.x, self.y, self.radius * 2, self.radius * 2)
48+
self.x_speed = 4
49+
self.y_speed = -4
50+
self.max_speed = 5
51+
self.game_over = 0
52+
53+
def motion(self):
54+
collision_threshold = 5
55+
block_object = Block.bricks
56+
brick_destroyed = 1
57+
count_row = 0
58+
for row in block_object:
59+
count_item = 0
60+
for item in row:
61+
# check collision with gaming window
62+
if self.rect.colliderect(item[0]):
63+
if abs(self.rect.bottom - item[0].top) < collision_threshold and self.y_speed > 0:
64+
self.y_speed *= -1
65+
66+
if abs(self.rect.top - item[0].bottom) < collision_threshold and self.y_speed < 0:
67+
self.y_speed *= -1
68+
if abs(self.rect.right -item[0].left) < collision_threshold and self.x_speed > 0:
69+
self.x_speed *= -1
70+
if abs(self.rect.left - item[0].right) < collision_threshold and self.x_speed < 0:
71+
self.x_speed *= -1
72+
73+
if block_object[count_row][count_item][1] > 1:
74+
block_object[count_row][count_item][1] -= 1
75+
else:
76+
block_object[count_row][count_item][0] = (0, 0, 0, 0)
77+
78+
if block_object[count_row][count_item][0] != (0, 0, 0, 0):
79+
brick_destroyed = 0
80+
count_item += 1
81+
count_row += 1
82+
83+
if brick_destroyed == 1:
84+
self.game_over = 1
85+
86+
87+
# check for collision with bricks
88+
if self.rect.left < 0 or self.rect.right > Window_width:
89+
self.x_speed *= -1
90+
91+
if self.rect.top < 0:
92+
self.y_speed *= -1
93+
if self.rect.bottom > Window_height:
94+
self.game_over = -1
95+
96+
97+
# check for collission with base
98+
if self.rect.colliderect(user_basepad):
99+
if abs(self.rect.bottom - user_basepad.rect.top) < collision_threshold and self.y_speed > 0:
100+
self.y_speed *= -1
101+
self.x_speed += user_basepad.direction
102+
if self.x_speed > self.max_speed:
103+
self.x_speed = self.max_speed
104+
elif self.x_speed < 0 and self.x_speed < -self.max_speed:
105+
self.x_speed = -self.max_speed
106+
else:
107+
self.x_speed *= -1
108+
109+
self.rect.x += self.x_speed
110+
self.rect.y += self.y_speed
111+
112+
return self.game_over
113+
114+
115+
def draw(self):
116+
pygame.draw.circle(window, (0, 0, 255), (self.rect.x + self.radius, self.rect.y + self.radius), self.radius)
117+
pygame.draw.circle(window, (255, 255, 255), (self.rect.x + self.radius, self.rect.y + self.radius), self.radius, 1)
118+
119+
120+
121+
def reset(self, x, y):
122+
123+
self.radius = 10
124+
self.x = x - self.radius
125+
self.y = y - 50
126+
self.rect = Rect(self.x, self.y, self.radius * 2, self.radius * 2)
127+
self.x_speed = 4
128+
self.y_speed = -4
129+
self.max_speed = 5
130+
self.game_over = 0
131+
132+
133+
134+
class Block():
135+
'''
136+
This class will help me create Blocks/bricks of the game
137+
'''
138+
def __init__(self):
139+
self.width = Window_width // game_coloumns
140+
self.height = 40
141+
142+
def make_brick(self):
143+
self.bricks = []
144+
single_brick = []
145+
for row in range(game_rows):
146+
147+
brick_row = []
148+
149+
for coloumn in range(game_coloumns):
150+
151+
x_brick = coloumn * self.width
152+
y_brick = row * self.height
153+
rect = pygame.Rect(x_brick, y_brick, self.width, self.height)
154+
# assign power to the bricks based on row
155+
if row < 2:
156+
power = 3
157+
elif row < 4:
158+
power = 2
159+
elif row < 6:
160+
power = 1
161+
162+
single_brick = [rect, power]
163+
164+
brick_row.append(single_brick)
165+
166+
self.bricks.append(brick_row)
167+
168+
169+
def draw_brick(self):
170+
for row in self.bricks:
171+
for brick in row:
172+
173+
if brick[1] == 3:
174+
brick_colour = O_brick
175+
elif brick[1] == 2:
176+
brick_colour = w_brick
177+
elif brick[1] == 1:
178+
brick_colour = g_brick
179+
pygame.draw.rect(window, brick_colour, brick[0])
180+
pygame.draw.rect(window, black, (brick[0]), 1)
181+
182+
183+
184+
185+
186+
class base():
187+
'''
188+
This class is to create the base pad of the game
189+
'''
190+
def __init__(self):
191+
192+
self.height = 20
193+
self.width = int(Window_width / game_coloumns)
194+
self.x = int((Window_width / 2) - (self.width / 2))
195+
self.y = Window_height - (self.height * 2)
196+
self.speed = 8
197+
self.rect = Rect(self.x, self.y, self.width, self.height)
198+
self.direction = 0
199+
200+
201+
def slide(self):
202+
203+
self.direction = 0
204+
key = pygame.key.get_pressed()
205+
if key[pygame.K_LEFT] and self.rect.left > 0:
206+
self.rect.x -= self.speed
207+
self.direction = -1
208+
if key[pygame.K_RIGHT] and self.rect.right < Window_width:
209+
self.rect.x += self.speed
210+
self.direction = 1
211+
212+
def draw(self):
213+
pygame.draw.rect(window,(0, 0, 255), self.rect)
214+
pygame.draw.rect(window, (255, 255, 255), self.rect, 1)
215+
216+
217+
def reset(self):
218+
219+
self.height = 20
220+
self.width = int(Window_width / game_coloumns)
221+
self.x = int((Window_width / 2) - (self.width / 2))
222+
self.y = Window_height - (self.height * 2)
223+
self.speed = 8
224+
self.rect = Rect(self.x, self.y, self.width, self.height)
225+
self.direction = 0
226+
227+
228+
229+
def draw_text(text, font, w_brick, x, y):
230+
'''
231+
Funtion for showing text in gaming window
232+
'''
233+
image = font.render(text, True, w_brick)
234+
window.blit(image, (x, y))
235+
236+
237+
238+
Block = Block()
239+
Block.make_brick() # Creating Brick
240+
user_basepad = base() # Defining base pad
241+
ball = Ball(user_basepad.x + (user_basepad.width // 2), user_basepad.y - user_basepad.height) # Defining ball
242+
243+
game = True
244+
while game:
245+
246+
clock.tick(frame_rate)
247+
window.fill(black) # Gaming window Background
248+
Block.draw_brick() # Drawing bricks
249+
user_basepad.draw() # Drawing user basepad
250+
ball.draw() # Drawing gaming ball
251+
252+
if my_ball:
253+
user_basepad.slide()
254+
game_over = ball.motion()
255+
if game_over != 0:
256+
my_ball = False
257+
258+
259+
# Game Info on the gaming window
260+
if not my_ball:
261+
if game_over == 0:
262+
draw_text('CLICK ANYWHERE TO START', font, w_brick, 90, Window_height // 2 + 100)
263+
elif game_over == 1:
264+
draw_text('YOU WON!', font, w_brick, 180, Window_height // 2 + 50)
265+
draw_text('CLICK ANYWHERE TO RESTART', font, w_brick, 90, Window_height // 2 + 100)
266+
elif game_over == -1:
267+
draw_text('GAME OVER!', font, w_brick, 180, Window_height // 2 + 50)
268+
draw_text('CLICK ANYWHERE TO RESTART', font, w_brick, 90, Window_height // 2 + 100)
269+
270+
271+
for event in pygame.event.get():
272+
if event.type == pygame.QUIT:
273+
game = False
274+
if event.type == pygame.MOUSEBUTTONDOWN and my_ball == False:
275+
my_ball = True
276+
ball.reset(user_basepad.x + (user_basepad.width // 2), user_basepad.y - user_basepad.height)
277+
user_basepad.reset()
278+
Block.make_brick()
279+
280+
281+
282+
pygame.display.update()
283+
284+
pygame.quit()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /