当前位置: 欣欣网 > 码农

Python消消乐游戏

2024-02-05码农

大家好!今天给你们带来了用pygame写的消消乐游戏。

  • import pygameimport random# 初始化pygamepygame.init()# 设置窗口大小和标题screen_width, screen_height = 320, 480screen = 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 = 40blocks_wide = screen_width // block_sizeblocks_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:continueif 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:continueif 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 = 0for below_y in range(y + 1, blocks_high): # 查找当前方块下方的空位数量if grid[below_y][x] == EMPTY: fall_distance += 1else:breakif 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 = Nonewhile running:for event in pygame.event.get():if event.type == pygame.QUIT: running = Falseelif 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()