[Python] 纯文本查看 复制代码
import pygame
import random
# 初始化 Pygame
pygame.init()
# 屏幕尺寸
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
# 球拍属性
PADDLE_WIDTH = 100
PADDLE_HEIGHT = 15
PADDLE_SPEED = 10
# 球的属性
BALL_SIZE = 15
BALL_SPEED_X = 5
BALL_SPEED_Y = 5
# 砖块属性
BRICK_WIDTH = 75
BRICK_HEIGHT = 20
BRICK_ROWS = 5
BRICK_COLUMNS = 10
BRICK_PADDING = 5
# 初始化屏幕
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Pygame 打砖块")
# 球拍类
class Paddle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([PADDLE_WIDTH, PADDLE_HEIGHT])
self.image.fill(BLUE)
self.rect = self.image.get_rect()
self.rect.x = SCREEN_WIDTH // 2 - PADDLE_WIDTH // 2
self.rect.y = SCREEN_HEIGHT - PADDLE_HEIGHT - 10
self.speed = 0
def update(self):
self.rect.x += self.speed
# 边界检测
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
def move_left(self):
self.speed = -PADDLE_SPEED
def move_right(self):
self.speed = PADDLE_SPEED
def stop(self):
self.speed = 0
# 球类
class Ball(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([BALL_SIZE, BALL_SIZE])
pygame.draw.circle(self.image, WHITE, (BALL_SIZE // 2, BALL_SIZE // 2), BALL_SIZE // 2)
self.image.set_colorkey(BLACK) # 使背景透明
self.rect = self.image.get_rect()
self.rect.x = SCREEN_WIDTH // 2 - BALL_SIZE // 2
self.rect.y = SCREEN_HEIGHT // 2 - BALL_SIZE // 2
self.speed_x = BALL_SPEED_X * random.choice([-1, 1]) # 随机初始方向
self.speed_y = BALL_SPEED_Y
def update(self):
self.rect.x += self.speed_x
self.rect.y += self.speed_y
# 边界检测
if self.rect.left < 0 or self.rect.right > SCREEN_WIDTH:
self.speed_x = -self.speed_x
if self.rect.top < 0:
self.speed_y = -self.speed_y
if self.rect.bottom > SCREEN_HEIGHT:
return True # 返回 True 表示游戏结束
return False # 返回 False 表示游戏继续
# 砖块类
class Brick(pygame.sprite.Sprite):
def __init__(self, x, y, color):
super().__init__()
self.image = pygame.Surface([BRICK_WIDTH, BRICK_HEIGHT])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# 创建精灵组
all_sprites = pygame.sprite.Group()
bricks = pygame.sprite.Group()
# 创建球拍
paddle = Paddle()
all_sprites.add(paddle)
# 创建球
ball = Ball()
all_sprites.add(ball)
# 创建砖块
for row in range(BRICK_ROWS):
for col in range(BRICK_COLUMNS):
x = col * (BRICK_WIDTH + BRICK_PADDING)
y = row * (BRICK_HEIGHT + BRICK_PADDING) + 50 # 稍微向下移动砖块
color = RED # 可以根据行数设置不同颜色
brick = Brick(x, y, color)
bricks.add(brick)
all_sprites.add(brick)
# 游戏循环
running = True
clock = pygame.time.Clock()
game_over = False
score = 0 # 添加分数
font = pygame.font.Font(None, 36) # 创建一个字体对象
def show_text(surface, text, x, y, color=WHITE):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
surface.blit(text_surface, text_rect)
while running:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
paddle.move_left()
elif event.key == pygame.K_RIGHT:
paddle.move_right()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
paddle.stop()
if not game_over:
# 更新游戏状态
paddle.update()
ball_lost = ball.update()
if ball_lost:
game_over = True
# 碰撞检测
if pygame.sprite.collide_rect(ball, paddle):
ball.speed_y = -ball.speed_y
brick_collisions = pygame.sprite.spritecollide(ball, bricks, True)
if brick_collisions:
ball.speed_y = -ball.speed_y
score += len(brick_collisions) # 增加分数
if len(bricks) == 0: # 游戏胜利条件
show_text(screen, "You Win!", SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
pygame.display.flip()
pygame.time.delay(3000) # 暂停 3 秒
running = False # 退出游戏循环
# 绘制屏幕
screen.fill(BLACK)
all_sprites.draw(screen)
# 显示分数
show_text(screen, f"Score: {score}", 70, 20) # 在屏幕左上角显示分数
pygame.display.flip()
else:
# 游戏结束画面
screen.fill(BLACK)
show_text(screen, "Game Over!", SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
show_text(screen, f"Final Score: {score}", SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50)
pygame.display.flip()
pygame.time.delay(3000) # 暂停 3 秒
running = False # 退出游戏循环
# 控制帧率
clock.tick(60)
# 退出 Pygame
pygame.quit()