Video Game Design and Coding

PLECK lesson 34 · suggested week 7, day 4

Cap resource use and test a long run

Objective. Bound spawning and prove that cleanup prevents accumulation.

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.

Meteor Mail spawning, instancing and cleanup

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

A spawn cap is a safety limit, not a substitute for removing obsolete objects. C-TIMER refuses to add more when the Hazards container has spawn_limit children. Falling hazards should still free themselves off-screen. Test a ninety-second run and note counts every fifteen seconds. To verify the cap, temporarily disable motion in a duplicate test project so hazards remain; the count should stop at twelve. Restore the working version rather than leaving the artificial fault in your submission.

Worked example

A Timer with wait_time 2 creates one hazard per timeout. A hazard traveling 100 pixels/second moves 50 pixels during half a second and is removed after leaving the playfield. 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) ``` Scaffold C-HAZARD — Falling hazard with player notification and cleanup Setup: Area2D Hazard root on layer 4 (bit 8), mask layer 1 only, child CollisionShape2D circle radius 12. Attach this script. Player in group player. A Player damage method is deliberately not hardcoded: connect hit_player to a manager’s damage handler when instantiating. Parent Hazards must be unscaled and unrotated. ```gdscript extends Area2D signal hit_player @export var fall_speed: float = 110.0 var hit: bool = false func _ready() -> void: body_entered.connect(_on_body_entered) func _draw() -> void: draw_rect(Rect2(-12, -12, 24, 24), Color.TOMATO) draw_line(Vector2(-8,-8),Vector2(8,8),Color.BLACK,2.0) func _physics_process(delta: float) -> void: position.y += fall_speed * delta if position.y > 390.0: queue_free() func _on_body_entered(body: Node2D) -> void: if hit or not body.is_in_group("player"): return hit = true hit_player.emit() queue_free() ```

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.

Falling hazard with player notification and cleanup

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

Scene and node setup

Area2D Hazard root on layer 4 (bit 8), mask layer 1 only, child CollisionShape2D circle radius 12. Attach this script. Player in group player. A Player damage method is deliberately not hardcoded: connect hit_player to a manager’s damage handler when instantiating. Parent Hazards must be unscaled and unrotated.

extends Area2D
signal hit_player
@export var fall_speed: float = 110.0
var hit: bool = false
func _ready() -> void:
    body_entered.connect(_on_body_entered)
func _draw() -> void:
    draw_rect(Rect2(-12, -12, 24, 24), Color.TOMATO)
    draw_line(Vector2(-8,-8),Vector2(8,8),Color.BLACK,2.0)
func _physics_process(delta: float) -> void:
    position.y += fall_speed * delta
    if position.y > 390.0:
        queue_free()
func _on_body_entered(body: Node2D) -> void:
    if hit or not body.is_in_group("player"):
        return
    hit = true
    hit_player.emit()
    queue_free()

Checks to run in the editor

  • Hazard moves downward and disappears beyond y390.
  • One overlap emits one damage event.

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.

Cap resource use and test a long run — evidence task

  1. 1. Run the normal spawner for ninety seconds and record six counts.
  2. 2. In a duplicate, freeze hazards and verify the twelve-object cap.

Deliverable. Count table, cap-test evidence and restored project.

Acceptance checks

  • VGC-L034-T1. Normal count does not grow without bound.
  • VGC-L034-T2. Frozen test stops at twelve and the real project is restored.

Understanding question. Why keep both cleanup and a cap?

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. Use one manually placed falling hazard before enabling SpawnTimer. Keep the world unscaled and viewport 640 by 360. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.

Optional extra. Compare two spawn intervals while keeping hazard speed fixed. 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.

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

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