Monster monster; //declare the monster
void setup() {
smooth();
background(255);
size (600, 400);
monster = new Monster(300, 200);
}
void draw() {
background(255);
monster.move(); //calling the different functions. Moving function
monster.display(); //display function: displays the monsters
}
class Monster { //creating a monster class
int locx;
int locy;
int mouthsize = 2; //monster mouthsize
//constructor. constructing the monster
Monster(int xpos, int ypos) {
locx = xpos;
locy = ypos;
}
void display () {
fill(0);
ellipse(locx, locy, 50, 50); //the face
fill(255);
ellipse(locx-17, locy-13, 10, 10);// the eyes
ellipse(locx+17, locy-13, 10, 10);
if ((mouseX > (locx-25)) && (mouseX < (locx+25)) && (mouseY > (locy-25)) && (mouseY < (locy+25))) //if the mouse x goes over the monster, then the monster's mouth opens up
mouthsize = 20;
else //or else, the mouth goes back to normal
mouthsize=2;
ellipse (locx, locy+10, mouthsize, 30); //drawing the mouthsize
}
void move() //movement of the monster
{
locx=(int)(locx+(random(-3,4))); //location x of the monster is the integer version of float random between any number -3 and 3. Meaning, random numbers between -3 and 3 are added to the loaciton x of the monster.
locy=(int)(locy+(random(-3,4))); //same as locx.
if((locx>600)||(locx<0)||(locy>400)||(locy<0)) //restriction: if monster reaches the boundaries, then it resets to a random number.
{
locx=(int)random(0,600); //random resets when monster reaches the borders
locy=(int)random(0,400);
}
}
}
Lena-L
on Sunday, Nov 23, 2008 – 5:27 pm