Skip to main content

Stage 1: Build the Game Screen

Course progressStage 1 of 10
~40 min
One game, one Trinket

Keep building in your saved Python Arcade project.

This stage is part of the same game you started in Setup. Do the work in your own remixed Trinket — it’s the workspace on the right of this page.

Build

a game window, screen boundaries, and a player cannon

Learn

how coordinates name positions on the screen

Ship

a cannon sitting at the bottom of the playfield

The big idea

Before we draw anything, we need a map — a way to say "this thing is here." Python's Turtle library gives us one. The screen has a center point at (0, 0). X values move left and right. Y values move up and down.

Think of the screen like a sheet of graph paper with the middle marked. Every alien, every laser, and every cannon we build over the next nine stages will live somewhere on that paper.

A coordinate is just a pair of numbers — (x, y) — that says where something is. A Turtle is a thing the library draws for you; you tell it a coordinate, and it goes there.

(0, TOP)


(LEFT, 0)─────(0, 0)─────(RIGHT, 0)


(0, BOTTOM)

Today we will name the four edges (LEFT, RIGHT, TOP, BOTTOM) and a fifth one called FLOOR_LEVEL — the line the player cannon sits on. Once those names exist, every later stage gets to say "spawn at the TOP" instead of "spawn at y = 312." Names that mean something today become the vocabulary of the whole game.

Before you start

Open your one Python Arcade Trinket. Run the setup test once, then replace it with the Stage 1 code below.

Build it

Step 1 — Open the screen and name its edges

Every game needs a playfield before anything can happen on it. We create the screen first, then save its edges with names we will reuse for the rest of the course.

Replace the setup test program with this:

import turtle

screen = turtle.Screen()
screen.setup(width=0.8, height=0.8)
screen.bgcolor("black")
screen.title("Python Arcade")

LEFT = -screen.window_width() / 2
RIGHT = screen.window_width() / 2
TOP = screen.window_height() / 2
BOTTOM = -screen.window_height() / 2
FLOOR_LEVEL = BOTTOM + 50

turtle.done()

Run it. You should see a black game window appear. It is empty — that is fine. The four edge values are calculated from whatever size Trinket gives you, so the game fits any window.

Step 2 — Drop a cannon at the floor

The cannon is the player. It needs a position before it can shoot, move, or take damage. We will create one Turtle, give it a color and a basic shape, and place it at FLOOR_LEVEL.

Add this above turtle.done():

cannon = turtle.Turtle()
cannon.penup()
cannon.color("white")
cannon.shape("square")
cannon.setposition(0, FLOOR_LEVEL)

Run the game again. You should see a white square near the bottom, centered left-to-right. Notice that the square is exactly at FLOOR_LEVEL — the name we picked in Step 1 is already doing real work.

Step 3 — Give it a cannon shape

A single square is not very cannon-looking. Instead of importing an image, we will stamp three rectangles in a stack. A stamp is a snapshot of the Turtle's current shape — once stamped, the Turtle can move on and the stamp stays behind.

Add this function above turtle.done():

def draw_cannon():
cannon.clear()
cannon.turtlesize(1, 4)
cannon.stamp()
cannon.sety(FLOOR_LEVEL + 10)
cannon.turtlesize(1, 1.5)
cannon.stamp()
cannon.sety(FLOOR_LEVEL + 20)
cannon.turtlesize(0.8, 0.3)
cannon.stamp()
cannon.sety(FLOOR_LEVEL)

Then call it before turtle.done():

draw_cannon()

Run it. Now your cannon has a base, a body, and a barrel — three stacked rectangles instead of one square.

Understand it

The cannon you just built is still one Turtle that moved through three positions, leaving a stamp behind at each one. That is a design choice worth pausing on.

We could have used three separate Turtles — one for the base, one for the body, one for the barrel — but every Turtle adds a little work for the computer to track. By the end of the course we will have lots of lasers and aliens that each need their own Turtle, so we keep "decoration" cheap and reuse one Turtle wherever we can.

Wrapping the stamping into a draw_cannon function also means we can call it again later, after the player moves. Calling cannon.clear() first wipes the old stamps so we do not pile up forty cannons on top of each other.

Try this

Learning beat

Try this

Three short experiments. Predict before you run, then test your guess.

Predict first

Change FLOOR_LEVEL = BOTTOM + 50 to FLOOR_LEVEL = BOTTOM + 200. Where will the cannon end up? Decide before you run — then run it. Were you right?

Compare

Swap the cannon's shape("square") for shape("triangle") and run. Which one looks more like a cannon to you? Which one would a brand-new player understand faster?

Connect

Where else in this game will you need a position at the bottom of the screen? (Hint: when aliens reach the player, they have to know they got there.)

Test your stage

  • The window opens with a black background.
  • The cannon appears near the bottom of the screen.
  • The cannon is centered left-to-right.
  • Running the program twice does not pile up extra cannons.
  • Design check. Read your code out loud. Do LEFT, RIGHT, and FLOOR_LEVEL sound like real things, or like mystery numbers?

If it breaks

  • The window is blank. draw_cannon() probably needs to be above turtle.done(). The turtle.done() line is the "we are done setting up" signal — nothing after it runs.
  • The cannon is missing entirely. Check the spelling on cannon.color("white"). Color names are case-sensitive in some setups, and a typo silently leaves the cannon invisible against the black background.
  • The cannon is way too high or way too low. Re-read FLOOR_LEVEL = BOTTOM + 50 carefully. BOTTOM is already negative, so adding 50 nudges it up a little. If your cannon is at the top of the screen, you probably wrote TOP + 50 by mistake.
  • The cannon is one fat square, not three rectangles. The cannon.sety(...) calls between stamps need to actually run between the stamps. Read your draw_cannon function top-to-bottom and check the order.