﻿

# # Tk_pseudo_maze2.py

Nov 24th, 2020
567
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # Tk_pseudo_maze2.py
2.
3. from random import shuffle, randrange
4.
5. import msvcrt
6.
7. def kbfunc():
8.     x = msvcrt.kbhit()
9.     if x:
10.         ret = msvcrt.getch()
11.     else:
12.         ret = False
13.     return ret
14.
15. tk = 1
16. try:
17.     from Tkinter import *
18. except:
19.     try:
20.         from tkinter import *
21.     except:
22.         tk = 0
23.
24. def keyup(e):
25.     print 'up', e.char
26. def keydown(e):
27.     print 'down', e.char
28.
29. import math
30. import random
31. import time
32.
33. def make_maze(w=25, h=18):
34.     vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
35.     ver = [['| '] * w + ['|.'] for _ in range(h)] + [[]]
36.     hor = [['+-'] * w + ['+.'] for _ in range(h + 1)]
37.
38.     def walk(x, y):
39.         vis[y][x] = 1
40.
41.         d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
42.         shuffle(d)
43.         for (xx, yy) in d:
44.             if vis[yy][xx]: continue
45.             if xx == x: hor[max(y, yy)][x] = '+ '
46.             if yy == y: ver[y][max(x, xx)] = '  '
47.             walk(xx, yy)
48.
49.     walk(randrange(w), randrange(h))
50.
51.     s = ""
52.     for (a, b) in zip(hor, ver):
53.         s += ''.join(a + ['\n'] + b + ['\n'])
54.     0
55.     if tk:
56.         canvas.delete('all')
57.         xy = s[:s.rfind('.')]
58.         color = '#%02x%02x%02x' % (180, 0, 180)
59.         c = []
60.         lines = []
61.         x = 25
62.         y = 25
63.         sq = 30
64.         d = {}
65.         while xy:
66.             d[x/sq,y/sq] = '.'
67.             t,xy = xy[:2],xy[2:]
68.             z = 'X'
69.             if t == '+-':
70.                 lines += [(x,y,x+sq,y)]
71.                 d[x/sq,y/sq] = '0'
72.                 z = 0
73.             elif '.' in t:
74.                 if '+' in t:
75.                     lines += [(x,y,x,y+sq)]
76.                 x = 25
77.                 if '+' in t:
78.                     lines += [(x,y,x,y+sq)]
79.                     y += sq
80.                 x -= sq
81.                 z = 0
82.             elif '|' in t:
83.                 lines += [(x,y-sq,x,y)]
84.                 z = 'O'
85.             if z and [x,y] not in c:
86.                 y2 = y-sq/2
87.                 x2 = x-sq/2
88.                 c.append([x,y])
89.                 if z == 'X':
90.                     lines += [(x2,y2,x2+sq,y2)]
91.                 prev = 0
92.             x += sq
93.
94.     for y in range(h):
95.         for x in range(w):
96.             x2 = (x*sq)+(sq/2)+25
97.             y2 = (y*sq)-(sq/2)+25
98.             if d[(x,y)] == '.':
99.                 lines += [(x2,y2,x2,y2+sq)]
100.     for line in lines:
101.         x,y,x2,y2 = line
102.         canvas.create_line((x,y+20,x2,y2+20), fill=color)
103.     sss = 'Press Spacebar or Z to Randomize'
104.     canvas.create_text((120,20), text=sss)
105.     canvas.update()
106. 0
107.
108. if tk:
109.     root = Tk()
110.     c_width = 1320
111.     c_height = 700
112.     canvas = Canvas(root, width=c_width, height=c_height, bg='white')
113.     canvas.pack()
114.     root.bind("<z>", lambda x: make_maze(randrange(9,42),randrange(9,20)))
115.     root.bind("<space>", lambda x: make_maze(42,20))
116.
117. make_maze(42,20)
118. 0
119. root.mainloop()
RAW Paste Data