import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class SnakeHead here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SnakeHead extends Animal
{
int speed = 20;
int Diamondseaten = 0;
int count = 0;
/**
* Act - do whatever the SnakeHead wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveAround();
if(isTouching(Diamond.class)) {
removeTouching(Diamond.class);
Diamondseaten++;
SnakeWorld world = (SnakeWorld)getWorld();
world.addDiamond();
}
if(isTouching(Wall.class)) {
Greenfoot.setWorld(new SnakeWorld());
Greenfoot.stop();
}
if(isTouching(SnakeTail.class)) {
Greenfoot.setWorld(new SnakeWorld());
Greenfoot.stop();
}
}
private void moveAround()
{
count ++;
if(count==speed){
getWorld().addObject(new SnakeTail(Diamondseaten*speed), getX(), getY());
move(1);
count = 0;
}
move();
if (Greenfoot.isKeyDown("left")) {
turn(-25);
}
if (Greenfoot.isKeyDown("right")){
turn(25);
}
if (Greenfoot.isKeyDown("up")){
move();
}
if (Greenfoot.isKeyDown("down")){
move(-2);
}
}
}
