public class Snake extends Animal
{
private int turnCounter = 0;
/**
* Act - do whatever the Snake wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
checkKeypress();
lookForFood();
snakeDetection();
}
public void move()
{
move(-4);
}
public void checkKeypress()
{
if(Greenfoot.isKeyDown("left")) {
setRotation(0);
move(5);
}
if(Greenfoot.isKeyDown("up"))
{
setRotation(90);
move(5);
}
if(Greenfoot.isKeyDown("right"))
{
setRotation(180);
move(5);
}
if(Greenfoot.isKeyDown("down"))
{
setRotation(270);
move(5);
}
}
public void lookForFood()
{
if(canSee(Food.class))
{
eat(Food.class);
}
}
public void snakeDetection()
{
Actor a = getOneIntersectingObject (Snake.class);
if(a != null){
World myWorld = getWorld();
GameOver gameover = new GameOver();
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
getWorld().removeObject(a);
}
}
private void growTail()
{
}
}
