使用Pyglet制作“彩色方块连连看”游戏(续)
在前面的部分,我们已经构建了游戏的基本框架,并实现了方块的随机生成和显示。接下来,我们将继续开发这款连连看游戏,实现方块的点击和消除功能。以下是具体的步骤和代码示例。
环境准备
确保你已经安装了Pyglet库,可以通过以下命令进行安装:
pip install pyglet
然后,创建一个新的Python文件,比如lianliankan.py
,并导入所需的模块。
import pyglet
import random
继续上一步的游戏设计
我们将定义一些常量,比如窗口的宽度和高度,以及方块的大小。然后,创建一个用于存储方块的容器。
# 游戏设置
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
BLOCK_SIZE = 40
GRID_WIDTH = WINDOW_WIDTH // BLOCK_SIZE
GRID_HEIGHT = WINDOW_HEIGHT // BLOCK_SIZE
# 颜色
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),
(255, 165, 0), (128, 0, 128), (0, 255, 255), (255, 20, 147)]
# 创建窗口
window = pyglet.window.Window(WINDOW_WIDTH, WINDOW_HEIGHT)
# 创建网格数据
grid = [[None for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
初始化游戏网格
我们需要初始化游戏的网格,并为每个方块随机赋予一种颜色。
def init_grid():
for row in range(GRID_HEIGHT):
for col in range(GRID_WIDTH):
# 随机选择颜色
color = random.choice(COLORS)
grid[row][col] = color
# 初始化
init_grid()
渲染方块
我们需要在窗口上绘制这些方块。重写on_draw
事件来实现这一功能。
@window.event
def on_draw():
window.clear()
for row in range(GRID_HEIGHT):
for col in range(GRID_WIDTH):
color = grid[row][col]
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS,
('v2f', [col * BLOCK_SIZE, row * BLOCK_SIZE,
col * BLOCK_SIZE, (row + 1) * BLOCK_SIZE,
(col + 1) * BLOCK_SIZE, (row + 1) * BLOCK_SIZE,
(col + 1) * BLOCK_SIZE, row * BLOCK_SIZE]),
('c3B', color * 4))
处理点击事件
为了实现点击方块的功能,我们需要添加一个鼠标事件侦听器来检测玩家的点击。
selected_block = None
@window.event
def on_mouse_press(x, y, button, modifiers):
global selected_block
col = x // BLOCK_SIZE
row = y // BLOCK_SIZE
if 0 <= col < GRID_WIDTH and 0 <= row < GRID_HEIGHT:
if selected_block is None:
selected_block = (row, col) # 选中方块
else:
if grid[row][col] == grid[selected_block[0]][selected_block[1]]:
# 消除方块
grid[selected_block[0]][selected_block[1]] = None
grid[row][col] = None
selected_block = None # 重置选中
主循环
最后,我们需要启动主循环,保持窗口的打开状态。
pyglet.app.run()
完整代码
将以上所有代码整合在一起,形成完整的lianliankan.py
文件。运行程序,你会看到一个400x400的窗口,里面有彩色方块。点击相同颜色的方块,能够将其消除。
import pyglet
import random
# 游戏设置
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
BLOCK_SIZE = 40
GRID_WIDTH = WINDOW_WIDTH // BLOCK_SIZE
GRID_HEIGHT = WINDOW_HEIGHT // BLOCK_SIZE
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),
(255, 165, 0), (128, 0, 128), (0, 255, 255), (255, 20, 147)]
window = pyglet.window.Window(WINDOW_WIDTH, WINDOW_HEIGHT)
grid = [[None for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
selected_block = None
def init_grid():
for row in range(GRID_HEIGHT):
for col in range(GRID_WIDTH):
color = random.choice(COLORS)
grid[row][col] = color
init_grid()
@window.event
def on_draw():
window.clear()
for row in range(GRID_HEIGHT):
for col in range(GRID_WIDTH):
color = grid[row][col]
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS,
('v2f', [col * BLOCK_SIZE, row * BLOCK_SIZE,
col * BLOCK_SIZE, (row + 1) * BLOCK_SIZE,
(col + 1) * BLOCK_SIZE, (row + 1) * BLOCK_SIZE,
(col + 1) * BLOCK_SIZE, row * BLOCK_SIZE]),
('c3B', color * 4))
@window.event
def on_mouse_press(x, y, button, modifiers):
global selected_block
col = x // BLOCK_SIZE
row = y // BLOCK_SIZE
if 0 <= col < GRID_WIDTH and 0 <= row < GRID_HEIGHT:
if selected_block is None:
selected_block = (row, col)
else:
if grid[row][col] == grid[selected_block[0]][selected_block[1]]:
grid[selected_block[0]][selected_block[1]] = None
grid[row][col] = None
selected_block = None
pyglet.app.run()
通过这段代码,你已经实现了一个基础的“彩色方块连连看”游戏。可以继续扩展游戏,比如增加计时器、分数系统、重新开始游戏等功能,让游戏更加丰富和有趣。希望你能享受这个开发的过程!