I've been trying to run escape.py in Visual Studio Code and as usual had a few teething troubles setting up the environment. I found what I guess is an error (this is my first time playing with python) which is in the routine draw_squares

print number rather than print(number)

I've also had problems with getting the windows version of python 3.7.1 to properly understand method references here and there. I've also added breaks into the draw_squares and draw_triangles routines so they terminate when leaving the paper bad. Anyhow, I've copied my own version of the code below. I must admit I've made all the lines drawn by turtles as I like turtles!

Best wishes, Mark Lawrence

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import argparse

import inspect

import random

import pickle

import turtle

def draw_bag():

bag = turtle.Turtle()

bag.pen(pencolor='brown', pensize=5)

bag.penup()

bag.goto(-35, 35)

bag.pendown()

bag.right(90)

bag.forward(70)

bag.left(90)

bag.forward(70)

bag.left(90)

bag.forward(70)

bag.hideturtle()

def draw_line():

angle = 0

step = 5

t = turtle.Turtle()

while not escaped(t.position()):

t.left(angle)

t.forward(step)

def draw_square(t, size):

L = []

for i in range(4):

t.forward(size)

t.left(90)

store_position_data(L, t)

return L

def draw_squares(number):

print(number)

t = turtle.Turtle()

t.shape('turtle')

t.pen(pencolor='red', pensize=2)

L = []

for i in range(1, number + 1):

t.penup()

t.goto(-i, -i)

t.pendown()

L.extend(draw_square(t, i * 2))

if escaped(t.position()):

break

return L

def draw_triangles(number):

t = turtle.Turtle()

t.shape('turtle')

t.pen(pencolor='blue', pensize=2)

for i in range(1, number):

t.forward(i*10)

t.right(120)

if escaped(t.position()):

break

def escaped(position):

x = int(position[0])

y = int(position[1])

return x < -35 or x > 35 or y < -35 or y > 35

def store_position_data(L, t):

position = t.position()

L.append([position[0], position[1], escaped(position)])

def draw_spirals_until_escaped():

t = turtle.Turtle()

t.shape('turtle')

t.pen(pencolor='green', pensize=2)

t.penup()

t.left(random.randint(0, 360))

t.pendown()

i = 0

turn = 360/random.randint(1, 10)

L = []

store_position_data(L, t)

while not escaped(t.position()):

i += 1

t.forward(i*5)

t.right(turn)

store_position_data(L, t)

return L

def draw_squares_until_escaped(n):

t = turtle.Turtle()

L = draw_squares(n)

with open("data_square", "wb") as f:

pickle.dump(L, f)

def draw_random_spirangles():

L = []

for i in range (10):

L.extend(draw_spirals_until_escaped())

with open("data_rand", "wb") as f:

pickle.dump(L, f)

if __name__ == '__main__':

fns = {"line": draw_line,

"squares": draw_squares_until_escaped,

"triangles": draw_triangles,

"spirangles" : draw_random_spirangles}

parser = argparse.ArgumentParser()

parser.add_argument("-f", "--function",

choices = fns,

help="One of " + ', '.join(fns.keys()))

parser.add_argument("-n", "--number",

default = 50,

type=int, help="How many?")

args = parser.parse_args()

try:

f = fns[args.function]

turtle.setworldcoordinates(-70., -70., 70., 70.)

draw_bag()

if len(inspect.getargspec(f).args)==1:

f(args.number)

else:

f()

turtle.mainloop()

except KeyError:

parser.print_help()

--Mark Lawrence