基于Python/Tkinter的拼图单机小游戏

这是很早之前写的拼图游戏,基于Py/Tk,今天翻出来,然后重新整理,并且发布出来,供大家参考学习,自己看CSDN里有很多了类似的游戏代码,虽然代码逻辑上大同小异,但每个开发者都有自己独特的开发个性和习惯,这并无优劣,而且都可以从代码中都可以一窥究竟。下午整理代码的时候才发现之前写Code的时候,居然一句注释也没有加,搞得自己又重新梳理了一遍Code逻辑,才将注释全部添加上,不添加注释实在不是个好的习惯。虽然拼图小游戏虽然只有几百行的代码,但是对于了解Tkinter的模块以及熟悉Py编程却是既有好处,废话少说,直接上代码吧

基于Python/Tkinter的拼图单机小游戏_第1张图片 主界面

基于Python/Tkinter的拼图单机小游戏_第2张图片 4*3模式

基于Python/Tkinter的拼图单机小游戏_第3张图片 3*3模式

基于Python/Tkinter的拼图单机小游戏_第4张图片 游戏图片选择弹窗

基于Python/Tkinter的拼图单机小游戏_第5张图片 配置文件包

1.类初始化

import random
import os
import Pmw
import time
from Tkinter import *
from PIL import Image,ImageTk

class image_crop():
    #拼图游戏
    def __init__(self):
        self.now_path=os.getcwd()
        self.num_page=0

2.设置--图片设置界面--游戏图片的选择

    def image_set(self):
       #设置界面关于拼图游戏中--游戏图片的选择
        path1=[]
        path_flod=self.now_path+"\image_\\"
        for i in(os.listdir(path_flod)):
            path1.append(path_flod+i)
        main_frame1=self.root.grid_slaves(row=1,column=0)
        main_frame1[0].grid_remove()
        self.imageset_toplevel=Frame(self.root)
        self.imageset_toplevel.grid(row=1,column=0,columnspan=3,rowspan=3,sticky=NSEW)
        frame_titleimage=Frame(self.imageset_toplevel)
        frame_titleimage.pack(side=TOP)
        label_tag=Label(frame_titleimage,text=u"图片名称:",width=10,font=('Verdana bold',10))
        label_tag.grid(row=0,column=0,sticky='ws')
        self.image_name=StringVar()
        label_name=Label(frame_titleimage,textvariable=self.image_name,width=20,font=('Verdana bold',10))
        label_name.grid(row=0,column=1)
        self.image_name.set(' ')
        but=Button(frame_titleimage,text=u"确定",width=10,font=('Verdana bold',9),command=sure_comd)
        but.grid(row=0,column=2)
        self.widgets_group=Pmw.ScrolledFrame(self.imageset_toplevel,usehullsize=1,frame_relief="flat",hull_width=500,
                                             hull_height=400,
                                             scrollmargin=0)
        self.widgets_group.pack(side=TOP,fill=BOTH,expand=1)
        frame_page=Frame(self.imageset_toplevel)
        frame_page.pack(side=TOP)
        up_page=Button(frame_page,text=u"上一页",width=10,font=('Verdana bold',9),command=test_up)
        up_page.pack(side=LEFT)
        self.ent=StringVar()
        entry_page=Entry(frame_page,width=10,font=('Verdana bold',10),textvariable=self.ent,state='readonly')
        entry_page.pack(side=LEFT)
        self.ent.set(u"第1页")
        down_page=Button(frame_page,text=u"下一页",width=10,font=('Verdana bold',9),command=test_down)
        down_page.pack(side=LEFT)
        
        for i in range(3):
            for j in range(3):
                #生成图片标签Label
                
        img_labelq=Label(self.widgets_group.interior(),borderwidth=2,relief='groove')
                img_labelq.grid(row=i,column=j)
        #加载图片
        image_1(path1[0])
        image_2(path1[1])
        image_3(path1[2])
        image_4(path1[3])
        image_5(path1[4])
        image_6(path1[5])
        image_7(path1[6])
        image_8(path1[7])
        image_9(path1[8])
        #注意:这里需要注意一下,图片文件至少需要大于9张,如果不是,系统报错,因为时间关系这块没有修改

3.设置---图片设置界面--鼠标选中图片后的操作

        def comd_show(event,fr_,path_img):
            #左键-选中图片后--加载图片名称
            fr_.configure(relief='solid')
            self.root.update()
            time.sleep(0.2)
            fr_.configure(relief='groove')
            image_path=os.path.basename(path_img)
            self.image_name.set(image_path.decode("cp936"))
        def doublue_comd(event,path_img):
            #右键-选中图片后--弹窗显示选中图片大图
            imageshow_toplevel=Toplevel()
            imageshow_toplevel.geometry('+400+140')
            imageshow_toplevel.overrideredirect(0)
            image_show=Image.open(path_img)
            if image_show.size[1]>image_show.size[0]:
                self.size_img_2=image_show.resize((350, 450))
            elif image_show.size[1]

4.设置---图片设置界面--所有图片包中图片显示模块

注:这部分代码虽然有些重复,有优化的地方,但逻辑上完备的,这里先将代码贴出来,如果读者有兴趣,可以将此部分优化

         def image_1(path_image):
            #图片选择器---加载第一张图片
            image_set_1=Image.open(path_image)
            if image_set_1.size[1]>image_set_1.size[0]:
                self.size_img_1=image_set_1.resize((128, 170))
            elif image_set_1.size[1]",lambda e,fr_=qws1[0],path_img=path_image: comd_show(e,fr_,path_img))
            qws1[0].bind("",lambda e,path_img=path_image: doublue_comd(e,path_img))
        def image_2(path_image):
            #图片选择器---加载第二张图片
            image_set_2=Image.open(path_image)
            if image_set_2.size[1]>image_set_2.size[0]:
                self.size_img_2=image_set_2.resize((128, 170))
            elif image_set_2.size[1]",lambda e,fr_=qws2[0],path_img=path_image: comd_show(e,fr_,path_img))
            qws2[0].bind("",lambda e,path_img=path_image: doublue_comd(e,path_img))
        def image_3(path_image):
            #图片选择器---加载第二张图片
            image_set_3=Image.open(path_image)
            if image_set_3.size[1]>image_set_3.size[0]:
                self.size_img_3=image_set_3.resize((128, 170))
            elif image_set_3.size[1]",lambda e,fr_=qws3[0],path_img=path_image: comd_show(e,fr_,path_img))
            qws3[0].bind("",lambda e,path_img=path_image: doublue_comd(e,path_img))
        def image_4(path_image):
            image_set_4=Image.open(path_image)
            if image_set_4.size[1]>image_set_4.size[0]:
                self.size_img_4=image_set_4.resize((128, 170))
            elif image_set_4.size[1]",lambda e,fr_=qws4[0],path_img=path_image: comd_show(e,fr_,path_img))
            qws4[0].bind("",lambda e,path_img=path_image: doublue_comd(e,path_img))
        def image_5(path_image):
            image_set_5=Image.open(path_image)
            if image_set_5.size[1]>image_set_5.size[0]:
                self.size_img_5=image_set_5.resize((128, 170))
            elif image_set_5.size[1]",lambda e,fr_=qws5[0],path_img=path_image: comd_show(e,fr_,path_img))
            qws5[0].bind("",lambda e,path_img=path_image: doublue_comd(e,path_img))
        def image_6(path_image):
            image_set_6=Image.open(path_image)
            if image_set_6.size[1]>image_set_6.size[0]:
                self.size_img_6=image_set_6.resize((128, 170))
            elif image_set_6.size[1]",lambda e,fr_=qws6[0],path_img=path_image: comd_show(e,fr_,path_img))
            qws6[0].bind("",lambda e,path_img=path_image: doublue_comd(e,path_img))
        def image_7(path_image):
            image_set_7=Image.open(path_image)
            if image_set_7.size[1]>image_set_7.size[0]:
                self.size_img_7=image_set_7.resize((128, 170))
            elif image_set_7.size[1]",lambda e,fr_=qws7[0],path_img=path_image: comd_show(e,fr_,path_img))
            qws7[0].bind("",lambda e,path_img=path_image: doublue_comd(e,path_img))
        def image_8(path_image):
            image_set_8=Image.open(path_image)
            if image_set_8.size[1]>image_set_8.size[0]:
                self.size_img_8=image_set_8.resize((128, 170))
            elif image_set_8.size[1]",lambda e,fr_=qws8[0],path_img=path_image: comd_show(e,fr_,path_img))
            qws8[0].bind("",lambda e,path_img=path_image: doublue_comd(e,path_img))
        def image_9(path_image):
            image_set_9=Image.open(path_image)
            if image_set_9.size[1]>image_set_9.size[0]:
                self.size_img_9=image_set_9.resize((128, 170))
            elif image_set_9.size[1]",lambda e,fr_=qws9[0],path_img=path_image: comd_show(e,fr_,path_img))
            qws9[0].bind("",lambda e,path_img=path_image: doublue_comd(e,path_img))

5.设置---图片设置界面--下一页--图片显示

        def test_down():
            #下一页--图片缩略图实现
            self.num_page+=1
            chu=len(path1)/9
            if self.num_page<=chu:
                #加载图片
                self.ent.set(u"第"+str(self.num_page+1)+u"页")
                image_1(self.now_path+"\\no_show.png")
                image_2(self.now_path+"\\no_show.png")
                image_3(self.now_path+"\\no_show.png")
                image_4(self.now_path+"\\no_show.png")
                image_5(self.now_path+"\\no_show.png")
                image_6(self.now_path+"\\no_show.png")
                image_7(self.now_path+"\\no_show.png")
                image_8(self.now_path+"\\no_show.png")
                image_9(self.now_path+"\\no_show.png")
                if self.num_page

6.设置---图片设置界面--上一页--图片显示

        def test_up():
            #上一页---图片显示
            self.num_page-=1
            chu=len(path1)/9
            if self.num_page<=chu and self.num_page>=0:
                self.ent.set(u"第"+str(self.num_page+1)+u"页")
                if self.num_page

7.设置---图片设置界面--选中图片后--点击确定

        def sure_comd():
            #选中图片--点击确定--操作函数
            self.image_name.get()
            if self.image_name.get()!=' ':
                image_sh_path=path_flod+self.image_name.get()
                self.num_page=0
                #启动游戏
                self.start(image_sh_path)

8.游戏主界面

    def st_(self):
        #主界面
        self.root=Tk()
        self.root.title(u"拼图")
        self.root.geometry('+400+150')
        self.root.maxsize(850,590)
        self.root.minsize(640,450)
        self.root.iconbitmap(self.now_path+'\\1.ico')
        self.menuBar=Pmw.MenuBar(self.root,hull_relief=RAISED,hull_borderwidth=1)
        self.menuBar.grid(row=0,column=0,columnspan=4,rowspan=1,sticky=NSEW)
        self.menuBar.addmenu(u'选项',' Option',font=('Verdana bold',10))
        self.menuBar.addmenuitem(u'选项','command',label=u"刷新",font=('Verdana bold',10),
                                 activebackground='blue',command=shuaxin)
        self.menuBar.addmenuitem(u'选项','command',label=u"关闭",font=('Verdana bold',10),
                                 activebackground='blue',command=close_)
        self.menuBar.addmenu(u'设置',' Option',font=('Verdana bold',10))
        self.menuBar.addmenu(u'帮助',' Option',font=('Verdana bold',10))
        self.menuBar.addmenuitem(u'帮助','command',label=u"关于",font=('Verdana bold',10),
                                 activebackground='blue',command=help_)
        self.menuBar.addmenuitem(u'设置','command',label=u"图片设置",font=('Verdana bold',10),
                                 activebackground='blue',command=self.image_set)
        self.menuBar.addcascademenu(u'设置', u'模式设置',
                'Set some other preferences',tearoff = 0,font=('Verdana bold',10))
        for size in (' 3*3 ',' 4*3 '):
            self.menuBar.addmenuitem(u'模式设置', 'command', 'Set size to ' + size,
                    label = size,font=('Verdana bold',8))
        self.frame1=Frame(self.root,relief='groove',borderwidth=2,width=500,heigh=50)
        self.frame1.grid(row=1,column=0,columnspan=3,rowspan=3,sticky=NSEW)
        self.frame2=Frame(self.root)
        self.frame2.grid(row=1,column=3,rowspan=3,sticky=NSEW)
        self.img_label=Label(self.frame2,borderwidth=2,relief='groove',width=26,heigh=18)
        self.img_label.grid(row=0,column=0)
        frame2_2=Frame(self.frame2)
        frame2_2.grid(row=1,column=0,pady=60)
        self.win_tishi=StringVar() 
        label_win=Label(frame2_2,textvariable=self.win_tishi,font=('Verdana bold',10))
        label_win.pack(side=TOP)
        self.win_tishi.set(" ")
        but2=Button(frame2_2,text=u"退出",font=('Verdana bold',10),width=10,command=close_)
        but2.pack(side=TOP,padx=20,pady=10)
        self.root.mainloop()

9.游戏执行主逻辑---4*3模式

注:因为代码注释比较详细,所以直接将整个函数代码块贴出来

    def four_three_mod(self):
        #4*3模式
        test_arr=[1,2,3,4,5,6,7,8,9,10,11]
        new_array=[]
        tag_chongfu=False
        for i in range(len(test_arr)):
            #获取一个打乱后的次序数组
            rand_int=random.randint(0,10-i)
            new_array.append(test_arr[rand_int])
            del test_arr[rand_int]
        print(new_array)
        self.list=[[99,99,99,99,99,99],
                   [99,0,new_array[0],new_array[1],new_array[2],99],
                   [99,new_array[3],new_array[4],new_array[5],new_array[6],99],
                   [99,new_array[7],new_array[8],new_array[9],new_array[10],99],
                   [99,99,99,99,99,99]]
        #将打乱的一维次序数组映射到图盘二维数组
        #注意这里是套用数组5*5模板
        list_biaozhun=[[99,99,99,99,99,99],
                       [99,0,1,2,3,99],
                       [99,4,5,6,7,99],
                       [99,8,9,10,11,99],
                       [99,99,99,99,99,99]]
        #正确图像的原始二维数组
        main_frame=self.root.grid_slaves(row=1,column=0)
        main_frame[0].grid_remove()
        #清空图盘
        suoluetu_fr=self.frame2.grid_slaves(row=0,column=0)
        suoluetu_fr[0].grid_remove()
        #清空参照图片区
        self.frame1=Frame(self.root,relief='groove',borderwidth=2,bg='white')
        self.frame1.grid(row=1,column=0,columnspan=3,rowspan=3,sticky=NSEW)
        #生成新的图盘表格
        self.img_label=Label(self.frame2,borderwidth=2,relief='groove')
        self.img_label.grid(row=0,column=0)
        #生成新的参照图片区
        def commd1(event,tag):
            #绑定4*3点击函数
            for i in range(len(self.list)):
                for j in range(len(self.list[i])):
                    #遍历与图盘对应的数组
                    if self.list[i][j]==tag:
                        list_canshu=[i,j]
                        #获取当前点击图盘位置的截图次序值
                        break
            for i in range(len(self.list)):
                for j in range(len(self.list[i])):
                    #遍历与图盘对应的数组
                    if self.list[i][j]==0:
                        list_kong_canshu=[i,j]
                        #获取当前图盘位置为空的截图次序值
                        break
            x_1=list_canshu[0]
            #获取当前点击图盘位置的截图次序值的X轴坐标
            y_1=list_canshu[1]
            #获取当前点击图盘位置的截图次序值的Y轴坐标
            x_0=list_kong_canshu[0]
            #获取当前图盘位置为空的截图次序值的X轴坐标
            y_0=list_kong_canshu[1]
            #获取当前图盘位置为空的截图次序值的Y轴坐标
            if x_1+1==x_0 or x_1-1==x_0 or y_1+1==y_0 or y_1-1==y_0:
                #对空值的位置与点击图片的位置进行一个判断(即相邻判断-上-下-左-右)
                shuzu=[x_0,y_0]
                qws=self.frame1.grid_slaves(row=x_1-1,column=y_1-1)
                #获取点击位置上所在布局的Label
                qws[0].grid(row=shuzu[0]-1,column=shuzu[1]-1)
                #将获取点击位置上所在布局的Label移动到空位置的布局上
                for i in range(len(self.list)):
                    for j in range(len(self.list[i])):
                        #遍历与图盘对应的数组
                        if self.list[i][j]==0:
                            #获取当前点击图盘位置的截图次序值
                            self.list[i][j]=tag
                        elif self.list[i][j]==tag:
                            #获取当前图盘位置为空的截图次序值
                            self.list[i][j]=0
                        else:
                            pass
                if self.list==list_biaozhun:
                    #如果当前与图片对应的数组与原图的数组次序一致,则判断游戏结束
                    self.win_tishi.set("游戏结束")
        out=self.image_public.resize((240, 180))
        self.imtag_label=ImageTk.PhotoImage(out)
        self.img_label.configure(image=self.imtag_label)
        
        self.public_image_s=self.image_public.resize((600, 450))
        #重新更改图盘尺寸
        size_x=self.public_image_s.size[0]
        size_y=self.public_image_s.size[1]
        x_average=size_x/4
        y_average=size_y/3
        
        self.lab_public1=Label(self.frame1,borderwidth=1)
        self.lab_public1.grid(row=0,column=1)
        self.lab_public1.bind("",lambda e,tag=new_array[0]: commd1(e,tag))
        #双击操作绑定事件
        self.lab_public2=Label(self.frame1,borderwidth=0.5)
        self.lab_public2.grid(row=0,column=2)
        self.lab_public2.bind("",lambda e,tag=new_array[1]: commd1(e,tag))
        #同上
        self.lab_public3=Label(self.frame1,borderwidth=0.5)
        self.lab_public3.grid(row=0,column=3)
        self.lab_public3.bind("",lambda e,tag=new_array[2]: commd1(e,tag))
        
        self.lab_public4=Label(self.frame1,borderwidth=0.5)
        self.lab_public4.grid(row=1,column=0)
        self.lab_public4.bind("",lambda e,tag=new_array[3]: commd1(e,tag))

        self.lab_public5=Label(self.frame1,borderwidth=0.5)
        self.lab_public5.grid(row=1,column=1)
        self.lab_public5.bind("",lambda e,tag=new_array[4]: commd1(e,tag))

        self.lab_public6=Label(self.frame1,borderwidth=0.5)
        self.lab_public6.grid(row=1,column=2)
        self.lab_public6.bind("",lambda e,tag=new_array[5]: commd1(e,tag))

        self.lab_public7=Label(self.frame1,borderwidth=0.5)
        self.lab_public7.grid(row=1,column=3)
        self.lab_public7.bind("",lambda e,tag=new_array[6]: commd1(e,tag))

        self.lab_public8=Label(self.frame1,borderwidth=0.5)
        self.lab_public8.grid(row=2,column=0)
        self.lab_public8.bind("",lambda e,tag=new_array[7]: commd1(e,tag))

        self.lab_public9=Label(self.frame1,borderwidth=0.5)
        self.lab_public9.grid(row=2,column=1)
        self.lab_public9.bind("",lambda e,tag=new_array[8]: commd1(e,tag))
        self.lab_public10=Label(self.frame1,borderwidth=0.5)
        self.lab_public10.grid(row=2,column=2)
        self.lab_public10.bind("",lambda e,tag=new_array[9]: commd1(e,tag))
        self.lab_public11=Label(self.frame1,borderwidth=0.5)
        self.lab_public11.grid(row=2,column=3)
        self.lab_public11.bind("",lambda e,tag=new_array[10]: commd1(e,tag))
        
        region1=(x_average,0,x_average+x_average,y_average)
        cropImg1=self.public_image_s.crop(region1)
        #对原来的图片进行按照区域进行截图
        self.im_public1=ImageTk.PhotoImage(cropImg1)
        qws=self.frame1.grid_slaves(row=new_array[0]/4,column=new_array[0]%4)
        #将第一个截图显示在frame1
        qws[0].configure(image=self.im_public1)

        region2=(x_average+x_average,0,x_average+x_average+x_average,y_average)
        cropImg2=self.public_image_s.crop(region2)
        self.im_public2=ImageTk.PhotoImage(cropImg2)
        qws=self.frame1.grid_slaves(row=new_array[1]/4,column=new_array[1]%4)
        qws[0].configure(image=self.im_public2)
        #将第二个截图显示在frame2
        
        region3=(x_average*3,0,x_average*4,y_average)
        cropImg3=self.public_image_s.crop(region3)
        self.im_public3=ImageTk.PhotoImage(cropImg3)
        qws=self.frame1.grid_slaves(row=new_array[2]/4,column=new_array[2]%4)
        qws[0].configure(image=self.im_public3)
        #同上

        region4=(0,y_average,x_average,y_average*2)
        cropImg4=self.public_image_s.crop(region4)
        self.im_public4=ImageTk.PhotoImage(cropImg4)
        qws=self.frame1.grid_slaves(row=new_array[3]/4,column=new_array[3]%4)
        qws[0].configure(image=self.im_public4)
        #同上

        region5=(x_average,y_average,x_average*2,y_average*2)
        cropImg5=self.public_image_s.crop(region5)
        self.im_public5=ImageTk.PhotoImage(cropImg5)
        qws=self.frame1.grid_slaves(row=new_array[4]/4,column=new_array[4]%4)
        qws[0].configure(image=self.im_public5)
        #同上

        region6=(x_average*2,y_average,x_average*3,y_average*2)
        cropImg6=self.public_image_s.crop(region6)
        self.im_public6=ImageTk.PhotoImage(cropImg6)
        qws=self.frame1.grid_slaves(row=new_array[5]/4,column=new_array[5]%4)
        qws[0].configure(image=self.im_public6)
        #同上

        region7=(x_average*3,y_average,x_average*4,y_average*2)
        cropImg7=self.public_image_s.crop(region7)
        self.im_public7=ImageTk.PhotoImage(cropImg7)
        qws=self.frame1.grid_slaves(row=new_array[6]/4,column=new_array[6]%4)
        qws[0].configure(image=self.im_public7)
        #同上

        region8=(0,y_average*2,x_average,y_average*3)
        cropImg8=self.public_image_s.crop(region8)
        self.im_public8=ImageTk.PhotoImage(cropImg8)
        qws=self.frame1.grid_slaves(row=new_array[7]/4,column=new_array[7]%4)
        qws[0].configure(image=self.im_public8)
        #同上

        region9=(x_average,y_average*2,x_average*2,y_average*3)
        cropImg9=self.public_image_s.crop(region9)
        self.im_public9=ImageTk.PhotoImage(cropImg9)
        qws=self.frame1.grid_slaves(row=new_array[8]/4,column=new_array[8]%4)
        qws[0].configure(image=self.im_public9)
        #同上

        region10=(x_average*2,y_average*2,x_average*3,y_average*3)
        cropImg10=self.public_image_s.crop(region10)
        self.im_public10=ImageTk.PhotoImage(cropImg10)
        qws=self.frame1.grid_slaves(row=new_array[9]/4,column=new_array[9]%4)
        qws[0].configure(image=self.im_public10)
        #同上

        region11=(x_average*3,y_average*2,x_average*4,y_average*3)
        cropImg11=self.public_image_s.crop(region11)
        self.im_public11=ImageTk.PhotoImage(cropImg11)
        qws=self.frame1.grid_slaves(row=new_array[10]/4,column=new_array[10]%4)
        qws[0].configure(image=self.im_public11)
        #同上

10.游戏执行主逻辑---3*3模式

注:因为代码注释比较详细,所以直接将整个函数代码块贴出来

    def three_three_mod(self):
        #3*3模型
        test_arr=[1,2,3,4,5,6,7,8]
        new_array=[]
        tag_chongfu=False
        num=len(test_arr)
        for i in range(len(test_arr)):
            #获取一个打乱后的次序数组
            rand_int=random.randint(0,7-i)
            new_array.append(test_arr[rand_int])
            del test_arr[rand_int]
        self.list=[[9,9,9,9,9],
                   [9,0,new_array[0],new_array[1],9],
                   [9,new_array[2],new_array[3],new_array[4],9],
                   [9,new_array[5],new_array[6],new_array[7],9],
                   [9,9,9,9,9]]
        #将打乱的一维次序数组映射到图盘二维数组
        #注意这里是套用数组5*5模板
        list_biaozhun=[[9,9,9,9,9],
                       [9,0,1,2,9],
                       [9,3,4,5,9],
                       [9,6,7,8,9],
                       [9,9,9,9,9]]
        #正确图像的原始二维数组
        main_frame=self.root.grid_slaves(row=1,column=0)
        main_frame[0].grid_remove()
        suoluetu_fr=self.frame2.grid_slaves(row=0,column=0)
        suoluetu_fr[0].grid_remove()
        self.frame1=Frame(self.root,relief='groove',borderwidth=2,bg='white')
        self.frame1.grid(row=1,column=0,columnspan=3,rowspan=3,sticky=NSEW)
        #生成图盘布局Frame
        self.img_label=Label(self.frame2,borderwidth=2,relief='groove')
        self.img_label.grid(row=0,column=0)
        def commd1(event,tag):
            #绑定点击函数
            for i in range(len(self.list)):
                for j in range(len(self.list[i])):
                    #遍历与图盘对应的数组
                    if self.list[i][j]==tag:
                        #获取当前点击图盘位置的截图次序值
                        list_canshu=[i,j]
                        break
            for i in range(len(self.list)):
                for j in range(len(self.list[i])):
                    #遍历与图盘对应的数组
                    if self.list[i][j]==0:
                        #获取当前图盘位置为空的截图次序值
                        list_kong_canshu=[i,j]
                        break
            x_1=list_canshu[0]
            #获取当前点击图盘位置的截图次序值的X轴坐标
            y_1=list_canshu[1]
            #获取当前点击图盘位置的截图次序值的Y轴坐标
            x_0=list_kong_canshu[0]
            #获取当前图盘位置为空的截图次序值的X轴坐标
            y_0=list_kong_canshu[1]
            #获取当前图盘位置为空的截图次序值的Y轴坐标
            if x_1+1==x_0 or x_1-1==x_0 or y_1+1==y_0 or y_1-1==y_0:
                #对空值的位置与点击图片的位置进行一个判断(即相邻判断-上-下-左-右)
                shuzu=[x_0,y_0]
                qws=self.frame1.grid_slaves(row=x_1-1,column=y_1-1)
                #获取点击位置上所在布局的Label
                qws[0].grid(row=shuzu[0]-1,column=shuzu[1]-1)
                #将获取点击位置上所在布局的Label移动到空位置的布局上
                for i in range(len(self.list)):
                    for j in range(len(self.list[i])):
                        #遍历与图盘对应的数组
                        if self.list[i][j]==0:
                            #将图盘位置为空的截图次序值置为点击图盘位置截图次序值
                            self.list[i][j]=tag
                        elif self.list[i][j]==tag:
                            #将原来点击图盘位置的截图次序值置为空
                            self.list[i][j]=0
                        else:
                            pass
                if self.list==list_biaozhun:
                    #如果当前与图片对应的数组与原图的数组次序一致,则判断游戏结束
                    self.win_tishi.set("游戏结束")
        out=self.image_public.resize((180, 220))
        self.imtag_label=ImageTk.PhotoImage(out)
        self.img_label.configure(image=self.imtag_label)
        self.public_image_s=self.image_public.resize((450,550))
        size_x=self.public_image_s.size[0]
        size_y=self.public_image_s.size[1]
        x_average=size_x/3
        y_average=size_y/3
        
        self.lab_public1=Label(self.frame1,borderwidth=1)
        self.lab_public1.grid(row=0,column=1)
        self.lab_public1.bind("",lambda e,tag=new_array[0]: commd1(e,tag))
        #双击操作绑定事件
        self.lab_public2=Label(self.frame1,borderwidth=0.5)
        self.lab_public2.grid(row=0,column=2)
        self.lab_public2.bind("",lambda e,tag=new_array[1]: commd1(e,tag))
        #同上
        self.lab_public3=Label(self.frame1,borderwidth=0.5)
        self.lab_public3.grid(row=1,column=0)
        self.lab_public3.bind("",lambda e,tag=new_array[2]: commd1(e,tag))
        #同上
        self.lab_public4=Label(self.frame1,borderwidth=0.5)
        self.lab_public4.grid(row=1,column=1)
        self.lab_public4.bind("",lambda e,tag=new_array[3]: commd1(e,tag))
        #同上
        self.lab_public5=Label(self.frame1,borderwidth=0.5)
        self.lab_public5.grid(row=1,column=2)
        self.lab_public5.bind("",lambda e,tag=new_array[4]: commd1(e,tag))
        #同上
        self.lab_public6=Label(self.frame1,borderwidth=0.5)
        self.lab_public6.grid(row=2,column=0)
        self.lab_public6.bind("",lambda e,tag=new_array[5]: commd1(e,tag))
        #同上
        self.lab_public7=Label(self.frame1,borderwidth=0.5)
        self.lab_public7.grid(row=2,column=1)
        self.lab_public7.bind("",lambda e,tag=new_array[6]: commd1(e,tag))
        #同上
        self.lab_public8=Label(self.frame1,borderwidth=0.5)
        self.lab_public8.grid(row=2,column=2)
        self.lab_public8.bind("",lambda e,tag=new_array[7]: commd1(e,tag))
        #同上
        x_2=x_average
        y_2=y_average
        test_shunxu=[]
        
        region1=(x_average,0,x_average+x_average,y_average)
        cropImg1=self.public_image_s.crop(region1)
        #对原来的图片进行按照区域进行截图
        self.im_public1=ImageTk.PhotoImage(cropImg1)
        qws=self.frame1.grid_slaves(row=new_array[0]/3,column=new_array[0]%3)
        qws[0].configure(image=self.im_public1)
        #将第一个截图显示在frame1
        test_shunxu.append((1,new_array[0]))
        #同时将绑定的次序写入一个二维数组
        region2=(x_average+x_average,0,x_average+x_average+x_average,y_average)
        cropImg2=self.public_image_s.crop(region2)
        self.im_public2=ImageTk.PhotoImage(cropImg2)
        qws=self.frame1.grid_slaves(row=new_array[1]/3,column=new_array[1]%3)
        qws[0].configure(image=self.im_public2)
        test_shunxu.append((2,new_array[1]))
        #同上

        region3=(0,y_average,x_average,y_average+y_average)
        cropImg3=self.public_image_s.crop(region3)
        self.im_public3=ImageTk.PhotoImage(cropImg3)
        qws=self.frame1.grid_slaves(row=new_array[2]/3,column=new_array[2]%3)
        qws[0].configure(image=self.im_public3)
        test_shunxu.append((3,new_array[2]))
        #同上
        
        region4=(x_average,y_average,x_average+x_average,y_average+y_average)
        cropImg4=self.public_image_s.crop(region4)
        self.im_public4=ImageTk.PhotoImage(cropImg4)
        qws=self.frame1.grid_slaves(row=new_array[3]/3,column=new_array[3]%3)
        qws[0].configure(image=self.im_public4)
        test_shunxu.append((4,new_array[3]))
        #同上
        
        region5=(x_average+x_average,y_average,x_average+x_average+x_average,y_average+y_average)
        cropImg5=self.public_image_s.crop(region5)
        self.im_public5=ImageTk.PhotoImage(cropImg5)
        qws=self.frame1.grid_slaves(row=new_array[4]/3,column=new_array[4]%3)
        qws[0].configure(image=self.im_public5)
        test_shunxu.append((5,new_array[4]))
        #同上
        
        region6=(0,y_average+y_average,x_average,y_average+y_average+y_average)
        cropImg6=self.public_image_s.crop(region6)
        self.im_public6=ImageTk.PhotoImage(cropImg6)
        qws=self.frame1.grid_slaves(row=new_array[5]/3,column=new_array[5]%3)
        qws[0].configure(image=self.im_public6)
        test_shunxu.append((6,new_array[5]))
        #同上
        
        region7=(x_average,y_average+y_average,x_average+x_average,y_average+y_average+y_average)
        cropImg7=self.public_image_s.crop(region7)
        self.im_public7=ImageTk.PhotoImage(cropImg7)
        qws=self.frame1.grid_slaves(row=new_array[6]/3,column=new_array[6]%3)
        qws[0].configure(image=self.im_public7)
        test_shunxu.append((7,new_array[6]))
        #同上

        region8=(x_average+x_average,y_average+y_average,x_average+x_average+x_average,y_average+y_average+y_average)
        cropImg8=self.public_image_s.crop(region8)
        self.im_public8=ImageTk.PhotoImage(cropImg8)
        qws=self.frame1.grid_slaves(row=new_array[7]/3,column=new_array[7]%3)
        qws[0].configure(image=self.im_public8)
        test_shunxu.append((8,new_array[7]))
        #同上

11.启动游戏模块

    def start(self,image_path):
        path=image_path
        self.image_public=Image.open(path)
        #读取图片
        size_x=self.image_public.size[0]
        size_y=self.image_public.size[1]
        x_average=size_x/4
        y_average=size_y/3
        if size_x>size_y:
            if size_y+y_average>=size_x or size_x-x_average>=size_y:
                #4*3模式
                self.four_three_mod()
        else:
            #3*3模式
            self.three_three_mod()

《完》

感谢阅读,欢迎指正

你可能感兴趣的:(python,开发语言,游戏)