Video Game Design and Coding

PLECK lesson 26 · suggested week 6, day 1

Refactor repeated feedback

Objective. Extract a function while keeping observable behavior unchanged.

Godot Engine Standard desktop editor 4.7.2-stable, GDScript (Godot 4), Compatibility renderer. Headless Godot 4.7.2.stable.official.ed1daf0bf passed 106 assertions on the supplied scaffolds, including malformed JSON saves. Audio used the Dummy driver and a synthetic WAV, not a speaker. The visual editor, browser export, and signed-in submission were not tested. The desk still does not run a student's own upload.

About 55 minutes. The due date is chosen by a parent.

Functions, parameter experiments, original challenges and evidence

Suggested pace: Recall and predict 5 min; Read instruction and scaffold 12 min; Build or design the assigned artifact 25 min; Run acceptance tests 8 min; Explain and checkpoint 5 min.

Materials: Godot Standard 4.7.2 desktop editor (GDScript, Compatibility renderer), Local course folder and prior checkpoint, No paid assets or public account required.

Comes after VGC-L025.

Refactoring changes internal organization without changing the game’s observable rules. C-MANAGER uses _refresh so score formatting and completion text are written in one place. Find another repeated operation, such as a debug status message, and put it into a named function. Call it from the same events that previously contained the repeated lines. Do not add a new mechanic in the same edit. Run the existing zero-to-three and restart tests to prove the reorganization did not break the game.

Worked example

Two parameter trials can use the same start and route while changing only speed. Record an observation such as two wall contacts rather than an unsupported claim such as more fun. Scaffold C-MANAGER — A complete three-coin collection round Setup: Create World Node2D root with Player (C-BODY), three Coin instances (C-PICKUP), and CanvasLayer HUD > Label named Score plus Button named Restart (text Restart, position 500,10). Root and parents unscaled. Name coins Coin1, Coin2, Coin3. Place them at (250,100),(380,220),(520,100). Player group player. Attach script to World. UI paths and coin names must match exactly. Connect nothing else in the editor. Label at (10,10). ```gdscript extends Node2D var score: int = 0 var playing: bool = true @onready var score_label: Label = $HUD/Score func _ready() -> void: $HUD/Restart.pressed.connect(_restart) for coin in [$Coin1, $Coin2, $Coin3]: coin.collected.connect(_on_collected) _refresh() func _on_collected(points: int) -> void: if not playing: return score += points if score >= 3: playing = false $Player.set_physics_process(false) _refresh() func _refresh() -> void: score_label.text = "Score: %d / 3" % score if not playing: score_label.text += " — Orchard complete!" func _restart() -> void: get_tree().reload_current_scene() ```

A complete three-coin collection round

GDScript (Godot 4, not Scratch, Python, JavaScript or C#). Scaffold C-MANAGER. Copy starts from this text. The desk does not run it.

Scene and node setup

Create World Node2D root with Player (C-BODY), three Coin instances (C-PICKUP), and CanvasLayer HUD > Label named Score plus Button named Restart (text Restart, position 500,10). Root and parents unscaled. Name coins Coin1, Coin2, Coin3. Place them at (250,100),(380,220),(520,100). Player group player. Attach script to World. UI paths and coin names must match exactly. Connect nothing else in the editor. Label at (10,10).

extends Node2D
var score: int = 0
var playing: bool = true
@onready var score_label: Label = $HUD/Score
func _ready() -> void:
    $HUD/Restart.pressed.connect(_restart)
    for coin in [$Coin1, $Coin2, $Coin3]:
        coin.collected.connect(_on_collected)
    _refresh()
func _on_collected(points: int) -> void:
    if not playing:
        return
    score += points
    if score >= 3:
        playing = false
        $Player.set_physics_process(false)
    _refresh()
func _refresh() -> void:
    score_label.text = "Score: %d / 3" % score
    if not playing:
        score_label.text += " — Orchard complete!"
func _restart() -> void:
    get_tree().reload_current_scene()

Checks to run in the editor

  • Collect three coins: score is 3 and a completion message appears.
  • Restart recreates three coins and score is zero.

Native Godot 4.7.2.stable.official.ed1daf0bf headless compile and representative node-tree/runtime tests passed; see native-qa-results.json. Visual UI, audible output, exports and website submission still need separate checks.

Pocket Orchard starter, as text files. Open the files in Godot on the family computer. This page does not download a zip or run the project.

Refactor repeated feedback — evidence task

  1. 1. Extract one genuinely repeated operation into a function.
  2. 2. List its callers and run both round tests.

Deliverable. Refactored project and a caller diagram.

Acceptance checks

  • VGC-L026-T1. Before/after behavior is identical.
  • VGC-L026-T2. The repeated logic has one definition rather than copied blocks.

Understanding question. How is refactoring different from adding a fourth coin?

Include a downloadable project ZIP with project.godot, .tscn, .gd and original asset files when code changes. A private, teacher-accessible download link to that ZIP is equivalent. Screenshots or a playthrough do not replace editable source.

If this is still shaky. Keep the three-coin version working. Duplicate the project folder before experimenting with one parameter at a time. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.

Optional extra. Make one alternate layout with the same rules and compare route clarity. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.

Turn in typed notes and a JPEG, PNG, or WebP screenshot. One https address is stored as text and is not opened, extracted, or run. An MP3, WAV, M4A, MP4, or WebM file up to 4 MB can show a demo. The demo does not replace editable source. Zip and code files are refused. If there is no private link, keep the project on the family computer and review the code in person.

This is a preview. A parent assigns the lesson and chooses the due date after the student is enrolled. The answer key is not on this page.

Print this lesson