Market Day London · Lesson 08 of 50 · Beginner · About 28 min
A market visitor is playing on a laptop. Nia can follow the same map whether the visitor presses a real arrow key or a button on screen.

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/07 when the lesson builds on prior work, or copy the checkpoints/08 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 _input(), InputEventKey, pressed, echo, match, viewport input handling.
-
Find the input entry point
Open Lesson 8 and scroll down to _input. Compare its name with the new functions you called yourself in earlier lessons.
func _input(event: InputEvent) -> void:Why it works _input is an event callback. Godot calls it when an input event arrives.
-
Filter the events
Read the first if line aloud. Find three ways for this game to choose to ignore the event.
if not event is InputEventKey or not event.pressed or event.echo:Why it works Pointer events are not key events. Key releases are not presses. An echo is a system repeat triggered by holding a key.
-
Read the selected key
Locate match and follow its branch for KEY_UP. Compare it with the original screen Up button.
_move(Vector2i.UP)Why it works Both send Vector2i.UP to _move, so the same one-square rule works for both input methods.
-
Leave other controls alone
Find the underscore branch. Press Space or a letter in the running game and compare it with a fresh Right arrow press.
match event.keycode:Why it works The fallback returns for unrelated keys. It leaves them available for controls such as a focused button.
-
Mark the arrow as used
Check the last input line. Try tapping a real arrow while a directional button has focus.
get_viewport().set_input_as_handled()Why it works The viewport marks the event as handled, reducing the chance that another handler reacts to the same arrow.
-
Compare touch and keyboard
Run from (3,3). Click Up and press Right. Repeat after restarting using the real Up key first and the screen Right button second.
_move(Vector2i.RIGHT)Why it works The rules receive the same directions in each test. The final cell should be (4,2) both times.
Watch the values change
-
Give the window a real Up press. The key passes the filter and matches KEY_UP.
What you see. Nia moves from (3,3) to (3,2). -
Hold the arrow to trigger repeat. Extra events have echo set.
What you see. Those repeats leave Nia still. -
Tap the screen Right button. A signal supplies Vector2i.RIGHT.
What you see. Nia reaches (4,2).
Predict before you run What happens if you press Left at column 0 on a real keyboard?
Reveal the answer
The event asks for a left move, then the shared boundary check rejects it. The marker stays at column 0 and the end-of-lane message appears.
Complete checkpoint
Fresh arrow presses and screen buttons move Nia in the same way. Held-key repeats do not cause extra moves.
main.gd
extends Control
const NIGHT := Color("#0d2335")
const PANEL := Color("#17374b")
const CREAM := Color("#fff4da")
const MINT := Color("#9ce4ce")
const GOLD := Color("#ffd789")
var board: Label
const ORIGIN := Vector2(47, 134)
const TILE := 57.0
const COLUMNS := 7
const ROWS := 5
var nia := Vector2i(3, 3)
func _ready() -> void:
var title := Label.new()
title.text = "Market Day London"
title.position = Vector2(42, 34)
title.add_theme_font_size_override("font_size", 34)
title.modulate = CREAM
add_child(title)
board = Label.new()
board.position = Vector2(45, 88)
board.add_theme_font_size_override("font_size", 22)
board.modulate = MINT
add_child(board)
_refresh()
_add_button("Up", Vector2(635, 163), _move.bind(Vector2i.UP))
_add_button("Left", Vector2(517, 221), _move.bind(Vector2i.LEFT))
_add_button("Down", Vector2(635, 221), _move.bind(Vector2i.DOWN))
_add_button("Right", Vector2(753, 221), _move.bind(Vector2i.RIGHT))
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, get_viewport_rect().size), NIGHT)
draw_rect(Rect2(32, 120, 431, 316), PANEL)
draw_rect(Rect2(487, 120, 437, 316), PANEL)
for row in range(ROWS):
for column in range(COLUMNS):
var point := cell_centre(Vector2i(column, row))
var tile := Rect2(point - Vector2.ONE * 27.0, Vector2.ONE * 54.0)
var tint := Color("#31596b") if (row + column) % 2 == 0 else Color("#284d61")
draw_rect(tile, tint)
var marker := cell_centre(nia)
draw_circle(marker, 18.0, MINT)
draw_string(ThemeDB.fallback_font, marker + Vector2(-10, 5), "Nia", HORIZONTAL_ALIGNMENT_LEFT, -1, 14, NIGHT)
draw_string(ThemeDB.fallback_font, Vector2(46, 468), "BRIAR LANE • TRAINING MAP", HORIZONTAL_ALIGNMENT_LEFT, -1, 16, GOLD)
func _refresh() -> void:
board.text = "Briar Lane • Nia is at column %d, row %d" % [nia.x, nia.y]
func cell_centre(cell: Vector2i) -> Vector2:
return ORIGIN + Vector2(cell) * TILE + Vector2.ONE * (TILE / 2.0)
func _add_button(caption: String, point: Vector2, action: Callable) -> void:
var button := Button.new()
button.text = caption
button.position = point
button.custom_minimum_size = Vector2(108, 50)
button.pressed.connect(action)
add_child(button)
func _move(step: Vector2i) -> void:
var next := nia + step
if not Rect2i(0, 0, COLUMNS, ROWS).has_point(next):
board.text = "End of the lane • Nia stays put."
return
nia = next
_refresh()
queue_redraw()
func _input(event: InputEvent) -> void:
if not event is InputEventKey or not event.pressed or event.echo:
return
match event.keycode:
KEY_UP:
_move(Vector2i.UP)
KEY_LEFT:
_move(Vector2i.LEFT)
KEY_DOWN:
_move(Vector2i.DOWN)
KEY_RIGHT:
_move(Vector2i.RIGHT)
_:
return
get_viewport().set_input_as_handled()
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 these screen-building instructions to a Godot Control. That gives the script positions, children and input events. -
const NIGHT := Color("#0d2335")Use this dark ink colour behind the market. The fixed colour is also used for lettering on the mint marker and brighter signs. -
const PANEL := Color("#17374b")Use one lighter shade to group the map and controls against the dark background. -
const CREAM := Color("#fff4da")Keep the title and labels light enough to read against dark panels. -
const MINT := Color("#9ce4ce")Use this accent to highlight the status label and Nia’s marker. -
const GOLD := Color("#ffd789")Mark utility labels and the collection point with a pale yellow that stands apart from the paving. -
var board: LabelSave the opening label so later methods can change it without needing to create a new one. -
const ORIGIN := Vector2(47, 134)Set the pixel position of the upper-left corner used to lay out the market grid. -
const TILE := 57.0Keep neighbouring tile centres 57 pixels apart. -
const COLUMNS := 7Give the training lane seven columns with indices from 0 to 6. -
const ROWS := 5Give the training lane five rows with indices from 0 to 4. -
var nia := Vector2i(3, 3)Start Nia at column 3 and row 3. Store grid values instead of pixel coordinates. -
func _ready() -> void:Godot calls this once when the Market scene is ready to create the title, notice and controls. -
var title := Label.new()Create an unattached Label for the permanent title. It becomes visible when it is added to the scene. -
title.text = "Market Day London"Put the game name into the title label. The title stays fixed while smaller words underneath change. -
title.position = Vector2(42, 34)Leave a margin of 42 pixels from the left and 34 from the top. -
title.add_theme_font_size_override("font_size", 34)Set this title to a larger local font size. The rest of the labels can keep different sizes. -
title.modulate = CREAMSet the title’s drawing colour to the pale cream chosen above. -
add_child(title)Add the title to the running scene so Godot draws and maintains it. -
board = Label.new()Create the changing notice label and store it in the field used by other methods. -
board.position = Vector2(45, 88)Position the variable status words underneath the game name. -
board.add_theme_font_size_override("font_size", 22)Set a smaller font for status messages, which can be longer than the title. -
board.modulate = MINTTint the notice mint so it stands out as the changing information. -
add_child(board)Attach the notice beneath the title so the player sees the current message. -
_refresh()Write the latest cell location into the notice without creating another label. -
_add_button("Up", Vector2(635, 163), _move.bind(Vector2i.UP))Place the Up button above the centre of the direction button cluster. It later supplies a step of (0,-1). -
_add_button("Left", Vector2(517, 221), _move.bind(Vector2i.LEFT))Place the Left button to the left of Down. It supplies a step of (-1,0). -
_add_button("Down", Vector2(635, 221), _move.bind(Vector2i.DOWN))Place the Down button below Up. It supplies a step of (0,1). -
_add_button("Right", Vector2(753, 221), _move.bind(Vector2i.RIGHT))Place the Right button beside Down. It supplies a step of (1,0). -
func _draw() -> void:Paint this section when Godot refreshes the Control. Draw the background first and the frontmost details last. -
draw_rect(Rect2(Vector2.ZERO, get_viewport_rect().size), NIGHT)Fill the visible window so there is a consistent dark background behind the game panels. -
draw_rect(Rect2(32, 120, 431, 316), PANEL)Draw the dark rectangle under the tile map. -
draw_rect(Rect2(487, 120, 437, 316), PANEL)Draw an adjacent rectangle behind the controls and basket information. -
for row in range(ROWS):Visit each row index from 0 through 4. -
for column in range(COLUMNS):For each row, visit column indices from 0 through 6. -
var point := cell_centre(Vector2i(column, row))Convert this loop pair into the pixel centre for one paving square. -
var tile := Rect2(point - Vector2.ONE * 27.0, Vector2.ONE * 54.0)Give the tile a 54-pixel square shape. The 57-pixel spacing leaves a small border between squares. -
var tint := Color("#31596b") if (row + column) % 2 == 0 else Color("#284d61")Use the remainder to alternate the paving colour. Equal parities use the same ink. -
draw_rect(tile, tint)Place the current square using the pixel rectangle and the chosen tint. -
var marker := cell_centre(nia)Convert the saved grid cell to the exact point where Nia’s visual marker belongs. -
draw_circle(marker, 18.0, MINT)Paint the small mint circle over the map paving or stall square. -
draw_string(ThemeDB.fallback_font, marker + Vector2(-10, 5), "Nia", HORIZONTAL_ALIGNMENT_LEFT, -1, 14, NIGHT)Put Nia’s name inside the circle so the marker does not rely on its colour alone. -
draw_string(ThemeDB.fallback_font, Vector2(46, 468), "BRIAR LANE • TRAINING MAP", HORIZONTAL_ALIGNMENT_LEFT, -1, 16, GOLD)Label the map with a quiet footer set apart from the playable square area. -
func _refresh() -> void:Update the status line from Nia’s currently stored map cell. -
board.text = "Briar Lane • Nia is at column %d, row %d" % [nia.x, nia.y]Read x as the column and y as the row, then show both values. -
func cell_centre(cell: Vector2i) -> Vector2:Convert a whole-number tile position into a pixel point for drawing. -
return ORIGIN + Vector2(cell) * TILE + Vector2.ONE * (TILE / 2.0)Multiply the tile coordinate by the 57-pixel spacing, start at ORIGIN and move another half tile to reach its centre. -
func _add_button(caption: String, point: Vector2, action: Callable) -> void:Receive a caption, screen position and callable to reuse the same control construction for different actions. -
var button := Button.new()Create a Godot button that the player will be able to activate after it is attached. -
button.text = captionUse the words provided by the caller to explain what this button does. -
button.position = pointPlace the button using the point sent by _ready. -
button.custom_minimum_size = Vector2(108, 50)Reserve a large area for the button so its label and touch target are easier to distinguish. -
button.pressed.connect(action)Tell Godot which stored action should run when the button emits its pressed signal. -
add_child(button)Put the button into the active scene so it can display and receive input. -
func _move(step: Vector2i) -> void:Receive one direction as a whole-number change to Nia’s column and row. -
var next := nia + stepCalculate the proposed grid cell. Nia is still at the saved cell while the game checks the proposal. -
if not Rect2i(0, 0, COLUMNS, ROWS).has_point(next):Ask whether the proposal is outside this zero-based rectangle. Points on the far right or bottom border are outside. -
board.text = "End of the lane • Nia stays put."Explain the rejected press while leaving the saved grid position alone. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
nia = nextAccept the new position only after the bounds branch has been passed. -
_refresh()Write the latest cell location into the notice without creating another label. -
queue_redraw()Request a repaint so Nia’s marker now appears at the new cell. -
func _input(event: InputEvent) -> void:Receive a Godot input event. Keyboard events and button signals meet at the same move method. -
if not event is InputEventKey or not event.pressed or event.echo:Ignore non-key events, key releases and automatic repeats. Only a fresh key press goes further. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
match event.keycode:Compare the key that arrived with the four arrow values. -
KEY_UP:Match the actual Up arrow, then ask for a one-tile move toward the top of the screen. -
_move(Vector2i.UP)Calls _move with the values in its parentheses. -
KEY_LEFT:Match the actual Left arrow, then ask for a one-column move to the left. -
_move(Vector2i.LEFT)Calls _move with the values in its parentheses. -
KEY_DOWN:Match the actual Down arrow, then ask for a one-tile move toward the bottom of the screen. -
_move(Vector2i.DOWN)Calls _move with the values in its parentheses. -
KEY_RIGHT:Match the actual Right arrow, then ask for a one-column move to the right. -
_move(Vector2i.RIGHT)Calls _move with the values in its parentheses. -
_:Recognise every other key as unrelated to market movement. -
returnFinishes the function right here. Any later instructions in this function are skipped. -
get_viewport().set_input_as_handled()Tell Godot this arrow was used for movement so another handler can avoid acting on it.
Find and fix a bug
If the map never responds to arrows, first click inside the running game window. Then check method spelling, the KEY_* constants and the key filter.
Try a change
Ages 10–12 can reach one square using both sets of controls. Ages 13–16 can add W/A/S/D with extra match patterns while reusing the same movement method.