Midnight Deck Dash · Lesson 32 of 50 · Intermediate to advanced · About 36 min
A sliding trolley looks natural aboard a ship. Its board address must stay exact even between pictures.

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/31 when the lesson builds on prior work, or copy the checkpoints/32 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 clampi, guard clause, Tween, tween_property, kill.
-
Open the editable checkpoint
In Godot Project Manager, choose Import, select this lesson's project.godot and click Import & Edit. Open main.gd from FileSystem and press F5 to see the finished checkpoint. Press F8 before trying a change.
Why it works The lesson scene already has a Control root with this script attached.
-
Keep the real address
At tile 00, press Left. In move, follow the clamp and the return below it.
var next := clampi(cell + direction, 0, 8)Why it works clampi returns a whole number within the bounds. Left of zero stays at zero, so there is no new turn to animate.
-
Commit the move
Find the line after the guard. Press Right once and watch the label.
cell = nextWhy it works The state is now tile 1. The animation can show the change after the game has already recorded it.
-
Move the drawing in pixels
Change 0.21 to 1.0 in tween_property and run before restoring it. Identify the target object, property, final value and duration.
slide.tween_property(courier, "position", tile_position(cell), 0.21)Why it works The Tween writes courier.position each frame. tile_position converts the logical address to the destination in pixels.
-
Replace a running movement
Press Right three times quickly, then Left. Read the condition that checks slide before killing it.
if slide and slide.is_running():Why it works A new move stops the previous Tween so two animations cannot fight over the same property.
-
Restart cleanly
Press Reset position while a long animation is running. Confirm both the label and picture return to tile 00.
func reset_turn() -> void:Why it works Reset cancels pending motion and updates both the saved tile and the Control's pixel position.
Watch the values change
-
Left from 00 The candidate clamps to zero.
What you see. Nothing moves. -
Right cell becomes 1 and the Tween starts.
What you see. The label changes, then the picture arrives. -
Reset The Tween stops and cell becomes 0.
What you see. The picture stays on tile 00.
Predict before you run If the animation lasts two seconds, how long does it take cell to become 1?
Reveal the answer
It becomes 1 during the call to move. Two seconds is just the animation's duration.
Complete checkpoint
The courier glides along tiles 00 to 08 and resets cleanly mid-move.
main.gd
extends Control
const TRACK := Vector2(54, 180)
const TILE := 84
var cell := 0
var courier: Panel
var status: Label
var slide: Tween
func _ready() -> void:
label("Midnight Deck Dash / Animate a turn", 28)
status = label("Courier at tile 00", 76)
courier = Panel.new()
courier.size = Vector2(48, 48)
var paint := StyleBoxFlat.new()
paint.bg_color = Color("#ffd16f")
paint.set_corner_radius_all(12)
courier.add_theme_stylebox_override("panel", paint)
add_child(courier)
courier.position = tile_position(cell)
button("Left", 53, 347, move.bind(-1), 140)
button("Right", 205, 347, move.bind(1), 140)
button("Reset position", 357, 347, reset_turn, 152)
func tile_position(index: int) -> Vector2:
return TRACK + Vector2(index * TILE + 16, 16)
func move(direction: int) -> void:
var next := clampi(cell + direction, 0, 8)
if next == cell:
return
cell = next
if slide and slide.is_running():
slide.kill()
slide = create_tween()
slide.tween_property(courier, "position", tile_position(cell), 0.21)
status.text = "Courier at tile %02d" % cell
func reset_turn() -> void:
if slide:
slide.kill()
cell = 0
courier.position = tile_position(cell)
status.text = "Courier at tile 00"
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#112632"))
for index in range(9):
var corner := TRACK + Vector2(index * TILE, 0)
draw_rect(Rect2(corner, Vector2(TILE - 4, TILE - 4)), Color("#30596d"))
draw_string(ThemeDB.fallback_font, corner + Vector2(7, 74), "%02d" % index, HORIZONTAL_ALIGNMENT_LEFT, -1, 16, Color("#f6edce"))
func label(text: String, y: float, font_size: int = 25) -> Label:
var new_label := Label.new()
new_label.text = text
new_label.position = Vector2(52, y)
new_label.add_theme_font_size_override("font_size", font_size)
add_child(new_label)
return new_label
func button(text: String, x: float, y: float, action: Callable, width: float = 214.0) -> Button:
var control := Button.new()
control.text = text
control.position = Vector2(x, y)
control.custom_minimum_size = Vector2(width, 48)
add_child(control)
control.pressed.connect(action)
return control
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 ControlAttach this script to a Control so the checkpoint can draw, size itself, place buttons and respond to input. -
const TRACK := Vector2(54, 180)Creates a constant named TRACK. The expression packs x and y into one value. Godot infers the type from the first value. -
const TILE := 84Creates a constant named TILE. The expression uses the whole number 84. Godot infers the type from the first value. -
var cell := 0Creates a variable named cell. The expression uses the whole number 0. Godot infers the type from the first value. -
var courier: PanelDeclares courier for later use. A colon followed by a type says which kind of value belongs here. -
var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here. -
var slide: TweenDeclares slide for later use. A colon followed by a type says which kind of value belongs here. -
func _ready() -> void:Godot calls this setup function when the scene enters the running tree. void means the function gives the caller no value back. -
label("Midnight Deck Dash / Animate a turn", 28)Calls label with the values in its parentheses. -
status = label("Courier at tile 00", 76)Stores the new value in status. The expression calls label with the values in its parentheses. -
courier = Panel.new()Stores the new value in courier. The expression creates a new Panel object. -
courier.size = Vector2(48, 48)Updates courier.size, which controls the control's width and height. The expression packs x and y into one value. -
var paint := StyleBoxFlat.new()Creates a variable named paint. The expression creates a new StyleBoxFlat object. Godot infers the type from the first value. -
paint.bg_color = Color("#ffd16f")Stores the new value in paint.bg_color. The expression builds a colour from these values. -
paint.set_corner_radius_all(12)Calls paint.set_corner_radius_all with the values in its parentheses. -
courier.add_theme_stylebox_override("panel", paint)Calls courier.add_theme_stylebox_override with the values in its parentheses. -
add_child(courier)Adds the new node to the scene tree so that Godot can run and draw it. -
courier.position = tile_position(cell)Set the sprite's pixel position from its logical tile in one step. This path is immediate rather than animated. -
button("Left", 53, 347, move.bind(-1), 140)Calls button with the values in its parentheses. -
button("Right", 205, 347, move.bind(1), 140)Calls button with the values in its parentheses. -
button("Reset position", 357, 347, reset_turn, 152)Calls button with the values in its parentheses. -
func tile_position(index: int) -> Vector2:Defines tile position, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
return TRACK + Vector2(index * TILE + 16, 16)Finishes the function and hands its result back to the caller. The expression calculates a new value from the expressions on either side of the operator. -
func move(direction: int) -> void:Defines move, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
var next := clampi(cell + direction, 0, 8)Add the signed direction and clamp the candidate integer between the first tile 0 and last tile 8. -
if next == cell:Check if clamping left you at your current address. Leave the function early when there is no newly accepted move. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
cell = nextRecord the new logical position before the drawing begins to travel. -
if slide and slide.is_running():Check there is a Tween before asking if it is active. This protects the first move, when slide is empty. -
slide.kill()Cancel this Tween so a second animation can own the position without interference. -
slide = create_tween()Ask the current Control to create a new live animation controller. -
slide.tween_property(courier, "position", tile_position(cell), 0.21)Animate the courier's position towards the new tile centre in 0.21 seconds. -
status.text = "Courier at tile %02d" % cellUpdates status.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func reset_turn() -> void:Defines reset turn, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
if slide:Checks slide. The indented instructions below run only if this test passes. -
slide.kill()Cancel this Tween so a second animation can own the position without interference. -
cell = 0Stores the new value in cell. The expression uses the whole number 0. -
courier.position = tile_position(cell)Set the sprite's pixel position from its logical tile in one step. This path is immediate rather than animated. -
status.text = "Courier at tile 00"Updates status.text, which controls the words a player sees. The expression uses the text inside the quotes. -
func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state. -
draw_rect(Rect2(Vector2.ZERO, size), Color("#112632"))Draw the full dark blue practice backdrop from local (0, 0) to the current size. Shapes drawn later can appear above it. -
for index in range(9):Repeats the indented instructions for each value in range(9). -
var corner := TRACK + Vector2(index * TILE, 0)Creates a variable named corner. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value. -
draw_rect(Rect2(corner, Vector2(TILE - 4, TILE - 4)), Color("#30596d"))Paints a rectangle at the given position and size. -
draw_string(ThemeDB.fallback_font, corner + Vector2(7, 74), "%02d" % index, HORIZONTAL_ALIGNMENT_LEFT, -1, 16, Color("#f6edce"))Paints a piece of text with a font at a position measured from the node's origin. -
func label(text: String, y: float, font_size: int = 25) -> Label:Defines label, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
var new_label := Label.new()Create a Godot node for visible text. The colon and equals together ask GDScript to infer the type from Label.new(). -
new_label.text = textCopy the supplied string into the actual label so the learner can read it. -
new_label.position = Vector2(52, y)Use a fixed 52-pixel left margin and the caller's chosen height in local design pixels. -
new_label.add_theme_font_size_override("font_size", font_size)Give this label the caller's text size. Other labels can keep their own sizes. -
add_child(new_label)Attach the Label to the scene root so Godot can draw and manage it. -
return new_labelGive the caller this particular label so a later delivery or move can change its words. -
func button(text: String, x: float, y: float, action: Callable, width: float = 214.0) -> Button:Defines button, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
var control := Button.new()Create a GUI button. It can respond to pointer taps and to the keyboard when focused. -
control.text = textShow the caller's chosen action name on the new button. -
control.position = Vector2(x, y)Place the action at the coordinates passed by the caller. -
control.custom_minimum_size = Vector2(width, 48)Ask the Control layout for at least the supplied width and 48 design pixels of height. -
add_child(control)Connect the new button to the visible scene root. -
control.pressed.connect(action)Connect the Godot pressed signal to the Callable received as action. The action runs on a press later. -
return controlReturn this button so the caller can customise it or keep its reference.
Find and fix a bug
If the picture drifts away after reset, make sure you kill the existing Tween before resetting position.
Try a change
Ages 10–12 can try 0.45 seconds. Ages 13–16 can set Tween.TRANS_QUAD and Tween.EASE_OUT after creating the Tween, then compare the start and end speeds.