Tiny Shelf Studio · Lesson 31 of 50 · Intermediate to advanced · About 35 min
A shelf looks more convincing when you can tell a snack tin from a milk carton at a glance. Build the tiny wrappers from shapes the game can redraw.

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/30 when the lesson builds on prior work, or copy the checkpoints/31 folder to try this lesson on its own. Read the file names in the steps before editing. Keep the checkpoint ZIP intact and make edits in an extracted working copy.


Build it step by step
Language and tools in this lesson CanvasItem._draw, draw_rect, draw_style_box, Color, Rect2, queue_redraw.
-
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.
-
Describe three wrappers
Find WRAPPERS. Swap Moon Milk’s ink for #a64b39 and run again. Compare the carton with the other wrappers.
var wrapper := Color(str(item["ink"]))Why it works Each dictionary names the properties for one product. The draw function reads these keys for every box.
-
Place the package on a shelf card
Locate front. Change its x offset from 81 to 66. Run and watch all three packages shift left inside their cards.
var front := Rect2(bay.position + Vector2(81, 32), Vector2(108, 138))Why it works Rect2 stores a position and size. Adding an offset puts the package inside its own card.
-
Give the front a shadow
In package_front, compare the two panel calls. Change the shadow offset from (8, 10) to (14, 15) and run.
panel(Rect2(front.position + Vector2(8, 10), front.size), INK)Why it works The earlier drawing sits beneath the later drawing. Adding x moves right and adding y moves down.
-
Leave room for the lettering
Find decal, then change its y offset from 35 to 39. The letters stay where they were. Restore the offset or move the letters down by four pixels too.
draw_rect(decal, PAPER)Why it works The stripe and the letters have independent coordinates. The contrast stays clearest while the full letters fit on the paper.
-
Wrap around the catalogue
Tap Next wrapper four times. Check the selected outline and caption match. Predict what happens after card 3.
selected = (selected + 1) % WRAPPERS.size()Why it works The remainder of dividing 3 by 3 is 0. queue_redraw asks Godot to repaint the selected outline using the new index.
Watch the values change
-
Open the bench selected is 0.
What you see. The outline frames Moon Milk and its caption reads Wrapper 1. -
Tap Next wrapper selected changes from 0 to 1.
What you see. The highlight and caption move to Comet Crisps. -
Advance past Pocket Paints (2 + 1) % 3 becomes 0.
What you see. Moon Milk is selected again.
Predict before you run Which part will cover the product letters if you move the decal drawing below both paint_words calls?
Reveal the answer
The paper stripe will sit on top and hide the covered letters. Keep the stripe call first.
Complete checkpoint
Three small original packages with a wrapper caption and outline that cycle together.
main.gd
extends Control
const WRAPPERS := [
{"name": "Moon Milk", "kind": "CARTON", "ink": "#b85c52", "mark": "MM"},
{"name": "Comet Crisps", "kind": "TIN", "ink": "#498e83", "mark": "CC"},
{"name": "Pocket Paints", "kind": "BOX", "ink": "#657fa0", "mark": "PP"},
]
var selected := 0
var caption: Label
func _ready() -> void:
title_page("PACKAGING", "Draw a tiny package in layers", "Put a shadow, wrapper, label and mark on separate layers.")
caption = label("Wrapper 1 · Moon Milk", Vector2(46, 436), 22, GOLD)
place_button("Next wrapper", Vector2(705, 425), next_wrapper)
func next_wrapper() -> void:
selected = (selected + 1) % WRAPPERS.size()
caption.text = "Wrapper %d · %s" % [selected + 1, WRAPPERS[selected]["name"]]
queue_redraw()
func _draw() -> void:
backdrop()
for i in range(WRAPPERS.size()):
var bay := Rect2(50 + 290 * i, 166, 268, 232)
var edge := GOLD if i == selected else EDGE
panel(bay, Color("#203f51"), edge)
var item: Dictionary = WRAPPERS[i]
var front := Rect2(bay.position + Vector2(81, 32), Vector2(108, 138))
package_front(front, item)
paint_words(str(item["name"]), bay.position + Vector2(34, 202), 19)
func package_front(front: Rect2, item: Dictionary) -> void:
var wrapper := Color(str(item["ink"]))
var decal := Rect2(front.position + Vector2(9, 35), Vector2(90, 76))
panel(Rect2(front.position + Vector2(8, 10), front.size), INK)
panel(front, wrapper, PAPER)
draw_rect(decal, PAPER)
paint_words(str(item["mark"]), front.position + Vector2(25, 77), 32, INK)
paint_words(str(item["kind"]), front.position + Vector2(14, 99), 15, INK)
draw_line(front.position + Vector2(12, 118), front.position + Vector2(96, 118), PAPER, 2)
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 WRAPPERS := [Keep one fixed array of product dictionaries. The names, package shapes, colours and short marks are data you can draw repeatedly. -
{"name": "Moon Milk", "kind": "CARTON", "ink": "#b85c52", "mark": "MM"},Starts a list or dictionary used as part of the instruction above. -
{"name": "Comet Crisps", "kind": "TIN", "ink": "#498e83", "mark": "CC"},Starts a list or dictionary used as part of the instruction above. -
{"name": "Pocket Paints", "kind": "BOX", "ink": "#657fa0", "mark": "PP"},Starts a list or dictionary used as part of the instruction above. -
]Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
var selected := 0Creates a variable named selected. The expression uses the whole number 0. Godot infers the type from the first value. -
var caption: LabelDeclares caption 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("PACKAGING", "Draw a tiny package in layers", "Put a shadow, wrapper, label and mark on separate layers.")Calls title_page with the values in its parentheses. -
caption = label("Wrapper 1 · Moon Milk", Vector2(46, 436), 22, GOLD)Stores the new value in caption. The expression calls label with the values in its parentheses. -
place_button("Next wrapper", Vector2(705, 425), next_wrapper)Calls place_button with the values in its parentheses. -
func next_wrapper() -> void:Defines next wrapper, 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 = (selected + 1) % WRAPPERS.size()Advance the displayed card and use the remainder to return to zero after the last card. -
caption.text = "Wrapper %d · %s" % [selected + 1, WRAPPERS[selected]["name"]]Compose the caption from the new card number and name. Add one to the zero-based index for a human reader. -
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. -
backdrop()Calls backdrop with the values in its parentheses. -
for i in range(WRAPPERS.size()):Repeats the indented instructions for each value in range(WRAPPERS.size()). -
var bay := Rect2(50 + 290 * i, 166, 268, 232)Give each wrapper a display card using a regular horizontal step. -
var edge := GOLD if i == selected else EDGEUse the gold border only when the loop index is the selected shelf card. -
panel(bay, Color("#203f51"), edge)Calls panel with the values in its parentheses. -
var item: Dictionary = WRAPPERS[i]Creates a variable named item. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold. -
var front := Rect2(bay.position + Vector2(81, 32), Vector2(108, 138))Work from the corner of a card. Position and size stay combined in one rectangle value. -
package_front(front, item)Render each model using the same layering instructions with its own data. -
paint_words(str(item["name"]), bay.position + Vector2(34, 202), 19)Calls paint_words with the values in its parentheses. -
func package_front(front: Rect2, item: Dictionary) -> void:Defines package front, 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 wrapper := Color(str(item["ink"]))Translate the saved colour string into a Godot Color. -
var decal := Rect2(front.position + Vector2(9, 35), Vector2(90, 76))Place the light paper over the coloured wrapper where text will be shown. -
panel(Rect2(front.position + Vector2(8, 10), front.size), INK)Draw the offset shadow first, so the wrapper paints over its upper-left area. -
panel(front, wrapper, PAPER)Calls panel with the values in its parentheses. -
draw_rect(decal, PAPER)Paints a rectangle at the given position and size. -
paint_words(str(item["mark"]), front.position + Vector2(25, 77), 32, INK)Read the initials from the item's dictionary and draw them on top of the label. -
paint_words(str(item["kind"]), front.position + Vector2(14, 99), 15, INK)Calls paint_words with the values in its parentheses. -
draw_line(front.position + Vector2(12, 118), front.position + Vector2(96, 118), PAPER, 2)Paints a line between two points with the chosen colour and width. -
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 every wrapper becomes dark, inspect item["ink"] in the package function and check the # and six hexadecimal digits in each colour. A valid colour also needs enough contrast against the outline.
Try a change
Design a fourth fictional wrapper. Give it a different short mark and adapt the card width and spacing so all four fit the 960 px preview.