Video Game Design and Coding

PLECK lesson 129 · suggested week 26, day 4

Create physics walls from grid data

Objective. Keep rendered and collision geometry driven by the same cells.

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.

Seeded randomness, grids and guaranteed playable routes

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

Build a Wall.tscn with a StaticBody2D, forty-by-forty RectangleShape2D and matching visual. For each blocked grid cell, instantiate the wall under an unscaled Walls Node2D and set its position to Vector2(x*40+20, y*40+20). Do not also place independent invisible walls by hand. If regenerating, remove the old walls before adding new ones. The player’s diameter must fit an open cell with comfortable clearance; use a smaller test player if necessary.

Worked example

A four-cell corridor reserved before random walls guarantees a required path. Other open cells may still be isolated, so the guarantee must be stated narrowly. Scaffold C-GRID — A deterministic grid with a guaranteed clear route Setup: Attach to Node2D GridLab. This example draws, not physics, so the assignment adds matching wall collision only after validation. Grid is 8 columns by 6 rows at 40 pixels per cell. Row 0 and column 7 are a guaranteed clear route from (0,0) to (7,5). RNG seed is a nonpersonal integer. ```gdscript extends Node2D const COLS: int = 8 const ROWS: int = 6 const CELL: int = 40 var cells: Array = [] func _ready() -> void: var rng := RandomNumberGenerator.new() rng.seed = 2468 for y in range(ROWS): var row: Array[int] = [] for x in range(COLS): var blocked: bool = y != 0 and x != COLS - 1 and rng.randf() < 0.25 row.append(1 if blocked else 0) cells.append(row) queue_redraw() func _draw() -> void: for y in range(cells.size()): for x in range(cells[y].size()): var color := Color.DIM_GRAY if cells[y][x] == 1 else Color.BEIGE draw_rect(Rect2(x * CELL, y * CELL, CELL - 2, CELL - 2), color) ``` Scaffold C-BODY — Reusable top-down player with collision Setup: Create CharacterBody2D root Player at (80,180); add CollisionShape2D with CircleShape2D radius 14. Attach this script to Player and save Player.tscn. Use C-FREE-MOVE input actions. Root collision layer 1, mask 2. Create a separate StaticBody2D Wall on layer 2 mask 1 with RectangleShape2D collision and a Polygon2D rectangle as visible child. Instance both under an unscaled Node2D World. Do not put a body inside another physics body. ```gdscript extends CharacterBody2D @export var speed: float = 160.0 func _draw() -> void: draw_circle(Vector2.ZERO, 14.0, Color.CORNFLOWER_BLUE) draw_line(Vector2(-8, 0), Vector2(8, 0), Color.WHITE, 3.0) func _physics_process(_delta: float) -> void: velocity = Input.get_vector("move_left", "move_right", "move_up", "move_down") * speed move_and_slide() ```

A deterministic grid with a guaranteed clear route

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

Scene and node setup

Attach to Node2D GridLab. This example draws, not physics, so the assignment adds matching wall collision only after validation. Grid is 8 columns by 6 rows at 40 pixels per cell. Row 0 and column 7 are a guaranteed clear route from (0,0) to (7,5). RNG seed is a nonpersonal integer.

extends Node2D
const COLS: int = 8
const ROWS: int = 6
const CELL: int = 40
var cells: Array = []
func _ready() -> void:
    var rng := RandomNumberGenerator.new()
    rng.seed = 2468
    for y in range(ROWS):
        var row: Array[int] = []
        for x in range(COLS):
            var blocked: bool = y != 0 and x != COLS - 1 and rng.randf() < 0.25
            row.append(1 if blocked else 0)
        cells.append(row)
    queue_redraw()
func _draw() -> void:
    for y in range(cells.size()):
        for x in range(cells[y].size()):
            var color := Color.DIM_GRAY if cells[y][x] == 1 else Color.BEIGE
            draw_rect(Rect2(x * CELL, y * CELL, CELL - 2, CELL - 2), color)

Checks to run in the editor

  • Same seed and version produce same grid.
  • All cells on top row and rightmost column are empty.

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.

Reusable top-down player with collision

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

Scene and node setup

Create CharacterBody2D root Player at (80,180); add CollisionShape2D with CircleShape2D radius 14. Attach this script to Player and save Player.tscn. Use C-FREE-MOVE input actions. Root collision layer 1, mask 2. Create a separate StaticBody2D Wall on layer 2 mask 1 with RectangleShape2D collision and a Polygon2D rectangle as visible child. Instance both under an unscaled Node2D World. Do not put a body inside another physics body.

extends CharacterBody2D
@export var speed: float = 160.0
func _draw() -> void:
    draw_circle(Vector2.ZERO, 14.0, Color.CORNFLOWER_BLUE)
    draw_line(Vector2(-8, 0), Vector2(8, 0), Color.WHITE, 3.0)
func _physics_process(_delta: float) -> void:
    velocity = Input.get_vector("move_left", "move_right", "move_up", "move_down") * speed
    move_and_slide()

Checks to run in the editor

  • Player stops at the wall rather than passing through.
  • Diagonal speed matches horizontal speed.
  • Do not multiply velocity by delta before move_and_slide.

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.

Create physics walls from grid data — evidence task

  1. 1. Instantiate walls for every cell with value one.
  2. 2. Compare wall count with the number of blocked entries and walk the reserved route.

Deliverable. Grid-to-physics checkpoint and wall-count/path test.

Acceptance checks

  • VGC-L129-T1. One blocked cell produces one wall.
  • VGC-L129-T2. Visuals and colliders agree and the route remains traversable.

Understanding question. Why derive visuals and colliders from the same data?

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 the fixed 8 by 6 grid in C-GRID. Keep the guaranteed top-row/right-column path empty before adding any random walls. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.

Optional extra. Compare three integer seeds while checking the same route invariant. 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