0

I am trying to write a python program of two turtles chasing another turtle. yet the code crashes a few seconds into the game giving me an "Exception in Tkinter callback"

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 1885, in call return self.func(*args) File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 806, in callit func(*args) File "D:\ZC-CSCI 101\pythonProject\myturtle.py", line 41, in follow_runner follow.setheading(follow.towards(run)) File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 1937, in setheading self._rotate(angle) File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3279, in _rotate self._update() File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2662, in _update self._drawturtle() File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3009, in _drawturtle shape = self._polytrafo(self._getshapepoly(tshape)) File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2961, in _polytrafo e0, e1 = (1.0 / abs(e)) * e File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 260, in rmul return Vec2D(self[0]*other, self[1]*other) File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 251, in new return tuple.new(cls, (x, y)) RecursionError: maximum recursion depth exceeded while calling a Python object Fatal Python error: _Py_CheckRecursiveCall: Cannot recover from stack overflow. Python runtime state: initialized

 from turtle import Turtle, Screen
playGround = Screen()
playGround.screensize(500, 500)
playGround.title("Turtle Keys")

run = Turtle("turtle")
run.speed("fastest")
run.color("blue")
run.penup()
run.setposition(250, 250)

follow2 = Turtle("turtle")
follow2.speed("fastest")
follow2.color("purple")
follow2.penup()
follow2.shape()
follow2.setposition(250, -250)
follow = Turtle("turtle")
follow.speed("fastest")
follow.color("red")
follow.penup()
follow.shape()
follow.setposition(-250, -250)

def k1():
    run.forward(10)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(10)

def quitThis():
    playGround.bye()

def follow_runner():
    follow.setheading(follow.towards(run))
    follow.forward(5)
    playGround.ontimer(follow_runner, 10)
    follow2.setheading(follow2.towards(run))
    follow2.forward(5)
    playGround.ontimer(follow_runner, 10)


playGround.onkeypress(k1, "Up")
playGround.onkeypress(k2, "Left")
playGround.onkeypress(k3, "Right")
playGround.onkeypress(k4, "Down")
playGround.onkey(quitThis, 'q')

playGround.listen()

follow_runner()

playGround.mainloop()

i tried it for one turtle fllowing another and it worked just fine.

from turtle import Turtle, Screen
playGround = Screen()
playGround.screensize(500, 500)
playGround.title("Turtle Keys")

run = Turtle("turtle")
run.speed("fastest")
run.color("blue")
run.penup()
run.setposition(250, 250)


follow = Turtle("turtle")
follow.speed("fastest")
follow.color("red")
follow.penup()
follow.shape()
follow.setposition(-250, -250)

def k1():
    run.forward(10)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(10)

def quitThis():
    playGround.bye()

def follow_runner():
    follow.setheading(follow.towards(run))
    follow.forward(5)
    playGround.ontimer(follow_runner, 10)


playGround.onkeypress(k1, "Up")
playGround.onkeypress(k2, "Left")
playGround.onkeypress(k3, "Right")
playGround.onkeypress(k4, "Down")
playGround.onkey(quitThis, 'q')

playGround.listen()

follow_runner()

playGround.mainloop()
nada
  • 1

1 Answers1

0

Playing with your code, I'd say the problem is your two calls to playGround.ontimer(follow_runner, 10) in follow_runner(). One call at the end of the function is sufficient, two cause a growing tree of calls that ends up looking like a faux recursion to Python:

from turtle import Turtle, Screen

def k1():
    run.forward(10)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(10)

def quitThis():
    playGround.bye()

def follow_runner():
    follow_1.setheading(follow_1.towards(run))
    follow_1.forward(5)

    follow_2.setheading(follow_2.towards(run))
    follow_2.forward(5)

    playGround.ontimer(follow_runner, 10)

playGround = Screen()
playGround.screensize(500, 500)
playGround.title("Turtle Keys")

run = Turtle('turtle')
run.speed('fastest')
run.penup()
run.color('blue')
run.setposition(250, 250)

follow_1 = run.clone()
follow_1.color('red')
follow_1.setposition(-250, -250)

follow_2 = run.clone()
follow_2.color('purple')
follow_2.setposition(250, -250)

playGround.onkeypress(k1, 'Up')
playGround.onkeypress(k2, 'Left')
playGround.onkeypress(k3, 'Right')
playGround.onkeypress(k4, 'Down')
playGround.onkey(quitThis, 'q')

playGround.listen()

follow_runner()

playGround.mainloop()

If you want the two followers to move at different speeds with respect to ontimer(), that's possible but not the way you've implemented it.

cdlane
  • 40,441
  • 5
  • 32
  • 81