๐Ÿงฌ
OOP Studio ยท Ages 9โ€“15

The Critter Factory

Object-Oriented Programming is all about blueprints. You design one class (a blueprint), then stamp out as many objects as you like from it. Here, the blueprint is a Critter โ€” and every critter you create is its own little creature with its own properties and methods.

One blueprint, many critters

A class describes what every critter has and can do. Each new Critter() is a separate object.

๐Ÿ“ class Critter
โ€ข color, name
โ€ข energy
โ€ข feed() ยท play() ยท speak()
โžœ
๐Ÿ“Classthe blueprint
๐ŸฃObjectone real critter
๐ŸŽจPropertyits color, name, energy
โšกMethodan action it can do
Play it

Critter playground

Press New Critter to stamp objects from the blueprint. Click a critter to select it, then call a method on it. Watch the code you're writing appear below!

No critters yet โ€” press โ€œโž• New Critterโ€!

๐Ÿ“ The code your clicks are writing:

// your critter code shows up hereโ€ฆ

What the blueprint looks like in code

This is the class behind every critter on this page.

class Critter {
  // the blueprint's starting values
  constructor(color, name) {
    this.color = color;
    this.name = name;
    this.energy = 40;
  }

  // methods = things a critter can do
  feed() { this.energy = this.energy + 25; }
  speak() { say("Hi, I'm " + this.name); }
}

let sparky = new Critter("blue", "Sparky");
sparky.feed();   // Sparky's energy is now 65
Now you code it

Code Lab: build with classes

Use the Critter blueprint to create objects and call their methods. Click blocks to build your program, then Run it to bring your critters to life.

๐Ÿ“‹ How this Code Lab works

  1. Read the ๐ŸŽฏ Mission goal at the top.
  2. Click a block on the left to add it to Your program. Lines run top to bottom.
  3. Drag a block (or use โ†‘ โ†“) to reorder; tap ร— to remove.
  4. Press โ–ถ Run code โ€” your critters appear and act out your code.
  5. Green โœ… = solved (Next mission appears). Blue ๐Ÿ’ก = a hint to try again.

๐Ÿ’ก Tip: new Critter("blue") makes an object. sparky.feed() calls a method on that one object.

๐Ÿงฑ Code blocks

Click to add to your program.

๐Ÿ“ Your program

Runs top to bottom.

๐Ÿฃ Your critters: