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

主界面

4*3模式

3*3模式

游戏图片选择弹窗

配置文件包

1.类初始化

import random
import os
import Pmw
import time
from Tkinter import *
from PIL import Image,ImageTkclass 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):#生成图片标签Labelimg_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]<image_show.size[0]:self.size_img_2=image_show.resize((450, 350))self.showimage=ImageTk.PhotoImage(self.size_img_2)img_label_show=Label(imageshow_toplevel,borderwidth=2,relief='groove',image=self.showimage)img_label_show.grid(row=0,column=0)

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]<image_set_1.size[0]:self.size_img_1=image_set_1.resize((170, 128))self.setimage_label_1=ImageTk.PhotoImage(self.size_img_1)qws1=self.widgets_group.interior().grid_slaves(row=0,column=0)qws1[0].configure(image=self.setimage_label_1,relief='groove')qws1[0].bind("<Button-1>",lambda e,fr_=qws1[0],path_img=path_image: comd_show(e,fr_,path_img))qws1[0].bind("<Button-3>",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]<image_set_2.size[0]:self.size_img_2=image_set_2.resize((170, 128))self.setimage_2=ImageTk.PhotoImage(self.size_img_2)qws2=self.widgets_group.interior().grid_slaves(row=0,column=1)qws2[0].configure(image=self.setimage_2,relief='groove')qws2[0].bind("<Button-1>",lambda e,fr_=qws2[0],path_img=path_image: comd_show(e,fr_,path_img))qws2[0].bind("<Button-3>",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]<image_set_3.size[0]:self.size_img_3=image_set_3.resize((170, 128))self.setimage_label_3=ImageTk.PhotoImage(self.size_img_3)qws3=self.widgets_group.interior().grid_slaves(row=0,column=2)qws3[0].configure(image=self.setimage_label_3,relief='groove')qws3[0].bind("<Button-1>",lambda e,fr_=qws3[0],path_img=path_image: comd_show(e,fr_,path_img))qws3[0].bind("<Button-3>",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]<image_set_4.size[0]:self.size_img_4=image_set_4.resize((170, 128))self.setimage_label_4=ImageTk.PhotoImage(self.size_img_4)qws4=self.widgets_group.interior().grid_slaves(row=1,column=0)qws4[0].configure(image=self.setimage_label_4,relief='groove')qws4[0].bind("<Button-1>",lambda e,fr_=qws4[0],path_img=path_image: comd_show(e,fr_,path_img))qws4[0].bind("<Button-3>",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]<image_set_5.size[0]:self.size_img_5=image_set_5.resize((170, 128))self.setimage_label_5=ImageTk.PhotoImage(self.size_img_5)qws5=self.widgets_group.interior().grid_slaves(row=1,column=1)qws5[0].configure(image=self.setimage_label_5,relief='groove')qws5[0].bind("<Button-1>",lambda e,fr_=qws5[0],path_img=path_image: comd_show(e,fr_,path_img))qws5[0].bind("<Button-3>",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]<image_set_6.size[0]:self.size_img_6=image_set_6.resize((170, 128))self.setimage_label_6=ImageTk.PhotoImage(self.size_img_6)qws6=self.widgets_group.interior().grid_slaves(row=1,column=2)qws6[0].configure(image=self.setimage_label_6,relief='groove')qws6[0].bind("<Button-1>",lambda e,fr_=qws6[0],path_img=path_image: comd_show(e,fr_,path_img))qws6[0].bind("<Button-3>",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]<image_set_7.size[0]:self.size_img_7=image_set_7.resize((170, 128))self.setimage_label_7=ImageTk.PhotoImage(self.size_img_7)qws7=self.widgets_group.interior().grid_slaves(row=2,column=0)qws7[0].configure(image=self.setimage_label_7,relief='groove')qws7[0].bind("<Button-1>",lambda e,fr_=qws7[0],path_img=path_image: comd_show(e,fr_,path_img))qws7[0].bind("<Button-3>",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]<image_set_8.size[0]:self.size_img_8=image_set_8.resize((170, 128)) self.setimage_label_8=ImageTk.PhotoImage(self.size_img_8)qws8=self.widgets_group.interior().grid_slaves(row=2,column=1)qws8[0].configure(image=self.setimage_label_8,relief='groove')qws8[0].bind("<Button-1>",lambda e,fr_=qws8[0],path_img=path_image: comd_show(e,fr_,path_img))qws8[0].bind("<Button-3>",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]<image_set_9.size[0]:self.size_img_9=image_set_9.resize((170, 128))self.setimage_label_9=ImageTk.PhotoImage(self.size_img_9)qws9=self.widgets_group.interior().grid_slaves(row=2,column=2)qws9[0].configure(image=self.setimage_label_9,relief='groove')qws9[0].bind("<Button-1>",lambda e,fr_=qws9[0],path_img=path_image: comd_show(e,fr_,path_img))qws9[0].bind("<Button-3>",lambda e,path_img=path_image: doublue_comd(e,path_img))

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

        def test_down():#下一页--图片缩略图实现self.num_page+=1chu=len(path1)/9if 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<chu:image_1(path1[self.num_page*9+0])image_2(path1[self.num_page*9+1])image_3(path1[self.num_page*9+2])image_4(path1[self.num_page*9+3])image_5(path1[self.num_page*9+4])image_6(path1[self.num_page*9+5])image_7(path1[self.num_page*9+6])image_8(path1[self.num_page*9+7])image_9(path1[self.num_page*9+8])elif self.num_page==chu and len(path1)%9==1:image_1(path1[self.num_page*9+0])elif self.num_page==chu and len(path1)%9==2:image_1(path1[self.num_page*9+0])image_2(path1[self.num_page*9+1])elif self.num_page==chu and len(path1)%9==3:image_1(path1[self.num_page*9+0])image_2(path1[self.num_page*9+1])image_3(path1[self.num_page*9+2])elif self.num_page==chu and len(path1)%9==4:image_1(path1[self.num_page*9+0])image_2(path1[self.num_page*9+1])image_3(path1[self.num_page*9+2])image_4(path1[self.num_page*9+3])elif self.num_page==chu and len(path1)%9==5:image_1(path1[self.num_page*9+0])image_2(path1[self.num_page*9+1])image_3(path1[self.num_page*9+2])image_4(path1[self.num_page*9+3])image_5(path1[self.num_page*9+4])elif self.num_page==chu and len(path1)%9==6:image_1(path1[self.num_page*9+0])image_2(path1[self.num_page*9+1])image_3(path1[self.num_page*9+2])image_4(path1[self.num_page*9+3])image_5(path1[self.num_page*9+4])image_6(path1[self.num_page*9+5])elif self.num_page==chu and len(path1)%9==7:image_1(path1[self.num_page*9+0])image_2(path1[self.num_page*9+1])image_3(path1[self.num_page*9+2])image_4(path1[self.num_page*9+3])image_5(path1[self.num_page*9+4])image_6(path1[self.num_page*9+5])image_7(path1[self.num_page*9+6])elif self.num_page==chu and len(path1)%9==8:image_1(path1[self.num_page*9+0])image_2(path1[self.num_page*9+1])image_3(path1[self.num_page*9+2])image_4(path1[self.num_page*9+3])image_5(path1[self.num_page*9+4])image_6(path1[self.num_page*9+5])image_7(path1[self.num_page*9+6])image_8(path1[self.num_page*9+7])

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

        def test_up():#上一页---图片显示self.num_page-=1chu=len(path1)/9if self.num_page<=chu and self.num_page>=0:self.ent.set(u"第"+str(self.num_page+1)+u"页")if self.num_page<chu:image_1(path1[self.num_page*9+0])image_2(path1[self.num_page*9+1])image_3(path1[self.num_page*9+2])image_4(path1[self.num_page*9+3])image_5(path1[self.num_page*9+4])image_6(path1[self.num_page*9+5])image_7(path1[self.num_page*9+6])image_8(path1[self.num_page*9+7])image_9(path1[self.num_page*9+8])

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=Falsefor 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]#获取当前点击图盘位置的截图次序值breakfor 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]#获取当前图盘位置为空的截图次序值breakx_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)#获取点击位置上所在布局的Labelqws[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]=tagelif self.list[i][j]==tag:#获取当前图盘位置为空的截图次序值self.list[i][j]=0else:passif 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/4y_average=size_y/3self.lab_public1=Label(self.frame1,borderwidth=1)self.lab_public1.grid(row=0,column=1)self.lab_public1.bind("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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)#将第一个截图显示在frame1qws[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)#将第二个截图显示在frame2region3=(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=Falsenum=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)#生成图盘布局Frameself.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]breakfor 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]breakx_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)#获取点击位置上所在布局的Labelqws[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]=tagelif self.list[i][j]==tag:#将原来点击图盘位置的截图次序值置为空self.list[i][j]=0else:passif 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/3y_average=size_y/3self.lab_public1=Label(self.frame1,borderwidth=1)self.lab_public1.grid(row=0,column=1)self.lab_public1.bind("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",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("<Double-Button-1>",lambda e,tag=new_array[7]: commd1(e,tag))#同上x_2=x_averagey_2=y_averagetest_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)#将第一个截图显示在frame1test_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_pathself.image_public=Image.open(path)#读取图片size_x=self.image_public.size[0]size_y=self.image_public.size[1]x_average=size_x/4y_average=size_y/3if 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/Tkinter的拼图单机小游戏相关推荐

  1. canvas绘制竖排的数字_大佬教你用Python Tkinter实现数字猜谜小游戏

    Tkinter是Python的Tk GUI(图形用户界面)工具包和事实上的标准GUI 的标准接口.GUI使您可以使用大多数操作系统使用的可视项(例如窗口,图标和菜单)与计算机进行交互.这个功能强大的工 ...

  2. 基于python tkinter的课堂点名小程序

    import datetime import json import os import random import tkinter as tk import openpyxl# 花名册文件名 很多人 ...

  3. 基于Python制作的热血足球小游戏

    开发工具 Python 版本:3.7.8 相关模块: pygame 模块: 以及一些 python 自带的模块. 环境搭建 安装 Python 并添加到环境变量,pip 安装需要的相关模块即可. 游戏 ...

  4. 基于LabVIEW的飞机大作战小游戏(可做毕设)

    一.前言 Python是目前相当流行的一门编程语言,网上有人用Python做了一个<飞机大作战>的小游戏,并且出了一份视频教程,很有意思."基于Python的飞机大作战小游戏&q ...

  5. 基于Python/Tkinter的飞机大战单机小游戏

    这是很早之前课余时间写的基于Python/Tkinter单机小游戏,用来练手,今天将代码贴出来,方便大家一起学习,通过Py/Tk对于学习GUI作为一个入口,其实是个不错入口,在这里推荐一下Tcl/Tk ...

  6. python点名代码_基于python tkinter的点名小程序功能的实例代码

    基于python tkinter的点名小程序功能的实例代码,花名册,次数,窗口,未找到,初始化 基于python tkinter的点名小程序功能的实例代码 易采站长站,站长之家为您整理了基于pytho ...

  7. python小猴子摘桃子的故事_基于Python-Pycharm实现的猴子摘桃小游戏(源代码)

    源码及注释: import pygame from sys import exit from random import randint import time import os # 定义窗口分辨率 ...

  8. python基于pygame的飞机大作战小游戏

    基于pygame的飞机大作战小游戏,适合新手,不能直接运行,只能在命令行进入当前游戏目录,输入python game.py才能够运行,尚不知道是什么原因 游戏截图如下,我们用黄色的圆圈代表敌机, 代码 ...

  9. Python实现消消乐小游戏

    本文主要介绍了Python实现消消乐小游戏,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,编程学习资料点击免费领取 提到开心消消乐这款小游戏,相信大家都不陌生,其曾 ...

最新文章

  1. 生成html页面的ftl文件,FreeMarker生成静态HTML页面的工具类FreeMarkerUtil
  2. 015_面向对象_异常,包和Object类
  3. C#——委托(delegate)DEMO
  4. HttpClient 使用
  5. java中三种常见内存溢出错误的处理方法(good)
  6. pythonjava app切出后无网络连接_写了一个java的Server 用python的client访问却访问不通问题。...
  7. 设计模式第三篇-装饰者模式
  8. IIS设置HTTP To HTTPS
  9. 2021-秋招你准备好了吗?软件测试面试题
  10. python实现将字符串转化为数字(逆序输出)
  11. 【GPU精粹与Shader编程】(二) 《GPU Gems 1》全书核心内容提炼总结 · 上篇
  12. 怎么看263邮箱的服务器信息,263邮箱真的不能用,刚刚上了一当,劝大家务必注意...
  13. python安装 错误 “User installations are disabled via policy on the machine”
  14. BIT2023 智慧社区综合管理系统-一周目
  15. Android App内截屏监控及涂鸦功能实现
  16. Altium Designer14安装教程及注意事项
  17. ffmpeg学习 函数分析sws_scale
  18. 照片估计明星身高matlab,对比上万张合照,国内男星身高终极论证
  19. 牛客网社区项目——p3.4事务管理
  20. 爱心熊 Game Jam 来啦,30,000 SAND等你们来赢取!

热门文章

  1. (附源码)计算机毕业设计SSM火车票预定管理系统
  2. (二十二)岁月无声 - 6
  3. php crypt函数缓冲区溢出漏洞,简单缓冲区溢出漏洞攻击实验
  4. 对话赞奇科技总经理金伟:云渲染何以成为元宇宙“基建”
  5. 新手须知:友情链接互换5个知识点
  6. LDblock绘制连锁不平衡和单体型图
  7. Sleuth Kit、Autopsy 的使用
  8. 程序员兼职 接单网站
  9. 将代码注入到进程的三种方式
  10. 输入框内内容过多,显示不全内容