前言
在上一章中,介绍了在消消乐中连接路径如何判定,什么是广度优先搜索算法,它的作用。在本章中我们将会将使用广度优先搜索算法来搜索两个元素之间的连接路径。
一、扩展节点
由上一章我们可以看到每个元素可以扩展的节点是与其相邻的上下左右的节点。
同时我们也可以看到在边缘的元素节点扩展的节点是有限的。
1、中间节点的扩展节点有四个节点,分别为上下左右
2、四个角节点的扩展节点只有两个节点
3、边节点的扩展节点有三个节点
我们可以在GameBlock
类中单独定义一个扩展节点函数expandNode
。
def expandNode(self,node):
neighbors = []
if node[1] < self.blocks_type.shape[1]-1:#节点上方还有节点则可以扩展上方节点
neighbors.append((node[0],node[1]+1))
if node[1] > 0:#节点下方还有节点则可以扩展下方节点
neighbors.append((node[0],node[1]-1))
if node[0] < self.blocks_type.shape[0]-1: #节点右方还有节点则可以扩展右方节点
neighbors.append((node[0]+1,node[1]))
if node[0] > 0:#节点左方还有节点则可以扩展左方节点
neighbors.append((node[0]-1,node[1]))
return neighbors
完整代码:
-MagicBlock.py
import pygame
import random
import numpy as np
TYPE_RECT = 0
TYPE_IMAGE = 1
class Block:
def __init__(self,screen,left,top,width,height,type,image=None,color=None):
self.screen = screen
self.left = left
self.top = top
self.type = type
self.image = image
self.color = color
self.width = width
self.height = height
def draw(self):
if self.type == TYPE_RECT:
position = self.left,self.top,self.width,self.height
pygame.draw.rect(self.screen,self.color,position)
elif self.type == TYPE_IMAGE:
self.screen.blit(self.image,(self.left,self.top))
class GameBlock:
def __init__(self,screen,start_left,start_top,block_height,block_width,row,col,type):
self.row = row
self.col = col
self.screen = screen
self.start_left = start_left
self.start_top = start_top
self.block_width = block_width
self.block_height = block_height
self.type = type
self.blocks=[[0]*self.col for i in range(self.row)]
self.blocks_type = np.zeros((self.row,self.col),dtype=np.int8)
self.click_num = 0
self.click_list = list()
def initMagicBlock(self):
for i in range(0,self.row,1):
for j in range(0,self.col,1):
if self.type == TYPE_IMAGE:
self.blocks_type[i][j] = random.randint(1,5)
self.blocks[i][j] = Block(self.screen,self.start_left+self.block_width*j,self.start_top+self.block_height*i,self.block_width,self.block_height,self.type,image=pygame.image.load('./image/0'+str(self.blocks_type[i][j])+"_hightlight.png"))
self.blocks[i][j].draw()
elif self.type == TYPE_RECT:
self.blocks[i][j] = Block(self.screen,self.start_left+self.block_width*j,self.start_top+self.block_height*i,self.block_width,self.block_height,self.type,color=(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
self.blocks[i][j].draw()
def mouseClick(self,x,y):
i = (y-self.start_top)//self.block_height
j = (x-self.start_left)//self.block_width
if i<0 or i>self.row-1 or j<0 or j >self.col-1 or self.blocks_type[i][j] == 0:
return False
if self.click_num %2 == 1 and self.click_list[0][0] == i and self.click_list[0][1] == j:
return False
self.click_num += 1
self.click_list.append([i,j])
if self.click_num % 2 == 0:
if self.blocks_type[self.click_list[0][0]][self.click_list[0][1]] == self.blocks_type[self.click_list[1][0]][self.click_list[1][1]]:
self.blocks[self.click_list[0][0]][self.click_list[0][1]].type = TYPE_RECT
self.blocks[self.click_list[0][0]][self.click_list[0][1]].color = (255,255,255)
self.blocks[self.click_list[0][0]][self.click_list[0][1]].draw()#进行重渲染
self.blocks[self.click_list[1][0]][self.click_list[1][1]].type = TYPE_RECT
self.blocks[self.click_list[1][0]][self.click_list[1][1]].color = (255,255,255)
self.blocks[self.click_list[1][0]][self.click_list[1][1]].draw()#进行重渲染
self.click_list = list()
def expandNode(self,node):
neighbors = []
if node[1] < self.blocks_type.shape[1]-1:#节点上方还有节点则可以扩展上方节点
neighbors.append((node[0],node[1]+1))
if node[1] > 0:#节点下方还有节点则可以扩展下方节点
neighbors.append((node[0],node[1]-1))
if node[0] < self.blocks_type.shape[0]-1: #节点右方还有节点则可以扩展右方节点
neighbors.append((node[0]+1,node[1]))
if node[0] > 0:#节点左方还有节点则可以扩展左方节点
neighbors.append((node[0]-1,node[1]))
return neighbors
我们已经可以扩展节点了,接着我们判断是否有连接路径。
二、搜索路径
在上一章的广度优先搜索算法的基础上,我们需要在在访问邻近节点时,将其换成扩展节点。并且需要判断扩展的节点是否为目标节点,将未访问过的节点且可以扩展的节点加入待扩展队列中。
def breadth_first_search(self,startnode,destinationnode):
visited = set()#访问过的节点集合
queue = deque([tuple(startnode)]) #队列,存储待访问的节点
while queue:
node = queue.popleft() #从队列左侧弹出一个节点
if node not in visited:
visited.add(node)
for neighbor in self.expandNode(node):
if tuple(neighbor) == tuple(destinationnode):
return True
if tuple(neighbor) not in visited and self.blocks_type[neighbor[0]][neighbor[1]] == 0:
queue.append(tuple(neighbor)) # 将未访问的邻居加入队列
return False
完整代码:
-MagicBlock.py
import pygame
import random
import numpy as np
from collections import deque
TYPE_RECT = 0
TYPE_IMAGE = 1
class Block:
def __init__(self,screen,left,top,width,height,type,image=None,color=None):
self.screen = screen
self.left = left
self.top = top
self.type = type
self.image = image
self.color = color
self.width = width
self.height = height
def draw(self):
if self.type == TYPE_RECT:
position = self.left,self.top,self.width,self.height
pygame.draw.rect(self.screen,self.color,position)
elif self.type == TYPE_IMAGE:
self.screen.blit(self.image,(self.left,self.top))
class GameBlock:
def __init__(self,screen,start_left,start_top,block_height,block_width,row,col,type):
self.row = row
self.col = col
self.screen = screen
self.start_left = start_left
self.start_top = start_top
self.block_width = block_width
self.block_height = block_height
self.type = type
self.blocks=[[0]*self.col for i in range(self.row)]
self.blocks_type = np.zeros((self.row,self.col),dtype=np.int8)
self.click_num = 0
self.click_list = list()
def initMagicBlock(self):
for i in range(0,self.row,1):
for j in range(0,self.col,1):
if self.type == TYPE_IMAGE:
self.blocks_type[i][j] = random.randint(1,5)
self.blocks[i][j] = Block(self.screen,self.start_left+self.block_width*j,self.start_top+self.block_height*i,self.block_width,self.block_height,self.type,image=pygame.image.load('./image/0'+str(self.blocks_type[i][j])+"_hightlight.png"))
self.blocks[i][j].draw()
elif self.type == TYPE_RECT:
self.blocks[i][j] = Block(self.screen,self.start_left+self.block_width*j,self.start_top+self.block_height*i,self.block_width,self.block_height,self.type,color=(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
self.blocks[i][j].draw()
def mouseClick(self,x,y):
i = (y-self.start_top)//self.block_height
j = (x-self.start_left)//self.block_width
if i<0 or i>self.row-1 or j<0 or j >self.col-1 or self.blocks_type[i][j] == 0:
return False
if self.click_num %2 == 1 and self.click_list[0][0] == i and self.click_list[0][1] == j:
return False
self.click_num += 1
self.click_list.append([i,j])
if self.click_num % 2 == 0:
if self.blocks_type[self.click_list[0][0]][self.click_list[0][1]] == self.blocks_type[self.click_list[1][0]][self.click_list[1][1]] :
self.blocks[self.click_list[0][0]][self.click_list[0][1]].type = TYPE_RECT
self.blocks[self.click_list[0][0]][self.click_list[0][1]].color = (255,255,255)
self.blocks[self.click_list[0][0]][self.click_list[0][1]].draw()#进行重渲染
self.blocks[self.click_list[1][0]][self.click_list[1][1]].type = TYPE_RECT
self.blocks[self.click_list[1][0]][self.click_list[1][1]].color = (255,255,255)
self.blocks[self.click_list[1][0]][self.click_list[1][1]].draw()#进行重渲染
self.blocks_type[self.click_list[0][0]][self.click_list[0][1]] = 0
self.blocks_type[self.click_list[1][0]][self.click_list[1][1]] = 0
self.click_list = list()
def breadth_first_search(self,startnode,destinationnode):
visited = set()#访问过的节点集合
queue = deque([tuple(startnode)]) #队列,存储待访问的节点
while queue:
node = queue.popleft() #从队列左侧弹出一个节点
if node not in visited:
visited.add(node)
for neighbor in self.expandNode(node):
if tuple(neighbor) == tuple(destinationnode):
return True
if tuple(neighbor) not in visited and self.blocks_type[neighbor[0]][neighbor[1]] == 0:
queue.append(tuple(neighbor)) # 将未访问的邻居加入队列
return False
def expandNode(self,node):
neighbors = []
if node[1] < self.blocks_type.shape[1]-1:#节点上方还有节点则可以扩展上方节点
neighbors.append((node[0],node[1]+1))
if node[1] > 0:#节点下方还有节点则可以扩展下方节点
neighbors.append((node[0],node[1]-1))
if node[0] < self.blocks_type.shape[0]-1: #节点右方还有节点则可以扩展右方节点
neighbors.append((node[0]+1,node[1]))
if node[0] > 0:#节点左方还有节点则可以扩展左方节点
neighbors.append((node[0]-1,node[1]))
return neighbors
这样在执行完广度优先搜索算法后,我们就可以知道两个元素之间有没有连接路径。
三、调用连接路径判断
我们可以在第二次点击事件时搜索连接路径。代码如下:
def mouseClick(self,x,y):
i = (y-self.start_top)//self.block_height
j = (x-self.start_left)//self.block_width
if i<0 or i>self.row-1 or j<0 or j >self.col-1 or self.blocks_type[i][j] == 0:
return False
if self.click_num %2 == 1 and self.click_list[0][0] == i and self.click_list[0][1] == j:
return False
self.click_num += 1
self.click_list.append([i,j])
if self.click_num % 2 == 0:
if self.blocks_type[self.click_list[0][0]][self.click_list[0][1]] == self.blocks_type[self.click_list[1][0]][self.click_list[1][1]] and self.breadth_first_search((self.click_list[0][0],self.click_list[0][1]),(self.click_list[1][0],self.click_list[1][1])):
self.blocks[self.click_list[0][0]][self.click_list[0][1]].type = TYPE_RECT
self.blocks[self.click_list[0][0]][self.click_list[0][1]].color = (255,255,255)
self.blocks[self.click_list[0][0]][self.click_list[0][1]].draw()#进行重渲染
self.blocks[self.click_list[1][0]][self.click_list[1][1]].type = TYPE_RECT
self.blocks[self.click_list[1][0]][self.click_list[1][1]].color = (255,255,255)
self.blocks[self.click_list[1][0]][self.click_list[1][1]].draw()#进行重渲染
self.blocks_type[self.click_list[0][0]][self.click_list[0][1]] = 0
self.blocks_type[self.click_list[1][0]][self.click_list[1][1]] = 0
self.click_list = list()
效果图:
这样我们就完成了游戏的基本逻辑。
四、完整代码
完整代码我会传到资源上,大家有需要可以在资源上免费获得,进行进一步开发。
总结
至此,我们的python小游戏开心消消乐/连连看就已经完成了,大家如果感兴趣可以自行下载我上传的资源,可以在此基础上进行二次开发,可以发出来一起分享各自的成果一起交流。