Tiny Shelf Studio · Lesson 34 of 50 · Intermediate to advanced · About 35 min
Colour adds character to tiny wrappers. A teammate using a monochrome screen still needs to identify products and see the selection.

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/33 when the lesson builds on prior work, or copy the checkpoints/34 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 Boolean, ternary expression, for, theme overrides, Button.focus_mode.
-
Open the design bench
Import this checkpoint's project.godot into Godot 4.7.2. Open main.gd from FileSystem. Press F5 for the starting view and F8 to stop it. Keep a copy to undo experiments.
Why it works The checkpoint scene attaches main.gd to a Control. Each test starts from that known version, so compare one change with the original.
-
Keep meaning in stable words
Read the names and categories in each array. Change the second category to BAKERY SNACKS. Compare the shelf card before and after.
const GROUPS := ["DRINKS", "SNACKS", "CRAFTS"]Why it works The text stays available if the tint is hard to distinguish. Keep the names and groups in the same order.
-
Change selection and check the mark
Press Pocket Paints. Follow selected through refresh. Check the mark moves and the status says the new name.
var prefix := "✓ " if selected == i else "○ "Why it works The comparison is true for exactly one index. The prefix changes independently from the palette.
-
Check a high-contrast palette
Turn High contrast on. Look for white text and black fills. Run the check again with the contrast off.
var ink: Color = Color.WHITE if high else TINTS[i]Why it works The toggle stores a Boolean. An expression can choose the ink and fill together from the same state.
-
Preserve the text in both styles
Compare the card label with the toggle on and off. Change the separator from a dot to a slash and preview each style.
buttons[i].text = prefix + NAMES[i] + " · " + GROUPS[i]Why it works The text statement does not depend on high. It keeps category words and selection marks in both palettes.
-
Navigate without a mouse
Press F5 and move focus with Tab. Select another product with Enter. Verify the gold outline and selected mark serve different roles.
control.focus_mode = Control.FOCUS_ALLWhy it works Focus shows which control will activate. The tick shows which product the game has already selected.
Watch the values change
-
Load the shelf selected is 0 and high is false.
What you see. Moon Milk has a tick and a DRINKS label. -
Choose Pocket Paints selected becomes 2.
What you see. Its card has a tick and the status shows Pocket Paints. -
Switch to High contrast high is true but selected is still 2.
What you see. The cards show white on black and the labels stay the same.
Predict before you run If a player changes the palette, does their selected product change?
Reveal the answer
No. The toggle updates high and redraws the labels. selected changes when select_item runs.
Complete checkpoint
Three labelled product cards, a text selection message and a working high-contrast toggle.
main.gd
extends Control
const NAMES := ["Moon Milk", "Comet Crisps", "Pocket Paints"]
const GROUPS := ["DRINKS", "SNACKS", "CRAFTS"]
const TINTS := [Color("#f0b8a4"), Color("#abe0d2"), Color("#b6d1f4")]
var buttons: Array[Button] = []
var selected := 0
var contrast: Button
var status: Label
func _ready() -> void:
title_page("ACCESSIBILITY", "Make the shelf readable without colour", "Look for a written category and a selection mark on every card.")
for i in range(NAMES.size()):
var control := place_button("", Vector2(57, 214 + 61 * i), select_item.bind(i), 365)
control.focus_mode = Control.FOCUS_ALL
buttons.append(control)
status = label("", Vector2(496, 240), 23, SEA)
contrast = place_button("High contrast", Vector2(496, 321), refresh, 190)
contrast.toggle_mode = true
refresh()
buttons[0].grab_focus()
func select_item(index: int) -> void:
selected = index
refresh()
func refresh() -> void:
var high := contrast.button_pressed
for i in range(buttons.size()):
var prefix := "✓ " if selected == i else "○ "
var ink: Color = Color.WHITE if high else TINTS[i]
var fill := Color.BLACK if high else Color("#244456")
buttons[i].text = prefix + NAMES[i] + " · " + GROUPS[i]
buttons[i].add_theme_color_override("font_color", ink)
buttons[i].add_theme_stylebox_override("normal", style(fill, ink, 2))
contrast.text = "High contrast: on" if high else "High contrast: off"
status.text = "Selected · " + NAMES[selected]
func _draw() -> void:
backdrop()
panel(Rect2(470, 188, 432, 223), Color("#244457"), GOLD)
const PAPER := Color("#fff4de")
const GOLD := Color("#f5bd79")
const SEA := Color("#9ddcd0")
const INK := Color("#172c3c")
const EDGE := Color("#54817f")
func title_page(unit: String, heading: String, subtitle: String) -> void:
label("TINY SHELF STUDIO / " + unit, Vector2(44, 36), 14, GOLD)
label(heading, Vector2(44, 65), 33)
label(subtitle, Vector2(46, 119), 16, SEA)
func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> Label:
var item := Label.new()
item.text = words
item.position = at
item.add_theme_font_size_override("font_size", pixels)
item.add_theme_color_override("font_color", ink)
add_child(item)
return item
func place_button(words: String, at: Vector2, action: Callable, width: float = 170.0) -> Button:
var control := Button.new()
control.text = words
control.position = at
control.custom_minimum_size = Vector2(width, 48)
control.add_theme_font_size_override("font_size", 17)
control.add_theme_color_override("font_color", PAPER)
control.add_theme_stylebox_override("normal", style(Color("#244557"), EDGE))
control.add_theme_stylebox_override("hover", style(Color("#365a62"), SEA))
control.add_theme_stylebox_override("pressed", style(Color("#315b56"), SEA))
control.add_theme_stylebox_override("focus", style(Color.TRANSPARENT, GOLD, 3))
control.pressed.connect(action)
add_child(control)
return control
func style(fill: Color, edge: Color, line: int = 1) -> StyleBoxFlat:
var block := StyleBoxFlat.new()
block.bg_color = fill
block.border_color = edge
block.set_border_width_all(line)
block.set_corner_radius_all(8)
return block
func panel(rect: Rect2, fill: Color, edge: Color = EDGE) -> void:
draw_style_box(style(fill, edge), rect)
func backdrop() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#102333"))
panel(Rect2(22, 19, size.x - 44, size.y - 38), Color("#183344"), Color("#325665"))
func paint_words(words: String, at: Vector2, pixels: int = 16, ink: Color = PAPER) -> void:
draw_string(ThemeDB.fallback_font, at, words, HORIZONTAL_ALIGNMENT_LEFT, -1, pixels, ink)
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 ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules. -
const NAMES := ["Moon Milk", "Comet Crisps", "Pocket Paints"]Creates a constant named NAMES. The expression makes a list whose items keep their order. Godot infers the type from the first value. -
const GROUPS := ["DRINKS", "SNACKS", "CRAFTS"]The categories follow the same order as the names, so one index reads both. -
const TINTS := [Color("#f0b8a4"), Color("#abe0d2"), Color("#b6d1f4")]Creates a constant named TINTS. The expression makes a list whose items keep their order. Godot infers the type from the first value. -
var buttons: Array[Button] = []Creates a variable named buttons. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold. -
var selected := 0Creates a variable named selected. The expression uses the whole number 0. Godot infers the type from the first value. -
var contrast: ButtonDeclares contrast 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. -
func _ready() -> void:Godot calls this once after the scene and its children are ready. Use it to set up the room and connect controls. -
title_page("ACCESSIBILITY", "Make the shelf readable without colour", "Look for a written category and a selection mark on every card.")Calls title_page with the values in its parentheses. -
for i in range(NAMES.size()):Repeats the indented instructions for each value in range(NAMES.size()). -
var control := place_button("", Vector2(57, 214 + 61 * i), select_item.bind(i), 365)Creates a variable named control. The expression calls place_button with the values in its parentheses. Godot infers the type from the first value. -
control.focus_mode = Control.FOCUS_ALLAllow the player to reach each card with Tab. The toggle and cards are separate controls. -
buttons.append(control)Keep references to the created controls so refresh can update them later. -
status = label("", Vector2(496, 240), 23, SEA)Stores the new value in status. The expression calls label with the values in its parentheses. -
contrast = place_button("High contrast", Vector2(496, 321), refresh, 190)Stores the new value in contrast. The expression calls place_button with the values in its parentheses. -
contrast.toggle_mode = trueStores the new value in contrast.toggle_mode. The expression uses the yes-or-no value true. -
refresh()Calls refresh with the values in its parentheses. -
buttons[0].grab_focus()Continues this part of the script. Read the expression on this line in the context of the nearby declaration or function. -
func select_item(index: int) -> void:Defines select item, 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. -
selected = indexStore a new selected product before applying the visible tick and text. -
refresh()Calls refresh with the values in its parentheses. -
func refresh() -> void:Defines refresh, 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 high := contrast.button_pressedRead the remembered toggle state once before redrawing the cards. -
for i in range(buttons.size()):Repeats the indented instructions for each value in range(buttons.size()). -
var prefix := "✓ " if selected == i else "○ "Use a tick for the selected card and a hollow circle for other cards. The difference survives a monochrome screen. -
var ink: Color = Color.WHITE if high else TINTS[i]Use the same clear ink for every card when high contrast is on. -
var fill := Color.BLACK if high else Color("#244456")Darken the card background in high-contrast mode. Keep the text contents untouched. -
buttons[i].text = prefix + NAMES[i] + " · " + GROUPS[i]Updates buttons[i].text, which controls the words a player sees. The expression looks up the item or key written between the square brackets. -
buttons[i].add_theme_color_override("font_color", ink)Continues this part of the script. Read the expression on this line in the context of the nearby declaration or function. -
buttons[i].add_theme_stylebox_override("normal", style(fill, ink, 2))Continues this part of the script. Read the expression on this line in the context of the nearby declaration or function. -
contrast.text = "High contrast: on" if high else "High contrast: off"Updates contrast.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle. -
status.text = "Selected · " + NAMES[selected]Say which card is selected in full words as a second cue. -
func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state. -
backdrop()Calls backdrop with the values in its parentheses. -
panel(Rect2(470, 188, 432, 223), Color("#244457"), GOLD)Calls panel with the values in its parentheses. -
const PAPER := Color("#fff4de")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value. -
const GOLD := Color("#f5bd79")Creates a constant named GOLD. The expression builds a colour from these values. Godot infers the type from the first value. -
const SEA := Color("#9ddcd0")Creates a constant named SEA. The expression builds a colour from these values. Godot infers the type from the first value. -
const INK := Color("#172c3c")Creates a constant named INK. The expression builds a colour from these values. Godot infers the type from the first value. -
const EDGE := Color("#54817f")Creates a constant named EDGE. The expression builds a colour from these values. Godot infers the type from the first value. -
func title_page(unit: String, heading: String, subtitle: String) -> void:Defines title page, 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. -
label("TINY SHELF STUDIO / " + unit, Vector2(44, 36), 14, GOLD)Make a small line above the main heading so you can tell which checkpoint is open. -
label(heading, Vector2(44, 65), 33)Calls label with the values in its parentheses. -
label(subtitle, Vector2(46, 119), 16, SEA)Calls label with the values in its parentheses. -
func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> 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 item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value. -
item.text = wordsUpdates item.text, which controls the words a player sees. The expression reads words. -
item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at. -
item.add_theme_font_size_override("font_size", pixels)This Label has its own readable type size. Changing that value leaves the other labels alone. -
item.add_theme_color_override("font_color", ink)This label keeps the chosen text ink even when the default theme changes. -
add_child(item)Adds the new node to the scene tree so that Godot can run and draw it. -
return itemFinishes the function and hands its result back to the caller. The expression reads item. -
func place_button(words: String, at: Vector2, action: Callable, width: float = 170.0) -> Button:Defines place 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()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value. -
control.text = wordsUpdates control.text, which controls the words a player sees. The expression reads words. -
control.position = atUpdates control.position, which controls the node's local x and y location. The expression reads at. -
control.custom_minimum_size = Vector2(width, 48)Give the button room for its label and an easy target for taps. Containers and layouts can allocate more space. -
control.add_theme_font_size_override("font_size", 17)Calls control.add_theme_font_size_override with the values in its parentheses. -
control.add_theme_color_override("font_color", PAPER)Calls control.add_theme_color_override with the values in its parentheses. -
control.add_theme_stylebox_override("normal", style(Color("#244557"), EDGE))Calls control.add_theme_stylebox_override with the values in its parentheses. -
control.add_theme_stylebox_override("hover", style(Color("#365a62"), SEA))Calls control.add_theme_stylebox_override with the values in its parentheses. -
control.add_theme_stylebox_override("pressed", style(Color("#315b56"), SEA))Calls control.add_theme_stylebox_override with the values in its parentheses. -
control.add_theme_stylebox_override("focus", style(Color.TRANSPARENT, GOLD, 3))Draw a gold outline around keyboard focus. Other button styles can still indicate hover and press independently. -
control.pressed.connect(action)pressed fires on a successful control activation. Godot calls the function passed in without running it at setup time. -
add_child(control)Adds the new node to the scene tree so that Godot can run and draw it. -
return controlFinishes the function and hands its result back to the caller. The expression reads control. -
func style(fill: Color, edge: Color, line: int = 1) -> StyleBoxFlat:Defines style, 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 block := StyleBoxFlat.new()Creates a variable named block. The expression creates a new StyleBoxFlat object. Godot infers the type from the first value. -
block.bg_color = fillStores the new value in block.bg_color. The expression reads fill. -
block.border_color = edgeStores the new value in block.border_color. The expression reads edge. -
block.set_border_width_all(line)Apply one border line width to all four edges of the card. -
block.set_corner_radius_all(8)Round each corner of this reusable style by eight pixels. -
return blockFinishes the function and hands its result back to the caller. The expression reads block. -
func panel(rect: Rect2, fill: Color, edge: Color = EDGE) -> void:Defines panel, 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. -
draw_style_box(style(fill, edge), rect)Calls draw_style_box with the values in its parentheses. -
func backdrop() -> void:Defines backdrop, 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. -
draw_rect(Rect2(Vector2.ZERO, size), Color("#102333"))Paints a rectangle at the given position and size. -
panel(Rect2(22, 19, size.x - 44, size.y - 38), Color("#183344"), Color("#325665"))Subtract space on both sides of the canvas. As the window changes, the outer frame fits within it. -
func paint_words(words: String, at: Vector2, pixels: int = 16, ink: Color = PAPER) -> void:Defines paint words, 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. -
draw_string(ThemeDB.fallback_font, at, words, HORIZONTAL_ALIGNMENT_LEFT, -1, pixels, ink)Godot draws the words from the baseline at the supplied pixel position using its fallback typeface.
Find and fix a bug
If keyboard focus becomes hard to spot after a restyle, keep the separate focus style alongside the normal background.
Try a change
Add a small text legend that explains the tick and hollow circle. Test it with a classmate using the keyboard only.