200行python实现一个迷宫

代码如下:

#coding=utf-8
import random, sys, time
from Tkinter import *
 
def timing(out_prefix, func, *args, **kwargs):
  print("%s:timing start..."%(out_prefix))
  t1 = time.time()
  r = func(*args, **kwargs)
  t2 = time.time()
  print("%s:%ss"%(out_prefix, t2 - t1))
  return r

class UI:
  def __init__(self, maze):
    self.maze = maze
    self.w, self.b = 1, 2
    self.root, self.canvas = None, None

  def output_cell(self, clac_func):
    self.root = Tk()
    self.root.title('MAZE')
    self.canvas = Canvas(self.root, width=self.maze.width*self.w*4+self.w+self.b*2-4, height=self.maze.heigh*self.w*4+self.w+self.b*2-4)
    self.canvas.create_rectangle(self.b, self.b, self.maze.width*self.w*4+self.w+self.b, self.b+self.w, fill='dimgray', width = 0)
    self.canvas.create_rectangle(self.b, self.b, self.b+self.w, self.maze.heigh*self.w*4+self.w+self.b, fill='dimgray', width = 0)
    for y in range(self.maze.heigh):
      for x in range(self.maze.width):
        if not self.maze.cells[x][y].is_open(Cell.EDGE_RIGHT):
          self.canvas.create_rectangle(self.b+(x+1)*self.w*4, self.b+y*self.w*4, self.b+(x+1)*self.w*4+self.w, self.b+y*self.w*4+self.w*5, fill='dimgray', width = 0)
        if not self.maze.cells[x][y].is_open(Cell.EDGE_BOTTOM):
          self.canvas.create_rectangle(self.b+x*self.w*4, self.b+(y+1)*self.w*4, self.b+x*self.w*4+self.w*5, self.b+(y+1)*self.w*4+self.w, fill='dimgray', width = 0)
    self.canvas.pack()
    self.root.bind('<Button-1>', clac_func)
    self.root.mainloop()

  def output_path(self, paths, entrance, exit):
    self.canvas.create_rectangle(self.b+entrance[0]*self.w*4+self.w, self.b+entrance[1]*self.w*4+self.w, self.b+entrance[0]*self.w*4+self.w+
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值