拼图小游戏&Python程序封装方法

Python小白,参考大佬代码改编的程序,请多多指教,需要者自取。

3x3拼图

4x4拼图

源码展示:

import pygame, sys, random
from pygame.locals import *

window_width= 500       #窗口宽度
window_height = 500     #窗口高度
background_color = (255, 255, 255)     #背景颜色为白色
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
FPS = 40   

# 定义变量
a = 3
b = a * a     
c = 100  

# 定义一个退出程序的函数
def terminate():
    pygame.quit()  #停止运行pygame库
    sys.exit()   #退出程序

# 随机生成游戏盘面
def GameBoard():
    board = []
    for i in range(b):
        board.append(i)  #将上一行代码生成的数添加到board列表中
    blackCell = b - 1
    board[blackCell] = -1

    for i in range(c):        #将图片分成b张
        direction = random.randint(0, 3)
        if (direction == 0):
            blackCell = moveLeft(board, blackCell)   #向左移动
        elif (direction == 1):
            blackCell = moveRight(board, blackCell)  #向右移动
        elif (direction == 2):
            blackCell = moveUp(board, blackCell)    #向上移动
        elif (direction == 3):
            blackCell = moveDown(board, blackCell)  #向下移动
    return board, blackCell


# 若空白图像块不在最左边,则将空白块左边的块移动到空白块位置
def moveRight(board, blackCell):
    if blackCell % a == 0:
        return blackCell
    board[blackCell - 1], board[blackCell] = board[blackCell], board[blackCell - 1]
    return blackCell - 1


# 若空白图像块不在最右边,则将空白块右边的块移动到空白块位置
def moveLeft(board, blackCell):
    if blackCell % a == a - 1:
        return blackCell
    board[blackCell + 1], board[blackCell] = board[blackCell], board[blackCell + 1]
    return blackCell + 1

# 若空白图像块不在最上边,则将空白块上边的块移动到空白块位置
def moveDown(board, blackCell):
    if blackCell < a:
        return blackCell
    board[blackCell - a], board[blackCell] = board[blackCell], board[blackCell - a]
    return blackCell - a

# 若空白图像块不在最下边,则将空白块下边的块移动到空白块位置
def moveUp(board, blackCell):
    if blackCell >= b - a:
        return blackCell
    board[blackCell + a], board[blackCell] = board[blackCell], board[blackCell + a]
    return blackCell + a

# 是否完成
def isFinished(board, blackCell):
    for i in range(b - 1):
        if board[i] != i:
            return False
    return True

# 初始化
pygame.init()  #运行pygame库
mainClock = pygame.time.Clock()

# 导入图片
gameImage = pygame.image.load('D:\桌面\小蘑菇.jpg')  #选择图片位置
gameRect = gameImage.get_rect()

# 设置窗口,窗口的宽度和高度取决于图片的宽高
windowSurface = pygame.display.set_mode((gameRect.width+100, gameRect.height))
pygame.display.set_caption('小蘑菇')  #设置窗口图标
image = pygame.image.load('D:\桌面\小蘑菇.jpg')
pygame.display.set_icon(image)   #设置图标

pygame.display.update()  #更新画面

cellWidth = int(gameRect.width / a)   #设置每一个小图片的宽度
cellHeight = int(gameRect.height / a) #设置每一个小图片的高度

finish = False

gameBoard, blackCell = GameBoard()  #创建元组变量赋值

# 游戏主循环
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            terminate()
        if finish:
            continue
        if event.type == KEYDOWN:    #用键盘(a、s、d、w)控制拼图移动
            if event.key == K_LEFT or event.key == ord('a'):   #白色图片的右边图片向左移
                blackCell = moveLeft(gameBoard, blackCell)
            if event.key == K_RIGHT or event.key == ord('d'):  #白色图片的左边图片向右移
                blackCell = moveRight(gameBoard, blackCell)
            if event.key == K_UP or event.key == ord('w'):     #白色图片的下边图片向上移
                blackCell = moveUp(gameBoard, blackCell)
            if event.key == K_DOWN or event.key == ord('s'):   #白色图片的上边图片向下移
                blackCell = moveDown(gameBoard, blackCell)
        if event.type == MOUSEBUTTONDOWN and event.button == 1:   #用鼠标控制图片移动
            x, y = pygame.mouse.get_pos()
            col = int(x / cellWidth)
            row = int(y / cellHeight)
            index = col + row * a
            if (
                    index == blackCell - 1 or index == blackCell + 1 or index == blackCell - a or index == blackCell + a):
                gameBoard[blackCell], gameBoard[index] = gameBoard[index], gameBoard[blackCell]
                blackCell = index

    if (isFinished(gameBoard, blackCell)):
        gameBoard[blackCell] = b - 1
        finish = True

    windowSurface.fill(background_color)

    for i in range(b):
        rowDst = int(i / a)
        colDst = int(i % a)
        rectDst = pygame.Rect(colDst * cellWidth, rowDst * cellHeight, cellWidth, cellHeight)

        if gameBoard[i] == -1:
            continue

        rowArea = int(gameBoard[i] / a)
        colArea = int(gameBoard[i] % a)
        rectArea = pygame.Rect(colArea * cellWidth, rowArea * cellHeight, cellWidth, cellHeight)
        windowSurface.blit(gameImage, rectDst, rectArea)

    for i in range(a + 1):
        pygame.draw.line(windowSurface, BLACK, (i * cellWidth, 0), (i * cellWidth, gameRect.height))
    for i in range(a + 1):
        pygame.draw.line(windowSurface, BLACK, (0, i * cellHeight), (gameRect.width, i * cellHeight))

    pygame.display.update()

    mainClock.tick(FPS)   #每秒刷新40帧

注:程序的第83行和第89行的图片位置一定要选择正确;改变a的值可以设置拼图分成多少块;所选图片分辨率过大会使拼图布满整个电脑屏幕,剪切一下就好了;需要先下载pygame, sys, random库,下载方法和下面程序封装方法的1步骤一样,只需将pyinstall改变就好了,其它库函数的下载方法相同。

程序封装方法:

1.安装打包工具:同时按住win+R打开运行对话框,输入cmd点击确定,输入pip install pyinstall按下回车键,进行安装。

2.打开命令提示符输入pyinstaller -F -w python源代码位置名称(例如D:\桌面\拼图.py) --distpath=D:\桌面。一定要注意空格!

3.打开后缀为.exe的文件

4.发送给好友,只能在电脑上运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器人天笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值