import sys import time import pygame import random from collections import OrderedDict from PIL import Image, ImageTk import tkinter as tk import datetime #定义棋盘的类 class Qipan: def __init__(self,size=4): #随机方向 self.position=[ [1,0],#下 [-1,0],#上 [0,1],#右 [0,-1]#左 ] #棋盘大小 self.size=size #棋盘点击坐标字典 self.click_react={'x':{},'y':{}} #pos(有序字典)记录每个xy坐标 self.pos=OrderedDict() #初始化数字化棋盘 num=1 for i in range(size):#i是行j是列 for j in range(size): posxy=tuple([i,j]) self.pos[posxy]=num num+=1 #保证是随机棋盘,从正确的棋盘移动至随机棋盘 for i in range(1000): positions=random.choice(self.position) change_op=addtu(posxy,positions)#相加移动 if change_op in self.pos: tmp=self.pos[change_op] self.pos[change_op]=size*size self.pos[posxy]=tmp posxy=change_op #将点击区域与坐标相挂钩(xy) for y in range(self.size): for x in range(self.size): #x x0=x*qizi_size+100 x1=(x+1)*qizi_size+100 click_x=(x0,x1)################################ self.click_react['x'][click_x]=x #y y0=y*qizi_size+150 y1=(y+1)*qizi_size+150 click_y=(y0,y1) self.click_react['y'][click_y]=y #实现移动棋子 def move(self,x,y): #判断对应的是第几行第几列 xnum=-1 for i,j in self.click_react['x'].items(): if i[0]<=x