Skip to content

Commit 4a92320

Browse files
Merge pull request #2480 from Shikhar9425/patch-3
Brick.py
2 parents e0de1a2 + 09cbcbc commit 4a92320

File tree

2 files changed

+330
-0
lines changed

2 files changed

+330
-0
lines changed

brick_game/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Package/Script Name: BrickBreakerPy
2+
3+
Short Description:
4+
BrickBreakerPy is a Python package that implements a simple Brick Breaker game using the Pygame library. The game allows the player to control a paddle to bounce a ball and break bricks to earn points.
5+
6+
Functionalities/Scripts:
7+
8+
BrickBreakerGame: The main script that runs the Brick Breaker game.
9+
Setup Instructions:
10+
11+
Install Python: Make sure you have Python 3.x installed on your system. You can download it from the official website: https://www.python.org/downloads/
12+
13+
Install Pygame: BrickBreakerPy requires the Pygame library. You can install it using pip by running the following command in your terminal or command prompt:
14+
15+
Copy code
16+
pip install pygame
17+
Download the Package: You can download the BrickBreakerPy package from the GitHub repository or any other source where it is hosted.
18+
19+
Extract Package: Extract the contents of the downloaded package to your desired location on your system.
20+
21+
Explain How to Setup and Run the Package/Script:
22+
23+
Open a terminal or command prompt and navigate to the directory where you extracted the BrickBreakerPy package.
24+
25+
Run the Brick Breaker game script:
26+
27+
Copy code
28+
python BrickBreakerGame.py
29+
The game window will open, and you can start playing the Brick Breaker game using your keyboard.
30+
31+
Detailed Explanation of the Script:
32+
33+
The script BrickBreakerGame.py implements the Brick Breaker game using the Pygame library. It sets up the game window, loads game assets (sprites, sounds), initializes the paddle, ball, and bricks. The game loop updates the game state, handles player input, and renders the game objects on the screen.
34+
35+
The player can control the paddle using the left and right arrow keys or the 'A' and 'D' keys. The objective is to bounce the ball off the paddle to break all the bricks on the screen. The game provides multiple levels of increasing difficulty.
36+
37+
When the ball hits a brick, the brick disappears, and the player earns points. If the ball hits the bottom of the screen, the player loses a life. The game ends when the player runs out of lives or clears all the bricks.
38+
39+
The script manages collisions between the ball, paddle, and bricks using simple collision detection techniques. It also handles game events, such as level transitions and game-over conditions.
40+
41+
Output:
42+
43+
Here are a few images showcasing the output of the Brick Breaker game
44+
45+
46+
47+
Authors:
48+
Shikhar9425
49+
50+
51+
52+
53+

brick_game/brick.py

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

0 commit comments

Comments
 (0)