Midnight Deck Dash · Lesson 10 of 50 · Beginner · About 35 min
The ship’s first delivery is almost ready. Put luggage and a dock on the map, then verify that Sol can reach each location.

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/09 when the lesson builds on prior work, or copy the checkpoints/10 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 data symbols, match, draw_rect, draw_circle, elif, game state, reset handler.
-
Add cargo and the dock to the map
Replace DECK_ROWS with these five strings. Mark C as luggage and D as the delivery dock. Check that the cabin hashes remain in the same places.
const DECK_ROWS: Array[String] = [ ".......", ".##..C.", "...#...", ".C.#.D.", ".......", ]Why it works The new symbols are data. They can add meaning to a tile without changing its size or making it an obstacle.
-
Draw two kinds of badge
Add _draw_marker below the drawing functions. Read its two match cases aloud and find the rectangular bag and round dock shapes.
func _draw_marker(point: Vector2, marker: String) -> void: match marker: "C": draw_rect(Rect2(point - Vector2(15, 12), Vector2(30, 24)), BRASS) draw_rect(Rect2(point - Vector2(4, 12), Vector2(8, 24)), INK) "D": draw_circle(point, 15.0, Color("#79e4c4")) draw_circle(point, 8.0, OCEAN)Why it works The helper draws a suitable symbol where the cell centre is passed in. Giving each marker a different shape helps players distinguish them without relying on colour.
-
Place badges after the floor
Inside the deepest _draw loop, call _draw_marker just after draw_rect(tile, paint). Pass the cell centre and its symbol.
_draw_marker(cell_centre(cell), marker)Why it works Painting floor first and badges second keeps the marker visible. After all tiles and markers are drawn, the courier is painted on top.
-
Label special places in the log
Replace _refresh_log with the new version. Look for the default Walkway, then the two conditions for C and D.
func _refresh_log() -> void: var marker := _tile_at(player_cell) var place := "Walkway" if marker == "C": place = "Cargo marker" elif marker == "D": place = "Delivery dock" sign.text = "%s | Courier at (%d, %d)" % [place, player_cell.x, player_cell.y]Why it works if names C. elif checks D only when the first test was false. An ordinary tile keeps the Walkway label.
-
Provide a reset button
Add the Reset practice deck setup in _ready, then add _reset_deck at the far left. Wire the pressed signal to that method without bind because it expects no direction.
func _reset_deck() -> void: player_cell = Vector2i.ZERO _refresh_log() queue_redraw()Why it works The new method sets the starting position directly, then updates both the drawing and the sign. The map is unchanged.
-
Play the training route
Run with F5. From (0, 0), move South three times then East to reach (1, 3). Go South, East four times and North to reach (5, 3), the dock. Check Reset after getting there.
Why it works Trying an actual route checks the cabin layout and both types of marker. Stepping on a cargo badge names the tile. Pickup rules come in the next lesson.
Watch the values change
-
Reach cargo The courier is at (1, 3) and the map reads C.
What you see. The sign says Cargo marker. -
Reach the dock The courier is at (5, 3) and the map reads D.
What you see. The sign says Delivery dock. -
Press Reset The stored player address becomes (0, 0).
What you see. The courier goes to the starting tile.
Predict before you run What happens when you step off a C tile? Does the cargo badge vanish?
Reveal the answer
The cargo badge stays because the map symbol has not changed. The log describes the next tile. Pickup state comes next.
Complete checkpoint
The deck has walls, two luggage badges, a dock ring, working direction controls and a reset button.
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.ZERO
var sign: Label
const DECK_ROWS: Array[String] = [
".......",
".##..C.",
"...#...",
".C.#.D.",
".......",
]
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()
_add_direction("North", Vector2(679, 171), Vector2i.UP)
_add_direction("West", Vector2(607, 233), Vector2i.LEFT)
_add_direction("East", Vector2(751, 233), Vector2i.RIGHT)
_add_direction("South", Vector2(679, 295), Vector2i.DOWN)
var reset_button := Button.new()
reset_button.text = "Reset practice deck"
reset_button.position = Vector2(624, 400)
reset_button.custom_minimum_size = Vector2(225, 52)
reset_button.pressed.connect(_reset_deck)
add_child(reset_button)
func _refresh_log() -> void:
var marker := _tile_at(player_cell)
var place := "Walkway"
if marker == "C":
place = "Cargo marker"
elif marker == "D":
place = "Delivery dock"
sign.text = "%s | Courier at (%d, %d)" % [place, 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))
var marker := _tile_at(cell)
var paint := Color("#637389") if marker == "#" else Color("#16445a")
draw_rect(tile, paint)
_draw_marker(cell_centre(cell), marker)
draw_circle(cell_centre(player_cell), 20.0, BRASS)
draw_circle(cell_centre(player_cell), 7.0, INK)
func _add_direction(caption: String, at: Vector2, delta: Vector2i) -> void:
var button := Button.new()
button.text = caption
button.position = at
button.custom_minimum_size = Vector2(122, 52)
button.pressed.connect(_try_move.bind(delta))
add_child(button)
func _try_move(delta: Vector2i) -> void:
var next_cell := player_cell + delta
if not _inside(next_cell):
sign.text = "Rail ahead | Courier stays at (%d, %d)" % [player_cell.x, player_cell.y]
return
if _tile_at(next_cell) == "#":
sign.text = "Cabin wall | Find a route around it"
return
player_cell = next_cell
_refresh_log()
queue_redraw()
func _inside(cell: Vector2i) -> bool:
return cell.x >= 0 and cell.x < DECK_SIZE.x and cell.y >= 0 and cell.y < DECK_SIZE.y
func _unhandled_key_input(event: InputEvent) -> void:
if not event is InputEventKey:
return
if not event.pressed or event.echo:
return
match event.keycode:
KEY_UP:
_try_move(Vector2i.UP)
KEY_LEFT:
_try_move(Vector2i.LEFT)
KEY_RIGHT:
_try_move(Vector2i.RIGHT)
KEY_DOWN:
_try_move(Vector2i.DOWN)
_:
return
get_viewport().set_input_as_handled()
func _tile_at(cell: Vector2i) -> String:
return DECK_ROWS[cell.y].substr(cell.x, 1)
func _draw_marker(point: Vector2, marker: String) -> void:
match marker:
"C":
draw_rect(Rect2(point - Vector2(15, 12), Vector2(30, 24)), BRASS)
draw_rect(Rect2(point - Vector2(4, 12), Vector2(8, 24)), INK)
"D":
draw_circle(point, 15.0, Color("#79e4c4"))
draw_circle(point, 8.0, OCEAN)
func _reset_deck() -> void:
player_cell = Vector2i.ZERO
_refresh_log()
queue_redraw()
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.ZEROPlaces the courier at (0, 0), the upper-left cell, which touches the north and west safety rails. -
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. -
const DECK_ROWS: Array[String] = [Begins an ordered list of row strings. Each string contains seven tile symbols, and the rows are indexed from zero. -
".......",Defines one row of the deck map. Periods are walkable floor, # is a cabin wall, C is luggage and D is the dock. The exact character position sets the column. -
".##..C.",Defines one row of the deck map. Periods are walkable floor, # is a cabin wall, C is luggage and D is the dock. The exact character position sets the column. -
"...#...",Defines one row of the deck map. Periods are walkable floor, # is a cabin wall, C is luggage and D is the dock. The exact character position sets the column. -
".C.#.D.",Defines one row of the deck map. Periods are walkable floor, # is a cabin wall, C is luggage and D is the dock. The exact character position sets the column. -
".......",Defines one row of the deck map. Periods are walkable floor, # is a cabin wall, C is luggage and D is the dock. The exact character position sets the column. -
]Closes the row list. The opening bracket and closing bracket surround all of the map rows. -
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. -
_add_direction("North", Vector2(679, 171), Vector2i.UP)Creates the North button at the given pixel position and stores the (0, -1) direction, which will subtract one from the row when pressed. -
_add_direction("West", Vector2(607, 233), Vector2i.LEFT)Creates the West button at the given pixel position and stores the (-1, 0) direction, which will subtract one from the column when pressed. -
_add_direction("East", Vector2(751, 233), Vector2i.RIGHT)Creates the East button at the given pixel position and stores the (1, 0) direction, which will add one to the column when pressed. -
_add_direction("South", Vector2(679, 295), Vector2i.DOWN)Creates the South button at the given pixel position and stores the (0, 1) direction, which will add one to the row when pressed. -
var reset_button := Button.new()Creates a separate Reset button during scene setup. -
reset_button.text = "Reset practice deck"Gives the button a label that describes what it does. -
reset_button.position = Vector2(624, 400)Places Reset below the four direction controls. -
reset_button.custom_minimum_size = Vector2(225, 52)Gives Reset a wide touch target and room for its three words. -
reset_button.pressed.connect(_reset_deck)Runs _reset_deck when one press occurs. The handler does not take a direction, so this signal connection does not bind an argument. -
add_child(reset_button)Adds the Reset button to the scene so players can see and activate it. -
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. -
var marker := _tile_at(player_cell)Looks up the actual occupied tile so the log can use the place the player is standing, not the place an invalid move attempted. -
var place := "Walkway"Starts with the normal location name. The following branches replace it only for particular marker symbols. -
if marker == "C":Checks whether the occupied tile contains the cargo symbol. -
place = "Cargo marker"Uses the cargo name for the position label. This line does not add a bag to inventory. -
elif marker == "D":Checks the delivery symbol only if the cargo check above failed. -
place = "Delivery dock"Replaces the normal walkway label with the dock’s name. -
sign.text = "%s | Courier at (%d, %d)" % [place, player_cell.x, player_cell.y]Builds a new log line from the chosen location name and the current address. -
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. -
var marker := _tile_at(cell)Reads the tile symbol for the current drawing loop. One symbol can drive both its colour and its badge. -
var paint := Color("#637389") if marker == "#" else Color("#16445a")Chooses slate for a cabin wall and teal for anything else. The two colour strings use hashes as part of their values. -
draw_rect(tile, paint)Paints this tile using the selected colour instead of a fixed walkway colour. -
_draw_marker(cell_centre(cell), marker)Draws a badge at the tile centre after painting the floor. The map symbol chooses the badge rather than requiring another coordinate map. -
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. -
func _add_direction(caption: String, at: Vector2, delta: Vector2i) -> void:Creates one direction button. The arguments carry its visible text, its pixel placement and the direction it will use when pressed. -
var button := Button.new()Creates a new Godot Button in memory. Each call to the builder creates a separate Button. -
button.text = captionShows the caller’s text, such as East, on the button face. -
button.position = atPlaces the control at the pixel address passed in by the call that created it. -
button.custom_minimum_size = Vector2(122, 52)Gives each direction button a 122-by-52-pixel target so it is easier to tap. -
button.pressed.connect(_try_move.bind(delta))Connects the pressed signal to the movement function. bind saves this button’s direction as the argument to pass at tap time. -
add_child(button)Attaches the Button to the scene so that Godot can draw it, give it input and emit its signal when pressed. -
func _try_move(delta: Vector2i) -> void:Handles a one-cell movement request from any direction button or arrow key. Each call receives the requested coordinate change. -
var next_cell := player_cell + deltaCalculates the newly requested address in a separate variable. The current cell stays the same until the guards approve the move. -
if not _inside(next_cell):Checks for an off-deck address. The indented warning and early return run only when a rail blocks the requested move. -
sign.text = "Rail ahead | Courier stays at (%d, %d)" % [player_cell.x, player_cell.y]Prints the rail warning with the address the courier still occupies, which might differ from the proposed next_cell. -
returnLeaves the current function immediately. The later position update is skipped if this early exit belongs to a blocked movement check. -
if _tile_at(next_cell) == "#":Stops a move into a cabin wall. This lookup is safe here because the earlier rail guard already rejected any address outside the map. -
sign.text = "Cabin wall | Find a route around it"Replaces the coordinate log with a specific reason for the refused turn. The player stays at the previous cell. -
returnLeaves the current function immediately. The later position update is skipped if this early exit belongs to a blocked movement check. -
player_cell = next_cellCommits the approved position after the guard checks. The refresh and repaint that follow can now read the new cell. -
_refresh_log()Rebuilds the address text from the current position. A later move or reset calls the same helper after it changes player_cell. -
queue_redraw()Asks Godot to repaint. The _draw method will read the current player_cell when Godot performs the redraw. -
func _inside(cell: Vector2i) -> bool:Answers whether the proposed address fits this deck. The caller receives true or false. -
return cell.x >= 0 and cell.x < DECK_SIZE.x and cell.y >= 0 and cell.y < DECK_SIZE.yRequires non-negative x and y, with each coordinate smaller than the count for that axis. In a seven-by-five grid, column seven and row five are already outside. -
func _unhandled_key_input(event: InputEvent) -> void:Receives keyboard events the interface did not already use. The event object stores which key changed and whether this is an ordinary press, release or repeat. -
if not event is InputEventKey:Keeps the rest of the handler limited to keyboard events, whose specific properties the next checks read. -
returnLeaves the current function immediately. The later position update is skipped if this early exit belongs to a blocked movement check. -
if not event.pressed or event.echo:Filters out released keys and repeat events caused by holding a key. Either condition is enough to stop this call. -
returnLeaves the current function immediately. The later position update is skipped if this early exit belongs to a blocked movement check. -
match event.keycode:Chooses the branch for the key number. Each arrow case passes its direction to _try_move, where the game validates it. -
KEY_UP:Matches only the Up arrow, the input for requesting one cell north. -
_try_move(Vector2i.UP)Requests North with the vector (0, -1), which will subtract one from the row if the move is legal. -
KEY_LEFT:Matches only the Left arrow, the input for requesting one cell west. -
_try_move(Vector2i.LEFT)Requests West with the vector (-1, 0), which will subtract one from the column if the move is legal. -
KEY_RIGHT:Matches only the Right arrow, the input for requesting one cell east. -
_try_move(Vector2i.RIGHT)Requests East with the vector (1, 0), which will add one to the column if the move is legal. -
KEY_DOWN:Matches only the Down arrow, the input for requesting one cell south. -
_try_move(Vector2i.DOWN)Requests South with the vector (0, 1), which will add one to the row if the move is legal. -
_:Catches any key not listed above. The fallback returns without moving the courier. -
returnLeaves the current function immediately. The later position update is skipped if this early exit belongs to a blocked movement check. -
get_viewport().set_input_as_handled()Marks an arrow request as used after processing it. An unrecognised key returns earlier and skips this instruction. -
func _tile_at(cell: Vector2i) -> String:Reads the map symbol for a valid address and returns it as a one-character String. -
return DECK_ROWS[cell.y].substr(cell.x, 1)Chooses the row using y, then reads one character at column x. For (1, 1), the second row’s second character is a wall hash. -
func _draw_marker(point: Vector2, marker: String) -> void:Receives a pixel point and the one-character map symbol. It draws a bag or ring on that square when the symbol matches. -
match marker:Selects a badge based on the symbol. Ordinary walkway and wall symbols have no matching branch and draw no extra badge. -
"C":Starts the bag badge instructions for a cargo square. -
draw_rect(Rect2(point - Vector2(15, 12), Vector2(30, 24)), BRASS)Paints a gold rectangle centred on the tile. The offset is half of the 30-by-24-pixel rectangle’s size. -
draw_rect(Rect2(point - Vector2(4, 12), Vector2(8, 24)), INK)Draws a narrow dark strap over the centre of the bag’s gold rectangle. -
"D":Starts the ring instructions for a delivery dock. -
draw_circle(point, 15.0, Color("#79e4c4"))Paints a mint circle with a radius of 15 pixels for the dock’s outer edge. -
draw_circle(point, 8.0, OCEAN)Draws the dark centre on top of the mint circle so the dock reads as a ring. -
func _reset_deck() -> void:Restores the starting cell of this training board, refreshes the label and requests a repaint. -
player_cell = Vector2i.ZEROStores (0, 0) again, placing the courier in the top-left starting cell without changing the map. -
_refresh_log()Rebuilds the address text from the current position. A later move or reset calls the same helper after it changes player_cell. -
queue_redraw()Asks Godot to repaint. The _draw method will read the current player_cell when Godot performs the redraw.
Find and fix a bug
If a badge hides under the tile, paint the floor first, then draw the marker and finally draw the courier after the loops.
Try a change
Find a new route from (0, 0) to the cargo at (5, 1). Write it as compass directions, check it in the game and compare your number of turns with another route.