消消乐游戏是一种经典的益智游戏,玩家通过交换相邻的方块来形成三个或更多相同颜色的连续方块,从而消除它们。本文将介绍如何使用Python的Pygame库来创建一个简单的消消乐游戏。
准备工作
在开始之前,请确保已安装Pygame库。可以通过以下命令安装:
pip install pygame
游戏设计
我们的游戏将包括以下几个部分:
- 初始化Pygame:设置游戏窗口和基本参数。
- 创建网格:生成一个随机颜色的方块网格。
- 实现交换和消除功能:允许玩家点击交换方块,并检测和消除匹配的方块。
- 更新游戏状态:处理方块下落和新方块生成。
- 显示分数:记录玩家的得分。
代码实现
以下是完整的代码实现:
import pygame
import random
import time# 初始化Pygame
pygame.init()# 设置屏幕大小
screen_width, screen_height = 600, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("消消乐")# 定义颜色
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 165, 0)]
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)# 定义网格大小
GRID_SIZE = 8
BLOCK_SIZE = screen_width // GRID_SIZE# 创建网格
grid = [[random.choice(COLORS) for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]# 选中的方块
selected = Nonedef draw_grid():for row in range(GRID_SIZE):for col in range(GRID_SIZE):# 绘制方块pygame.draw.rect(screen, grid[row][col], (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))# 绘制边框pygame.draw.rect(screen, BLACK, (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)# 如果有方块被选中,绘制高亮边框if selected:row, col = selectedpygame.draw.rect(screen, WHITE, (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 3)def get_clicked_pos(pos):x, y = posreturn y // BLOCK_SIZE, x // BLOCK_SIZEdef is_adjacent(pos1, pos2):row1, col1 = pos1row2, col2 = pos2return abs(row1 - row2) + abs(col1 - col2) == 1def check_matches():matches = set()# 检查水平匹配for row in range(GRID_SIZE):for col in range(GRID_SIZE-2):if (grid[row][col] == grid[row][col+1] == grid[row][col+2] and grid[row][col] is not None):matches.update([(row, col), (row, col+1), (row, col+2)])# 检查垂直匹配for row in range(GRID_SIZE-2):for col in range(GRID_SIZE):if (grid[row][col] == grid[row+1][col] == grid[row+2][col] and grid[row][col] is not None):matches.update([(row, col), (row+1, col), (row+2, col)])return matchesdef remove_matches(matches):# 移除匹配的方块for row, col in matches:grid[row][col] = None# 方块下落for col in range(GRID_SIZE):empty_cells = []# 从下往上检查空格for row in range(GRID_SIZE-1, -1, -1):if grid[row][col] is None:empty_cells.append(row)elif empty_cells:# 将方块下移到最低的空位grid[empty_cells[0]][col] = grid[row][col]grid[row][col] = Noneempty_cells.pop(0)empty_cells.append(row)# 填充顶部空格for row in empty_cells:grid[row][col] = random.choice(COLORS)def main():global selectedrunning = Truescore = 0font = pygame.font.Font(None, 36)while running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.MOUSEBUTTONDOWN:pos = get_clicked_pos(pygame.mouse.get_pos())if pos[0] < GRID_SIZE and pos[1] < GRID_SIZE: # 确保点击在网格内if selected is None:selected = poselse:if is_adjacent(selected, pos):# 交换方块row1, col1 = selectedrow2, col2 = posgrid[row1][col1], grid[row2][col2] = grid[row2][col2], grid[row1][col1]# 检查是否有匹配matches = check_matches()if matches:score += len(matches) * 10remove_matches(matches)else:# 如果没有匹配,换回来grid[row1][col1], grid[row2][col2] = grid[row2][col2], grid[row1][col1]selected = None# 持续检查和消除匹配matches = check_matches()if matches:score += len(matches) * 10remove_matches(matches)pygame.time.wait(200) # 添加短暂延迟使动画更流畅# 绘制游戏界面screen.fill(BLACK)draw_grid()# 显示分数score_text = font.render(f"分数: {score}", True, WHITE)screen.blit(score_text, (10, 10))pygame.display.flip()pygame.quit()if __name__ == "__main__":main()
游戏玩法
- 选择方块:点击第一个方块选中它(会显示白色边框)。
- 交换方块:点击相邻的第二个方块进行交换。
- 消除匹配:如果形成三个或更多相同颜色的连续方块,它们会被消除。
- 方块下落:上方的方块会下落填充空缺,顶部会生成新的随机方块。
- 得分:每消除一个方块得10分。
结论
通过这篇博文,我们学习了如何使用Pygame创建一个简单的消消乐游戏。你可以在此基础上继续扩展功能,比如添加音效、特殊方块、关卡系统等。希望这能激发你对游戏开发的兴趣!