# 【python游戏制作】拼图永不过时，这就是我这个年龄该玩的游戏~

## 代码展示

🎯 这里仅展示部分代码，完整的免费源码领取处：点击

``````import os
import sys
import random
import pygame
from pygame.locals import *
``````

``````BACKGROUNDCOLOR = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
FPS = 40
NUMRANDOM = 100
``````

``````def Stop():
pygame.quit()
sys.exit()
``````

``````# 源码解答加Q裙：926207505
def isOver(board, size):
try:
num_cells = size * size
except:
num_cells = size[0] * size[1]
for i in range(num_cells-1):
if board[i] != i:
return False
return True
``````

``````def moveR(board, blank_cell_idx, num_cols):
if blank_cell_idx % num_cols == 0:
return blank_cell_idx
board[blank_cell_idx-1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-1]
return blank_cell_idx-1
``````

``````def moveL(board, blank_cell_idx, num_cols):
if (blank_cell_idx+1) % num_cols == 0:
return blank_cell_idx
board[blank_cell_idx+1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+1]
return blank_cell_idx+1
``````

``````def moveD(board, blank_cell_idx, num_cols):
if blank_cell_idx < num_cols:
return blank_cell_idx
board[blank_cell_idx-num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-num_cols]
return blank_cell_idx-num_cols
``````

``````def moveU(board, blank_cell_idx, num_rows, num_cols):
if blank_cell_idx >= (num_rows-1) * num_cols:
return blank_cell_idx
board[blank_cell_idx+num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+num_cols]
return blank_cell_idx+num_cols
``````

``````# 源码解答加Q裙：926207505
def CreateBoard(num_rows, num_cols, num_cells):
board = []
for i in range(num_cells):
board.append(i)
# 去掉右下角那块
blank_cell_idx = num_cells - 1
board[blank_cell_idx] = -1
for i in range(NUMRANDOM):
# 0: left, 1: right, 2: up, 3: down
direction = random.randint(0, 3)
if direction == 0:
blank_cell_idx = moveL(board, blank_cell_idx, num_cols)
elif direction == 1:
blank_cell_idx = moveR(board, blank_cell_idx, num_cols)
elif direction == 2:
blank_cell_idx = moveU(board, blank_cell_idx, num_rows, num_cols)
elif direction == 3:
blank_cell_idx = moveD(board, blank_cell_idx, num_cols)
return board, blank_cell_idx
``````

``````def GetImagePath(filepath):
imgs = os.listdir(filepath)
if len(imgs) == 0:
raise ValueError('No pictures in <%s>...' % filepath)
return os.path.join(filepath, random.choice(imgs))
``````

``````def ShowEndInterface(screen, width, height):
screen.fill(BACKGROUNDCOLOR)
font = pygame.font.Font('font/simkai.ttf', width // 8)
title = font.render('Finished!', True, (233, 150, 122))
rect = title.get_rect()
rect.midtop = (width/2, height/2.5)
screen.blit(title, rect)
pygame.display.update()
pygame.time.wait(500)
# 源码解答加Q裙：926207505
while True:
for event in pygame.event.get():
if event.type == QUIT:
Stop()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
Stop()
``````

🎯 博主所有文章素材、解答、源码领取处：点击

## 效果展示

🎯 博主所有文章素材、解答、源码领取处：点击

## 推荐往期文章

【python 游戏】闲的无聊？那就和博主一起来滑雪吧~

【python游戏制作】僵尸来袭 ~ 快来一起创造植物叭~

【python制作小游戏】大鼻子马里奥等你来挑战，还原度超高哦~

## 尾语

``````🏵

🏵 小时候的要求：吃好睡好学习好；

🏵 长大了的期盼：吃饱睡饱卡里饱；

🏵 小时候遇见困难，总希望超人出现；

🏵 长大了遇见困难，却只靠自己摆平。

🏵 人生坎坷，需坚强面对！

————————心灵鸡汤
``````

👇问题解答 · 源码获取 · 技术交流 · 抱团学习请联系👇

THE END