GRADES 8–9 · LESSON 2
Enemy Rules: A Chaser That Follows My 3 Rules
Write 3 rules in plain English, then build an enemy that follows them and nothing else.
By the end of this lesson you will have — one chaser enemy whose moves match the 3 rules you wrote before coding
What you'll show at the end: Your 3 written rules plus a p5.js sketch where a red chaser follows them, with the times of 3 test runs written down.
p5.js Web Editor
Check each tool's age rule before you sign up. If you are under 13, ask a parent first — some tools here are 13+ only.
Follow these steps
- Open the p5.js Web Editor delete the example code in the editor first, then paste your moving sprite from Lesson 1. No sprite yet? Paste the starter code from this page.
- Before any code, write your chaser's 3 rules in plain English. A rule looks like this: "Each frame, the chaser moves 3 pixels toward the player." One rule must say what counts as CAUGHT. You move 3 pixels a frame, so a chaser slower than 3 may never catch you.
- Turn each rule into code inside draw(). Tip: compare the chaser's x with the player's x (and y with y) to pick a direction, and use dist() to check whether they touch.
- Play 3 rounds and write how many seconds each lasted before CAUGHT. Stuck? Copy the question from the box next to these steps into the AI you're allowed to use (under 13: a parent's account, with your parent), and keep only the ideas that match your 3 rules.
Free tools for this lesson
Starting fresh?
No moving sprite from Lesson 1? Paste this. The blue square moves 3 pixels a frame and stops at the walls. The red chaser has no rules yet; give it a speed of at least 3 so it can catch you.
let px = 50, py = 50; // player
let cx = 350, cy = 350; // chaser
let chaserSpeed = 3; // 3 or more, or it may never catch you
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
if (keyIsDown(LEFT_ARROW)) px -= 3;
if (keyIsDown(RIGHT_ARROW)) px += 3;
if (keyIsDown(UP_ARROW)) py -= 3;
if (keyIsDown(DOWN_ARROW)) py += 3;
px = constrain(px, 0, width - 30);
py = constrain(py, 0, height - 30);
// Your 3 chaser rules go here
fill(0, 120, 255);
square(px, py, 30);
fill(255, 60, 60);
square(cx, cy, 30);
}
Source (adapted for this grade: wording changed, facts unchanged)
anthropic.com/learn — AI Fluency: Framework and Foundations, Description and Discernment (adapted for Grades 8–9)
anthropic.com/learn — AI Fluency: Framework and Foundations, Description and Discernment (adapted for Grades 8–9)