2025年5月15日 星期四

打磚塊 II



# -*- coding: utf-8 -*-

import pygame

import sys

import random

pygame.init()


# 畫面設定

WIDTH, HEIGHT = 1400, 600

WHITE = (255, 255, 255)

RED = (255, 0, 0)

BLUE = (0, 100, 255)

YELLOW = (255, 255, 0)

PURPLE1 = (153, 0, 153)

COLORS = [(255, 100, 100), (255, 200, 0), (0, 200, 100), (100, 100, 255), (200, 0, 200)]

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("打磚塊遊戲")

# 玩家方塊

player_width = 100

player_height = 20

player_x = WIDTH // 2 - player_width // 2

player_y = HEIGHT - 50

player_speed = 8

player2_width = 100

player2_height = 20

player2_x = WIDTH // 2 - player_width // 2

player2_y =  50

player2_speed = 8


# 球


ball_radius = 10


ball_x = WIDTH // 2


ball_y = HEIGHT // 2 - 100 *random.randint(-1,1)


ball_x_speed = 24


ball_y_speed = -12




# 磚塊設定(5列×10行)


brick_rows = 3


brick_cols = 30


brick_width = WIDTH // brick_cols


brick_height = 25


bricks = []




for row in range(brick_rows):


    for col in range(brick_cols):


        x = col * brick_width


        y = row * brick_height + HEIGHT//2 - 25  # 從 y=40 開始畫


        rect = pygame.Rect(x, y, brick_width - 2, brick_height - 2)


        color = COLORS[row % len(COLORS)]


        bricks.append({'rect': rect, 'color': color, 'alive': True})




# 分數


score = 0


font = pygame.font.SysFont(None, 36)




clock = pygame.time.Clock()


running = True


while running:


    clock.tick(60)




    # 事件處理


    for event in pygame.event.get():


        if event.type == pygame.QUIT:


            running = False




    # 玩家左右控制


    keys = pygame.key.get_pressed()


    if keys[pygame.K_LEFT]:


        player_x -= player_speed


    if keys[pygame.K_RIGHT]:


        player_x += player_speed


    player_x = max(0, min(WIDTH - player_width, player_x))

    player_x =  ball_x -50

    player2_x =  ball_x -50



    # 球移動


    ball_x += ball_x_speed


    ball_y += ball_y_speed




    # 左右牆反彈


    if ball_x - ball_radius <= 0 or ball_x + ball_radius >= WIDTH:


        ball_x_speed = -ball_x_speed




    # 上下邊界反彈


    if ball_y - ball_radius <= 0:


        ball_y_speed = -ball_y_speed




    # 掉到底部,遊戲重置


    if ball_y > HEIGHT:


        ball_x = WIDTH // 2


        ball_y = HEIGHT // 2


        ball_x_speed = 4


        ball_y_speed = -4


        score = 0


        for b in bricks:


            b['alive'] = True




    # 撞玩家方塊


    player_rect = pygame.Rect(player_x, player_y, player_width, player_height)

    player2_rect = pygame.Rect(player2_x, player2_y, player2_width, player2_height)


    if player_rect.collidepoint(ball_x, ball_y + ball_radius) and ball_y_speed > 0:


        ball_y_speed = -ball_y_speed

    if player2_rect.collidepoint(ball_x, ball_y + ball_radius) and ball_y_speed < 0:


        ball_y_speed = -ball_y_speed




    # 撞磚塊


    for b in bricks:


        if b['alive'] and b['rect'].collidepoint(ball_x, ball_y):


            b['alive'] = False


            ball_y_speed = -ball_y_speed


            score += 10


            break




    # 畫面更新


    screen.fill(WHITE)




    # 畫磚塊


    for b in bricks:


        if b['alive']:


            pygame.draw.rect(screen, b['color'], b['rect'])




    # 畫玩家與球


    pygame.draw.rect(screen, RED, player_rect)

    pygame.draw.rect(screen, PURPLE1, player2_rect)


    pygame.draw.circle(screen, BLUE, (int(ball_x), int(ball_y)), ball_radius)




    # 分數顯示


    score_text = font.render(f"Score: {score}", True, (0, 0, 0))


    screen.blit(score_text, (10, 10))




    pygame.display.flip()




pygame.quit()


sys.exit()


沒有留言:

張貼留言