In my game there are fish spawning at different sizes. I want to make it so that when the player touches a fish it will eat the fish (if bigger) or get eaten (if smaller) depending on the size. I have no idea how to do this and would appreciate any help. Thanks.
code for the user fish
code for the ai
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class User_fish extends Actor
{
private int movementspeed = 6;
public User_fish()
{
int percentage = 80;
GreenfootImage image = getImage();
image.scale(image.getWidth()*percentage/100, image.getHeight()*percentage/100);
}
public void User_fish()
{
int percentage = 80;
GreenfootImage image = getImage();
image.scale(image.getWidth()*percentage/100, image.getHeight()*percentage/100);
}
public void act()
{
checkKeys();
checkLeft();
if (getWorld() != null) checkRight();
}
public void checkKeys()
{
if (Greenfoot.isKeyDown("left") )
{
moveLeft();
User_fish();
}
if (Greenfoot.isKeyDown("Up"))
{
moveUp();
}
if (Greenfoot.isKeyDown("Down"))
{
moveDown();
}
if (Greenfoot.isKeyDown("Right"))
{
moveRight();
User_fish();
}
}
public void moveRight()
{
setImage("User_fish-right.png");
int worldWidth = getWorld().getWidth();
int x = (getX()+movementspeed)%worldWidth;
setLocation(x, getY());
}
public void moveUp()
{
setLocation(getX(), getY() - movementspeed);
}
public void moveDown()
{
setLocation(getX(), getY() + movementspeed);
}
public void moveLeft()
{
setImage ("User_fish-left.png");
int worldWidth = getWorld().getWidth();
int x = (getX()-movementspeed+worldWidth)%worldWidth;
setLocation(x, getY());
}
public void checkLeft()
{
Actor AI_left = getOneObjectAtOffset(0, 0, AI_left.class);
if (AI_left != null)getWorld().removeObject(this);
}
public void checkRight()
{
Actor AI_right = getOneObjectAtOffset(0, 0, AI_right.class);
if (AI_right != null) getWorld().removeObject(this);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class AI_right extends Actor
{
private int movementspeed = 5;
public void act()
{
AI_right();
remove();
}
public void AI_right()
{
setLocation(getX() + movementspeed, getY());
}
public void remove()
{
if (getX() >= getWorld().getWidth() -1)
getWorld().removeObject(this);
else if (getX() <=1)
getWorld().removeObject(this);
else if (getY() >= getWorld().getHeight() -1)
getWorld().removeObject(this);
else if (getY() <=1)
getWorld().removeObject(this);
}
public AI_right()
{
GreenfootImage img = new GreenfootImage("AI_fish-right.png");
double scaleFactor = (Greenfoot.getRandomNumber(13)+1) / 10.0; //random number between 0.1 and 4.0
img.scale( (int)(img.getWidth()*scaleFactor), (int)(img.getHeight()*scaleFactor));
setImage(img);
}
}
