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