Jupyter at Bryn Mawr College |
|||
Public notebooks: /services/public/dblank / CS110 Intro to Computing / 2015-Fall / Notes |
Our goal is to make a standard type of creature, and then have variations.
We can have slight variations in a single class, but when you want to have many differences, you might want to have completely different classes.
First, we make a Base Class of a creature. Let's call it BaseCreature
and put it in a file called BaseCreature.java
:
You can get it from here:
or with:
%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS110%20Intro%20to%20Computing/2015-Fall/Notes/BaseCreature.java
%%file BaseCreature.java
class BaseCreature {
// Properties of the BaseCreature:
String state;
float x, y, width, height;
// Limits of movement:
float min_x, max_x, min_y, max_y;
// Other creatures:
BaseCreature[] others;
// Constructor:
BaseCreature(float x, float y,
float width, float height,
float min_x, float max_x,
float min_y, float max_y) {
this.state = "alive";
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.min_x = min_x;
this.max_x = max_x;
this.min_y = min_y;
this.max_y = max_y;
}
void setOthers(BaseCreature[] others) {
this.others = others;
}
float distance(BaseCreature other) {
return sqrt(pow(int(this.x - other.x), 2) + pow(int(this.y - other.y), 2));
}
// Common methods:
void move() {
}
void draw() {
}
void die() {
}
}
To use the BaseCreature
, we extend it (like we did with the Robot class). You can create a specific type of creature by extending the BaseCreature
.
%%file Fish.java
class Fish extends BaseCreature {
//Properties of Fish:
color mycolor;
String direction;
float speed;
// Fish constructor:
Fish(float x, float y,
float width, float height,
float min_x, float max_x,
float min_y, float max_y,
color mycolor, float speed) {
// Initialize the BaseCreature:
super(x, y, width, height, min_x, max_x, min_y, max_y);
// Set the fish-specific properties:
this.mycolor = mycolor;
this.direction = "left";
this.speed = speed;
}
// Overload base methods:
void move() {
if (this.state == "alive") {
if (this.direction == "left") {
this.x -= this.speed;
} else if (this.direction == "right") {
this.x += this.speed;
}
}
if (this.x < this.min_x)
this.direction = "right";
if (this.x > this.max_x)
this.direction = "left";
}
void draw() {
if (this.state == "alive") {
fill(this.mycolor);
ellipse(this.x, this.y, this.width, this.height);
} else {
fill(color(128));
ellipse(this.x, this.y, this.height, this.width);
}
}
}
Now we can create multiple instances of the Fish. Note that creature1's and creature2's type is BaseCreature
not Fish
:
%include BaseCreature.java
%include Fish.java
BaseCreature creature1;
BaseCreature creature2;
void setup() {
size(500, 500);
creature1 = new Fish(100, 100, 50, 25, 100, 400, 0, 500, color(255, 0, 0), 5);
creature2 = new Fish(400, 300, 50, 25, 400, 500, 0, 500, color(0, 255, 0), 1);
}
void draw() {
background(color(0, 0, 255));
// Move the creatures:
creature1.move();
creature2.move();
// Draw the creatures:
creature1.draw();
creature2.draw();
}
We can also create other creatures, extending BaseCreature
in the same way:
%%file BlowFish.java
class BlowFish extends BaseCreature {
//Properties of BlowFish:
String direction;
float speed;
// Fish constructor:
BlowFish(float x, float y,
float width, float height,
float min_x, float max_x,
float min_y, float max_y,
float speed) {
// Initialize the BaseCreature:
super(x, y, width, height, min_x, max_x, min_y, max_y);
// Set the fish-specific properties:
this.direction = "up";
this.speed = speed;
}
// Overload base methods:
void move() {
if (this.state == "alive") {
if (this.direction == "up") {
this.y -= this.speed;
} else if (this.direction == "down") {
this.y += this.speed;
}
}
if (this.y < this.min_y)
this.direction = "down";
if (this.y > this.max_y)
this.direction = "up";
// if near other, kill them:
if (this.others != null && this.state == "alive") {
for (int i=0; i<this.others.length; i++) {
if (this.others[i] != this) {
if (this.distance(this.others[i]) < 10) {
this.others[i].state = "dead";
}
}
}
}
}
void draw() {
if (this.state == "alive") {
fill(color(128, 0, 128));
rect(this.x, this.y, this.width, this.height);
} else {
fill(color(0));
rect(this.x, this.y, this.height, this.width);
}
}
}
We now put them both together in the same sea. Note again that creature 1 through 3 are all of type BaseCreature
.
%include BaseCreature.java
%include Fish.java
%include BlowFish.java
BaseCreature creature1;
BaseCreature creature2;
BaseCreature creature3;
void setup() {
size(500, 500);
creature1 = new Fish(100, 100, 50, 25, 100, 400, 0, 500, color(255, 0, 0), 5);
creature2 = new Fish(400, 300, 50, 25, 400, 500, 0, 500, color(0, 255, 0), 1);
creature3 = new BlowFish(250, 400, 100, 50, 400, 500, 100, 400, 1);
}
void draw() {
background(color(0, 0, 255));
// Move the creatures:
creature1.move();
creature2.move();
creature3.move();
// Draw the creatures:
creature1.draw();
creature2.draw();
creature3.draw();
}
And finally, allow them to be able to eat each other:
%include BaseCreature.java
%include Fish.java
%include BlowFish.java
BaseCreature creature1;
BaseCreature creature2;
BaseCreature creature3;
void setup() {
size(500, 500);
creature1 = new Fish(100, 100, 50, 25, 100, 400, 0, 500, color(255, 0, 0), 5);
creature2 = new Fish(400, 300, 50, 25, 400, 500, 0, 500, color(0, 255, 0), 1);
creature3 = new BlowFish(250, 400, 100, 50, 400, 500, 100, 400, 1);
// Make an array with all:
BaseCreature[] all = {creature1, creature2, creature3};
// Set the others:
creature1.setOthers(all);
creature2.setOthers(all);
creature3.setOthers(all);
}
void draw() {
background(color(0, 0, 255));
// Move the creatures:
creature1.move();
creature2.move();
creature3.move();
// Draw the creatures:
creature1.draw();
creature2.draw();
creature3.draw();
}