ni好
import pygame
import random
import math
# 初始化 Pygame
pygame.init()
# 设置屏幕尺寸
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("打砖块游戏 - 蓄力挡板版")
# 颜色定义
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# 游戏状态
class GameState:
SHOW_INSTRUCTIONS = 0
PLAYING = 1
PAUSED = 2
game_state = GameState.SHOW_INSTRUCTIONS # 初始状态为显示操作说明
# 挡板属性
paddle_width = 100
paddle_height = 10
paddle_x = (SCREEN_WIDTH - paddle_width) // 2
paddle_y = SCREEN_HEIGHT - paddle_height - 10
paddle_speed = 10
# 球属性
ball_radius = 5
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT // 2
ball_speed = 5 # 球的初始速度
# 随机化球的初始方向
def randomize_ball_direction():
angle = random.uniform(math.pi / 4, 3 * math.pi / 4) # 45°~135° 之间的随机角度
ball_speed_x = ball_speed * math.cos(angle)
ball_speed_y = -ball_speed * math.sin(angle) # 负号表示向上运动
return ball_speed_x, ball_speed_y
ball_speed_x, ball_speed_y = randomize_ball_direction()
# 砖块属性
brick_width = 10
brick_height = 10
# 固定挡板属性
fixed_paddles = [
pygame.Rect(50, 400, 80, 10), # 第一个固定挡板
pygame.Rect(200, 500, 80, 10), # 第二个固定挡板
pygame.Rect(650, 400, 80, 10), # 第三个固定挡板
pygame.Rect(500, 500, 80, 10), # 第四个固定挡板
]
# 关卡系统
levels = [
[
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111000000000000000000000000000000000011111111",
"11111111000000000000000000000000000000000011111111",
"11111111000000000000000000000000000000000011111111",
"11111111000000000000000000000000000000000011111111",
"11111111000000000000000000000000000000000011111111",
"11111111000000000000000000000000000000000011111111",
"11111111000000000000000000000000000000000011111111",
"11111111000000000000000000000000000000000011111111",
"11111111000000000000000000000000000000000011111111",
"11111111111111110000000000000000001111111111111111",
"11111111111111111000000000000000011111111111111111",
"11111111111111111100000000000000111111111111111111",
"11111111111111111110000000000001111111111111111111",
"11111111111111111111111111111111111111111111111111",
],
[
"10101010101010101010101010101010101010101010101010",
"01010101010101010101010101010101010101010101010101",
"10101010101010101010101010101010101010101010101010",
"01010101010101010101010101010101010101010101010101",
"10101010101010101010101010101010101010101010101010",
"01010101010101010101010101010101010101010101010101",
"10101010101010101010101010101010101010101010101010",
"01010101010101010101010101010101010101010101010101",
"10101010101010101010101010101010101010101010101010",
"01010101010101010101010101010101010101010101010101",
"10101010101010101010101010101010101010101010101010",
"01010101010101010101010101010101010101010101010101",
"10101010101010101010101010101010101010101010101010",
"01010101010101010101010101010101010101010101010101",
"10101010101010101010101010101010101010101010101010",
"01010101010101010101010101010101010101010101010101",
"10101010101010101010101010101010101010101010101010",
"01010101010101010101010101010101010101010101010101",
],
[
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
"11111111111111111111111111111111111111111111111111",
]
# ...(和之前的关卡数据一样,此处省略)
]
current_level = 0
# 分数系统
score = 0
font = pygame.font.Font(None, 36)
small_font = pygame.font.Font(None, 24)
# 生命系统
lives = 10
# 蓄力挡板系统
paddle_power = 1.0 # 默认反弹力度
MAX_POWER = 2.0 # 最大蓄力倍数
POWER_GAIN = 0.05 # 每次蓄力增加的力度
# 初始化砖块
def create_bricks(level):
bricks = []
for row in range(len(level)):
for col in range(len(level[row])):
if level[row][col] == "1":
brick_x = col * (brick_width + 5) + 30
brick_y = row * (brick_height + 5) + 50
bricks.append(pygame.Rect(brick_x, brick_y, brick_width, brick_height))
return bricks
bricks = create_bricks(levels[current_level])
# 游戏主循环
clock = pygame.time.Clock()
running = True
while running:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if game_state == GameState.SHOW_INSTRUCTIONS:
game_state = GameState.PLAYING # 第一次按空格开始游戏
elif game_state == GameState.PLAYING:
game_state = GameState.PAUSED # 游戏中按空格暂停
elif game_state == GameState.PAUSED:
game_state = GameState.PLAYING # 暂停时按空格继续
# 根据游戏状态更新游戏逻辑
if game_state == GameState.PLAYING:
# 挡板移动
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle_x > 1:
paddle_x -= paddle_speed
if keys[pygame.K_RIGHT] and paddle_x < SCREEN_WIDTH - paddle_width:
paddle_x += paddle_speed
# 蓄力挡板逻辑(改为下方向键)
if keys[pygame.K_DOWN]:
paddle_power = min(MAX_POWER, paddle_power + POWER_GAIN)
else:
paddle_power = 1.0 # 松开时恢复默认力度
# 球移动
ball_x += ball_speed_x
ball_y += ball_speed_y
# 球与墙碰撞检测
if ball_x - ball_radius <= 0 or ball_x + ball_radius >= SCREEN_WIDTH:
ball_speed_x = -ball_speed_x
if ball_y - ball_radius <= 0:
ball_speed_y = -ball_speed_y
# 球与挡板碰撞检测(应用蓄力效果)
paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height)
if paddle_rect.collidepoint(ball_x, ball_y + ball_radius):
ball_speed_y = -ball_speed_y * paddle_power # 应用蓄力倍数
ball_speed_x += random.choice([-1, 1]) # 随机微调水平速度
# 球与固定挡板碰撞检测
for fixed_paddle in fixed_paddles:
if fixed_paddle.collidepoint(ball_x, ball_y):
ball_speed_y = -ball_speed_y
ball_speed_x += random.choice([-1, 1])
# 球与砖块碰撞检测
for brick in bricks[:]:
if brick.collidepoint(ball_x, ball_y):
ball_speed_y = -ball_speed_y
bricks.remove(brick)
score += 10
break
# 游戏失败检测
if ball_y + ball_radius >= SCREEN_HEIGHT:
lives -= 1
if lives == 0:
running = False
else:
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT // 2
ball_speed_x, ball_speed_y = randomize_ball_direction()
# 游戏胜利检测
if not bricks:
current_level += 1
if current_level < len(levels):
bricks = create_bricks(levels[current_level])
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT // 2
ball_speed_x, ball_speed_y = randomize_ball_direction()
else:
running = False
# 清屏
screen.fill(BLACK)
# 绘制游戏元素(挡板、球、砖块等)
if game_state != GameState.SHOW_INSTRUCTIONS:
# 绘制挡板(显示蓄力状态)
power_color = (min(255, int(255 * paddle_power / MAX_POWER)), 0, 0) # 蓄力越强,颜色越红
pygame.draw.rect(screen, power_color, (paddle_x, paddle_y, paddle_width, paddle_height))
# 绘制固定挡板
for fixed_paddle in fixed_paddles:
pygame.draw.rect(screen, GREEN, fixed_paddle)
# 绘制球
pygame.draw.circle(screen, RED, (int(ball_x), int(ball_y)), ball_radius)
# 绘制砖块
for brick in bricks:
pygame.draw.rect(screen, YELLOW, brick)
# 显示分数、生命和蓄力状态
score_text = font.render(f"Score: {score}", True, WHITE)
lives_text = font.render(f"Lives: {lives}", True, WHITE)
power_text = font.render(f"Power: {paddle_power:.1f}x", True, WHITE)
screen.blit(score_text, (10, 10))
screen.blit(lives_text, (SCREEN_WIDTH - 150, 10))
screen.blit(power_text, (SCREEN_WIDTH // 2 - 50, 10))
# 显示操作说明(游戏开始时)
if game_state == GameState.SHOW_INSTRUCTIONS:
instructions = [
"操作说明:",
"左右移动:← / → 方向键",
"蓄力:按住 ↓ 方向键(挡板会变红)",
"暂停:空格键",
"",
"按空格键开始游戏"
]
# 绘制半透明背景
s = pygame.Surface((400, 180), pygame.SRCALPHA)
s.fill((0, 0, 0, 180)) # 半透明黑色背景
screen.blit(s, (SCREEN_WIDTH // 2 - 200, SCREEN_HEIGHT // 2 - 90))
# 绘制文字
for i, line in enumerate(instructions):
text = small_font.render(line, True, WHITE)
screen.blit(text, (SCREEN_WIDTH // 2 - 180, SCREEN_HEIGHT // 2 - 80 + i * 30))
# 显示暂停信息
elif game_state == GameState.PAUSED:
pause_text = font.render("游戏暂停 - 按空格键继续", True, WHITE)
screen.blit(pause_text, (SCREEN_WIDTH // 2 - 150, SCREEN_HEIGHT // 2 - 18))
# 更新屏幕
pygame.display.flip()
# 控制帧率
clock.tick(60)
# 游戏结束
pygame.quit()
if lives == 0:
print("游戏失败!")
else:
print("恭喜你,通关了!")
评论