My turtle code doesn't hide the turtle or move to where expected

I wanted to hide the turtle cursor but it still appears on the screen and the “Tap!” text doesn’t appear where expected.

import turtle
import random
import pygame
count = 0
rando = 2
turtle.penup()
pygame.init()
sound_file = r'C:\Users\Yarden\OneDrive\Documents\Python\Recording.wav'
tap = turtle.Turtle()
turtle.goto(0, 150)
turtle.write('Highscore: ', align = 'center', font = ('Ink Free', 30))
def play_sound(sound_file):
    pygame.mixer.init()
    pygame.mixer.music.load(sound_file)
    pygame.mixer.music.play()
def click(x, y):
    global count
    if -100 <= x <= 100 and -100 <= y <= 100:
        count += 1
        rando = random.randint(1, 100)
        turtle.hideturtle()
        circle_number(count)
        tap.clear()
        if rando <= count:
            turtle.goto(0, -150)
            turtle.write('You Lost!', align='center', font=('Ink Free', 30))
            turtle.onscreenclick(None)
            play_sound(sound_file)
def circle():
    turtle.tracer(0)
    turtle.hideturtle()
    turtle.goto(0, 0)
    turtle.dot(200, 'Red')
    turtle.color('Black')
    turtle.goto(0,-30)
    tap.write('Tap!', align = 'center', font = ('Ink Free', 30))
    turtle.hideturtle()
def circle_number(number):
    circle()
    turtle.write(number, align = 'center', font = ('Ink Free', 30))
    turtle.update()
turtle.speed(0)
turtle.Screen().onclick(click)
circle()
turtle.mainloop()

turtle.goto(0,-30)
tap.write('Tap!', align = 'center', font = ('Ink Free', 30))
turtle.hideturtle() #<========== HERE

Change that line to tap.hideturtle(). It will hide your second turtle :slight_smile:

When you call functions like turtle.goto, turtle.color etc., they are actually operating on a pre-created Turtle that the turtle module gives you. The turtle module will automatically “forward” them to that Turtle object. They are not a global “do this for every Turtle in the program”.

Of course, there are some functions in the module, like mainloop, that don’t delegate to the Turtle - basically, the ones that Turtle objects don’t have, like mainloop (that one actually delegates to tkinter).

When you do tap = turtle.Turtle(), you make a separate Turtle that has its own position, orientation, line colour, etc. So it isn’t hidden by turtle.hideturtle(), because that’s hiding a different Turtle.

1 Like