Video Game Design and Coding

PLECK lesson 10 · suggested week 2, day 5

Repeat a known number of steps

Objective. Use loops and functions without creating an infinite frame loop.

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.

Values, types, expressions, branches and repeatable experiments

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

A for loop repeats over a finite collection. In GDScript, for stop in range(4) visits 0, 1, 2 and 3. A function can return a calculated value so callers can reuse it. Write delivery_bonus(parcel_count) to return parcel_count times two, then call it for counts zero through four. Do not use an endless while loop to animate a game; Godot already calls frame functions repeatedly. A long loop that never ends prevents input and drawing from progressing.

Worked example

A counter lab can store parcels as an int and route_title as a String. print exposes intermediate values; a function such as double_points(n) returns n*2 without needing any artwork. Scaffold C-HELLO — An observable first script Setup: Create a Node scene, name root Lab, attach lab.gd. Save as lab.tscn. Run current scene (editor clapboard/Run Current Scene). Output panel shows print output. All unindented lines belong to the script and indented lines belong to the function. ```gdscript extends Node var deliveries: int = 0 func _ready() -> void: deliveries += 1 print("Deliveries: ", deliveries) ```

An observable first script

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

Scene and node setup

Create a Node scene, name root Lab, attach lab.gd. Save as lab.tscn. Run current scene (editor clapboard/Run Current Scene). Output panel shows print output. All unindented lines belong to the script and indented lines belong to the function.

extends Node
var deliveries: int = 0
func _ready() -> void:
    deliveries += 1
    print("Deliveries: ", deliveries)

Checks to run in the editor

  • Run once: Deliveries: 1
  • Stop and run: value is 1, not 2.

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.

Repeat a known number of steps — evidence task

  1. 1. Create delivery_bonus and print its result for range(5).
  2. 2. Explain the last input visited and the number of outputs.

Deliverable. Lab project and loop trace.

Acceptance checks

  • VGC-L010-T1. Outputs are 0, 2, 4, 6, 8 in order.
  • VGC-L010-T2. There are five results, not six.

Understanding question. Does range(5) include the integer 5?

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. Return to C-HELLO and change one value per run. Write the expected output before running. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.

Optional extra. Create another small numeric example and predict the result without executing it first. 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