That worked great. Now if I add a public void eat, do I add a getColor and return color and then add the eat function?
for (int y=2; y<33; y+=4) for (int x=2; x<33; x+=4)
public void eat()
{
Actor tile;
tile = getOneObjectAtOffset(0, 0, Tile.class);
if (tile !=null)
{
World world;
world = getWorld();
world.removeObject(tile);
}
} public void getEaten()
{
color = 0;
setImage("egg0.png");
} Tile tile = (Tile)getOneObjectAtOffset(0, 0, Tile.class);
if (tile == null || tile.getColor() == 0)
{
return;
}
if (tile.getColor() != color)
{
setLocation(getX()-dx, getY()-dy);
}
else
{
tile.getEaten();
mutate();
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Arrow here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Arrow extends Actor
{
private Counter counter;
private int tileDelayCount;
public Arrow(Counter pointCounter)
{
counter = pointCounter;
tileDelayCount = 0;
}
/**
* Act - do whatever the Arrow wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int color;
public Arrow()
{
color = Greenfoot.getRandomNumber(5)+1;
setImage(new GreenfootImage("arrow"+color+".png"));
}
public int getColor()
{
return color;
}
public void addedToWorld(World world)
{
while (((Tile)getOneObjectAtOffset(0, 0, Tile.class)).getColor() != 0)
{
int x = Greenfoot.getRandomNumber(world.getWidth());
int y = Greenfoot.getRandomNumber(world.getHeight());
setLocation(x, y);
}
}
private void mutate()
{
color = Greenfoot.getRandomNumber(5)+1;
setImage("arrow"+color+".png");
}
public void act() {
move();
checkNewTile();
newTile();
}
public void move()
{
String key = Greenfoot.getKey();
int dx = 0, dy = 0; // the offsets from current location
if ("up".equals(key)) dy--;
if ("down".equals(key)) dy++;
if ("left".equals(key)) dx--;
if ("right".equals(key)) dx++;
setLocation(getX()+dx, getY()+dy);
Tile tile = (Tile)getOneObjectAtOffset(0, 0, Tile.class);
if (tile == null || tile.getColor() == 0)
{
return;
}
if (tile.getColor() != color)
{
setLocation(getX()-dx, getY()-dy);
}
else
{
tile.getEaten();
mutate();
newTile();
}
private void newTile()
{
tileDelayCount = 100;// check for illegal movement here and reset location adding negative offsets
}
private void checkNewTile()
{
if (tileDelayCount > 0) {
tileDelayCount--;
if (tileDelayCount == 0) {
createNewTile();
}
}
}
private void createNewTile()
{
int num = Greenfoot.getRandomNumber(15);
if (num > 5) num = 0;
addObject(new Tile(num), x, y);
}
}
} if (tileDelayCount > 0) {
tileDelayCount--;
if (tileDelayCount == 0) {
mutate();
}
}