Midnight Deck Dash · Lesson 05 of 50 · Beginner · About 30 min
The token can appear anywhere on the promenade. Give the deck officers visible squares so they can describe a route precisely.

Download the projects from the course page. In Godot’s Project Manager, choose Import, select project.godot, and use Run Project (F5). Start from checkpoints/04 when the lesson builds on prior work, or copy the checkpoints/05 folder to try this lesson on its own. Read the file names in the steps before editing.


Build it step by step
Language and tools in this lesson for, range, nested loops, Rect2, local variable, drawing order.
-
Keep the coordinate helper
Open main.gd from Lesson 4 and leave cell_centre in place. In a working copy, replace _draw with the new one in the complete checkpoint.
Why it works The same conversion positions both the courier and the floor, which makes them line up.
-
Repeat for each row
Inside _draw, put the first for line under the background draw. Keep its body indented further.
for row in range(DECK_SIZE.y):Why it works With five rows, range(DECK_SIZE.y) produces 0, 1, 2, 3 and 4. Godot runs the inner block for each value.
-
Repeat again for each column
Add the inner for and make a Vector2i with column first. Write the first three cell pairs down before running.
for column in range(DECK_SIZE.x): var cell := Vector2i(column, row)Why it works The column loop restarts at zero for every row. It runs from (0, 0) to (6, 0), then goes to (0, 1).
-
Paint one floor square per cell
Inside both loops, find the top-left by subtracting half a tile from the centre. Create a Rect2 with its position and size, then give it to draw_rect.
var top_left := cell_centre(cell) - Vector2.ONE * (TILE_SIZE / 2.0) var tile := Rect2(top_left + Vector2.ONE * 2.0, Vector2.ONE * (TILE_SIZE - 4.0)) draw_rect(tile, Color("#16445a"))Why it works The two-pixel margin and the width reduced by four form an even gap, so players can see where tiles split.
-
Paint Sol on top
Keep the two circles after the loops, indented only one level under _draw. Press F5 and count the squares.
draw_circle(cell_centre(player_cell), 20.0, BRASS) draw_circle(cell_centre(player_cell), 7.0, INK)Why it works Godot draws the token once above all the floor squares. Drawing in the inner loop instead would repeat that task.
-
Change the deck dimensions
In a practice copy change the deck to (6, 4). Run it and count the tiles. Put (7, 5) back for the next lesson.
Why it works The loop limits come from DECK_SIZE, so one change adjusts the entire drawing.
Watch the values change
-
Start the first row row is 0 and column starts at 0.
What you see. The first tile is drawn. -
Complete a row The columns reached 0 through 6.
What you see. Seven tiles are drawn before row 1 begins. -
Paint the courier The loops finished drawing five rows of seven tiles.
What you see. Sol appears above the 35 floor tiles.
Predict before you run How many floor rectangles are drawn for DECK_SIZE (6, 4)?
Reveal the answer
24. Each of the four rows has six floor rectangles.
Complete checkpoint
The game draws seven columns and five rows, with the courier centred in tile (2, 1).
main.gd
extends Control
const BOARD_ORIGIN := Vector2(40, 124)
const TILE_SIZE := 76
const DECK_SIZE := Vector2i(7, 5)
const OCEAN := Color("#0b2137")
const BRASS := Color("#f2bc64")
const INK := Color("#07111e")
var player_cell := Vector2i(2, 1)
var sign: Label
func _ready() -> void:
sign = Label.new()
sign.position = Vector2(40, 47)
sign.add_theme_font_size_override("font_size", 28)
add_child(sign)
_refresh_log()
func _refresh_log() -> void:
sign.text = "Promenade | Courier at (%d, %d)" % [player_cell.x, player_cell.y]
func cell_centre(cell: Vector2i) -> Vector2:
return BOARD_ORIGIN + Vector2(cell) * TILE_SIZE + Vector2.ONE * (TILE_SIZE / 2.0)
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, get_viewport_rect().size), OCEAN)
for row in range(DECK_SIZE.y):
for column in range(DECK_SIZE.x):
var cell := Vector2i(column, row)
var top_left := cell_centre(cell) - Vector2.ONE * (TILE_SIZE / 2.0)
var tile := Rect2(top_left + Vector2.ONE * 2.0, Vector2.ONE * (TILE_SIZE - 4.0))
draw_rect(tile, Color("#16445a"))
draw_circle(cell_centre(player_cell), 20.0, BRASS)
draw_circle(cell_centre(player_cell), 7.0, INK)
GDScript line by line
Follow the line numbers, including the indent at the start of each line. Find the code the game will run first, then look for the function that reacts when you tap or change something.
Read main.gd line by line
-
extends ControlGives the root of this lesson the positioning and sizing features of a Godot Control, which is the type created by choosing User Interface in the scene editor. -
const BOARD_ORIGIN := Vector2(40, 124)Defines the board’s top-left pixel position. All tile drawing starts relative to these two coordinates. -
const TILE_SIZE := 76Defines the width and height of one square tile in pixels. A move of one cell means a shift of 76 pixels in the drawing. -
const DECK_SIZE := Vector2i(7, 5)Defines seven columns and five rows. With zero-based addresses the last tile is (6, 4). -
const OCEAN := Color("#0b2137")Names the dark blue background colour. The hash sits inside a colour string here, so it is not a GDScript comment. -
const BRASS := Color("#f2bc64")Names the gold used by the courier and cargo art from a hex colour triplet. -
const INK := Color("#07111e")Names the dark colour used to draw smaller shapes on top of the gold for contrast. -
var player_cell := Vector2i(2, 1)Places the courier in the third column and second row because grid coordinates start at zero. -
var sign: LabelDeclares a script-wide reference with the Label type. _ready fills it, and other functions can then update the same on-screen label. -
func _ready() -> void:Godot calls this method after the scene is ready. Its indented steps make the dispatch label and any controls before the player can use them. It does not return a value. -
sign = Label.new()Makes the label and saves it to the script-wide sign variable. This line uses assignment without var so it does not create a second, local variable named sign. -
sign.position = Vector2(40, 47)Places the address readout near the top of the practice window, 40 pixels from the left and 47 down. -
sign.add_theme_font_size_override("font_size", 28)Makes the coordinate readout 28 points tall using a font size override for this particular Label. -
add_child(sign)Adds the Label to this node so it becomes part of the active scene and Godot can display its text. -
_refresh_log()Rebuilds the address text from the current position. A later move or reset calls the same helper after it changes player_cell. -
func _refresh_log() -> void:Defines where the courier’s position is converted into readable text. Keeping it here gives setup, movement and reset a shared update. -
sign.text = "Promenade | Courier at (%d, %d)" % [player_cell.x, player_cell.y]Builds the position text with x as the column first and y as the row second. Both coordinates are whole numbers. -
func cell_centre(cell: Vector2i) -> Vector2:Converts a grid address to a pixel position. The input cell has whole numbers, and the Vector2 result can represent fractional pixels. -
return BOARD_ORIGIN + Vector2(cell) * TILE_SIZE + Vector2.ONE * (TILE_SIZE / 2.0)Multiplies the column and row by the tile size, adds the board’s pixel origin, then moves half a tile across and down. For (2, 1), that yields (230, 238). -
func _draw() -> void:Godot calls this drawing method when the canvas needs a frame. Earlier drawing lines appear under later lines. -
draw_rect(Rect2(Vector2.ZERO, get_viewport_rect().size), OCEAN)Paints the dark blue behind the whole viewport. Rect2 combines a top-left point and a size. -
for row in range(DECK_SIZE.y):Repeats the nested drawing instructions for rows 0–4. The upper bound of range is excluded. -
for column in range(DECK_SIZE.x):Repeats tile drawing for columns 0–6 inside the current row. Each new row begins again at column 0. -
var cell := Vector2i(column, row)Creates the tile address for this loop iteration. The x part receives column and the y part receives row. -
var top_left := cell_centre(cell) - Vector2.ONE * (TILE_SIZE / 2.0)Subtracts half the tile width from both axes to convert the centre point back to its upper-left corner. -
var tile := Rect2(top_left + Vector2.ONE * 2.0, Vector2.ONE * (TILE_SIZE - 4.0))Starts the painted square two pixels inside the tile and subtracts four pixels from its size, giving a two-pixel margin on each edge. -
draw_rect(tile, Color("#16445a"))Paints the current walkway tile. The nested loops run this once per tile before the courier is drawn. -
draw_circle(cell_centre(player_cell), 20.0, BRASS)Draws one brass circle at the courier’s current pixel centre with a 20-pixel radius. -
draw_circle(cell_centre(player_cell), 7.0, INK)Adds a dark inner circle at the same centre. Drawing a smaller circle after the brass one produces the ring effect.
Find and fix a bug
If you get a single row or column, check the indentation guides. The new Rect2 and draw_rect belong inside both loops.
Try a change
Use the remainder of (row + column) divided by two to decide which squares could get a lighter shade. Make that change in a working copy.