有没有喜欢看火影忍者的小伙伴,我个比较喜欢鸣人,小樱,佐助,雏田,鼬等一些人物。
这次就跟大家弄一个他们的连连看吧!
喜欢的记得关注我
首先先定义这次我们需要的模块
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('<Button-1>', 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.geometry(size)
def file_new(self, event=None):
self.iniMap()
self.drawMap()
self.__isGameStart = True
def clickCanvas(self, event):
if self.__isGameStart:
point = self.getInnerPoint(Point(event.x, event.y))
# 有效点击坐标
if point.isUserful() and not self.isEmptyInMap(point):
if self