Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_pseudo_maze2.py
- from random import shuffle, randrange
- import msvcrt
- def kbfunc():
- x = msvcrt.kbhit()
- if x:
- ret = msvcrt.getch()
- else:
- ret = False
- return ret
- tk = 1
- try:
- from Tkinter import *
- except:
- try:
- from tkinter import *
- except:
- tk = 0
- def keyup(e):
- print 'up', e.char
- def keydown(e):
- print 'down', e.char
- 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)
- c = []
- lines = []
- x = 25
- y = 25
- sq = 30
- d = {}
- while xy:
- d[x/sq,y/sq] = '.'
- t,xy = xy[:2],xy[2:]
- z = 'X'
- if t == '+-':
- lines += [(x,y,x+sq,y)]
- d[x/sq,y/sq] = '0'
- z = 0
- elif '.' in t:
- if '+' in t:
- lines += [(x,y,x,y+sq)]
- x = 25
- if '+' in t:
- lines += [(x,y,x,y+sq)]
- y += sq
- x -= sq
- z = 0
- elif '|' in t:
- lines += [(x,y-sq,x,y)]
- z = 'O'
- if z and [x,y] not in c:
- y2 = y-sq/2
- x2 = x-sq/2
- c.append([x,y])
- if z == 'X':
- lines += [(x2,y2,x2+sq,y2)]
- prev = 0
- x += sq
- for y in range(h):
- for x in range(w):
- x2 = (x*sq)+(sq/2)+25
- y2 = (y*sq)-(sq/2)+25
- if d[(x,y)] == '.':
- lines += [(x2,y2,x2,y2+sq)]
- for line in lines:
- x,y,x2,y2 = line
- canvas.create_line((x,y+20,x2,y2+20), fill=color)
- sss = 'Press Spacebar or Z to Randomize'
- canvas.create_text((120,20), text=sss)
- canvas.update()
- 0
- if tk:
- root = Tk()
- c_width = 1320
- c_height = 700
- canvas = Canvas(root, width=c_width, height=c_height, bg='white')
- canvas.pack()
- root.bind("<z>", lambda x: make_maze(randrange(9,42),randrange(9,20)))
- root.bind("<space>", lambda x: make_maze(42,20))
- make_maze(42,20)
- 0
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement