Cruise Game 31 · Paint the ship one layer at a time

Midnight Deck Dash · Lesson 31 of 50 · Intermediate to advanced · About 36 min

Our cruise ship has warm wooden planks upstairs and steel decking below. Make the cargo stand out on both.

The cruise ship’s Service Deck with brass pipes, storage cabinets and small portholes.
What you’ll make Draw an ocean background, a tiled deck and a parcel in separate layers.

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.

Sea. Draw the background first. → Deck. Draw the rim and tiled floor. → Parcel. Draw cargo over the tiles.
Sea → Deck → Parcel
Godot checkpoint window for Paint the ship one layer at a time
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson draw_rect, draw_line, Rect2, nested for loops, queue_redraw.

  1. 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.

  2. Start with the water

    In main.gd find _draw. Change #112632 to #223d58 and run again. Only the distant background should change.

    draw_rect(Rect2(Vector2.ZERO, size), Color("#112632"))

    Why it works Godot draws these calls in order. Later floor and cargo shapes appear on top of the background.

  3. Build each tile

    Find the second draw_rect and trace the loops underneath it. Change TILE to 47 and compare the scale. Restore it after the test.

    var surface := Rect2(ORIGIN + Vector2(cell * TILE), Vector2(TILE - 2, TILE - 2))

    Why it works y counts rows and x counts columns. Multiplication by TILE converts a grid address to pixels. Rect2 contains the rectangle's corner and size.

  4. Mark blocked tiles

    Find WALLS at the top and look for the diagonal wall stripes. Move a wall from (2, 1) to (2, 0), then run.

    draw_rect(surface, Color("#223e4a") if cell in WALLS else floor_colour)

    Why it works The in operator tests membership in WALLS. The same grid address controls the dark floor and the line marking it.

  5. Paint cargo above the deck

    Follow the three parcel calls after both loops. Change PARCEL to Vector2i(5, 2) and see the parcel move one tile east.

    draw_circle(centre(PARCEL), 17, Color("#112632"))

    Why it works centre adds half a tile so that the parcel drawing uses the middle of a tile. Drawing after the floor keeps the parcel visible.

  6. Switch the theme

    Click Switch deck art twice. Watch both the caption and floor colours. Notice that the parcel stays still.

    steel_deck = not steel_deck

    Why it works not reverses the Boolean. queue_redraw requests a new frame from _draw, which now reads the changed Boolean.

Watch the values change

  1. Run steel_deck starts false.
    What you see. Cargo sits over oak decking.
  2. Switch art The Boolean becomes true and queues a redraw.
    What you see. Steel replaces oak at the same coordinates.
  3. Switch again Only the theme flag changes back.
    What you see. Oak and the same fixed parcel are visible.

Predict before you run What happens if the parcel's three drawing lines are placed above the grid loops?

Reveal the answer

The later tile drawing covers the parcel. Return those lines to the end of _draw.

Complete checkpoint

Switch deck art changes oak to blue steel and back. The grid, cargo and blocked tiles keep their addresses.

main.gd

extends Control

const TILE := 54
const ORIGIN := Vector2(58, 135)
const WALLS := [Vector2i(2, 1), Vector2i(3, 1), Vector2i(7, 3), Vector2i(8, 3)]
const PARCEL := Vector2i(4, 2)
var steel_deck := false
var caption: Label

func _ready() -> void:
	label("Midnight Deck Dash / Deck art", 29)
	caption = label("Promenade • oak decking", 76)
	button("Switch deck art", 53, 439, switch_art)

func switch_art() -> void:
	steel_deck = not steel_deck
	caption.text = "Service Deck • blue steel" if steel_deck else "Promenade • oak decking"
	queue_redraw()

func centre(cell: Vector2i) -> Vector2:
	return ORIGIN + Vector2(cell) * TILE + Vector2.ONE * TILE / 2.0

func _draw() -> void:
	draw_rect(Rect2(Vector2.ZERO, size), Color("#112632"))
	draw_rect(Rect2(ORIGIN - Vector2(12, 12), Vector2(564, 294)), Color("#244b59"))
	for y in range(5):
		for x in range(10):
			var cell := Vector2i(x, y)
			var floor_colour := Color("#326279") if steel_deck else Color("#92694f")
			var surface := Rect2(ORIGIN + Vector2(cell * TILE), Vector2(TILE - 2, TILE - 2))
			draw_rect(surface, Color("#223e4a") if cell in WALLS else floor_colour)
			if cell in WALLS:
				draw_line(surface.position + Vector2(7, 8), surface.end - Vector2(8, 8), Color("#7596a1"), 3)
	draw_circle(centre(PARCEL), 17, Color("#112632"))
	draw_rect(Rect2(centre(PARCEL) - Vector2(11, 8), Vector2(22, 16)), Color("#ffdd90"))
	draw_line(centre(PARCEL) - Vector2(0, 9), centre(PARCEL) + Vector2(0, 9), Color("#595633"), 3)

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
  1. extends ControlAttach this script to a Control so the checkpoint can draw, size itself, place buttons and respond to input.
  2. const TILE := 54Creates a constant named TILE. The expression uses the whole number 54. Godot infers the type from the first value.
  3. const ORIGIN := Vector2(58, 135)Creates a constant named ORIGIN. The expression packs x and y into one value. Godot infers the type from the first value.
  4. const WALLS := [Vector2i(2, 1), Vector2i(3, 1), Vector2i(7, 3), Vector2i(8, 3)]Creates a constant named WALLS. The expression makes a list whose items keep their order. Godot infers the type from the first value.
  5. const PARCEL := Vector2i(4, 2)Creates a constant named PARCEL. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value.
  6. var steel_deck := falseCreates a variable named steel_deck. The expression uses the yes-or-no value false. Godot infers the type from the first value.
  7. var caption: LabelDeclares caption for later use. A colon followed by a type says which kind of value belongs here.
  8. 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.
  9. label("Midnight Deck Dash / Deck art", 29)Calls label with the values in its parentheses.
  10. caption = label("Promenade • oak decking", 76)Stores the new value in caption. The expression calls label with the values in its parentheses.
  11. button("Switch deck art", 53, 439, switch_art)Calls button with the values in its parentheses.
  12. func switch_art() -> void:Defines switch art, 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.
  13. steel_deck = not steel_deckInvert the theme flag. If the practice deck currently uses wood, the next drawing uses steel.
  14. caption.text = "Service Deck • blue steel" if steel_deck else "Promenade • oak decking"Choose the label from the new flag in the same order as the painted floor colours.
  15. queue_redraw()Asks Godot to call _draw again on a later frame so the screen matches the new state.
  16. func centre(cell: Vector2i) -> Vector2:Defines centre, 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.
  17. return ORIGIN + Vector2(cell) * TILE + Vector2.ONE * TILE / 2.0Scale the grid cell to pixels first, add the origin, then add half a tile both across and down.
  18. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  19. 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.
  20. draw_rect(Rect2(ORIGIN - Vector2(12, 12), Vector2(564, 294)), Color("#244b59"))Paints a rectangle at the given position and size.
  21. for y in range(5):Repeats the indented instructions for each value in range(5).
  22. for x in range(10):Repeats the indented instructions for each value in range(10).
  23. var cell := Vector2i(x, y)Join the current whole-number column x and row y into one board address.
  24. var floor_colour := Color("#326279") if steel_deck else Color("#92694f")Use the theme flag to choose the floor paint without changing which coordinates are available.
  25. var surface := Rect2(ORIGIN + Vector2(cell * TILE), Vector2(TILE - 2, TILE - 2))Position a tile at its scaled grid address. Leave a two-pixel gap to divide it from the neighbouring tile.
  26. draw_rect(surface, Color("#223e4a") if cell in WALLS else floor_colour)Draw a wall in dark blue when its address is in WALLS. In every other tile use the deck floor colour.
  27. if cell in WALLS:Ask the wall list whether the current tile is blocked. Only those tiles get the diagonal line.
  28. draw_line(surface.position + Vector2(7, 8), surface.end - Vector2(8, 8), Color("#7596a1"), 3)Paints a line between two points with the chosen colour and width.
  29. draw_circle(centre(PARCEL), 17, Color("#112632"))Paints a filled circle or outline in this node's local coordinates.
  30. draw_rect(Rect2(centre(PARCEL) - Vector2(11, 8), Vector2(22, 16)), Color("#ffdd90"))Paints a rectangle at the given position and size.
  31. draw_line(centre(PARCEL) - Vector2(0, 9), centre(PARCEL) + Vector2(0, 9), Color("#595633"), 3)Paints a line between two points with the chosen colour and width.
  32. 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.
  33. 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().
  34. new_label.text = textCopy the supplied string into the actual label so the learner can read it.
  35. new_label.position = Vector2(52, y)Use a fixed 52-pixel left margin and the caller's chosen height in local design pixels.
  36. 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.
  37. add_child(new_label)Attach the Label to the scene root so Godot can draw and manage it.
  38. return new_labelGive the caller this particular label so a later delivery or move can change its words.
  39. 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.
  40. var control := Button.new()Create a GUI button. It can respond to pointer taps and to the keyboard when focused.
  41. control.text = textShow the caller's chosen action name on the new button.
  42. control.position = Vector2(x, y)Place the action at the coordinates passed by the caller.
  43. control.custom_minimum_size = Vector2(width, 48)Ask the Control layout for at least the supplied width and 48 design pixels of height.
  44. add_child(control)Connect the new button to the visible scene root.
  45. control.pressed.connect(action)Connect the Godot pressed signal to the Callable received as action. The action runs on a press later.
  46. return controlReturn this button so the caller can customise it or keep its reference.

Find and fix a bug

If the parcel moves with the theme, restore one ORIGIN and one PARCEL constant. Changing the theme should affect floor_colour only.

Try a change

Ages 10–12 can design a new wall stripe. Ages 13–16 can store three palettes in an array of dictionaries and cycle through them with modulo.