2025年5月16日 星期五

打磚塊左右對打版


 


import pygame
import sys
import random
pygame.init()

#  玩家設定 0:玩家右   2:玩家左   9:開始未指定玩家
onplay = 9

# 畫面設定
WIDTH, HEIGHT = 1920, 1080
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 100, 255)
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 = 20
player_height = 100
player_x = WIDTH - 75
player_y = HEIGHT // 2 - player_height // 2
player_speed = 16
player2_width = 20
player2_height = 100
player2_x = 75
player2_y = HEIGHT // 2 - player_height // 2
player2_speed = 16

# 球
ball_radius = 10
ball_x = WIDTH // 2+random.randint(-200, 200)
ball_y = HEIGHT // 2
ball_x_speed = 18+ random.randint(-6, 6)
ball_y_speed = -12+ random.randint(-5, 5)

# 磚塊設定(5列×10行)
brick_rows = 20
brick_cols = 1
brick_width = 20
brick_height = HEIGHT // brick_rows
bricks = []
for row in range(brick_rows):
    for col in range(brick_cols):
        x = col * brick_width+WIDTH//2-50
        y = row * brick_height  # 從 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})

for row in range(brick_rows):
    for col in range(brick_cols):
        x = col * brick_width+WIDTH//2-50-200
        y = row * brick_height  # 從 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})

for row in range(brick_rows):
    for col in range(brick_cols):
        x = col * brick_width+WIDTH//2-50+200
        y = row * brick_height  # 從 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
score2 = 0
font = pygame.font.SysFont(None, 36)
clock = pygame.time.Clock()
running = True
while running:
    clock.tick(60*1.0)

    # 事件處理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 玩家左右控制
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        player_y -= player_speed
    if keys[pygame.K_DOWN]:
        player_y += player_speed
  
    if keys[pygame.K_q]:
        player2_y -= player2_speed
    if keys[pygame.K_z]:
        player2_y += player2_speed
    player_y = max(0, min(WIDTH - player_height, player_y))
    player2_y = max(0, min(WIDTH - player2_height, player2_y))
    player_y = ball_y - 50
    player2_y = ball_y - 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 = abs(ball_y_speed)
    if ball_y > HEIGHT:
        ball_y_speed = -ball_y_speed

    # 掉到底部,遊戲重置
    if ball_y > WIDTH:
        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 player_rect.collidepoint(ball_x, ball_y + ball_radius) and ball_x_speed > 0:
        ball_x_speed = -(ball_x_speed+ random.randint(-4, 4))
        ball_y_speed = (ball_y_speed+ random.randint(-4, 4))
        onplay = 0
    if player2_rect.collidepoint(ball_x, ball_y + ball_radius) and ball_x_speed < 0:
        ball_x_speed = -(ball_x_speed+ random.randint(-4, 4))
        ball_y_speed = (ball_y_speed+ random.randint(-4, 4))
        onplay = 2
    # 撞磚塊
    for b in bricks:
        if b['alive'] and b['rect'].collidepoint(ball_x, ball_y):
            b['alive'] = False
            ball_x_speed = -ball_x_speed
            if onplay == 0:score += 10
            if onplay == 2:score2 += 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, BLUE, 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))
    score_text2 = font.render(f"Score: {score2}", True, (0, 0, 0))
    screen.blit(score_text, (WIDTH-150, 10))
    screen.blit(score_text2, (10, 10))
    pygame.display.flip()
pygame.quit()
sys.exit()

沒有留言:

張貼留言