哈嘍~大家好,我是恰恰,好久不見,我是一個不太喜歡玩游戲的人,但是小時候特別喜歡玩連連看,很消磨時間,所以我找來了連連看游戲的源代碼,畢竟熱愛才能學習的更久,興趣是最好的老師對吧!
另外還給同學們準備了其他學習資料,需要更多學習資料、軟體安裝包的同學可以點擊鏈接免費領取 :
https://docs.qq.com/doc/DZmpWZHpqZFFFZGhh?pub=1&dver=2.1.27114328
Python連連看小游戲源代碼,經典小游戲連連看Python源程式,連連看游戲資源包請前往:https://pan.baidu.com/s/1a5cmM8noQbGmH_WG49-maA下載,提取碼請回復:連連看提取碼,程式運行截圖:
main.py
''''''import os, randomimport tkinter as tkimport tkinter.messageboxfrom PIL import Image, ImageTkroot = tk.Tk()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 = NoneEMPTY = -1NONE_LINK = 0STRAIGHT_LINK = 1ONE_CORNER_LINK = 2TWO_CORNER_LINK = 3def __init__(self):root.title(self.__gameTitle)self.centerWindow(self.__windowWidth, self.__windowHeigth)root.minsize(460, 460)self.__addComponets()self.extractSmallIconList()def __addComponets(self):self.menubar = tk.Menu(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)root.configure(menu=self.menubar)self.canvas = tk.Canvas(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 = root.winfo_screenwidth()screenheight = root.winfo_screenheight()size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)root.geometry(size)def file_new(self, event=None):self.iniMap()self.drawMap()self.__isGameStart = Truedef clickCanvas(self, event):if self.__isGameStart:point = self.getInnerPoint(Point(event.x, event.y))# 有效點擊坐標if point.isUserful() and not self.isEmptyInMap(point):if self.__isFirst:self.drawSelectedArea(point)self.__isFirst= Falseself.__formerPoint = pointelse:if self.__formerPoint.isEqual(point):self.__isFirst = Trueself.canvas.delete("rectRedOne")else:linkType = self.getLinkType(self.__formerPoint, point)if linkType['type'] != self.NONE_LINK:# TODO Animationself.ClearLinkedBlocks(self.__formerPoint, point)self.canvas.delete("rectRedOne")self.__isFirst = Trueif self.isGameEnd():tk.messagebox.showinfo("You Win!", "Tip")self.__isGameStart = Falseelse:self.__formerPoint = pointself.canvas.delete("rectRedOne")self.drawSelectedArea(point)# 判斷游戲是否結束def isGameEnd(self):for y in range(0, self.__gameSize):for x in range(0, self.__gameSize):if self.__map[y][x] != self.EMPTY:return Falsereturn True'''提取小頭像陣列'''def extractSmallIconList(self):imageSouce = Image.open('圖片/NARUTO.png')for index in range(0, int(self.__iconKind)):region = imageSouce.crop((self.__iconWidth * index, 0,self.__iconWidth * index + self.__iconWidth - 1, self.__iconHeight - 1))self.__icons.append(ImageTk.PhotoImage(region))'''初始化地圖 存值為0-24'''def iniMap(self):self.__map = [] # 重置地圖tmpRecords = []records = []for i in range(0, int(self.__iconKind)):for j in range(0, 4):tmpRecords.append(i)total = self.__gameSize * self.__gameSizefor x in range(0, total):index = random.randint(0, total - x - 1)records.append(tmpRecords[index])del tmpRecords[index]# 一維陣列轉為二維,y為高維度for y in range(0, self.__gameSize):for x in range(0, self.__gameSize):if x == 0:self.__map.append([])self.__map[y].append(records[x + y * self.__gameSize])'''根據地圖繪制影像'''def drawMap(self):self.canvas.delete("all")for y in range(0, self.__gameSize):for x in range(0, self.__gameSize):point = self.getOuterLeftTopPoint(Point(x, y))im = self.canvas.create_image((point.x, point.y),image=self.__icons[self.__map[y][x]], anchor='nw', tags = 'im%d%d' % (x, y))'''獲取內部坐標對應矩形左上角頂點坐標'''def getOuterLeftTopPoint(self, point):return Point(self.getX(point.x), self.getY(point.y))'''獲取內部坐標對應矩形中心坐標'''def getOuterCenterPoint(self, point):return Point(self.getX(point.x) + int(self.__iconWidth / 2),self.getY(point.y) + int(self.__iconHeight / 2))def getX(self, x):return x * self.__iconWidth + self.__deltadef getY(self, y):return y * self.__iconHeight + self.__delta'''獲取內部坐標'''def getInnerPoint(self, point):x = -1y = -1for i in range(0, self.__gameSize):x1 = self.getX(i)x2 = self.getX(i + 1)if point.x >= x1 and point.x < x2:x = ifor j in range(0, self.__gameSize):j1 = self.getY(j)j2 = self.getY(j + 1)if point.y >= j1 and point.y < j2:y = jreturn Point(x, y)'''選擇的區域變紅,point為內部坐標'''def drawSelectedArea(self, point):pointLT = self.getOuterLeftTopPoint(point)pointRB = self.getOuterLeftTopPoint(Point(point.x + 1, point.y + 1))self.canvas.create_rectangle(pointLT.x, pointLT.y,pointRB.x - 1, pointRB.y - 1, outline = 'red', tags = "rectRedOne")'''消除連通的兩個塊'''def ClearLinkedBlocks(self, p1, p2):self.__map[p1.y][p1.x] = self.EMPTYself.__map[p2.y][p2.x] = self.EMPTYself.canvas.delete('im%d%d' % (p1.x, p1.y))self.canvas.delete('im%d%d' % (p2.x, p2.y))'''地圖上該點是否為空'''def isEmptyInMap(self, point):if self.__map[point.y][point.x] == self.EMPTY:return Trueelse:return False'''獲取兩個點連通型別'''def getLinkType(self, p1, p2):# 首先判斷兩個方塊中圖片是否相同if self.__map[p1.y][p1.x] != self.__map[p2.y][p2.x]:return { 'type': self.NONE_LINK }if self.isStraightLink(p1, p2):return {'type': self.STRAIGHT_LINK}res = self.isOneCornerLink(p1, p2)if res:return {'type': self.ONE_CORNER_LINK,'p1': res}res = self.isTwoCornerLink(p1, p2)if res:return {'type': self.TWO_CORNER_LINK,'p1': res['p1'],'p2': res['p2']}return {'type': self.NONE_LINK}'''直連'''def isStraightLink(self, p1, p2):start = -1end = -1# 水平if p1.y == p2.y:# 大小判斷if p2.x < p1.x:start = p2.xend = p1.xelse:start = p1.xend = p2.xfor x in range(start + 1, end):if self.__map[p1.y][x] != self.EMPTY:return Falsereturn Trueelif p1.x == p2.x:if p1.y > p2.y:start = p2.yend = p1.yelse:start = p1.yend = p2.yfor y in range(start + 1, end):if self.__map[y][p1.x] != self.EMPTY:return Falsereturn Truereturn Falsedef isOneCornerLink(self, p1, p2):pointCorner = Point(p1.x, p2.y)if self.isStraightLink(p1, pointCorner) and self.isStraightLink(pointCorner, p2) and self.isEmptyInMap(pointCorner):return pointCornerpointCorner = Point(p2.x, p1.y)if self.isStraightLink(p1, pointCorner) and self.isStraightLink(pointCorner, p2) and self.isEmptyInMap(pointCorner):return pointCornerdef isTwoCornerLink(self, p1, p2):for y in range(-1, self.__gameSize + 1):pointCorner1 = Point(p1.x, y)pointCorner2 = Point(p2.x, y)if y == p1.y or y == p2.y:continueif y == -1 or y == self.__gameSize:if self.isStraightLink(p1, pointCorner1) and self.isStraightLink(pointCorner2, p2):return {'p1': pointCorner1, 'p2': pointCorner2}else:if self.isStraightLink(p1, pointCorner1) and self.isStraightLink(pointCorner1, pointCorner2) and self.isStraightLink(pointCorner2, p2) and self.isEmptyInMap(pointCorner1) and self.isEmptyInMap(pointCorner2):return {'p1': pointCorner1, 'p2': pointCorner2}# 橫向判斷for x in range(-1, self.__gameSize + 1):pointCorner1 = Point(x, p1.y)pointCorner2 = Point(x, p2.y)if x == p1.x or x == p2.x:continueif x == -1 or x == self.__gameSize:if self.isStraightLink(p1, pointCorner1) and self.isStraightLink(pointCorner2, p2):return {'p1': pointCorner1, 'p2': pointCorner2}else:if self.isStraightLink(p1, pointCorner1) and self.isStraightLink(pointCorner1, pointCorner2) and self.isStraightLink(pointCorner2, p2) and self.isEmptyInMap(pointCorner1) and self.isEmptyInMap(pointCorner2):return {'p1': pointCorner1, 'p2': pointCorner2}class Point():def __init__(self, x, y):self.x = xself.y = ydef isUserful(self):if self.x >= 0 and self.y >= 0:return Trueelse:return False'''判斷兩個點是否相同'''def isEqual(self, point):if self.x == point.x and self.y == point.y:return Trueelse:return False'''克隆一份物件'''def clone(self):return Point(self.x, self.y)'''改為另一個物件'''def changeTo(self, point):self.x = point.xself.y = point.ym = MainWindow()root.mainloop()
可能大多數的同學想學習Python都是想賺錢,說著聽人說學了有用,也有人是因為行業內卷,但是我還是覺得學習任何東西熱愛是最好的老師,Python可以實作很多事情,都是很有趣的,讓人很有成就感的,所以同學們要跟我一起探索Python的樂趣,熱愛它并且學會它哦~
給同學們準備了其他學習資料,需要更多學習資料、軟體安裝包的同學可以點擊鏈接免費領取 :
https://docs.qq.com/doc/DZmpWZHpqZFFFZGhh?pub=1&dver=2.1.27114328
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/296678.html
標籤:其他
上一篇:【C語言專案】掃雷-滑鼠版
