1

Want the word Rock to show up centered in front of the pre-existing clickable square, but can't figure it out because the text stays behind the box. I can't find an answer for this anywhere.

def rockClick(x, y):
    if Rock.color()[0] == 'white':
        Rock.color('black', 'white')
    else:
        Rock.color('white', 'black')

#Rock
Rock = turtle.Turtle()
Rock.speed(0)
Rock.shape('square')
Rock.color('white', 'black')
Rock.penup()
Rock.shapesize(stretch_wid=7, stretch_len=7, outline=5)
Rock.goto(-230,-240)
Rock.onclick(rockClick)
playerChoice = 'Rock'
Rock.onrelease(rockClick)

#Rocktext
rockText = turtle.Turtle
rockText.speed(0)
rockText.color('white')
rockText.penup()
rockText.hideturtle()
rockText.goto(-380, -280)
rockText.write('Rock', align='center', font=('Courier', 24, 'bold'))

Tried placing the rockText turtle before the clickable squares creation, afterwards, in the main game loop. Tried having the rockText as a stamp but I got an error from that. I'm at a loss for solutions as even the solutions to Potatoezzz's similar question didn't work.

cdlane
  • 40,441
  • 5
  • 32
  • 81
Fr0gger_
  • 11
  • 3
  • Creating a button with text in it is tricky. Here are some existing approaches: [1](https://stackoverflow.com/questions/59902849/how-can-i-create-a-button-in-turtle/59906839#59906839), [2](https://stackoverflow.com/questions/61765658/how-to-create-a-button-with-python-turtle), [3](https://stackoverflow.com/questions/56960444/how-to-make-python-text-into-a-button-in-python-turtle), [4](https://stackoverflow.com/a/75153962/6243352) – ggorlen Jan 22 '23 at 16:03
  • Does this answer your question? [How to make python text into a button in python turtle?](https://stackoverflow.com/questions/56960444/how-to-make-python-text-into-a-button-in-python-turtle) – ggorlen Jan 22 '23 at 16:05

1 Answers1

0

First, I'm surprised your code runs at all with this statement:

rockText = turtle.Turtle

(Figure out why.) Next, you appear to be writing "Rock" as white text, on a white screen to the left of you box, not on it:

Rock.goto(-230,-240)
...
rockText.goto(-380, -280)

So, it'll never be seen as it's white on white. (Change the text color to see what's happening.)

But to get to the real problem you're trying to solve, one turtle always appears atop the other turtle and you have no way to fix that. Here's a simple fix to your code, not necessarily the best way to handle this, that should do the trick:

from turtle import Screen, Turtle

def rockClick(x, y):
    rock.color(*reversed(rock.color()))

    screen.ontimer(iAmRock)

def iAmRock():
    # force rockText to front and write its label
    rockText.write('Rock', align='center', font=('Courier', 24, 'bold'))

screen = Screen()

# Rock
rock = Turtle(visible=False)
rock.shape('square')
rock.shapesize(stretch_wid=7, stretch_len=7, outline=5)
rock.color('white', 'black')
rock.speed('fastest')
rock.penup()
rock.goto(-230, -240)
rock.onclick(rockClick)
rock.onrelease(rockClick)
rock.showturtle()

# Rocktext
rockText = Turtle(visible=False)
rockText.color('red')
rockText.speed('fastest')
rockText.penup()
rockText.goto(-230, -280)
iAmRock()

screen.mainloop()

Another way to handle this would be to give rock a polygon shape that looks like a rock and toss the "Rock" label altogether.

cdlane
  • 40,441
  • 5
  • 32
  • 81