Video Game Design and Coding

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

  • Guard reverses near each endpoint without shaking in place.
  • An obstructing wall stops motion; route design must remain clear.

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

  • VGC-L122-T1. Unobstructed patrol reverses cleanly.
  • VGC-L122-T2. The limitation at a wall is documented rather than called pathfinding.

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.

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