I am importing turtle module and then creating a screen and after creating I am trying to create a turtle object but it is not showing on the screen. Can anybody tell me what is the issue. I have my following code written in pyCharm :
import turtle as t
screen = t.Screen()
screen.title("My Snake Game")
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.exitonclick()
terry = t.Turtle(shape='square')
terry.color('white')
terry.goto(0, 0)
CodePudding user response:
screen.exitonclick() seems to wait for a mouse click before continuing. As you define your turtle after that, it doesn't come up on the screen. Try putting screen.exitonclick() at the very end of the code
CodePudding user response:
The problem here is that you have the screen.exitonclick()
in the middle. That generates another window that apears and desapears after screen.exitonclick()
is executed. What you should do is to put screen.exitonclick()
at the end.
import turtle as t
screen = t.Screen()
screen.title("My Snake Game")
screen.setup(width=600, height=600)
screen.bgcolor("black")
terry = t.Turtle(shape='square')
terry.color('white')
terry.goto(0, 0)
screen.exitonclick()
This would be the code.
CodePudding user response:
The screen.exitonclick()
method does not only set the property that the screen will close after clicking it, it does also wait for this to happen. As this method waits for the screen to close, every code that is after it won't be executed untill the user closes the screen by clicking on it.
This means that the turtle will be created after the screen is closed and thus does not show up as the screen is closed. The solution for this is moving the screen.exitonclick()
line to the end of the code.