Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_maze_genX.py
- from random import shuffle, randrange
- tk = 1
- try:
- from Tkinter import *
- except:
- try:
- from tkinter import *
- except:
- tk = 0
- import math
- import random
- import time
- def make_maze(w=25, h=18):
- vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
- ver = [['| '] * w + ['|.'] for _ in range(h)] + [[]]
- hor = [['+-'] * w + ['+.'] for _ in range(h + 1)]
- def walk(x, y):
- vis[y][x] = 1
- d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
- shuffle(d)
- for (xx, yy) in d:
- if vis[yy][xx]: continue
- if xx == x: hor[max(y, yy)][x] = '+ '
- if yy == y: ver[y][max(x, xx)] = ' '
- walk(xx, yy)
- walk(randrange(w), randrange(h))
- s = ""
- for (a, b) in zip(hor, ver):
- s += ''.join(a + ['\n'] + b + ['\n'])
- 0
- if tk:
- canvas.delete('all')
- xy = s[:s.rfind('.')]
- color = '#%02x%02x%02x' % (180, 0, 180)
- x = 25
- y = 30
- sq = 30
- while xy:
- t,xy = xy[:2],xy[2:]
- if t == '+-':
- canvas.create_line((x,y,x+sq,y), fill=color)
- elif '.' in t:
- if '+' in t:
- canvas.create_line((x,y,x,y+sq), fill=color)
- x = 25
- if '+' in t:
- canvas.create_line((x,y,x,y+sq), fill=color)
- y += sq
- x -= sq
- elif ' |' in t:
- canvas.create_line((x,y-sq,x,y), fill=color)
- x += sq
- canvas.update()
- print s
- 0
- if tk:
- root = Tk()
- c_width = 800
- c_height = 600
- canvas = Canvas(root, width=c_width, height=c_height, bg='white')
- canvas.pack()
- root.bind("<space>", lambda x: make_maze(randrange(9,25),randrange(9,18)))
- make_maze()
- 0
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement