Midnight Deck Dash · Lesson 35 of 50 · Intermediate to advanced · About 36 min
A phone player taps the pad. A learner testing on a computer can use the keyboard. The game should respond the same way.

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/34 when the lesson builds on prior work, or copy the checkpoints/35 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 GridContainer, Vector2i, InputEventKey, event.echo, resized signal.
-
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.
-
Set up a button grid
Find pad.columns. Change it from 2 to 1 and run once, then restore 2.
pad.columns = 2Why it works A GridContainer handles the spacing of its children. The column count changes the arrangement without moving each button separately.
-
Give every button a direction
Read make_direction and compare the argument passed for Up with the argument for Down. Use the on-screen buttons.
action.pressed.connect(move.bind(direction))Why it works Every button binds a Vector2i direction to move. A minimum size creates a large tap area.
-
Route the arrow keys
Restart and press arrow keys before clicking a button. Then compare the effect of the matching pad button.
if event is InputEventKey and event.pressed and not event.echo and INPUTS.has(event.keycode):Why it works Godot gives unhandled keyboard events here after controls have a chance to use them. The checks accept arrow presses and ignore automatic echoes.
-
Clamp both coordinates
Move beyond row 0, then beyond column 4. Find both clampi calls in move.
cell = Vector2i(clampi(target.x, 0, 4), clampi(target.y, 0, 4))Why it works x and y must remain in the legal ranges independently. Touch and keyboard input call the same function, so they get the same boundaries.
-
Make a compact layout
Find place_pad. For a landscape test keep the threshold at 565. To preview the stacked version in a wide viewport, temporarily change 565.0 to 1500.0 and compare.
if size.x >= 565.0:Why it works When the Control resizes, place_pad chooses whether the pad sits beside or below the board. Real phone testing can check screen edges and grip.
Watch the values change
-
Tap Right The button supplies (1, 0).
What you see. The courier moves one column east. -
Press Down The key handler supplies (0, 1).
What you see. The courier moves one row south. -
Press Down at row 4 The candidate row 5 clamps to 4.
What you see. The marker stays on the deck.
Predict before you run What happens when the courier is already in column 4 and the player presses Right?
Reveal the answer
The candidate column is 5. It clamps back to 4 and the row stays unchanged.
Complete checkpoint
A five-by-five practice deck responds to keyboard arrows and on-screen controls. Its pad has wide and compact positions.
main.gd
extends Control
const INPUTS := {
KEY_UP: Vector2i(0, -1), KEY_DOWN: Vector2i(0, 1),
KEY_LEFT: Vector2i(-1, 0), KEY_RIGHT: Vector2i(1, 0)
}
var cell := Vector2i(2, 2)
var status: Label
var pad: GridContainer
func _ready() -> void:
label("Midnight Deck Dash / Thumb controls", 29)
status = label("Courier at column 2, row 2.", 78)
pad = GridContainer.new()
pad.columns = 2
pad.add_theme_constant_override("h_separation", 10)
pad.add_theme_constant_override("v_separation", 10)
add_child(pad)
make_direction("Up", Vector2i(0, -1))
make_direction("Left", Vector2i(-1, 0))
make_direction("Down", Vector2i(0, 1))
make_direction("Right", Vector2i(1, 0))
resized.connect(place_pad)
place_pad()
func make_direction(title: String, direction: Vector2i) -> void:
var action := Button.new()
action.text = title
action.custom_minimum_size = Vector2(112, 58)
action.pressed.connect(move.bind(direction))
pad.add_child(action)
func place_pad() -> void:
if size.x >= 565.0:
pad.position = Vector2(size.x - 285.0, 219)
else:
pad.position = Vector2(52, 361)
queue_redraw()
func _unhandled_key_input(event: InputEvent) -> void:
if event is InputEventKey and event.pressed and not event.echo and INPUTS.has(event.keycode):
move(INPUTS[event.keycode])
get_viewport().set_input_as_handled()
func move(direction: Vector2i) -> void:
var target := cell + direction
cell = Vector2i(clampi(target.x, 0, 4), clampi(target.y, 0, 4))
status.text = "Courier at column %d, row %d." % [cell.x, cell.y]
queue_redraw()
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#112632"))
for y in range(5):
for x in range(5):
var corner := Vector2(52, 143) + Vector2(x, y) * 41
draw_rect(Rect2(corner, Vector2(37, 37)), Color("#34596a"))
draw_circle(Vector2(70, 161) + Vector2(cell) * 41, 14, Color("#ffdc86"))
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 INPUTS := {Creates a constant named INPUTS. The expression makes a dictionary that looks up values by key. Godot infers the type from the first value. -
KEY_UP: Vector2i(0, -1), KEY_DOWN: Vector2i(0, 1),Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
KEY_LEFT: Vector2i(-1, 0), KEY_RIGHT: Vector2i(1, 0)Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
}Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
var cell := Vector2i(2, 2)Creates a variable named cell. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value. -
var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here. -
var pad: GridContainerDeclares pad 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 / Thumb controls", 29)Calls label with the values in its parentheses. -
status = label("Courier at column 2, row 2.", 78)Stores the new value in status. The expression calls label with the values in its parentheses. -
pad = GridContainer.new()Create a layout node to place its child controls in evenly arranged rows. -
pad.columns = 2Put two action buttons in each row of the pad. -
pad.add_theme_constant_override("h_separation", 10)Calls pad.add_theme_constant_override with the values in its parentheses. -
pad.add_theme_constant_override("v_separation", 10)Calls pad.add_theme_constant_override with the values in its parentheses. -
add_child(pad)Adds the new node to the scene tree so that Godot can run and draw it. -
make_direction("Up", Vector2i(0, -1))Calls make_direction with the values in its parentheses. -
make_direction("Left", Vector2i(-1, 0))Calls make_direction with the values in its parentheses. -
make_direction("Down", Vector2i(0, 1))Calls make_direction with the values in its parentheses. -
make_direction("Right", Vector2i(1, 0))Calls make_direction with the values in its parentheses. -
resized.connect(place_pad)Ask the layout to choose the pad's position each time the root Control has a new size. -
place_pad()Calls place_pad with the values in its parentheses. -
func make_direction(title: String, direction: Vector2i) -> void:Defines make direction, 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 action := Button.new()Creates a variable named action. The expression creates a new Button object. Godot infers the type from the first value. -
action.text = titleUpdates action.text, which controls the words a player sees. The expression reads title. -
action.custom_minimum_size = Vector2(112, 58)Updates action.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value. -
action.pressed.connect(move.bind(direction))Keep this button's direction in the signal connection so a future press calls the same movement rule with the right direction. -
pad.add_child(action)Adds the new node to the scene tree so that Godot can run and draw it. -
func place_pad() -> void:Defines place pad, 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 size.x >= 565.0:Check the current Control width in design coordinates. Wide controls keep the pad beside the practice board. -
pad.position = Vector2(size.x - 285.0, 219)Updates pad.position, which controls the node's local x and y location. The expression packs x and y into one value. -
else:Runs the indented fallback instructions if none of the earlier tests passed. -
pad.position = Vector2(52, 361)Set the fallback pad position below the grid when horizontal space is limited. -
queue_redraw()Asks Godot to call _draw again on a later frame so the screen matches the new state. -
func _unhandled_key_input(event: InputEvent) -> void:Defines unhandled key input, 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 event is InputEventKey and event.pressed and not event.echo and INPUTS.has(event.keycode):Handle only an actual first key press and only one of the four explicitly mapped keycodes. -
move(INPUTS[event.keycode])Read the remembered Vector2i for the pressed key and pass it into the same move function the buttons use. -
get_viewport().set_input_as_handled()Mark the event as used so no further unhandled-input reader runs a second move for the same key. -
func move(direction: Vector2i) -> 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 target := cell + directionCreates a variable named target. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value. -
cell = Vector2i(clampi(target.x, 0, 4), clampi(target.y, 0, 4))Bound both integer coordinates to 0 through 4, then construct the new tile address. -
status.text = "Courier at column %d, row %d." % [cell.x, cell.y]Updates status.text, which controls the words a player sees. The expression uses the text inside the quotes. -
queue_redraw()Asks Godot to call _draw again on a later frame so the screen matches the new state. -
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 y in range(5):Repeats the indented instructions for each value in range(5). -
for x in range(5):Repeats the indented instructions for each value in range(5). -
var corner := Vector2(52, 143) + Vector2(x, y) * 41Creates a variable named corner. The expression packs x and y into one value. Godot infers the type from the first value. -
draw_rect(Rect2(corner, Vector2(37, 37)), Color("#34596a"))Paints a rectangle at the given position and size. -
draw_circle(Vector2(70, 161) + Vector2(cell) * 41, 14, Color("#ffdc86"))Paints a filled circle or outline in this node's local coordinates. -
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 arming the keyboard seems inconsistent, test before a menu button gains focus. Menu buttons can handle arrows during navigation. Hold an arrow and check not event.echo still guards repeated key presses.
Try a change
Ages 10–12 can test all four deck edges from both controls. Ages 13–16 can replace clamping with a can_enter rule that blocks marked wall tiles as well.