I am creating a snake game with food teleporting around map. I use ontimer with recursion at the end of function to emulate mole staying at a certain position for a time. I also make the mole to move if turtle ate it. But then, mole teleportation is called two time because of the already set function by ontimer.
def mole_teleport(is_loop=True):
rand_x = random.randint(-250, 250)
rand_y = random.randint(-250, 250)
mole.teleport(rand_x, rand_y)
if is_loop == False:
return
else:
screen.ontimer(mole_teleport, 3000)
tim = Player() #user_turtle
mole = Turtle("circle")
mole_teleport()
screen = Screen()
screen.setup(600, 600)
screen.listen()
screen.onkey(tim.turn_left, "Left")
screen.onkey(tim.turn_right, "Right")
game_is_on = True
while game_is_on:
tim.move()
if mole.distance(tim) < 20:
mole_teleport(is_loop=False)
my-ideal-diagram
I tried to block recursion with argument. What I expect is teleportation timer to be refreshed from start after the turtle has eaten the mole and call mole_teleportation()
tensai_bootleg. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.