|
| 1 | +import pygame |
| 2 | +import math |
| 3 | + |
| 4 | +pygame.init() |
| 5 | + |
| 6 | +WIDTH, HEIGHT = 800, 600 |
| 7 | +win = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 8 | +pygame.display.set_caption("Brick Breaker") |
| 9 | + |
| 10 | +FPS = 60 |
| 11 | +PADDLE_WIDTH = 100 |
| 12 | +PADDLE_HEIGHT = 15 |
| 13 | +BALL_RADIUS = 10 |
| 14 | + |
| 15 | +LIVES_FONT = pygame.font.SysFont("comicsans", 40) |
| 16 | + |
| 17 | + |
| 18 | +class Paddle: |
| 19 | + VEL = 5 |
| 20 | + |
| 21 | + def __init__(self, x, y, width, height, color): |
| 22 | + self.x = x |
| 23 | + self.y = y |
| 24 | + self.width = width |
| 25 | + self.height = height |
| 26 | + self.color = color |
| 27 | + |
| 28 | + def draw(self, win): |
| 29 | + pygame.draw.rect( |
| 30 | + win, self.color, (self.x, self.y, self.width, self.height)) |
| 31 | + |
| 32 | + def move(self, direction=1): |
| 33 | + self.x = self.x + self.VEL * direction |
| 34 | + |
| 35 | + |
| 36 | +class Ball: |
| 37 | + VEL = 5 |
| 38 | + |
| 39 | + def __init__(self, x, y, radius, color): |
| 40 | + self.x = x |
| 41 | + self.y = y |
| 42 | + self.radius = radius |
| 43 | + self.color = color |
| 44 | + self.x_vel = 0 |
| 45 | + self.y_vel = -self.VEL |
| 46 | + |
| 47 | + def move(self): |
| 48 | + self.x += self.x_vel |
| 49 | + self.y += self.y_vel |
| 50 | + |
| 51 | + def set_vel(self, x_vel, y_vel): |
| 52 | + self.x_vel = x_vel |
| 53 | + self.y_vel = y_vel |
| 54 | + |
| 55 | + def draw(self, win): |
| 56 | + pygame.draw.circle(win, self.color, (self.x, self.y), self.radius) |
| 57 | + |
| 58 | + |
| 59 | +class Brick: |
| 60 | + def __init__(self, x, y, width, height, health, colors): |
| 61 | + self.x = x |
| 62 | + self.y = y |
| 63 | + self.width = width |
| 64 | + self.height = height |
| 65 | + self.health = health |
| 66 | + self.max_health = health |
| 67 | + self.colors = colors |
| 68 | + self.color = colors[0] |
| 69 | + |
| 70 | + def draw(self, win): |
| 71 | + pygame.draw.rect( |
| 72 | + win, self.color, (self.x, self.y, self.width, self.height)) |
| 73 | + |
| 74 | + def collide(self, ball): |
| 75 | + if not (ball.x <= self.x + self.width and ball.x >= self.x): |
| 76 | + return False |
| 77 | + if not (ball.y - ball.radius <= self.y + self.height): |
| 78 | + return False |
| 79 | + |
| 80 | + self.hit() |
| 81 | + ball.set_vel(ball.x_vel, ball.y_vel * -1) |
| 82 | + return True |
| 83 | + |
| 84 | + def hit(self): |
| 85 | + self.health -= 1 |
| 86 | + self.color = self.interpolate( |
| 87 | + *self.colors, self.health/self.max_health) |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def interpolate(color_a, color_b, t): |
| 91 | + # 'color_a' and 'color_b' are RGB tuples |
| 92 | + # 't' is a value between 0.0 and 1.0 |
| 93 | + # this is a naive interpolation |
| 94 | + return tuple(int(a + (b - a) * t) for a, b in zip(color_a, color_b)) |
| 95 | + |
| 96 | + |
| 97 | +def draw(win, paddle, ball, bricks, lives): |
| 98 | + win.fill("white") |
| 99 | + paddle.draw(win) |
| 100 | + ball.draw(win) |
| 101 | + |
| 102 | + for brick in bricks: |
| 103 | + brick.draw(win) |
| 104 | + |
| 105 | + lives_text = LIVES_FONT.render(f"Lives: {lives}", 1, "black") |
| 106 | + win.blit(lives_text, (10, HEIGHT - lives_text.get_height() - 10)) |
| 107 | + |
| 108 | + pygame.display.update() |
| 109 | + |
| 110 | + |
| 111 | +def ball_collision(ball): |
| 112 | + if ball.x - BALL_RADIUS <= 0 or ball.x + BALL_RADIUS >= WIDTH: |
| 113 | + ball.set_vel(ball.x_vel * -1, ball.y_vel) |
| 114 | + if ball.y + BALL_RADIUS >= HEIGHT or ball.y - BALL_RADIUS <= 0: |
| 115 | + ball.set_vel(ball.x_vel, ball.y_vel * -1) |
| 116 | + |
| 117 | + |
| 118 | +def ball_paddle_collision(ball, paddle): |
| 119 | + if not (ball.x <= paddle.x + paddle.width and ball.x >= paddle.x): |
| 120 | + return |
| 121 | + if not (ball.y + ball.radius >= paddle.y): |
| 122 | + return |
| 123 | + |
| 124 | + paddle_center = paddle.x + paddle.width/2 |
| 125 | + distance_to_center = ball.x - paddle_center |
| 126 | + |
| 127 | + percent_width = distance_to_center / paddle.width |
| 128 | + angle = percent_width * 90 |
| 129 | + angle_radians = math.radians(angle) |
| 130 | + |
| 131 | + x_vel = math.sin(angle_radians) * ball.VEL |
| 132 | + y_vel = math.cos(angle_radians) * ball.VEL * -1 |
| 133 | + |
| 134 | + ball.set_vel(x_vel, y_vel) |
| 135 | + |
| 136 | + |
| 137 | +def generate_bricks(rows, cols): |
| 138 | + gap = 2 |
| 139 | + brick_width = WIDTH // cols - gap |
| 140 | + brick_height = 20 |
| 141 | + |
| 142 | + bricks = [] |
| 143 | + for row in range(rows): |
| 144 | + for col in range(cols): |
| 145 | + brick = Brick(col * brick_width + gap * col, row * brick_height + |
| 146 | + gap * row, brick_width, brick_height, 2, [(0, 255, 0), (255, 0, 0)]) |
| 147 | + bricks.append(brick) |
| 148 | + |
| 149 | + return bricks |
| 150 | + |
| 151 | + |
| 152 | +def main(): |
| 153 | + clock = pygame.time.Clock() |
| 154 | + |
| 155 | + paddle_x = WIDTH/2 - PADDLE_WIDTH/2 |
| 156 | + paddle_y = HEIGHT - PADDLE_HEIGHT - 5 |
| 157 | + paddle = Paddle(paddle_x, paddle_y, PADDLE_WIDTH, PADDLE_HEIGHT, "black") |
| 158 | + ball = Ball(WIDTH/2, paddle_y - BALL_RADIUS, BALL_RADIUS, "black") |
| 159 | + |
| 160 | + bricks = generate_bricks(3, 10) |
| 161 | + lives = 3 |
| 162 | + |
| 163 | + def reset(): |
| 164 | + paddle.x = paddle_x |
| 165 | + paddle.y = paddle_y |
| 166 | + ball.x = WIDTH/2 |
| 167 | + ball.y = paddle_y - BALL_RADIUS |
| 168 | + |
| 169 | + |
| 170 | + def display_text(text): |
| 171 | + text_render = LIVES_FONT.render(text, 1, "red") |
| 172 | + win.blit(text_render, (WIDTH/2 - text_render.get_width() / |
| 173 | + 2, HEIGHT/2 - text_render.get_height()/2)) |
| 174 | + pygame.display.update() |
| 175 | + pygame.time.delay(3000) |
| 176 | + |
| 177 | + run = True |
| 178 | + while run: |
| 179 | + clock.tick(FPS) |
| 180 | + |
| 181 | + for event in pygame.event.get(): |
| 182 | + if event.type == pygame.QUIT: |
| 183 | + run = False |
| 184 | + break |
| 185 | + |
| 186 | + keys = pygame.key.get_pressed() |
| 187 | + |
| 188 | + if keys[pygame.K_LEFT] and paddle.x - paddle.VEL >= 0: |
| 189 | + paddle.move(-1) |
| 190 | + if keys[pygame.K_RIGHT] and paddle.x + paddle.width + paddle.VEL <= WIDTH: |
| 191 | + paddle.move(1) |
| 192 | + |
| 193 | + ball.move() |
| 194 | + ball_collision(ball) |
| 195 | + ball_paddle_collision(ball, paddle) |
| 196 | + |
| 197 | + for brick in bricks[:]: |
| 198 | + brick.collide(ball) |
| 199 | + |
| 200 | + if brick.health <= 0: |
| 201 | + bricks.remove(brick) |
| 202 | + |
| 203 | + # lives check |
| 204 | + if ball.y + ball.radius >= HEIGHT: |
| 205 | + lives -= 1 |
| 206 | + ball.x = paddle.x + paddle.width/2 |
| 207 | + ball.y = paddle.y - BALL_RADIUS |
| 208 | + ball.set_vel(0, ball.VEL * -1) |
| 209 | + |
| 210 | + if lives <= 0: |
| 211 | + bricks = generate_bricks(3, 10) |
| 212 | + lives = 3 |
| 213 | + reset() |
| 214 | + display_text("You Lost!") |
| 215 | + |
| 216 | + if len(bricks) == 0: |
| 217 | + bricks = generate_bricks(3, 10) |
| 218 | + lives = 3 |
| 219 | + reset() |
| 220 | + display_text("You Won!") |
| 221 | + |
| 222 | + draw(win, paddle, ball, bricks, lives) |
| 223 | + |
| 224 | + pygame.quit() |
| 225 | + quit() |
| 226 | + |
| 227 | + |
| 228 | +if __name__ == "__main__": |
| 229 | + main() |
0 commit comments