|
| 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 | + # check for collision with bricks |
| 87 | + if self.rect.left < 0 or self.rect.right > Window_width: |
| 88 | + self.x_speed *= -1 |
| 89 | + |
| 90 | + if self.rect.top < 0: |
| 91 | + self.y_speed *= -1 |
| 92 | + if self.rect.bottom > Window_height: |
| 93 | + self.game_over = -1 |
| 94 | + |
| 95 | + # check for collission with base |
| 96 | + if self.rect.colliderect(user_basepad): |
| 97 | + if abs(self.rect.bottom - user_basepad.rect.top) < collision_threshold and self.y_speed > 0: |
| 98 | + self.y_speed *= -1 |
| 99 | + self.x_speed += user_basepad.direction |
| 100 | + if self.x_speed > self.max_speed: |
| 101 | + self.x_speed = self.max_speed |
| 102 | + elif self.x_speed < 0 and self.x_speed < -self.max_speed: |
| 103 | + self.x_speed = -self.max_speed |
| 104 | + else: |
| 105 | + self.x_speed *= -1 |
| 106 | + |
| 107 | + self.rect.x += self.x_speed |
| 108 | + self.rect.y += self.y_speed |
| 109 | + |
| 110 | + return self.game_over |
| 111 | + |
| 112 | + def draw(self): |
| 113 | + pygame.draw.circle(window, (0, 0, 255), (self.rect.x + |
| 114 | + self.radius, self.rect.y + self.radius), self.radius) |
| 115 | + pygame.draw.circle(window, (255, 255, 255), (self.rect.x + |
| 116 | + self.radius, self.rect.y + self.radius), self.radius, 1) |
| 117 | + |
| 118 | + def reset(self, x, y): |
| 119 | + |
| 120 | + self.radius = 10 |
| 121 | + self.x = x - self.radius |
| 122 | + self.y = y - 50 |
| 123 | + self.rect = Rect(self.x, self.y, self.radius * 2, self.radius * 2) |
| 124 | + self.x_speed = 4 |
| 125 | + self.y_speed = -4 |
| 126 | + self.max_speed = 5 |
| 127 | + self.game_over = 0 |
| 128 | + |
| 129 | + |
| 130 | +class Block(): |
| 131 | + ''' |
| 132 | +This class will help me create Blocks/bricks of the game |
| 133 | +''' |
| 134 | + |
| 135 | + def __init__(self): |
| 136 | + self.width = Window_width // game_coloumns |
| 137 | + self.height = 40 |
| 138 | + |
| 139 | + def make_brick(self): |
| 140 | + self.bricks = [] |
| 141 | + single_brick = [] |
| 142 | + for row in range(game_rows): |
| 143 | + |
| 144 | + brick_row = [] |
| 145 | + |
| 146 | + for coloumn in range(game_coloumns): |
| 147 | + |
| 148 | + x_brick = coloumn * self.width |
| 149 | + y_brick = row * self.height |
| 150 | + rect = pygame.Rect(x_brick, y_brick, self.width, self.height) |
| 151 | + # assign power to the bricks based on row |
| 152 | + if row < 2: |
| 153 | + power = 3 |
| 154 | + elif row < 4: |
| 155 | + power = 2 |
| 156 | + elif row < 6: |
| 157 | + power = 1 |
| 158 | + |
| 159 | + single_brick = [rect, power] |
| 160 | + |
| 161 | + brick_row.append(single_brick) |
| 162 | + |
| 163 | + self.bricks.append(brick_row) |
| 164 | + |
| 165 | + def draw_brick(self): |
| 166 | + for row in self.bricks: |
| 167 | + for brick in row: |
| 168 | + |
| 169 | + if brick[1] == 3: |
| 170 | + brick_colour = O_brick |
| 171 | + elif brick[1] == 2: |
| 172 | + brick_colour = w_brick |
| 173 | + elif brick[1] == 1: |
| 174 | + brick_colour = g_brick |
| 175 | + pygame.draw.rect(window, brick_colour, brick[0]) |
| 176 | + pygame.draw.rect(window, black, (brick[0]), 1) |
| 177 | + |
| 178 | + |
| 179 | +class base(): |
| 180 | + ''' |
| 181 | +This class is to create the base pad of the game |
| 182 | +''' |
| 183 | + |
| 184 | + def __init__(self): |
| 185 | + |
| 186 | + self.height = 20 |
| 187 | + self.width = int(Window_width / game_coloumns) |
| 188 | + self.x = int((Window_width / 2) - (self.width / 2)) |
| 189 | + self.y = Window_height - (self.height * 2) |
| 190 | + self.speed = 8 |
| 191 | + self.rect = Rect(self.x, self.y, self.width, self.height) |
| 192 | + self.direction = 0 |
| 193 | + |
| 194 | + def slide(self): |
| 195 | + |
| 196 | + self.direction = 0 |
| 197 | + key = pygame.key.get_pressed() |
| 198 | + if key[pygame.K_LEFT] and self.rect.left > 0: |
| 199 | + self.rect.x -= self.speed |
| 200 | + self.direction = -1 |
| 201 | + if key[pygame.K_RIGHT] and self.rect.right < Window_width: |
| 202 | + self.rect.x += self.speed |
| 203 | + self.direction = 1 |
| 204 | + |
| 205 | + def draw(self): |
| 206 | + pygame.draw.rect(window, (0, 0, 255), self.rect) |
| 207 | + pygame.draw.rect(window, (255, 255, 255), self.rect, 1) |
| 208 | + |
| 209 | + def reset(self): |
| 210 | + |
| 211 | + self.height = 20 |
| 212 | + self.width = int(Window_width / game_coloumns) |
| 213 | + self.x = int((Window_width / 2) - (self.width / 2)) |
| 214 | + self.y = Window_height - (self.height * 2) |
| 215 | + self.speed = 8 |
| 216 | + self.rect = Rect(self.x, self.y, self.width, self.height) |
| 217 | + self.direction = 0 |
| 218 | + |
| 219 | + |
| 220 | +def draw_text(text, font, w_brick, x, y): |
| 221 | + ''' |
| 222 | + Funtion for showing text in gaming window |
| 223 | + ''' |
| 224 | + image = font.render(text, True, w_brick) |
| 225 | + window.blit(image, (x, y)) |
| 226 | + |
| 227 | + |
| 228 | +Block = Block() |
| 229 | +# Creating Brick |
| 230 | +Block.make_brick() |
| 231 | +# Defining base pad |
| 232 | +user_basepad = base() |
| 233 | +ball = Ball(user_basepad.x + (user_basepad.width // 2), |
| 234 | + user_basepad.y - user_basepad.height) # Defining ball |
| 235 | + |
| 236 | +game = True |
| 237 | +while game: |
| 238 | + |
| 239 | + clock.tick(frame_rate) |
| 240 | + window.fill(black) # Gaming window Background |
| 241 | + Block.draw_brick() # Drawing bricks |
| 242 | + user_basepad.draw() # Drawing user basepad |
| 243 | + ball.draw() # Drawing gaming ball |
| 244 | + |
| 245 | + if my_ball: |
| 246 | + user_basepad.slide() |
| 247 | + game_over = ball.motion() |
| 248 | + if game_over != 0: |
| 249 | + my_ball = False |
| 250 | + |
| 251 | + # Game Info on the gaming window |
| 252 | + if not my_ball: |
| 253 | + if game_over == 0: |
| 254 | + draw_text('CLICK ANYWHERE TO START', font, |
| 255 | + w_brick, 90, Window_height // 2 + 100) |
| 256 | + elif game_over == 1: |
| 257 | + draw_text('YOU WON!', font, w_brick, 180, Window_height // 2 + 50) |
| 258 | + draw_text('CLICK ANYWHERE TO RESTART', font, |
| 259 | + w_brick, 90, Window_height // 2 + 100) |
| 260 | + elif game_over == -1: |
| 261 | + draw_text('GAME OVER!', font, w_brick, |
| 262 | + 180, Window_height // 2 + 50) |
| 263 | + draw_text('CLICK ANYWHERE TO RESTART', font, |
| 264 | + w_brick, 90, Window_height // 2 + 100) |
| 265 | + |
| 266 | + for event in pygame.event.get(): |
| 267 | + if event.type == pygame.QUIT: |
| 268 | + game = False |
| 269 | + if event.type == pygame.MOUSEBUTTONDOWN and my_ball == False: |
| 270 | + my_ball = True |
| 271 | + ball.reset(user_basepad.x + (user_basepad.width // 2), |
| 272 | + user_basepad.y - user_basepad.height) |
| 273 | + user_basepad.reset() |
| 274 | + Block.make_brick() |
| 275 | + |
| 276 | + pygame.display.update() |
| 277 | + |
| 278 | +pygame.quit() |
0 commit comments