快来看!我把小时候最喜欢的连连看游戏源代码拿来了~

游戏 游戏 1704 人阅读 | 0 人回复

<
  哈喽~各人好,我是恰好,良久没有睹,我是一个没有太喜好玩游戏的人,可是小时分出格喜好玩连连看,很消磨工夫,以是我找去了连连看游戏的源代码,究竟结果酷爱才调进修的更暂,爱好是最好的教师对吧!

   别的借给同窗们筹办了其他进修材料,需求更多进修材料、硬件装置包的同窗能够面击链接免费支付 :

  https://docs.qq.com/doc/DZmpWZHpqZFFFZGhh?pub=1&dver=2.1.27114328
 
Python连连看小游戏源代码,典范小游戏连连看Python源法式,连连看游戏资本包请前去:https://pan.百度.com/s/1a5cmM8noQbGmH_WG49-maA下载,提与码请复兴:连连看提与码。法式运转截图:
155140nfl1f9w15ik8ww91.jpg

 

main.py



  1. &#39;&#39;&#39;&#39;&#39;&#39;import os, randomimport tkinter as tkimport tkinter.messageboxfrom PIL import Image, ImageTk
  2. root = tk.Tk()
  3. 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
  4.   def __init__(self):    root.title(self.__gameTitle)    self.centerWindow(self.__windowWidth, self.__windowHeigth)    root.minsize(460, 460)
  5.     self.__addComponets()    self.extractSmallIconList()
  6.   def __addComponets(self):    self.menubar = tk.Menu(root, bg="lightgrey", fg="black")
  7.     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")
  8.     self.menubar.add_cascade(label="游戏", menu=self.file_menu)    root.configure(menu=self.menubar)
  9.     self.canvas = tk.Canvas(root, bg = &#39;white&#39;, width = 450, height = 450)    self.canvas.pack(side=tk.TOP, pady = 5)    self.canvas.bind(&#39;<Button-1>&#39;, self.clickCanvas)        
  10.   def centerWindow(self, width, height):    screenwidth = root.winfo_screenwidth()    screenheight = root.winfo_screenheight()    size = &#39;%dx%d+%d+%d&#39; % (width, height, (screenwidth - width)/2, (screenheight - height)/2)    root.geometry(size)
  11.   def file_new(self, event=None):    self.iniMap()    self.drawMap()    self.__isGameStart = True
  12.   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.__isFirst:          self.drawSelectedArea(point)          self.__isFirst= False          self.__formerPoint = point        else:          if self.__formerPoint.isEqual(point):            self.__isFirst = True            self.canvas.delete("rectRedOne")          else:            linkType = self.getLinkType(self.__formerPoint, point)            if linkType[&#39;type&#39;] != self.NONE_LINK:              # TODO Animation              self.ClearLinkedBlocks(self.__formerPoint, point)              self.canvas.delete("rectRedOne")              self.__isFirst = True              if self.isGameEnd():                tk.messagebox.showinfo("You Win!", "Tip")                self.__isGameStart = False            else:              self.__formerPoint = point              self.canvas.delete("rectRedOne")              self.drawSelectedArea(point)
  13.   # 判定游戏能否完毕  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 False    return True
  14.               
  15.   &#39;&#39;&#39;  提与小头像数组  &#39;&#39;&#39;  def extractSmallIconList(self):    imageSouce = Image.open(&#39;图片/NARUTO.png&#39;)    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))
  16.   &#39;&#39;&#39;  初初化舆图 存值为0-24  &#39;&#39;&#39;  def iniMap(self):    self.__map = [] # 重置舆图    tmpRecords = []    records = []    for i in range(0, int(self.__iconKind)):      for j in range(0, 4):        tmpRecords.append(i)
  17.     total = self.__gameSize * self.__gameSize    for x in range(0, total):      index = random.randint(0, total - x - 1)      records.append(tmpRecords[index])      del tmpRecords[index]
  18.     # 一维数组转为两维,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])
  19.   &#39;&#39;&#39;  按照舆图画造图象  &#39;&#39;&#39;  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=&#39;nw&#39;, tags = &#39;im%d%d&#39; % (x, y))
  20.   &#39;&#39;&#39;  获得内乱部坐标对应矩形左上角极点坐标  &#39;&#39;&#39;  def getOuterLeftTopPoint(self, point):    return Point(self.getX(point.x), self.getY(point.y))
  21.   &#39;&#39;&#39;  获得内乱部坐标对应矩形中间坐标  &#39;&#39;&#39;  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.__delta
  22.   def getY(self, y):    return y * self.__iconHeight + self.__delta
  23.   &#39;&#39;&#39;  获得内乱部坐标  &#39;&#39;&#39;  def getInnerPoint(self, point):    x = -1    y = -1
  24.     for i in range(0, self.__gameSize):      x1 = self.getX(i)      x2 = self.getX(i + 1)      if point.x >= x1 and point.x < x2:        x = i
  25.     for j in range(0, self.__gameSize):      j1 = self.getY(j)      j2 = self.getY(j + 1)      if point.y >= j1 and point.y < j2:        y = j
  26.     return Point(x, y)
  27.   &#39;&#39;&#39;  挑选的地区变白,point为内乱部坐标  &#39;&#39;&#39;  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 = &#39;red&#39;, tags = "rectRedOne")
  28.   &#39;&#39;&#39;  消弭连通的两个块  &#39;&#39;&#39;  def ClearLinkedBlocks(self, p1, p2):    self.__map[p1.y][p1.x] = self.EMPTY    self.__map[p2.y][p2.x] = self.EMPTY    self.canvas.delete(&#39;im%d%d&#39; % (p1.x, p1.y))    self.canvas.delete(&#39;im%d%d&#39; % (p2.x, p2.y))
  29.   &#39;&#39;&#39;  舆图上该面能否为空  &#39;&#39;&#39;  def isEmptyInMap(self, point):    if self.__map[point.y][point.x] == self.EMPTY:      return True    else:      return False
  30.   &#39;&#39;&#39;  获得两个面连通范例  &#39;&#39;&#39;  def getLinkType(self, p1, p2):    # 起首判定两个圆块中图片能否不异    if self.__map[p1.y][p1.x] != self.__map[p2.y][p2.x]:      return { &#39;type&#39;: self.NONE_LINK }
  31.     if self.isStraightLink(p1, p2):      return {        &#39;type&#39;: self.STRAIGHT_LINK      }    res = self.isOneCornerLink(p1, p2)    if res:      return {        &#39;type&#39;: self.ONE_CORNER_LINK,        &#39;p1&#39;: res      }    res = self.isTwoCornerLink(p1, p2)    if res:      return {        &#39;type&#39;: self.TWO_CORNER_LINK,        &#39;p1&#39;: res[&#39;p1&#39;],        &#39;p2&#39;: res[&#39;p2&#39;]      }    return {      &#39;type&#39;: self.NONE_LINK    }
  32.   &#39;&#39;&#39;  曲连  &#39;&#39;&#39;  def isStraightLink(self, p1, p2):    start = -1    end = -1    # 程度    if p1.y == p2.y:      # 巨细判定      if p2.x < p1.x:        start = p2.x        end = p1.x      else:        start = p1.x        end = p2.x      for x in range(start + 1, end):        if self.__map[p1.y][x] != self.EMPTY:          return False      return True    elif p1.x == p2.x:      if p1.y > p2.y:        start = p2.y        end = p1.y      else:        start = p1.y        end = p2.y      for y in range(start + 1, end):        if self.__map[y][p1.x] != self.EMPTY:          return False      return True    return False
  33.   def 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 pointCorner
  34.     pointCorner = Point(p2.x, p1.y)    if self.isStraightLink(p1, pointCorner) and self.isStraightLink(pointCorner, p2) and self.isEmptyInMap(pointCorner):      return pointCorner
  35.   def 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:        continue      if y == -1 or y == self.__gameSize:        if self.isStraightLink(p1, pointCorner1) and self.isStraightLink(pointCorner2, p2):          return {&#39;p1&#39;: pointCorner1, &#39;p2&#39;: 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 {&#39;p1&#39;: pointCorner1, &#39;p2&#39;: pointCorner2}
  36.     # 横背判定    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:        continue      if x == -1 or x == self.__gameSize:        if self.isStraightLink(p1, pointCorner1) and self.isStraightLink(pointCorner2, p2):          return {&#39;p1&#39;: pointCorner1, &#39;p2&#39;: 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 {&#39;p1&#39;: pointCorner1, &#39;p2&#39;: pointCorner2}
  37. class Point():  def __init__(self, x, y):    self.x = x    self.y = y
  38.   def isUserful(self):    if self.x >= 0 and self.y >= 0:      return True    else:      return False            &#39;&#39;&#39;  判定两个面能否不异  &#39;&#39;&#39;  def isEqual(self, point):    if self.x == point.x and self.y == point.y:      return True    else:      return False
  39.   &#39;&#39;&#39;  克隆一份工具  &#39;&#39;&#39;  def clone(self):    return Point(self.x, self.y)
  40.   &#39;&#39;&#39;  改成另外一个工具  &#39;&#39;&#39;  def changeTo(self, point):    self.x = point.x    self.y = point.y
  41. m = MainWindow()root.mainloop()
复造代码
 能够年夜大都的同窗念进修Python皆是念赢利,道着听人道教了有效,也有人是由于止业内乱卷,可是我仍是以为进修任何工具酷爱是最好的教师,Python能够完成很多工作,皆是很风趣的,让人很有成绩感的,以是同窗们要跟我一同探究Python的爱好,酷爱它而且教会它哦~
给同窗们筹办了其他进修材料,需求更多进修材料、硬件装置包的同窗能够面击链接免费支付 :

https://docs.qq.com/doc/DZmpWZHpqZFFFZGhh?pub=1&dver=2.1.27114328

免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作!
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则