day 3 - jayce122003/day-1 GitHub Wiki
import pygame import random
Initialize pygame
pygame.init()
Screen settings
WIDTH, HEIGHT = 600, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Brick Breaker")
Load image
background_image = pygame.image.load("assets/ux.png") background_image = pygame.transform.scale(background_image, (WIDTH, HEIGHT))
Colors
WHITE = (255, 255, 255) BLACK = (0, 0, 0) BLUE = (0, 0, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) YELLOW = (255, 255, 0)
Fonts
font = pygame.font.Font(None, 36)
def draw_text(text, x, y, color=WHITE): label = font.render(text, True, color) screen.blit(label, (x, y))
def difficulty_selection(): selecting = True button_width, button_height = 180, 50 easy_rect = pygame.Rect((WIDTH - button_width) // 2, HEIGHT // 2 - 70, button_width, button_height) medium_rect = pygame.Rect((WIDTH - button_width) // 2, HEIGHT // 2 - 10, button_width, button_height) hard_rect = pygame.Rect((WIDTH - button_width) // 2, HEIGHT // 2 + 50, button_width, button_height)
while selecting:
screen.blit(background_image, (0, 0)) # Display background image
# Center the "WELCOME" text
welcome_text = "WELCOME"
text_width, text_height = font.size(welcome_text)
draw_text(welcome_text, (WIDTH - text_width) // 2, HEIGHT // 4, BLACK) # Now centered in black
# Draw difficulty buttons
pygame.draw.rect(screen, GREEN, easy_rect, border_radius=10)
pygame.draw.rect(screen, YELLOW, medium_rect, border_radius=10)
pygame.draw.rect(screen, RED, hard_rect, border_radius=10)
draw_text("Easy", easy_rect.centerx - 30, easy_rect.centery - 10, BLACK)
draw_text("Medium", medium_rect.centerx - 45, medium_rect.centery - 10, BLACK)
draw_text("Hard", hard_rect.centerx - 30, hard_rect.centery - 10, BLACK)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if easy_rect.collidepoint(event.pos):
return "Easy"
if medium_rect.collidepoint(event.pos):
return "Medium"
if hard_rect.collidepoint(event.pos):
return "Hard"
Game settings based on difficulty
selected_difficulty = difficulty_selection()
difficulty_settings = { "Easy": {"ball_speed": 3, "brick_rows": 5}, "Medium": {"ball_speed": 4, "brick_rows": 7}, "Hard": {"ball_speed": 5, "brick_rows": 9}, }
Paddle settings
PADDLE_WIDTH = 100 PADDLE_HEIGHT = 10 paddle_x = (WIDTH - PADDLE_WIDTH) // 2 paddle_y = HEIGHT - 50 paddle_speed = 7
Ball settings
BALL_RADIUS = 8 initial_ball_speed = difficulty_settings[selected_difficulty]["ball_speed"] ball_speed_multiplier = 1.0 balls = [{"x": WIDTH // 2, "y": HEIGHT // 2, "dx": random.choice([-initial_ball_speed, initial_ball_speed]), "dy": -initial_ball_speed}]
Brick settings
BRICK_ROWS = difficulty_settings[selected_difficulty]["brick_rows"] BRICK_COLS = 8 BRICK_WIDTH = WIDTH // BRICK_COLS - 5 BRICK_HEIGHT = 20 bricks = [pygame.Rect(col * (BRICK_WIDTH + 5) + 5, row * (BRICK_HEIGHT + 5) + 5, BRICK_WIDTH, BRICK_HEIGHT) for row in range(BRICK_ROWS) for col in range(BRICK_COLS)]
def game_over(): screen.fill(BLACK) draw_text("Game Over", WIDTH // 2 - 60, HEIGHT // 2) pygame.display.flip() pygame.time.delay(2000) pygame.quit() exit()
Game loop
running = True while running: screen.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move paddle
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle_x > 0:
paddle_x -= paddle_speed
if keys[pygame.K_RIGHT] and paddle_x < WIDTH - PADDLE_WIDTH:
paddle_x += paddle_speed
paddle_rect = pygame.Rect(paddle_x, paddle_y, PADDLE_WIDTH, PADDLE_HEIGHT)
# Move balls
for ball in balls[:]:
ball["x"] += ball["dx"] * ball_speed_multiplier
ball["y"] += ball["dy"] * ball_speed_multiplier
if ball["x"] - BALL_RADIUS <= 0 or ball["x"] + BALL_RADIUS >= WIDTH:
ball["dx"] = -ball["dx"]
if ball["y"] - BALL_RADIUS <= 0:
ball["dy"] = -ball["dy"]
if paddle_rect.collidepoint(ball["x"], ball["y"] + BALL_RADIUS):
ball["dy"] = -abs(ball["dy"])
for brick in bricks[:]:
if brick.collidepoint(ball["x"], ball["y"] + BALL_RADIUS):
ball["dy"] = -ball["dy"]
bricks.remove(brick)
break
if ball["y"] + BALL_RADIUS >= HEIGHT:
balls.remove(ball)
# If all balls are lost, end the game
if not balls:
game_over()
# Draw paddle
pygame.draw.rect(screen, BLUE, paddle_rect)
# Draw balls
for ball in balls:
pygame.draw.circle(screen, RED, (int(ball["x"]), int(ball["y"])), BALL_RADIUS)
# Draw bricks
for brick in bricks:
pygame.draw.rect(screen, GREEN, brick)
pygame.display.flip()
pygame.time.delay(30)
pygame.quit()