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]] 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()
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

怪蜀黍客栈
- 粉丝: 175
最新资源
- 互联网+小学英语作业的初探.docx
- 化工行业信息化建设方案.pdf
- 太阳能光伏发电系统照明系统设计自动化专业毕业设计.doc
- ARM处理器LCD控制及触摸屏接口设计方案.doc
- 《数据库原理及应用》考试大纲.doc
- 软件项目管理—如何进行项目估算.docx
- 基于89C51单片机的数字钟方案设计书(2).doc
- 中国应用交付网络市场分析报告-行业竞争现状与前景评估预测.docx
- 分层互动教学模式在中职计算机应用基础课程中的探究.docx
- 计算机科学与工程项目个人简历.doc
- 软件工程课后习题答案.doc
- authorware课程设计方案5.doc
- 基于计算机辅助语料库对中美研究者医学论文功能词使用的对比分析.docx
- VB-ACCESS的工资管理系统本科生.doc
- 工程项目管理材料封样要求.doc
- 基于应用型人才培养的大学计算机课程改革研究.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


