当前位置: 首页>>代码示例>>Python>>正文


Python Maze.generate方法代码示例

本文整理汇总了Python中maze.Maze.generate方法的典型用法代码示例。如果您正苦于以下问题:Python Maze.generate方法的具体用法?Python Maze.generate怎么用?Python Maze.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在maze.Maze的用法示例。


在下文中一共展示了Maze.generate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from maze import Maze [as 别名]
# 或者: from maze.Maze import generate [as 别名]
class Gui:

    def __init__(self, rows, cols):
        pygame.init()

        self.rows = rows
        self.cols = cols

        self.maze = Maze(self.rows, self.cols)
        self.maze.generate()
        self.screen = pygame.display.set_mode((self.cols * SIZE_CELL, self.rows*SIZE_CELL))

        pygame.display.set_caption("OnlineByrinth")

        self.show_solution = False

        self.quit = False

    def update(self):
        return

    def draw(self):
        self.screen.fill((WHITE))
        for pos, cell in self.maze.grid.items():
            row, col = pos
            row, col = row * SIZE_CELL, col * SIZE_CELL
            if cell.n_wall:
                pygame.draw.line(self.screen, RED, (col, row), (col + SIZE_CELL, row))
            if cell.s_wall:
                pygame.draw.line(self.screen, RED, (col, row + SIZE_CELL), (col + SIZE_CELL, row + SIZE_CELL))
            if cell.w_wall:
                pygame.draw.line(self.screen, RED, (col, row), (col, row + SIZE_CELL))
            if cell.e_wall:
                pygame.draw.line(self.screen, RED, (col + SIZE_CELL, row), (col + SIZE_CELL, row + SIZE_CELL))

        # Bordure sud et est
        pygame.draw.line(self.screen, RED, (0, self.rows * SIZE_CELL - 1), (self.cols * SIZE_CELL, self.rows * SIZE_CELL - 1))
        pygame.draw.line(self.screen, RED, (self.cols * SIZE_CELL - 1, self.rows), (self.cols * SIZE_CELL - 1, self.rows * SIZE_CELL - 1))

        if self.show_solution:
            for cell in self.maze.path:
                row, col = cell.row * SIZE_CELL, cell.col * SIZE_CELL
                pygame.draw.ellipse(self.screen, GREEN, pygame.Rect(col + SIZE_CELL / 4, row + SIZE_CELL / 4, SIZE_CELL / 2, SIZE_CELL / 2))

        pygame.display.flip()

    def main_loop(self):
        while not self.quit:
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.quit = True
                if event.type == KEYDOWN and event.key == pygame.K_SPACE:
                    self.maze.generate()
                if event.type == KEYDOWN and event.key == pygame.K_s:
                    self.show_solution = not self.show_solution
            self.update()
            self.draw()
开发者ID:Mulix,项目名称:labyrinthe,代码行数:59,代码来源:gui.py

示例2: build

# 需要导入模块: from maze import Maze [as 别名]
# 或者: from maze.Maze import generate [as 别名]
    def build(self):

        material = self.material
        min_x, max_x = self.mc.min_x, self.mc.max_x
        min_z, max_z = self.mc.min_z, self.mc.max_z

        # Don't forget the zero block! 1 - -1 == 2, but it is 3 blocks!
        x_width = max_x - min_x + 1
        z_width = max_z - min_z + 1

        c_dim, c_height = 6, 3  # in world block units

        # in labyrinth cell units (remember walls overlap, need exact fit + 1 for outermost wall)
        x_cells, z_cells = (x_width - 1) / (c_dim - 1), (z_width - 1) / (c_dim - 1)
        castle_cells = 13

        maze = Maze(z_cells, x_cells)

        # Disconnect cells where castle resides, ensures compatible maze
        blk_x, blk_z = (x_cells - castle_cells) / 2, (z_cells - castle_cells) / 2
        for bx in xrange(blk_x, blk_x + castle_cells):
            for bz in xrange(blk_z, blk_z + castle_cells):
                maze[bz, bx].disconnect()

        maze.generate()

        # Remove the wall from graph where the labyrinth will meet with castle
        maze[blk_z + castle_cells / 2, blk_x + castle_cells].remove_wall(NORTH)

        # Render the labyrinth by generating blocks where there are walls
        for cell_x in xrange(0, x_cells):
            for cell_z in xrange(0, z_cells):
                walls = maze[cell_z, cell_x].walls
                north, west = min_x + cell_x * (c_dim - 1), min_z + cell_z * (c_dim - 1)
                if walls[NORTH] and walls[SOUTH] and walls[EAST] and walls[WEST]:
                    self.mc.make_cube(north, 0, west, c_dim, c_height, c_dim, material)
                else:
                    if walls[NORTH]:
                        self.mc.make_cube(north, 0, west, 1, c_height, c_dim, material)
                    if walls[SOUTH]:
                        self.mc.make_cube(north + (c_dim - 1), 0, west, 1, c_height, c_dim, material)
                    if walls[EAST]:
                        self.mc.make_cube(north, 0, west + (c_dim - 1), c_dim, c_height, 1, material)
                    if walls[WEST]:
                        self.mc.make_cube(north, 0, west, c_dim, c_height, 1, material)
开发者ID:batkinson,项目名称:minecraft-labyrinth,代码行数:47,代码来源:minecraft_labyrinth.py

示例3: __init__

# 需要导入模块: from maze import Maze [as 别名]
# 或者: from maze.Maze import generate [as 别名]
class Game:

  def __init__(self, diff, dim, path):
    self.size = (800,600)
    self.screen = pygame.display.set_mode(self.size)
    pygame.display.set_caption('Maze Demo')

    font = pygame.font.SysFont(pygame.font.get_default_font(), 55)
    text = font.render("Loading...", 1, (255,255,255))
    rect = text.get_rect()
    rect.center = self.size[0]/2, self.size[1]/2
    self.screen.blit(text, rect)
    pygame.display.update(rect)

    self.diff = diff
    self.dim = map(int, dim.split('x'))
    self.path = path
    scalesgui.setup_calibration()
    

  def start(self):
    self.maze_obj = Maze(*self.dim)# pass args to change maze size: Maze(10, 10)
    if self.diff == 0:
      self.maze_obj.generate(self.maze_obj.maze[(0,0)])
    else:
      self.maze_obj.generate()
    self.draw_maze()
    self.reset_player()
    self.loop()

  def reset_player(self):
    # Make the sprites for the player.
    w, h = self.cell_width - 3, self.cell_height - 3
    rect = 0, 0, w, h
    base = pygame.Surface((w,h))
    base.fill( (255,255,255) )
    self.red_p = base.copy()
    self.green_p = base.copy()
    self.blue_p = base.copy()
    self.goldy = base.copy()
    if self.path == 1:
      r = (255,0,0)
      g = (0,255,0)
    else:
      r = g = (255,255,255)
    b = (0,0,255)
    gold = (0xc5,0x93,0x48)
    pygame.draw.ellipse(self.red_p, r, rect)
    pygame.draw.ellipse(self.green_p, g, rect)
    pygame.draw.ellipse(self.blue_p, b, rect)
    pygame.draw.ellipse(self.goldy, gold, rect)

    # Make a same-size matrix for the player.
    self.player_maze = {}
    for y in xrange(self.maze_obj.rows):
      for x in xrange(self.maze_obj.cols):
        cell = {'visited' : 0} # if 1, draws green. if >= 2, draws red.
        self.player_maze[(x,y)] = cell
        self.screen.blit(base, (x*self.cell_width+2, y*self.cell_height+2))

    self.screen.blit(self.goldy, (x*self.cell_width+2, y*self.cell_height+2))
    self.cx = self.cy = 0
    self.curr_cell = self.player_maze[(self.cx, self.cy)] # starts at origin

    self.last_move = None # For last move fun

  def draw_maze(self):
    self.screen.fill( (255,255,255) )
    self.cell_width = self.size[0]/self.maze_obj.cols
    self.cell_height = self.size[1]/self.maze_obj.rows

    for y in xrange(self.maze_obj.rows):
      for x in xrange(self.maze_obj.cols):
        if self.maze_obj.maze[(x, y)]['south']: # draw south wall
          pygame.draw.line(self.screen, (0,0,0), \
            (x*self.cell_width, y*self.cell_height + self.cell_height), \
            (x*self.cell_width + self.cell_width, \
            y*self.cell_height + self.cell_height) )
        if self.maze_obj.maze[(x, y)]['east']: # draw east wall
          pygame.draw.line(self.screen, (0,0,0), \
            (x*self.cell_width + self.cell_width, y*self.cell_height), \
            (x*self.cell_width + self.cell_width, y*self.cell_height + \
            self.cell_height) )
    # Screen border
    pygame.draw.rect(self.screen, (0,0,0), (0,0, self.size[0], self.size[1]), 1)
    pygame.display.update()

  def loop(self):
    self.clock = pygame.time.Clock()
    self.keep_going = 1

    while self.keep_going:
      moved = 0
      self.clock.tick(10)
      '''
      for event in pygame.event.get():
        if event.type == QUIT:
          self.keep_going = 0
        elif event.type == KEYDOWN:
          if event.key == K_ESCAPE:
#.........这里部分代码省略.........
开发者ID:duncan2006,项目名称:WiiBoard-Project,代码行数:103,代码来源:PyMaze.py

示例4: __init__

# 需要导入模块: from maze import Maze [as 别名]
# 或者: from maze.Maze import generate [as 别名]
class GameSpace:
	def __init__(self, mazesize=20):
		# game characters
		self.p1 = 'H'
		self.p2 = 'G'
		self.exit = 'E'

		# game maze
		self.maze = Maze(mazesize)
		self.boot = 1
		self.size = int()

		# game objects
		self.player = None
		self.clock = None

		# game objects
		pygame.init()
                self.exit_sprite = pygame.image.load("../images/exit.png")
                self.exit_rect = self.exit_sprite.get_rect()
                self.gameover_sprite = pygame.image.load("../images/gameover.png")
                self.gameover_rect = self.gameover_sprite.get_rect()
                self.gameover_rect = self.gameover_rect.move(SCREEN_SIZE/4, SCREEN_SIZE/4)
		self.gameoverl_sprite = pygame.image.load("../images/gameover_l.png")
                self.gameoverl_rect = self.gameoverl_sprite.get_rect()
                self.gameoverl_rect = self.gameoverl_rect.move(SCREEN_SIZE/4, SCREEN_SIZE/4)
                self.bg = pygame.image.load("../images/background.png")
                self.bg_rect = self.bg.get_rect()
		self.wait_screen = pygame.image.load("../images/wait_screen.png")
                self.wait_rect = self.wait_screen.get_rect()
                #self.bg_rect = self.bg_rect.move(SCREEN_SIZE/2, SCREEN_SIZE/2)

		self.size = self.width, self.height = SCREEN_SIZE, SCREEN_SIZE
                self.green = 0, 255, 0
		self.connected = 0

		self.opponent = [(MAZE_SIZE/2 - 1) * WALL_SIZE * SCALE, 0, "../images/ghost_down.png"]
		self.ghostImage = pygame.image.load(self.opponent[2])
		self.ghostRect = self.ghostImage.get_rect()		
		self.ghostRect.centerx = self.opponent[0]
		self.ghostRect.centery = self.opponent[1]
		
		# network
		self.ghostProtocol = None

	def setup(self):
		# generate maze
		self.maze.generate()
		size = self.maze.getSize()

		# send data to ghost
		pd = pickle.dumps(self.maze)
		self.ghostProtocol.transport.write(pd)

		# set character start positions
		self.maze.setPos(size-1, size/2-1, self.p1)	# human
		self.maze.setPos(0, size/2-1, self.p2)	   	# ghost

		# set exit (try middle if path, otherwise on edge)
		if self.maze.getPos(size/2-1, size/2-1) == self.maze.path:
			self.maze.setPos(size/2-1, size/2-1, self.exit)
		else:
			side = randint(0,1)
			i = 0

			if side == 0:
				# set on left side
				while i < size:
					if self.maze.getPos(i, 0) == self.maze.path:
						self.maze.setPos(i, 0, self.exit)
						break
					i += 1
			else:
				# set on right side
				while i < self.size:
					if self.maze.getPos(i, size-1) == self.maze.path:
						self.maze.setPos(i, size-1, self.exit)
						break
					i += 1

		#self.maze.display()

		# reset data
		self.walls = []
		self.game_over = 0
		self.lose = 0
		self.break_flag = 0

		# music 
                pygame.mixer.music.load("../music/horror.wav")
                pygame.mixer.music.play(-1, 0.0)
		return

	def main(self):
		# establish connection with ghost
		ghostFactory = GhostFactory(self)
		reactor.listenTCP(GHOST_PORT, ghostFactory)

		# Initialize screen window
		pygame.display.set_caption("PATH")
#.........这里部分代码省略.........
开发者ID:ylin6,项目名称:thePath,代码行数:103,代码来源:human.py


注:本文中的maze.Maze.generate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。