大家好!今天给你们带来了用pygame写的消消乐游戏。
import pygame
import random
# 初始化pygame
pygame.init()
# 设置窗口大小和标题
screen_width, screen_height = 320, 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('消消乐')
# 颜色定义
BACKGROUND_COLOR = (30, 30, 30)
BLOCK_COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)]
EMPTY = (0, 0, 0, 0) # 透明,用于表示空位
# 方块参数
block_size = 40
blocks_wide = screen_width // block_size
blocks_high = screen_height // block_size
# 积分
score = 0
# 生成初始方块
defcreate_grid():
return [[random.choice(BLOCK_COLORS) for _ in range(blocks_wide)] for _ in range(blocks_high)]
# 绘制游戏界面
defdraw_grid(grid):
for y, row in enumerate(grid):
for x, color in enumerate(row):
if color != EMPTY:
pygame.draw.rect(screen, color, (x * block_size, y * block_size, block_size, block_size))
# 检查并消除相同颜色的方块
defcheck_and_eliminate(grid):
global score
to_eliminate = []
# 水平检查
for y in range(blocks_high):
for x in range(blocks_wide - 2):
if grid[y][x] == EMPTY:
continue
if grid[y][x] == grid[y][x+1] == grid[y][x+2]:
to_eliminate.extend([(x, y), (x+1, y), (x+2, y)])
# 垂直检查
for x in range(blocks_wide):
for y in range(blocks_high - 2):
if grid[y][x] == EMPTY:
continue
if grid[y][x] == grid[y+1][x] == grid[y+2][x]:
to_eliminate.extend([(x, y), (x, y+1), (x, y+2)])
# 消除方块
for x, y in to_eliminate:
grid[y][x] = EMPTY
score += 1# 每消除一个方块增加1分
return len(to_eliminate) > 0
# 方块下落填补空位
defdrop_blocks(grid):
for x in range(blocks_wide):
for y in range(blocks_high - 2, -1, -1): # 从下往上检查
if grid[y][x] != EMPTY:
fall_distance = 0
for below_y in range(y + 1, blocks_high): # 查找当前方块下方的空位数量
if grid[below_y][x] == EMPTY:
fall_distance += 1
else:
break
if fall_distance > 0:
grid[y + fall_distance][x] = grid[y][x]
grid[y][x] = EMPTY
# 在顶部填充新的方块
deffill_new_blocks(grid):
for x in range(blocks_wide):
for y in range(blocks_high):
if grid[y][x] == EMPTY:
grid[y][x] = random.choice(BLOCK_COLORS)
# 交换方块
defswap_blocks(grid, pos1, pos2):
grid[pos1[1]][pos1[0]], grid[pos2[1]][pos2[0]] = grid[pos2[1]][pos2[0]], grid[pos1[1]][pos1[0]]
# 主游戏循环
defgame_loop():
clock = pygame.time.Clock()
grid = create_grid()
running = True
selected_block = None
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
selected_block = (mouse_x // block_size, mouse_y // block_size)
elif event.type == pygame.MOUSEBUTTONUP and selected_block:
mouse_x, mouse_y = pygame.mouse.get_pos()
released_block = (mouse_x // block_size, mouse_y // block_size)
if selected_block != released_block:
swap_blocks(grid, selected_block, released_block)
ifnot check_and_eliminate(grid):
# 如果没有方块被消除,则交换回来
swap_blocks(grid, selected_block, released_block)
else:
drop_blocks(grid)
while check_and_eliminate(grid):
drop_blocks(grid)
fill_new_blocks(grid)
selected_block = None
screen.fill(BACKGROUND_COLOR)
draw_grid(grid)
# 显示积分
font = pygame.font.SysFont("SimHei", 36)#中文显示
text = font.render(f'分数: {score}', True, (255, 255, 255))
screen.blit(text, (10, 10))
pygame.display.flip()
clock.tick(60)
game_loop()
pygame.quit()