Python写简单的拼图小游戏(附源码、资源)

2023-11-12

郑重声明,嘿嘿

代码与图片已上传资源,需要者自取
资源地址:https://download.csdn.net/download/qq_44651842/20009562
Python小白一只,正在成长,程序自己设计,很多不足,算法很多地方能优化。欢迎大佬来指教。

游戏效果

在这里插入图片描述

创建设置类,储存游戏基础数据

可以不使用这个类,在程序中直接使用相应的数据。但是使用这个类更便于程序阅读和修改基础数据。

class Settings:
    def __init__(self):
        self.picture_num = 4  # 每行图片数
        self.screen_width = 408  # 窗口宽度
        self.screen_length = 809 # 窗口长度
        self.picture_length = 100 # 每个正方形图片的长
        self.screen_bgcol = (96, 127, 255) # 背景颜色
        self.picture_bian = 1 # 每个图片的边缘宽度 ,便于分清每个照片
        self.picture_distance = 102 # 两个图片之间的距离

创建图片类,储存游戏需要的图片

这样可以在游戏的开始把游戏用到的图片一起读到内存,显示照片时直接使用创建的图像对象列表即可。
类的构造函数要接收一个数字,按着这个数字读生成相应图片的路径和名称 picture_name。在按照这个打开相应的照片。
pygame相应方法可以简单学习一下。

class Picture:
    def __init__(self, num):
        self.picture_name = 'images/p{}.gif'.format(num)
        self.picture = pygame.image.load(self.picture_name) # 打开照片
        self.picture_rect = self.picture.get_rect() # 获得照片属性类
    def display_picture(self, screen, x, y): # 在屏幕上显示图片方法
        self.picture_rect.x = x
        self.picture_rect.y = y
        screen.blit(self.picture, self.picture_rect)

生成初始数据,创建窗口

游戏数据用两个4*4二维列表存储,一个存储图片位置,一个存储图片对象。
游戏开始,图片的顺序的应该是乱的。
先要对数据进行打乱,打乱时要按照原有的顺序打乱,不然可能会出现图片不可能复原的情况。

数据打乱函数

def data_begin(caozuoshu, p0, data):
    for i in caozuoshu:
        move(i, p0, data)

def move(i, p0, data):
    if i == 3 and p0[1] > 0:
        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
        data[p0[0]][p0[1]-1] = t
        p0[1] -= 1
    elif i == 4 and p0[1] < 3:
        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
        data[p0[0]][p0[1]+1] = t
        p0[1] += 1
    elif i == 1 and p0[0] > 0:
        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
        data[p0[0]-1][p0[1]] = t
        p0[0] -= 1
    elif i == 2 and p0[0] < 3:
        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]+1][p0[1]]
        data[p0[0]+1][p0[1]] = t
        p0[0] += 1

def create_caozuoshu():
    n = 30
    caozuo = [1, 2, 3, 4]
    caozuoshu = []
    for i in range(n):
        caozuoshu.append(random.choice(caozuo))
    return caozuoshu

这样之后,把data列表打乱,

在按照data生成picture列表

def create_pictures(picture, data, set):
    for i in range(set.picture_num):
        for j in range(set.picture_num):
            p = Picture(data[i][j])
            picture[i][j] = p

创建窗口函数

def screen_create(set):
    pygame.init()
    screen = pygame.display.set_mode((set.screen_length, set.screen_width))
    pygame.display.set_caption("拼图")
    return screen

主函数

if __name__ == '__main__':
    set = Settings()
    # 初始数据
    data = [[9, 1, 3, 4],
            [2, 16, 14, 8],
            [6, 10, 5, 12],
            [13, 7, 11, 15]]
    p0 = [1, 1]
    caozuoshu = create_caozuoshu()
    data_begin(caozuoshu, p0, data)
    bushu = [0]
    # 创建图片
    picture = [[None, None, None, None],
               [None, None, None, None],
               [None, None, None, None],
               [None, None, None, None]]
    yuantu = Picture(17)
    create_pictures(picture, data, set)  # 按照data生成相应顺序的picture列表
    # 创建窗口
    screen = screen_create(set)

    # 游戏主循环
    while True:
        check_events(picture, p0, data, bushu)
        screen_updata(picture, screen, set, yuantu)

响应按键控制

响应按键是,picture和data列表都要同步改变,data用来判断是否拼图完成。

响应按键,产生相应的控制

def check_events(picture, p0, data, bushu):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN and game_over(data, set, bushu):
            if event.key == pygame.K_DOWN and p0[0] > 0:
                xinhao = 1
                bushu[0] += 1
                updata(xinhao, picture, p0, data)
            elif event.key == pygame.K_UP and p0[0] < 3:
                xinhao = 2
                bushu[0] += 1
                updata(xinhao, picture, p0, data)
            elif event.key == pygame.K_RIGHT and p0[1] > 0:
                xinhao = 3
                bushu[0] += 1
                updata(xinhao, picture, p0, data)
            elif event.key == pygame.K_LEFT and p0[1] < 3:
                xinhao = 4
                bushu[0] += 1
                updata(xinhao, picture, p0, data)

按照控制数,更新picture和data

def updata(xinhao, picture, p0, data):
    if xinhao == 3:
        tmp = picture[p0[0]][p0[1]]
        picture[p0[0]][p0[1]] = picture[p0[0]][p0[1]-1]
        picture[p0[0]][p0[1]-1] = tmp

        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
        data[p0[0]][p0[1]-1] = t
        p0[1] -= 1

    elif xinhao == 4:
        tmp = picture[p0[0]][p0[1]]
        picture[p0[0]][p0[1]] = picture[p0[0]][p0[1] + 1]
        picture[p0[0]][p0[1] + 1] = tmp

        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
        data[p0[0]][p0[1]+1] = t
        p0[1] += 1
    elif xinhao == 1:
        tmp = picture[p0[0]][p0[1]]
        picture[p0[0]][p0[1]] = picture[p0[0] - 1][p0[1]]
        picture[p0[0] - 1][p0[1]] = tmp

        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
        data[p0[0]-1][p0[1]] = t
        p0[0] -= 1
    elif xinhao == 2:
        tmp = picture[p0[0]][p0[1]]
        picture[p0[0]][p0[1]] = picture[p0[0] + 1][p0[1]]
        picture[p0[0] + 1][p0[1]] = tmp

        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0] + 1][p0[1]]
        data[p0[0] +1][p0[1]] = t
        p0[0] += 1

判断是否拼图完成

def game_over(data, set,bushu):
    datao = [[1, 2, 3, 4],
             [5, 6, 7, 8],
             [9, 10, 11, 12],
             [13, 14, 15, 16]]
    for i in range(set.picture_num):
        for j in range(set.picture_num):
            if datao[i][j] != data[i][j]:
                return True
    print("牛逼!\n 游戏结束!\n 步数:{}".format(bushu[0]))
    return False

此函数要在响应按键函数中实时使用,监测是否完成拼图。

完整程序

import pygame
import random
import sys

class Settings:
    def __init__(self):
        self.picture_num = 4
        self.screen_width = 408
        self.screen_length = 809
        self.picture_length = 100
        self.screen_bgcol = (96, 127, 255)
        self.picture_speed = 5
        self.picture_bian = 1
        self.picture_distance = 102

class Picture:
    def __init__(self, num):
        self.picture_name = 'images/p{}.gif'.format(num)
        self.picture = pygame.image.load(self.picture_name)
        self.picture_rect = self.picture.get_rect()
    def display_picture(self, screen, x, y):
        self.picture_rect.x = x
        self.picture_rect.y = y
        screen.blit(self.picture, self.picture_rect)
'''def data_begin(data,p0):
    n = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
    ns = 16
    for i in range(4):
        for j in range(4):
            num = random.randint(0, ns-1)
            ns -= 1
            data[i][j] = n.pop(num)
            if data[i][j] == 16:
                p0[0] = i
                p0[1] = j'''
def data_begin(caozuoshu, p0, data):
    for i in caozuoshu:
        move(i, p0, data)

def move(i, p0, data):
    if i == 3 and p0[1] > 0:
        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
        data[p0[0]][p0[1]-1] = t
        p0[1] -= 1
    elif i == 4 and p0[1] < 3:
        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
        data[p0[0]][p0[1]+1] = t
        p0[1] += 1
    elif i == 1 and p0[0] > 0:
        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
        data[p0[0]-1][p0[1]] = t
        p0[0] -= 1
    elif i == 2 and p0[0] < 3:
        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]+1][p0[1]]
        data[p0[0]+1][p0[1]] = t
        p0[0] += 1

def create_caozuoshu():
    n = 30
    caozuo = [1, 2, 3, 4]
    caozuoshu = []
    for i in range(n):
        caozuoshu.append(random.choice(caozuo))
    return caozuoshu

def create_pictures(picture, data, set):
    for i in range(set.picture_num):
        for j in range(set.picture_num):
            p = Picture(data[i][j])
            picture[i][j] = p

def screen_updata(picture, screen, set, yuantu):
    screen.fill(set.screen_bgcol)
    x, y = 402, set.picture_bian
    for i in range(set.picture_num):
        for j in range(set.picture_num):
            picture[i][j].display_picture(screen, x, y)
            x += set.picture_distance
        x = 402
        y += set.picture_distance
    yuantu.display_picture(screen, 1, 4)
    pygame.display.flip()

def screen_create(set):
    pygame.init()
    screen = pygame.display.set_mode((set.screen_length, set.screen_width))
    pygame.display.set_caption("拼图")
    return screen

def game_over(data, set,bushu):
    datao = [[1, 2, 3, 4],
             [5, 6, 7, 8],
             [9, 10, 11, 12],
             [13, 14, 15, 16]]
    for i in range(set.picture_num):
        for j in range(set.picture_num):
            if datao[i][j] != data[i][j]:
                return True
    print("牛逼!\n 游戏结束!\n 步数:{}".format(bushu[0]))
    return False

def updata(xinhao, picture, p0, data):
    if xinhao == 3:
        tmp = picture[p0[0]][p0[1]]
        picture[p0[0]][p0[1]] = picture[p0[0]][p0[1]-1]
        picture[p0[0]][p0[1]-1] = tmp

        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
        data[p0[0]][p0[1]-1] = t
        p0[1] -= 1

    elif xinhao == 4:
        tmp = picture[p0[0]][p0[1]]
        picture[p0[0]][p0[1]] = picture[p0[0]][p0[1] + 1]
        picture[p0[0]][p0[1] + 1] = tmp

        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
        data[p0[0]][p0[1]+1] = t
        p0[1] += 1
    elif xinhao == 1:
        tmp = picture[p0[0]][p0[1]]
        picture[p0[0]][p0[1]] = picture[p0[0] - 1][p0[1]]
        picture[p0[0] - 1][p0[1]] = tmp

        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
        data[p0[0]-1][p0[1]] = t
        p0[0] -= 1
    elif xinhao == 2:
        tmp = picture[p0[0]][p0[1]]
        picture[p0[0]][p0[1]] = picture[p0[0] + 1][p0[1]]
        picture[p0[0] + 1][p0[1]] = tmp

        t = data[p0[0]][p0[1]]
        data[p0[0]][p0[1]] = data[p0[0] + 1][p0[1]]
        data[p0[0] +1][p0[1]] = t
        p0[0] += 1
    #print(data)

def check_events(picture, p0, data, bushu):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN and game_over(data, set, bushu):
            if event.key == pygame.K_DOWN and p0[0] > 0:
                xinhao = 1
                bushu[0] += 1
                updata(xinhao, picture, p0, data)
            elif event.key == pygame.K_UP and p0[0] < 3:
                xinhao = 2
                bushu[0] += 1
                updata(xinhao, picture, p0, data)
            elif event.key == pygame.K_RIGHT and p0[1] > 0:
                xinhao = 3
                bushu[0] += 1
                updata(xinhao, picture, p0, data)
            elif event.key == pygame.K_LEFT and p0[1] < 3:
                xinhao = 4
                bushu[0] += 1
                updata(xinhao, picture, p0, data)

if __name__ == '__main__':
    set = Settings()
    # 初始数据
    data = [[9, 1, 3, 4],
            [2, 16, 14, 8],
            [6, 10, 5, 12],
            [13, 7, 11, 15]]
    p0 = [1, 1]
    caozuoshu = create_caozuoshu()
    data_begin(caozuoshu, p0, data)
    bushu = [0]
    # 创建图片
    picture = [[None, None, None, None],
               [None, None, None, None],
               [None, None, None, None],
               [None, None, None, None]]
    yuantu = Picture(17)
    create_pictures(picture, data, set)
    # 创建窗口
    screen = screen_create(set)

    # 游戏主循环
    while True:
        check_events(picture, p0, data, bushu)
        screen_updata(picture, screen, set, yuantu)

游戏用到的图片,图片位置和文件名要和程序中的一致

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python写简单的拼图小游戏(附源码、资源) 的相关文章

随机推荐

  • ctf.show

    web21 自定义迭代器 先抓个包 发现authorization 授权 后面的basic认证很奇怪 用base64解码看看就是输入的用户名和密码 返回包里出现乱码 但可以看到admin 猜测用户名就叫admin 知道了爆破登录的形式是xx
  • spring与mybatis集成

    Spring 集成 MyBatis 将 MyBatis与 Spring 进行整合 主要解决的问题就是将 SqlSessionFactory 对象交由 Spring来管理 所以 该整合 只需要将 SqlSessionFactory 的对象生成
  • Nodejs+Express中页面控制器及脚本自动加载设计

    Express自身带强大的路由功能 这让我们可以详细拆分项目的需求 设计出优美的restful风格对外API 为了方便实现上述功能 我加入了页面控制器及脚本自动加载设计 比如针对会员模块 我们在app js中指定了模块的路由文件 app u
  • 【Java】你还在使用单线程处理大量数据么?

    Java 结合实际业务场景 使用多线程异步处理大量数据 业务场景 优化方案 多线程的实现 线程池 为什么要使用线程池 线程池的创建 1 Spring配置类 2 手动创建 提交任务 1 execute 2 submit 案例伪代码 后续优化
  • 【Python】OpenCV常用操作函数大全!

    目录 cv2常用类 1 图片加载 显示和保存 2 图像显示窗口创建与销毁 3 图片的常用属性的获取 4 生成指定大小的矩形区域 ROI 5 图片颜色通道的分离与合并 6 两张图片相加 改变对比度和亮度 7 像素运算 1 加减乘除 8 像素运
  • 【Windows】Windows下wget的安装与环境变量配置

    1 wget安装 GNU Wget常用于使用命令行下载网络资源 包括但不限于文件 网页等 GNU Wget官网 GNU Wget GNU Wget for Windows GNU Wget for Windows 安装时首先下载主安装包 C
  • 老王的24天,

    数组元素的反转 数组元素的反转 本来的样子 1 2 3 4 之后的样子 4 3 2 1 要求不能使用新数组 就用原来的一个数组 public class Demo07ArrayReverse public static void main
  • nRF52832 — 多通道ADC接口的使用

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XX 作 者 文化人 XX 联系方式 XX 版权声明 原创文章 欢迎评论和转载 转载时能告诉我一声就最好了 XX 要说的
  • Golang基础(项目结构)

    一 标准的项目结构 在实际开发中不可能只有一个main包 更不可能就只有一个 go文件 不同级别大小的项目中包和文件数量都不同 Go语言中组织单元最大的为项目 项目下包含包 一个包可以有多个文件 包在物理层面上就是文件夹 同一个文件夹中多个
  • iOS App的上架和版本更新流程

    一 前言 作为一名iOSDeveloper 把开发出来的App上传到App Store是必要的 下面就来详细讲解一下具体流程步骤 二 准备 一个已付费的开发者账号 账号类型分为个人 Individual 公司 Company 企业 Ente
  • curl使用总结

    curl使用官网 https curl haxx se docs manpage html 1 查看curl的安装版本以及支持的协议 curl V 2 CURL分析HTTPS请求耗时时间 HTTPS耗时 TCP握手 SSL握手 因为涉及到一
  • 短视频矩阵系统源代码开发搭建分享--代码开源SaaS

    一 什么是短视频矩阵系统 短视频矩阵系统是专门为企业号商家 普通号商家提供帐号运营从流量 到转化成交的一站式服务方案 具体包含 点赞关注评论主动私信 评论区回复 自动潜客户挖掘 矩阵号营销 自动化营销 粉丝 管理等功能 可以帮助企业或商家快
  • stl排序之sort函数

    STL容器的排序 支持随机访问的容器vector deque string没有sort成员 可调用std sort排序 list排序调用自带的list sort 下面是std sort函数 有两个版本 template
  • LED点阵书写显示屏

    LED点阵书写显示屏 题目的大概要求是做一个32 32的点阵书写屏 LED 点阵模块显示屏工作在人眼不易觉察的扫描微亮和人眼可见的 显示点亮模式下 当光笔触及 LED 点阵模块表面时 先由光笔检测触及位置处 LED 点 的扫描微亮以获取其行
  • springboot no tests were found

    springboot单元测试报错 no tests were found 如图所示 原因分析 1 进行单元测试的方法不能有返回值 2 方法不能私有化 以上两个问题都会报 no tests were found 错误 正确写法
  • 华为od机试 Java 【url拼接】

    题目 给定一个URL的前缀和后缀 我们需要将其合并成一个完整的URL 在合并时 请注意以下几点 如果前缀的结尾没有斜线 而后缀的开头也没有斜线 那么在两者之间需要添加一个斜线 如果前缀的结尾和后缀的开头都有斜线 那么需要保留其中的一个 删除
  • Vue-生命周期函数

    Vue 生命周期函数 一 生命周期和生命周期函数 生命周期 Life Cycle 是指一个组件从创建 gt 运行 gt 销毁的整个阶段 强调的是一个时间段 生命周期函数 是由vue 框架提供的内置函数 会伴随着组件的生命周期 自动按次序执行
  • 【js】从数组中随机选一个数,从数组中随机选几个数

    每组中随机选一个 每组中随机选一个 randomFun arr let ri Math floor Math random arr length return arr ri 使用 let arr 1 2 3 4 5 6 7 console
  • Android QQ 登录接入详细介绍

    今日科技快讯 近日 百度地图发布2022春节出行大数据 迁徙大数据显示 2022年春运迁徙规模较去年农历同期有明显上升 春节期间全国人口迁徙规模日均值为去年农历同期的近两倍 春节前的迁徙规模峰值出现在1月29日 腊月廿七 春节后于2月6日达
  • Python写简单的拼图小游戏(附源码、资源)

    郑重声明 嘿嘿 代码与图片已上传资源 需要者自取 资源地址 https download csdn net download qq 44651842 20009562 Python小白一只 正在成长 程序自己设计 很多不足 算法很多地方能优