#! /usr/bin/env python """Setup the maze so it could be drawn in the screen @license: U{http://creativecommons.org/licenses/by-sa/3.0/us/} @var FILLED: whether or nott the filled wall images are used """ import pygame from maze import Maze FILLED = True # Determines the set of maze wall images used, filled or unfilled class mazeSetup: """ Interprets the maze object made in maze.py, draws it all on the screen """ def __init__ (self, screen, MAZE_SIZE): """ Initialize the maze setup @param screen: the screen in which we will draw @param MAZE_SIZE: the size of the maze """ #Create the maze self.maze = Maze(MAZE_SIZE, MAZE_SIZE) # compute the size of the tiles given the screen size, etc. size = screen.get_size() self.tileSizeX = (625 / MAZE_SIZE) self.tileSizeY = (625 / MAZE_SIZE) #call the draw function self.drawMaze(screen, MAZE_SIZE) def drawPoint(self, screen, x, y): """ Draw a certain point of the maze on the screen @param screen: the screen to draw in @param x: the x coordinate of the maze map @param y: the y coordinate of the maze map """ rect = pygame.Rect(x*self.tileSizeX,y*self.tileSizeY, self.tileSizeX, self.tileSizeY) tile = self.maze.map[x][y] if tile == self.maze.EMPTY: #0 pygame.draw.rect(screen, (0, 0, 0), rect, 0) elif tile == self.maze.SOLID: #1 image = self.getWallImage(x,y) if image == False: pygame.draw.rect(screen, (0,0,0) , rect, 0) else: screen.blit( image, rect ) elif tile == self.maze.OFFL: #2 pygame.draw.rect(screen, (0,0,0), rect, 0) elif tile == self.maze.PELLET: #3 pygame.draw.rect(screen, (0, 0, 0), rect, 0) dot = rect.inflate(-self.tileSizeX/1.25, -self.tileSizeY/1.25) pygame.draw.ellipse(screen, (255, 255, 0), dot, 0) elif tile == self.maze.PPL: #4 pygame.draw.rect(screen, (0, 0, 0), rect, 0) dot = rect.inflate(-self.tileSizeX/2, -self.tileSizeY/2) pygame.draw.ellipse(screen, (94,246,0), dot, 0) elif tile == self.maze.PELLET_INT: #5 pygame.draw.rect(screen, (0, 0, 0), rect, 0) dot = rect.inflate(-self.tileSizeX/1.25, -self.tileSizeY/1.25) pygame.draw.ellipse(screen, (255, 255, 0), dot, 0) elif tile == self.maze.EMPTY_INT: #6 pygame.draw.rect(screen, (0, 0, 0), rect, 0) else: #same as off limits pygame.draw.rect(screen, (0,0,0), rect, 0) def drawMaze(self, screen, MAZE_SIZE): """ Draw the whole maze @param screen: the screen to draw into @param MAZE_SIZE: the maze size """ screen.fill((0,0,0)) #black screen for a in range(0, MAZE_SIZE): for b in range(0, MAZE_SIZE): self.drawPoint(screen, a, b) def getWallImage(self, x, y): """ Choses the correct image for wall positions based on the surrounding plots @param x: x coordinate @param y: y coordinate @return a converted image """ file = 'EMPTY' sides = [ self.maze.getPoint(x,y-1) == self.maze.SOLID, # top self.maze.getPoint(x+1,y) == self.maze.SOLID, # right self.maze.getPoint(x,y+1) == self.maze.SOLID, # bottom self.maze.getPoint(x-1,y) == self.maze.SOLID ] # left if sides == [0,0,0,0]: # draw box print "We need a box!" elif sides == [0,0,0,1]: # draw 180 right file = './images/25_c_r' elif sides == [0,0,1,0]: # draw 180 up file = './images/25_c_u' elif sides == [0,0,1,1]: # draw 90 top right file = './images/50_c_rt' elif sides == [0,1,0,0]: # draw 180 left file = './images/25_c_l' elif sides == [0,1,0,1]: # draw parallel left right file = './images/25_lines_lr' elif sides == [0,1,1,0]: # draw 90 top left file = './images/50_c_lt' elif sides == [0,1,1,1]: # draw line top file = './images/50_top' elif sides == [1,0,0,0]: # draw 180 bottom file = './images/25_c_d' elif sides == [1,0,0,1]: # draw 90 bottom right file = './images/50_c_rb' elif sides == [1,0,1,0]: # draw parallel top bottom file = './images/25_lines_ud' elif sides == [1,0,1,1]: # draw line right file = './images/50_right' elif sides == [1,1,0,0]: # draw 90 bottom left file = './images/50_c_lb' elif sides == [1,1,0,1]: # draw line bottom file = './images/50_bottom' elif sides == [1,1,1,0]: # draw line left file = './images/50_left' elif sides == [1,1,1,1]: # draw empty file = 'EMPTY' else: print "MAZE FAIL" if FILLED == True: # uses the filled image blocks instead if file == 'EMPTY': file = './images/25_box' file += '_filled' if not file == 'EMPTY': # empty is treated as 'use a black box' return pygame.image.load(file+'.png').convert_alpha() else: return False