"Reprint" Python Four maze generation algorithm (5): graphic display to achieve

Respect the original: Python maze generation algorithm: graphical display to achieve


Info maze graphics
code that describes
GameSearch.py
complete code


Maze Graphic Info


Pygame made using four kinds of a maze algorithm graphical display, as shown, there are four buttons, clicking with the mouse button to switch algorithm which generates a labyrinth. Press any key to generate a maze, while adding the A * pathfinding algorithm, to represent four different algorithms to generate maze, red dot is the starting point and end point is the green line using A * algorithm obtained from the beginning to the end.


Code Description
code implementation has four files

GameMap.py: Save map type basic information labyrinth, a note in front of the maze algorithm introduced in a separate side into one file.
MazeGenerator.py: This is the code that implements four kinds maze algorithm, in a single file.
AStarSearch.py: A * algorithm to achieve, there is introduced in front of the A * algorithm article.
GameSearch.py: pygame code written using a graphical display of the maze
in front of three documents have been said here, the main speaker at GameSearch.py


GameSearch.py


Labyrinth configuration parameters, the size of each unit maze REC_SIZE, REC_WIDTH and REC_HEIGHT are the width and length of the labyrinth, can be modified, but the installation requirements maze generation algorithm, it must be odd.

REC_SIZE = 10
REC_WIDTH = 51 # must be odd number
REC_HEIGHT = 51 # must be odd number
BUTTON_HEIGHT = 30
BUTTON_WIDTH = 120
SCREEN_WIDTH = REC_WIDTH * REC_SIZE
SCREEN_HEIGHT = REC_HEIGHT * REC_SIZE + BUTTON_HEIGHT


Algorithm button, __init__ function to set the location and size of a button, the color changes and unclick the click function to set the click of a button.

class Button():
	def __init__(self, screen, type, x, y):
		self.screen = screen
		self.width = BUTTON_WIDTH
		self.height = BUTTON_HEIGHT
		self.button_color = (128,128,128)
		self.text_color = [(0,255,0), (255,0,0)]
		self.font = pygame.font.SysFont(None, BUTTON_HEIGHT*2//3)
		
		self.rect = pygame.Rect(0, 0, self.width, self.height)
		self.rect.topleft = (x, y)
		self.type = type
		self.init_msg()
		
	def init_msg(self):
		self.msg_image = self.font.render(generator_types[self.type], True, self.text_color[0], self.button_color)
		self.msg_image_rect = self.msg_image.get_rect()
		self.msg_image_rect.center = self.rect.center
		
	def draw(self):
		self.screen.fill(self.button_color, self.rect)
		self.screen.blit(self.msg_image, self.msg_image_rect)
	
	def click(self, game):
		game.maze_type = self.type
		self.msg_image = self.font.render(generator_types[self.type], True, self.text_color[1], self.button_color)
	
	def unclick(self):
		self.msg_image = self.font.render(generator_types[self.type], True, self.text_color[0], self.button_color)


Game category, __init__ function initializes the class map, Prim algorithm randomly selected by default, and add algorithm button. generateMaze each function performs a key input operation, the loop executes four operations (1 maze generation, 2. generate the start and end 3. A * algorithm with the generated path, 4. Remove maze). check_buttons function algorithm maze algorithm is set for the selected button.

class Game():
	def __init__(self):
		pygame.init()
		self.screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
		self.clock = pygame.time.Clock()
		self.map = Map(REC_WIDTH, REC_HEIGHT)
		self.mode = 0
		self.maze_type = MAZE_GENERATOR_TYPE.RANDOM_PRIM
		self.buttons = []
		self.buttons.append(Button(self.screen, MAZE_GENERATOR_TYPE.RECURSIVE_BACKTRACKER, 0, 0))
		self.buttons.append(Button(self.screen, MAZE_GENERATOR_TYPE.RANDOM_PRIM, BUTTON_WIDTH + 10, 0))
		self.buttons.append(Button(self.screen, MAZE_GENERATOR_TYPE.RECURSIVE_DIVISION, (BUTTON_WIDTH + 10) * 2, 0))
		self.buttons.append(Button(self.screen, MAZE_GENERATOR_TYPE.UNION_FIND_SET, (BUTTON_WIDTH + 10) * 3, 0))
		self.buttons[0].click(self)

	def play(self):
		self.clock.tick(30)
		
		pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(0, 0, SCREEN_WIDTH, BUTTON_HEIGHT))
		for button in self.buttons:
			button.draw()

		for y in range(self.map.height):
			for x in range(self.map.width):
				type = self.map.getType(x, y)
				if type == MAP_ENTRY_TYPE.MAP_EMPTY:
					color = (255, 255, 255)
				elif type == MAP_ENTRY_TYPE.MAP_BLOCK:
					color = (0, 0, 0)
				elif type == MAP_ENTRY_TYPE.MAP_TARGET:
					color = (255, 0, 0)
				else:
					color = (0, 255, 0)
				pygame.draw.rect(self.screen, color, pygame.Rect(REC_SIZE*x, REC_SIZE*y+BUTTON_HEIGHT, REC_SIZE, REC_SIZE))
		
	def generateMaze(self):
		if self.mode >= 4:
			self.mode = 0
		if self.mode == 0:
			generateMap(self.map, self.maze_type)
		elif self.mode == 1:
			self.source = self.map.generatePos((1,1),(1, self.map.height-2))
			self.dest = self.map.generatePos((self.map.width-2, self.map.width-2), (1, self.map.height-2))
			self.map.setMap(self.source[0], self.source[1], MAP_ENTRY_TYPE.MAP_TARGET)
			self.map.setMap(self.dest[0], self.dest[1], MAP_ENTRY_TYPE.MAP_TARGET)
		elif self.mode == 2:
			AStarSearch(self.map, self.source, self.dest)
			self.map.setMap(self.source[0], self.source[1], MAP_ENTRY_TYPE.MAP_TARGET)
			self.map.setMap(self.dest[0], self.dest[1], MAP_ENTRY_TYPE.MAP_TARGET)
		else:
			self.map.resetMap(MAP_ENTRY_TYPE.MAP_EMPTY)
		self.mode += 1

def check_buttons(game, mouse_x, mouse_y):
	for button in game.buttons:
		if button.rect.collidepoint(mouse_x, mouse_y):
			button.click(game)
			for tmp in game.buttons:
				if tmp != button:
					tmp.unclick()
			break			

            
 

The main loop, access to the keyboard and mouse click events, this is mainly related to the pygame code.

game = Game()
while True:
	game.play()
	pygame.display.update()
	
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			exit()
		if event.type == pygame.KEYDOWN:
			game.generateMaze()
			break
		elif event.type == pygame.MOUSEBUTTONDOWN:
			mouse_x, mouse_y = pygame.mouse.get_pos()
			check_buttons(game, mouse_x, mouse_y)

The complete code

Complete implementation of the code github link  maze algorithm graphic display

Compiler environment
python3.7 + pygame1.9

Published 91 original articles · won praise 47 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_30007885/article/details/104447550