Market Day London · Lesson 06 of 50 · Beginner · About 26 min
Nia’s first trial uses a map and four blue buttons. Each press should move her exactly one square in the requested direction.

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/05 when the lesson builds on prior work, or copy the checkpoints/06 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 Button.new(), Callable, signal, bind(), clampi(), queue_redraw().
-
Read the new controls
Open Lesson 6 and inspect _ready. Find the four _add_button calls after the sign is set up.
_add_button("Up", Vector2(635, 163), _move.bind(Vector2i.UP))Why it works A single helper creates controls in the same way. Their captions and movement arguments make them behave differently.
-
Give a button touch space
Find _add_button below the drawing code. Inspect custom_minimum_size and check the corresponding button on screen.
button.custom_minimum_size = Vector2(108, 50)Why it works The 108×50 minimum leaves a clear button area. It is easier to hit than a narrow line of text.
-
Connect the press
Trace how action reaches pressed.connect. Start at the call from _ready and end at the signal line.
button.pressed.connect(action)Why it works A signal is a message a node sends. Connect stores the named callable so it runs only when pressed arrives.
-
Read a stored direction
Inspect the Right button’s _move.bind call. Translate Vector2i.RIGHT into x and y in your own notes.
_move.bind(Vector2i.RIGHT)Why it works Bind supplies an argument for later. Right is (1,0), so that press adds one to x.
-
Move and redraw
Press Right, Down and Left in that order. Compare the coordinates before and after every press.
queue_redraw()Why it works The move function adds a direction, keeps both coordinates inside the grid and redraws using the new cell.
-
Find a boundary
Keep pressing Left until x reads 0 and then press it once more. Confirm y stays the same.
nia = Vector2i(clampi(next.x, 0, COLUMNS - 1), clampi(next.y, 0, ROWS - 1))Why it works clampi keeps each coordinate between the first and last valid values. The boundary press keeps x at 0.
Watch the values change
-
Press Right from (3,3). The direction is (1,0).
What you see. Nia moves to (4,3). -
Press Down. The direction is (0,1).
What you see. Nia moves to (4,4). -
Press Left. The direction is (-1,0).
What you see. Nia moves to (3,4).
Predict before you run From (3,3), what follows two Up presses and one Left press?
Reveal the answer
Nia ends at (2,1). The two Up presses subtract 2 from y and Left subtracts 1 from x.
Complete checkpoint
Four screen buttons move the marker along the grid. The coordinates refresh after each press and remain inside the grid.
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
nia = Vector2i(clampi(next.x, 0, COLUMNS - 1), clampi(next.y, 0, ROWS - 1))
_refresh()
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 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. -
nia = Vector2i(clampi(next.x, 0, COLUMNS - 1), clampi(next.y, 0, ROWS - 1))Keep each candidate coordinate in the inclusive range. Pressing against the boundary leaves Nia on the last valid tile. -
_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.
Find and fix a bug
A button can appear and still fail to move. Check that pressed.connect is called inside _add_button and that the last argument is a callable such as _move.bind(Vector2i.RIGHT).
Try a change
Ages 10–12 can write and replay a six-move sequence. Ages 13–16 can add a Home button with a separate method that sets the starting cell and calls both refresh methods.