import turtle
def update_maps(graph, turtle, color):
graph[turtle_pos(turtle)] = color
return None
def turtle_pos(turtle):
return (round(turtle.xcor()), round(turtle.ycor()))
def langton_move(turtle, pos, maps, step):
if pos not in maps or maps[pos] == "white":
turtle.fillcolor("black")
turtle.stamp()
update_maps(maps, turtle, "black")
turtle.right(90)
turtle.forward(step)
pos = turtle_pos(turtle)
elif maps[pos] == "black":
turtle.fillcolor("white")
update_maps(maps, turtle, "white")
turtle.stamp()
turtle.left(90)
turtle.forward(step)
pos = turtle_pos(turtle)
return pos
def move(movements,trace):
# Screen
BACKGROUND_COLOR = "white"
WIDTH = 2000
HEIGHT = 2000
# Ant
SHAPE = "square"
SHAPE_SIZE = 0.5
SPEED = -1
STEP = 11
# Data structure ex. {(x, y): "black", ..., (x_n, y_n): "white"}
maps = {}
# Initialize Window
window = turtle.Screen()
window.bgcolor(BACKGROUND_COLOR)
window.screensize(WIDTH, HEIGHT)
window.tracer(trace)
# Initialize Ant
ant = turtle.Turtle()
ant.shape(SHAPE)
ant.shapesize(SHAPE_SIZE)
ant.penup()
ant.speed(SPEED)
ant.right(180)
pos = turtle_pos(ant)
moves = 0
while moves<movements:
pos = langton_move(ant, pos, maps, STEP)
moves += 1
window.exitonclick()
while True:
option = int(input("hey"))
if option == 1:
move(11000, False)
if option == 2:
move(100, True)
the code above works and it will display langtons ant and i can keep pressing option 1 forever and it will work, it will also work if option 2 (trace is on) is first and then i go on to press option 1 and display langtons ant, however after pressing option 1 even only once, if i try to press option 2 it will raise a turtle.Terminator Error, which means that option 2 only works if its pressed first, and no times after that, please help me as i am very stuck.