-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowmaze.py
58 lines (49 loc) · 1.91 KB
/
showmaze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from maze import Maze
import turtle
import sys
if __name__ == '__main__':
'''
This function uses Python's turtle library to draw a picture of the maze
given as an argument when running the script.
'''
# Create a maze based on input argument on command line.
testmaze = Maze( str(sys.argv[1]) )
# Intialize the window and drawing turtle.
window = turtle.Screen()
wally = turtle.Turtle()
wally.speed(0)
wally.hideturtle()
wally.penup()
# maze centered on (0,0), squares are 20 units in length.
sq_size = 20
origin = testmaze.dim * sq_size / -2
# iterate through squares one by one to decide where to draw walls
for x in range(testmaze.dim):
for y in range(testmaze.dim):
if not testmaze.is_permissible([x,y], 'up'):
wally.goto(origin + sq_size * x, origin + sq_size * (y+1))
wally.setheading(0)
wally.pendown()
wally.forward(sq_size)
wally.penup()
if not testmaze.is_permissible([x,y], 'right'):
wally.goto(origin + sq_size * (x+1), origin + sq_size * y)
wally.setheading(90)
wally.pendown()
wally.forward(sq_size)
wally.penup()
# only check bottom wall if on lowest row
if y == 0 and not testmaze.is_permissible([x,y], 'down'):
wally.goto(origin + sq_size * x, origin)
wally.setheading(0)
wally.pendown()
wally.forward(sq_size)
wally.penup()
# only check left wall if on leftmost column
if x == 0 and not testmaze.is_permissible([x,y], 'left'):
wally.goto(origin, origin + sq_size * y)
wally.setheading(90)
wally.pendown()
wally.forward(sq_size)
wally.penup()
window.exitonclick()