import pygame import random # 初始化pygame pygame.init() # 设置屏幕大小 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置颜色 black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) # 设置字体 font = pygame.font.Font(None, 36) # 挡板 paddle_width = 100 paddle_height = 20 paddle = pygame.Rect(screen_width // 2 - paddle_width // 2, screen_height - 50, paddle_width, paddle_height) paddle_speed = 7 paddle_color = red # 小球 ball_size = 20 ball = pygame.Rect(screen_width // 2 - ball_size // 2, screen_height - 100, ball_size, ball_size) ball_color = blue ball_speed_x = random.choice([-5, 5]) ball_speed_y = -5 # 砖块 brick_width = 70 brick_height = 30 bricks = [pygame.Rect(x + screen_width // 10, y + screen_height // 5, brick_width, brick_height) for y in range(0, screen_height // 5, brick_height + 20) for x in range(0, screen_width - screen_width // 10, brick_width + 20)] brick_color = green # 得分 score = 0 # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 移动挡板 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and paddle.left > 0: paddle.x -= paddle_speed if keys[pygame.K_RIGHT] and paddle.right < screen_width: paddle.x += paddle_speed # 移动小球 ball.x += ball_speed_x ball.y += ball_speed_y # 碰壁反弹 if ball.left <= 0 or ball.right >= screen_width: ball_speed_x = -ball_speed_x if ball.top <= 0: ball_speed_y = -ball_speed_y # 碰挡板反弹 if paddle.colliderect(ball): ball_speed_y = -ball_speed_y ball.bottom = paddle.top # 碰砖块反弹 for brick in bricks[:]: if ball.colliderect(brick): bricks.remove(brick) ball_speed_y = -ball_speed_y score += 1 break # 小球掉落到屏幕底部 if ball.bottom >= screen_height: font_game_over = pygame.font.Font(None, 72) text_game_over = font_game_over.render("Game Over", True, white) screen.blit(text_game_over, (screen_width // 2 - text_game_over.get_width() // 2, screen_height // 2 - text_game_over.get_height() // 2)) pygame.display.flip() pygame.time.wait(2000) running = False # 判断游戏胜利 if len(bricks) == 0: font_game_win = pygame.font.Font(None, 72) text_game_win = font_game_win.render("You Win", True, white) screen.blit(text_game_win, (screen_width // 2 - text_game_win.get_width() // 2, screen_height // 2 - text_game_win.get_height() // 2)) pygame.display.flip() pygame.time.wait(2000) running = False # 绘制游戏界面 screen.fill(black) for brick in bricks: pygame.draw.rect(screen, brick_color, brick) pygame.draw.rect(screen, paddle_color, paddle) pygame.draw.ellipse(screen, ball_color, ball) text_score = font.render("Score: " + str(score), True, white) screen.blit(text_score, (10, 10)) pygame.display.flip() # 控制游戏帧率 pygame.time.Clock().tick(60) pygame.quit()
时间: 2025-06-25 14:03:06 浏览: 44
### 使用 Pygame 开发的砖块消除游戏完整代码
以下是基于 `pygame` 库开发的一个简单版本的砖块消除游戏完整代码示例。此代码实现了基本的游戏逻辑,包括球的反弹、挡板的移动以及砖块的消除等功能。
```python
import pygame
import sys
# 初始化 Pygame
pygame.init()
# 设置窗口大小和标题
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("打砖块游戏")
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# 定义游戏对象参数
paddle_width, paddle_height = 100, 10
ball_radius = 7
brick_rows, brick_columns = 5, 10
brick_width, brick_height = 75, 20
# 创建挡板类
class Paddle:
def __init__(self):
self.x = (screen_width - paddle_width) // 2
self.y = screen_height - 30
self.width = paddle_width
self.height = paddle_height
self.color = BLUE
self.speed = 8
def draw(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
def move(self, keys):
if keys[pygame.K_LEFT] and self.x > 0:
self.x -= self.speed
if keys[pygame.K_RIGHT] and self.x + self.width < screen_width:
self.x += self.speed
# 创建球类
class Ball:
def __init__(self, x, y):
self.x = x
self.y = y
self.radius = ball_radius
self.dx = 4
self.dy = -4
self.color = RED
def draw(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)
def move(self):
self.x += self.dx
self.y += self.dy
# 碰撞检测:上下边界
if self.x - self.radius <= 0 or self.x + self.radius >= screen_width:
self.dx *= -1
if self.y - self.radius <= 0:
self.dy *= -1
# 创建砖块类
class Brick:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = brick_width
self.height = brick_height
self.color = WHITE
self.visible = True
def draw(self):
if self.visible:
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
def check_collision(ball, paddle, bricks):
# 检测与挡板碰撞
if (paddle.x <= ball.x <= paddle.x + paddle.width and
paddle.y <= ball.y + ball.radius <= paddle.y + paddle.height):
ball.dy *= -1
# 检测与砖块碰撞
for row in bricks:
for brick in row:
if brick.visible and (
brick.x <= ball.x <= brick.x + brick.width and
brick.y <= ball.y - ball.radius <= brick.y + brick.height):
brick.visible = False
ball.dy *= -1
# 主函数
def main():
clock = pygame.time.Clock()
paddle = Paddle()
ball = Ball(paddle.x + paddle.width // 2, paddle.y - ball_radius * 2)
bricks = []
for i in range(brick_rows):
row = []
for j in range(brick_columns):
brick_x = j * (brick_width + 5) + 35
brick_y = i * (brick_height + 5) + 50
row.append(Brick(brick_x, brick_y))
bricks.append(row)
running = True
while running:
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
paddle.move(keys)
ball.move()
check_collision(ball, paddle, bricks)
# 绘制所有物体
paddle.draw()
ball.draw()
for row in bricks:
for brick in row:
brick.draw()
# 如果球掉到屏幕下方,则结束游戏
if ball.y + ball.radius >= screen_height:
font = pygame.font.Font(None, 74)
text = font.render("Game Over", 1, WHITE)
screen.blit(text, ((screen_width - text.get_width())//2, (screen_height - text.get_height())//2))
pygame.display.flip()
pygame.time.wait(2000)
break
# 刷新画面
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
main()
pygame.quit()
sys.exit()
```
#### 游戏说明
- **挡板控制**:通过键盘左右箭头键来控制挡板的水平移动[^1]。
- **球运动**:球会在屏幕上弹跳,并根据碰到的对象改变方向[^2]。
- **砖块消除**:当球撞击到砖块时,该砖块会被标记为不可见并从游戏中移除[^3]。
- **游戏结束条件**:如果球掉落至屏幕底部,则显示“Game Over”,随后退出程序。
---
###
阅读全文
相关推荐


















