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.
A class describes what every critter has and can do. Each new Critter() is a separate object.
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!
๐ The code your clicks are writing:
// your critter code shows up hereโฆ
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
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.
๐ก Tip: new Critter("blue") makes an object. sparky.feed() calls a method on that one object.
Click to add to your program.
Runs top to bottom.
๐ฃ Your critters: