Python Snake Game Window
A game window is the rectangle on the screen where the game is drawn. This lesson opens that window with turtle and sets its size, its colour, and its title.
Opening the window
The import brings in turtle. Screen makes the window object the rest of the program uses.
import turtle
# The game window
screen = turtle.Screen()Size, colour, and title
These three lines set the size, the black background, and the title.
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake")Waiting for a click
Exitonclick waits for a click in the window. The window closes after that click.
screen.exitonclick()Example
import turtle
# The game window
screen = turtle.Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake")
screen.exitonclick()Key points
- Centre: the point (0, 0) in the middle of the window
- The x value grows to the right from the centre
- The y value grows upward from the centre
- The left and right edges sit at -300 and 300
- The top and bottom edges sit at 300 and -300
- Use exitonclick to close the window when the reader clicks it
In this example, the program imports turtle and makes a screen. It sets the screen to 600 wide and 600 tall. It paints the background black and sets the title to Snake. It then waits on exitonclick, so the window stays open until a click closes it.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does screen.exitonclick() do?