Video Game Design and Coding · PLECK lesson

PLECK lesson 122 · suggested week 25, day 2

Implement a bounded patrol

Objective. Move between two points without overshoot jitter.

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.

Patrol, detection, chase, return and readable fairness

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

C-PATROL moves a CharacterBody2D toward one of two global endpoints. It reverses when close enough to the target, using a threshold related to speed times the physics step. Without a threshold, a guard can overshoot and oscillate around the point. Keep the path unobstructed: this recipe steers directly and does not search around walls. Test endpoints at least eighty pixels apart before adding detection. Use a visible guard shape distinct from the player.

Worked example

A patrol guard follows two points. A separate manager handles damage, leaving steering reusable in another level with different round rules. Scaffold C-PATROL — A two-point bounded patrol Setup: CharacterBody2D root Guard with CollisionShape2D radius 12 and a visible Polygon2D or Sprite2D. Layer 4 mask 2 for walls, separate Area2D sensor if player detection is needed. Put under an unscaled Node2D level. Attach script. These endpoints are global coordinates; set points on one unobstructed walkway. This is direct steering, not pathfinding. ```gdscript extends CharacterBody2D @export var point_a: Vector2 = Vector2(200, 180) @export var point_b: Vector2 = Vector2(440, 180) @export var speed: float = 70.0 var toward_b: bool = true func _physics_process(delta: float) -> void: var target := point_b if toward_b else point_a var offset := target - global_position if offset.length() <= maxf(3.0, speed * delta): toward_b = not toward_b velocity = Vector2.ZERO else: velocity = offset.normalized() * speed move_and_slide() ```

A two-point bounded patrol

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

Scene and node setup

CharacterBody2D root Guard with CollisionShape2D radius 12 and a visible Polygon2D or Sprite2D. Layer 4 mask 2 for walls, separate Area2D sensor if player detection is needed. Put under an unscaled Node2D level. Attach script. These endpoints are global coordinates; set points on one unobstructed walkway. This is direct steering, not pathfinding.

extends CharacterBody2D
@export var point_a: Vector2 = Vector2(200, 180)
@export var point_b: Vector2 = Vector2(440, 180)
@export var speed: float = 70.0
var toward_b: bool = true
func _physics_process(delta: float) -> void:
    var target := point_b if toward_b else point_a
    var offset := target - global_position
    if offset.length() <= maxf(3.0, speed * delta):
        toward_b = not toward_b
        velocity = Vector2.ZERO
    else:
        velocity = offset.normalized() * speed
    move_and_slide()

Checks to run in the editor

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.

Implement a bounded patrol — evidence task

  1. 1. Build the patrol in an open test room.
  2. 2. Observe five reversals and test one deliberately obstructed route in a duplicate.

Deliverable. Patrol source and five-reversal observations.

Acceptance checks

Understanding question. Why can direct steering get stuck behind a wall?

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 an open room and C-PATROL before C-GUARD-AI. Do not expect direct steering to navigate a maze. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.

Optional extra. Add a visible state label or icon and compare how it changes player understanding. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.

Name ____________________ Date ____________________