import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Predator here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Predator extends Actor
{
int speed = Greenfoot.getRandomNumber(5) + 1;
int width = Greenfoot.getRandomNumber(5) + 5;
int height = Greenfoot.getRandomNumber(5) + 5;
int turnRate = Greenfoot.getRandomNumber(10) + 20;
int hunger = 700;
int heritedSpeed;
public Predator()
{
picture(0,0,width,height);
setRotation(Greenfoot.getRandomNumber(360));
heritedSpeed = Greenfoot.getRandomNumber(1) + speed;
}
public void picture(int x, int y, int width, int height)
{
GreenfootImage image = new GreenfootImage(width,height);
image.setColor(Color.RED);
image.fillRect(x,y,width,height);
setImage(image);
}
public void movement()
{
move(speed);
if(Greenfoot.getRandomNumber(turnRate) == 1)
{
setRotation(Greenfoot.getRandomNumber(360));
}
}
public void act()
{
movement();
bounceOffWall();
catchingPrey();
death();
}
public void bounceOffWall()
{
if (getY() == 0 || getY() == getWorld().getHeight()-1)
{
setRotation(360-getRotation());
}
if (getX() == 0 || getX() == getWorld().getWidth()-1)
{
setRotation(180-getRotation());
}
}
public void catchingPrey()
{
int x = getX();
int y = getY();
if(isTouching(Prey.class))
{
removeTouching(Prey.class);
hunger = 700;
getWorld().addObject(new Predator(),x,y);
}
}
public void death()
{
hunger -= (0.1 + ((width + height)/15) + (speed/3));
if(hunger < 1)
{
getWorld().removeObject(this);
}
}
public int reproduction()
{
speed = Greenfoot.getRandomNumber(1) + speed;
return speed;
}
}
