前言
关注我,一起玩游戏吧!
《趣味连连看经典版》还原童年经典连连看小游戏
任何年龄段都能玩的小游戏,休闲解压必备!如果你学Python学累了,玩一下这个游戏放松下吧!
一、效果展示
完整的项目源码素材等滴滴我即可免费啦~
开始界面——
点击游戏——新游戏开始:
消一消:
二、游戏的步骤
1)游戏规则
相同的人物即可消除很简单,有👉手就行,消除是2个相同即可,一段距离是可以连接消除的。
2)素材
3)导入模块
import os, random
import tkinter as tk
import tkinter.messagebox
from PIL import Image, ImageTk
4)界面设置:标题大小等。
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
5)进游戏界面设置:点击游戏——新游戏开始。
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)
6)提取小头像数组
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))
7)初始化地图 存值为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.__gameSize
for 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])
8)根据地图绘制图像
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))
9)获取内部坐标
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.__delta
def getY(self, y):
return y * self.__iconHeight + self.__delta
'''
获取内部坐标
'''
def getInnerPoint(self, point):
x = -1
y = -1
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
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
return 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")
10)消除连通的两个块
def ClearLinkedBlocks(self, p1, p2):
self.__map[p1.y][p1.x] = self.EMPTY
self.__map[p2.y][p2.x] = self.EMPTY
self.canvas.delete('im%d%d' % (p1.x, p1.y))
self.canvas.delete('im%d%d' % (p2.x, p2.y))
结尾
好啦!以上就是今天要给大家分享的内容,如果有需要可以收藏起来慢慢看哈~
边看的话也要试着自己敲代码啦!
完整的免费源码领取处:
如需完整的项目源码+素材源码基地见:#私信小编06#或者点击蓝色文字添加即可获取免费的福利!
你们的支持是我最大的动力!!记得三连哦~mua 欢迎大家阅读往期的文章哦~
快来跟我一起学习吧!关注小编,每天更新精彩内容哦~