需要自己添加图片素材呦
运行效果:
完整代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2017-10-02 15:19:24
# @Author : Salamander(1906747819@qq.com)
# @Link : http://51lucy.com
import os, random
import tkinter as tk
import tkinter.messagebox
from PIL import Image, ImageTk
class MainWindow():
__gameTitle = "连连看游戏"
__windowWidth = 700
__windowHeigth = 500
__icons = []
__gameSize = 10 # 游戏尺寸
__iconKind = __gameSize * __gameSize / 4 # 小图片种类数量
__iconWidth = 40
__iconHeight = 40
__map = [] # 游戏地图
__delta = 25
__isFirst = True
__isGameStart = False
__formerPoint = None
EMPTY = -1
NONE_LINK = 0
STRAIGHT_LINK = 1
ONE_CORNER_LINK = 2
TWO_CORNER_LINK = 3
def __init__(self):
self.root = tk.Tk()
self.root.title(self.__gameTitle)
self.centerWindow(self.__windowWidth, self.__windowHeigth)
self.root.minsize(460, 460)
self.__addComponets()
self.extractSmallIconList()
self.root.mainloop()
def __addComponets(self):
self.menubar = tk.Menu(self.root, bg="lightgrey", fg="black")
self.file_menu = tk.Menu(self.menubar, tearoff=0, bg="lightgrey", fg="black")
self.file_menu.add_command(label="新游戏", command=self.file_new, accelerator="Ctrl+N")
self.menubar.add_cascade(label="游戏", menu=self.file_menu)
self.root.configure(menu=self.menubar)
self.canvas = tk.Canvas(self.root, bg = 'white', width = 450, height = 450)
self.canvas.pack(side=tk.TOP, pady = 5)
self.canvas.bind('', self.clickCanvas)
def centerWindow(self, width, height):
screenwidth = self.root.winfo_screenwidth()
screenheight = self.root.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
self.root.geometr