游戏的主框架由 pygame
库支持,pygame
是一个流行的 Python 游戏开发库,可以轻松创建图形界面、处理用户输入和进行游戏逻辑处理。代码一开始进行了一些必要的初始化:
import pygame
import random
# 初始化 pygame
pygame.init()
# 游戏窗口的尺寸
WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("打砖块游戏")
这里,我们初始化了 pygame
,设置了游戏窗口的大小为 600x400,并为窗口设置了标题。
在游戏中,有几个常量是固定的,像是挡板、球的大小、砖块的尺寸等。定义常量是为了方便后续的调整和保持一致性。
# 游戏常量
PADDLE_WIDTH = 100
PADDLE_HEIGHT = 15
BALL_RADIUS = 10
BRICK_WIDTH = 60
BRICK_HEIGHT = 20
FPS = 60
# 游戏色彩
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
LIGHT_BLUE = (173, 216, 230) # 淡蓝色
DARK_BLUE = (0, 0, 139) # 深蓝色
PADDLE_WIDTH
和 PADDLE_HEIGHT
设定了挡板的大小。BALL_RADIUS
设定了球的半径。BRICK_WIDTH
和 BRICK_HEIGHT
定义了砖块的大小。FPS
用于控制游戏的帧率。同时,我们也定义了一些颜色常量,用于给游戏中的不同元素上色。
# 加载字体,确保支持中文
font = pygame.font.Font("C:/Windows/Fonts/simkai.ttf", 36)
为了在游戏中显示文本,我们加载了一个字体文件。在这里,使用的是系统字体文件 simkai.ttf
(楷体),字号设置为 36。为了显示中文,我们需要保证选择的字体支持中文字符。
接下来,我们创建了三个核心的游戏对象类:挡板 Paddle
、球 Ball
和砖块 Brick
。每个类都有自己的属性和方法。
Paddle
class Paddle:
def __init__(self):
self.x = WIDTH // 2 - PADDLE_WIDTH // 2
self.y = HEIGHT - PADDLE_HEIGHT - 10
self.speed = 10
def move(self, dx):
if self.x + dx >= 0 and self.x + dx <= WIDTH - PADDLE_WIDTH:
self.x += dx
def draw(self, surface):
pygame.draw.rect(surface, BLUE, (self.x, self.y, PADDLE_WIDTH, PADDLE_HEIGHT))
Paddle
类控制游戏中的挡板。它的 move
方法用于根据玩家的输入(左、右方向键)移动挡板,draw
方法则负责在屏幕上绘制挡板。
Ball
class Ball:
def __init__(self):
self.x = WIDTH // 2
self.y = HEIGHT // 2
self.radius = BALL_RADIUS
self.dx = 3 * random.choice([-1, 1])
self.dy = -3
def move(self):
self.x += self.dx
self.y += self.dy
def bounce(self, axis):
if axis == "horizontal":
self.dx = -self.dx
elif axis == "vertical":
self.dy = -self.dy
def draw(self, surface):
pygame.draw.circle(surface, RED, (self.x, self.y), self.radius)
Ball
类控制小球的运动。球的初始位置是窗口中心,move
方法更新球的位置,bounce
方法用于改变球的方向以实现反弹。draw
方法则在屏幕上绘制球。
Brick
class Brick:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, BRICK_WIDTH, BRICK_HEIGHT)
def draw(self, surface):
pygame.draw.rect(surface, GREEN, self.rect)
Brick
类表示砖块。每个砖块是一个矩形区域,draw
方法用于在屏幕上绘制砖块。
def check_collisions(ball, paddle, bricks):
global score
# 检查球与挡板碰撞
if (
ball.y + ball.radius >= paddle.y
and paddle.x <= ball.x <= paddle.x + PADDLE_WIDTH
):
# 判断球与滑板的接触点,避免卡住
if ball.dy > 0 and ball.y + ball.radius <= paddle.y + 5:
ball.bounce("vertical") # 只反弹竖直方向
ball.y = paddle.y - ball.radius # 确保小球不会穿透滑板
elif (
ball.x - ball.radius <= paddle.x + 10
or ball.x + ball.radius >= paddle.x + PADDLE_WIDTH - 10
):
ball.bounce("horizontal") # 反弹水平方向
check_collisions
函数处理了球与挡板、砖块以及屏幕边界的碰撞。通过检查球的位置与挡板、砖块的接触,更新游戏状态并增加分数。
为了让玩家能够开始、重新开始或退出游戏,我们定义了 Button
类,并创建了开始和退出按钮。
class Button:
def __init__(self, text, x, y, width, height, color, font_color):
self.text = text
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.font_color = font_color
def draw(self, surface, is_hovered=False):
# 按钮的颜色变化,当鼠标悬停时
button_color = LIGHT_BLUE if is_hovered else self.color
pygame.draw.rect(
surface,
button_color,
(self.x, self.y, self.width, self.height),
border_radius=10,
)
pygame.draw.rect(
surface,
DARK_BLUE,
(self.x, self.y, self.width, self.height),
width=3,
border_radius=10,
)
text_surface = font.render(self.text, True, self.font_color)
surface.blit(
text_surface,
(
self.x + (self.width - text_surface.get_width()) // 2,
self.y + (self.height - text_surface.get_height()) // 2,
),
)
按钮的 draw
方法负责绘制按钮,且当鼠标悬停时,按钮会改变颜色。is_clicked
方法检查玩家是否点击了按钮。
游戏主循环中,根据游戏状态(开始、暂停、游戏结束)进行不同的操作。当游戏结束时,玩家可以选择重新开始游戏或者退出。
# 游戏主循环
def game_loop():
global score, lives, game_over, game_started
paddle = Paddle()
ball = Ball()
bricks = [
Brick(x * (BRICK_WIDTH + 10), y * (BRICK_HEIGHT + 5))
for x in range(8)
for y in range(5)
]
start_button = Button(
"开始游戏", WIDTH // 2 - 100, HEIGHT // 2 - 50, 200, 50, BLUE, WHITE
)
quit_button = Button(
"退出", WIDTH // 2 - 100, HEIGHT // 2 + 10, 200, 50, RED, WHITE
)
restart_button = Button(
"重新开始", WIDTH // 2 - 100, HEIGHT // 2 - 50, 200, 50, BLUE, WHITE
)
exit_button = Button(
"退出", WIDTH // 2 - 100, HEIGHT // 2 + 10, 200, 50, RED, WHITE
)
while True:
screen.fill(WHITE)
draw_gradient(screen, (135, 206, 235), (0, 191, 255)) # 蓝色渐变背景
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if game_over:
if restart_button.is_clicked(event.pos):
# 重新开始游戏
game_started = True
game_over = False
lives = 3
score = 0
ball = Ball()
bricks = [
Brick(x * (BRICK_WIDTH + 10), y * (BRICK_HEIGHT + 5))
for x in range(8)
for y in range(5)
]
elif exit_button.is_clicked(event.pos):
pygame.quit()
quit()
else:
if start_button.is_clicked(event.pos):
game_started = True # 游戏开始
game_over = False
score = 0
lives = 3
ball = Ball()
bricks = [
Brick(x * (BRICK_WIDTH + 10), y * (BRICK_HEIGHT + 5))
for x in range(8)
for y in range(5)
]
elif quit_button.is_clicked(event.pos):
pygame.quit()
quit()
这段代码展示了 Python 游戏开发的基本框架,如何使用面向对象编程设计游戏中的对象和逻辑,同时展示了如何使用 pygame
实现图形渲染和事件处理。
本游戏是一个经典的打砖块游戏,通过 pygame
实现了游戏的图形界面、用户交互以及游戏逻辑。玩家控制挡板反弹球,击打屏幕上方的砖块,目标是尽可能多地击碎砖块并防止球掉出屏幕。游戏包括了分数、生命、重新开始和退出的功能,以及美丽的渐变背景和按钮界面。
完整代码请关注关注微信公众号“编程纵深”,后台私信:打砖块,即可获取!