Mobile Game 09 · Draw a mirror that turns in two directions

Museum of Lost Lights · Lesson 09 of 50 · Beginner to intermediate · About 44 min

Your new mirror is stuck on a little brass pivot. It has two positions. You need both before you can guide a light beam.

A gouache painting of an Ocean Gallery with glass jellyfish exhibits, fossils and a deep teal floor.
What you’ll make Draw a 3-by-3 floor and a mirror. Use a named choice for each possible direction and redraw whenever that choice changes.

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/08 when the lesson builds on prior work, or copy the checkpoints/09 folder to try this lesson on its own. Read the file names in the steps before editing.

Button receives a tap. The signal calls turn_mirror. → Flip the enum value. Pick the other position. → Read the right offset. A positive or negative y sets the tilt. → Redraw the mirror. The same state drives words and shapes.
Button receives a tap → Flip the enum value → Read the right offset → Redraw the mirror

Build it step by step

Language and tools in this lesson enum, type inference, float, function return, _draw, Rect2, draw_line, queue_redraw, mirror state.

  1. Name both mirror positions

    Import Lesson 09. Read the enum, then find the variable named facing. Draw a line that rises as it goes right and another that falls as it goes right.

    enum Mirror {RISING, FALLING}

    Why it works An enum is a list of named choices. Mirror.RISING and Mirror.FALLING are the two settings in this puzzle. The type Mirror says what kind of value facing holds.

  2. Make shapes for the board

    Find _draw. Trace where each corner comes from. Imagine row 0, column 1: start at ORIGIN, then add one CELL in the x direction.

    var corner := ORIGIN + Vector2(column, row) * CELL

    Why it works Godot calls _draw to read custom drawing instructions. Rect2 holds the rectangle’s top-left position and size. Reducing the width and height by 4 leaves narrow gaps between the tiles.

  3. Draw across the centre point

    Find offset and the draw_line instruction. Put a finger on CENTRE - offset and CENTRE + offset, then look at the mint circle behind the mirror.

    draw_line(CENTRE - offset, CENTRE + offset, Color("#ffd381"), 8.0, true)

    Why it works The line draws between two points on opposite sides of the same centre. The negative y in the rising offset points upward because the screen y coordinate increases downward. The final true makes the line edges smoother.

  4. Flip the named state

    Read turn_mirror from its if to its else. Run the checkpoint and tap once. Keep track of facing before and after the tap.

    if facing == Mirror.RISING:
    		facing = Mirror.FALLING

    Why it works The signal calls turn_mirror. == compares the current value with the named rising choice. The assignments switch it to the other choice for the next drawing.

  5. Schedule a fresh drawing

    Read the last two lines of turn_mirror. Tap twice more and check that the label describes the line you see.

    refresh_readout()
    	queue_redraw()

    Why it works refresh_readout updates the words. queue_redraw schedules Godot to call _draw again. Both read the same facing value.

Watch the values change

  1. Open the room facing is Mirror.RISING
    What you see. _draw paints the mirror up and to the right.
  2. Tap Rotate mirror The comparison is true and facing becomes Mirror.FALLING
    What you see. The words change to falls toward the right.
  3. Godot draws again mirror_offset returns (26, 26)
    What you see. The line falls toward the right.
  4. Tap again The else returns facing to Mirror.RISING
    What you see. Text and drawing return to the first position.

Predict before you run If you remove queue_redraw from turn_mirror and tap the button, what is most likely to update immediately?

Reveal the answer

The readout text updates because refresh_readout still writes to the Label. The game no longer asks the mirror’s drawing to refresh for that tap.

Complete checkpoint

A golden mirror starts leaning up toward the right. Tap Rotate mirror to flip it down toward the right and tap again to return it.

main.gd

extends Control

enum Mirror {RISING, FALLING}

const ORIGIN := Vector2(54, 149)
const CELL: float = 90.0
const CENTRE := Vector2(189, 284)
var facing: Mirror = Mirror.RISING
var readout: Label

func _ready() -> void:
	make_heading()
	readout = Label.new()
	readout.position = Vector2(54, 101)
	readout.add_theme_font_size_override("font_size", 22)
	add_child(readout)

	var turn_button := Button.new()
	turn_button.text = "Rotate mirror"
	turn_button.position = Vector2(356, 181)
	turn_button.custom_minimum_size = Vector2(220, 62)
	turn_button.add_theme_font_size_override("font_size", 22)
	turn_button.pressed.connect(turn_mirror)
	add_child(turn_button)
	turn_button.grab_focus()
	refresh_readout()

func turn_mirror() -> void:
	if facing == Mirror.RISING:
		facing = Mirror.FALLING
	else:
		facing = Mirror.RISING
	refresh_readout()
	queue_redraw()

func mirror_offset() -> Vector2:
	if facing == Mirror.RISING:
		return Vector2(26, -26)
	return Vector2(26, 26)

func _draw() -> void:
	for row in range(3):
		for column in range(3):
			var corner := ORIGIN + Vector2(column, row) * CELL
			draw_rect(Rect2(corner, Vector2(CELL - 4, CELL - 4)), Color("#22414c"))
	var offset := mirror_offset()
	draw_circle(CENTRE, 35.0, Color("#326b72"))
	draw_line(CENTRE - offset, CENTRE + offset, Color("#ffd381"), 8.0, true)

func refresh_readout() -> void:
	if facing == Mirror.RISING:
		readout.text = "Mirror position: rises toward the right"
	else:
		readout.text = "Mirror position: falls toward the right"

func make_heading() -> void:
	var heading := Label.new()
	heading.text = "Ocean Gallery · Turn the mirror"
	heading.position = Vector2(52, 36)
	heading.add_theme_font_size_override("font_size", 38)
	heading.add_theme_color_override("font_color", Color("#ffe1a8"))
	add_child(heading)

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 ControlThe scene root behaves like a Control, the kind of Godot node used for a game’s text, controls and other interface pieces.
  2. enum Mirror {RISING, FALLING}Name the two possible mirror tilts. RISING looks like a forward slash and FALLING tilts down as it goes right.
  3. const ORIGIN := Vector2(54, 149)Pin the top-left corner of the floor at screen position (54, 149). Every tile measures its position from this point.
  4. const CELL: float = 90.0Give every cell a 90-pixel pitch and store it as a decimal-capable float. The tile colour leaves a small gap inside that pitch.
  5. const CENTRE := Vector2(189, 284)Put the mirror in the centre of the middle tile. The left edge plus 135 and the top edge plus 135 lead to this point.
  6. var facing: Mirror = Mirror.RISINGStart with the rising orientation. This changeable value is the one source for the next mirror drawing.
  7. var readout: LabelReserve a name for a Label node. The setup function puts the real Label there before the game first displays it.
  8. func _ready() -> void:Godot automatically calls this setup function after bringing this node and its children into the scene. The code below it prepares the first frame.
  9. make_heading()Build the title before the room makes its other controls.
  10. readout = Label.new()Create the Label now. Its stored reference lets later signal handlers change the same sign.
  11. readout.position = Vector2(54, 101)Use two numbers to give this control its position, measured right and down from the root.
  12. readout.add_theme_font_size_override("font_size", 22)Set a larger or smaller font here without changing the typography on the other controls.
  13. add_child(readout)Put the stored Label on the visible scene. Changes to its text now show on screen.
  14. var turn_button := Button.new()Make the player control that will ask the mirror to move.
  15. turn_button.text = "Rotate mirror"Tell players exactly what the next activation does.
  16. turn_button.position = Vector2(356, 181)Place the control to the right of the board where it will not cover a tile.
  17. turn_button.custom_minimum_size = Vector2(220, 62)Keep the tap area at least 220 pixels wide and 62 pixels tall.
  18. turn_button.add_theme_font_size_override("font_size", 22)Increase the button caption to 22 pixels so its instruction is easy to read.
  19. turn_button.pressed.connect(turn_mirror)Give the pressed signal the turn_mirror function. Every future activation will call it.
  20. add_child(turn_button)Display the control under the scene root and let it receive input.
  21. turn_button.grab_focus()Move keyboard focus to this control. After the window has focus, the player can activate it with Space as well as tapping it.
  22. refresh_readout()Describe the initial rising mirror before the visitor touches the control.
  23. func turn_mirror() -> void:Group what happens after the player chooses Rotate mirror.
  24. if facing == Mirror.RISING:Check whether the current mirror choice equals RISING.
  25. facing = Mirror.FALLINGStore the falling orientation for the next label update and drawing.
  26. else:If the mirror was already falling, use the other branch to pick the rising choice.
  27. facing = Mirror.RISINGStore the rising orientation so the next update returns to that tilt.
  28. refresh_readout()Reword the sign before queuing the drawing update.
  29. queue_redraw()Ask Godot to run _draw again. In the new drawing the code will read the new value of facing.
  30. func mirror_offset() -> Vector2:Find the position of one end of the mirror relative to its centre. The drawing also uses the opposite point.
  31. if facing == Mirror.RISING:Check whether the current mirror choice equals RISING.
  32. return Vector2(26, -26)Use an endpoint 26 pixels right and 26 pixels up. On screen, negative y moves upward.
  33. return Vector2(26, 26)Use an endpoint 26 pixels right and 26 pixels down. The first return already handled the rising choice.
  34. func _draw() -> void:Godot uses this callback to collect drawing commands for the board and its pieces.
  35. for row in range(3):Draw three rows. The row count starts at zero so the first row begins at ORIGIN.
  36. for column in range(3):For each of those rows, draw three columns before advancing to the next row.
  37. var corner := ORIGIN + Vector2(column, row) * CELLMultiply the grid address by 90 to turn it into a pixel offset, then add the board’s top-left corner.
  38. draw_rect(Rect2(corner, Vector2(CELL - 4, CELL - 4)), Color("#22414c"))Paint a filled teal square at this corner. Four pixels of unpainted width and height leave a visible seam between neighbours.
  39. var offset := mirror_offset()Read a drawing offset that matches the current tilt.
  40. draw_circle(CENTRE, 35.0, Color("#326b72"))Paint the round teal base of the mirror at the middle tile.
  41. draw_line(CENTRE - offset, CENTRE + offset, Color("#ffd381"), 8.0, true)Put one end of the golden mirror on each side of its centre. Draw it 8 pixels thick with smoothed edges.
  42. func refresh_readout() -> void:Keep the code that changes the on-screen words together so both setup and player actions can call it.
  43. if facing == Mirror.RISING:Read the mirror tilt and pick which text should appear over the board.
  44. readout.text = "Mirror position: rises toward the right"Use words to describe what the rising line does when you read it from left to right.
  45. else:The other enum selection receives the alternate message.
  46. readout.text = "Mirror position: falls toward the right"Tell players the drawn diagonal now falls as it goes from left to right.
  47. func make_heading() -> void:Group the instructions that make the panel title. The room calls this once at startup.
  48. var heading := Label.new()Create a Label node and keep a reference under the name heading so the next lines all customise the same sign.
  49. heading.text = "Ocean Gallery · Turn the mirror"Show the name of this museum task above its controls.
  50. heading.position = Vector2(52, 36)Put the title 52 pixels to the right and 36 pixels down from the root’s upper-left corner.
  51. heading.add_theme_font_size_override("font_size", 38)Set this heading’s font size to 38. The other labels keep their own sizes.
  52. heading.add_theme_color_override("font_color", Color("#ffe1a8"))Give only this label a warm gold text colour. The six characters after # describe red, green and blue light.
  53. add_child(heading)Connect the title to the scene tree so Godot can display it.

Find and fix a bug

If the mirror’s tilt seems backwards, check the sign of the y number in mirror_offset. On a Godot 2D screen, a smaller y is higher up. Check that the text still describes the actual line.

Try a change

Ages 10–12: Label two sketches with the correct enum values. Ages 13–16: Make a well-named constant for the mirror half-width, change it in one place and check both slopes.