Video Game Design and Coding · PLECK lesson
PLECK lesson 42 · suggested week 9, day 2
Constrain randomness for a fair challenge
Objective. Distinguish unpredictable variation from unavoidable failure.
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.
Fair randomization, difficulty, accessibility and assessment
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-L041.
Random x positions create variation but can accidentally block every escape route. Constrain hazards to a minimum horizontal separation or spawn one at a time with enough travel time to react. Estimate travel: at 110 pixels per second, a hazard from y-20 to y180 takes about 1.82 seconds. Compare that with player speed and route width. A random system needs invariants such as at least one navigable lane; “it usually works” is not enough for a fair rule.
Worked example
An incoming three-lane wave blocks left and center but leaves right clear. Randomly choosing among valid two-lane combinations creates variation while preserving an escape route. Scaffold C-TIMER — Timer-based spawning with a hard cap Setup: World Node2D root contains Node2D Hazards and Timer SpawnTimer: wait_time 1.2, autostart On, one_shot Off. Hazard scene root Area2D has a CircleShape2D radius 12 and C-HAZARD. Attach script to World and drag Hazard.tscn into hazard_scene Inspector field. Set viewport 640x360. Name Hazards group not needed. This script is a separate lab; merge its _ready into an existing manager rather than declaring a second _ready. ```gdscript extends Node2D @export var hazard_scene: PackedScene @export var spawn_limit: int = 12 func _ready() -> void: $SpawnTimer.timeout.connect(_spawn) func _spawn() -> void: if hazard_scene == null or $Hazards.get_child_count() >= spawn_limit: return var hazard := hazard_scene.instantiate() as Area2D $Hazards.add_child(hazard) hazard.position = Vector2(randf_range(20.0, 620.0), -20.0) ```
Timer-based spawning with a hard cap
GDScript (Godot 4, not Scratch, Python, JavaScript or C#). Scaffold C-TIMER. Copy starts from this text. The desk does not run it.
Scene and node setup
World Node2D root contains Node2D Hazards and Timer SpawnTimer: wait_time 1.2, autostart On, one_shot Off. Hazard scene root Area2D has a CircleShape2D radius 12 and C-HAZARD. Attach script to World and drag Hazard.tscn into hazard_scene Inspector field. Set viewport 640x360. Name Hazards group not needed. This script is a separate lab; merge its _ready into an existing manager rather than declaring a second _ready.
extends Node2D
@export var hazard_scene: PackedScene
@export var spawn_limit: int = 12
func _ready() -> void:
$SpawnTimer.timeout.connect(_spawn)
func _spawn() -> void:
if hazard_scene == null or $Hazards.get_child_count() >= spawn_limit:
return
var hazard := hazard_scene.instantiate() as Area2D
$Hazards.add_child(hazard)
hazard.position = Vector2(randf_range(20.0, 620.0), -20.0)
Checks to run in the editor
- One object appears about each 1.2 seconds.
- At most 12 hazards exist, even during a long run.
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.
Constrain randomness for a fair challenge — evidence task
- 1. Choose three 160-pixel-wide lanes and permit at most two occupied incoming lanes at one spawn wave.
- 2. Draw and test five possible wave layouts.
Deliverable. Lane rule, five layouts and revised spawner.
Acceptance checks
- VGC-L042-T1. Every wave leaves at least one clear lane.
- VGC-L042-T2. Your test includes both adjacent and separated blocked lanes.
Understanding question. Why is testing only one random run weak evidence?
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. Tune just hazard speed and spawn interval on a copied project. Use fixed test runs before adding more mechanics. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.
Optional extra. Offer an optional challenge mode while retaining a gentle default. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.
- Your first 2D game
Free public tutorial; no account; external asset download is optional, not used by this course.
· Opening this link does not submit the lesson.
Name ____________________ Date ____________________