A beginner's guide for young coders aged 7+
Type code on the left â see it run on the right, live!
Lesson 1 of 6
p5.js gives the computer a magic paintbrush! Every sketch has two special functions. Type in the editor below and press Run âļ to see it come alive!
background() in the editor. Try (255, 200, 50) for golden yellow, (100, 0, 180) for purple, or (0, 150, 200) for ocean blue. Press Run each time!The screen is a grid of pixels. X goes left→right, Y goes top→bottom. Top-left is (0,0). Try adding or changing shapes in the editor!
rect(), or a path using line(x1,y1,x2,y2). Try adding fill(100,200,100) then rect(0,190,400,50) for green grass at the bottom!A variable is a labelled box for a number. Name it once at the top, use it anywhere, change it in one place â and everything updates! Try changing sunX or speed in the editor.
speed from 2 to 5 â is the sun faster? Try speed = -2 to make it go the other way! Then add sunY = sunY + 1 to make it move diagonally.A function is a recipe. Write the steps once, give it a name, call it with different ingredients (parameters) each time. Try calling drawCloud() a 4th time!
Quick check: in function greet(name) â what is name called?
A for loop lets you write code ONCE and repeat it automatically. The variable i counts for you. Try changing the loop limit from 8 to 20!
i < 8 to i < 20 and i * 40 to i * 18. Then try making sizes grow: circle(x, 80, i*8+10). The nested loop below makes a colour grid!draw() loops forever â move a shape a tiny bit each frame and it animates! if statements make the ball bounce. Try adding a second ball!
let bX2=50, bY2=80, spX2=-2, spY2=3;fill(50,200,255). You've built a game engine!đ¯ What you've learned
đšī¸ Next: Add mouseX/mouseY to control things with your mouse
đ¸ Use random() inside loops to make generative art
đ Full reference: p5js.org/reference | thecodingtrain.com