SnakeHead
SnakeBody
This code almost work although the body segments overlap each other and then i get an overflow error. Something in my code isn't quite right but I can't put my finger on it?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List; // import java for lists
/**
* Write a description of class SnakeHead here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SnakeHead extends Actor
{
public int offsetX = 0; //global variable storing the random X direction (1, 0 or -1)
public int offsetY = -1; //global variable storing the random Y direction (1, 0 or -1)
public int lastx; //variable for storing the last x co-ordinate
public int lasty; //variable for storing the last x co-ordinate
public SnakeBody nextSnakeBody=null;
public SnakeHead() // constructor
{
setDirection(); //set a direction for the lizard.
}
public void act()
{
lastx = getX(); //get current x co-ordinate
lasty = getY(); // get current y co-ordinate
processKeys();
move();
setDirection();
findEatFood();
}
private void processKeys()
{
if(Greenfoot.isKeyDown("left")) //move left
{
offsetX=-1;
offsetY=0;
}
if(Greenfoot.isKeyDown("right")) //move right
{
offsetX=1;
offsetY=0;
}
if(Greenfoot.isKeyDown("up")) //move up
{
offsetX=0;
offsetY=-1;
}
if(Greenfoot.isKeyDown("down")) //move down
{
offsetX=-0;
offsetY=1;
}
}
private void move() //
{
int currX = getX(); //get current x co-ordinate
int currY = getY(); // get current y co-ordinate
if(canMove(currX + offsetX, currY + offsetY))
setLocation(currX + offsetX, currY + offsetY); //if the snake can move it will move in a direction
}
private void setDirection() //method to set the current direction
{
do{ }while(offsetX==0 && offsetY==0);
if(offsetX == -1 && offsetY == 0) //if the snake will go west
setRotation(180); //set rotation to south
else if(offsetX == 0 && offsetY == -1) //if the snake will go north
setRotation(270); //set rotation to west
else if(offsetX == 0 && offsetY == 1) //if the snake will go south
setRotation(90); //set rotation to east
else if(offsetX == 1 && offsetY == 0) //if the snake will go east
setRotation(0); //set rotation to north
}
private void findEatFood()// funcion to eat an actor. Specifically Food
{
Actor thisFood=getOneObjectAtOffset(0,0,Food.class); // is there Food in the current square?
if(thisFood!=null) //there is no Food
{
Greenfoot.playSound("Belch.wav"); //play sound file
getWorld().removeObject(thisFood); //removes an actor from the world once it has been eaten.
List getScore=getWorld().getObjects(Score.class); //get the score
if(!getScore.isEmpty())
{
Score currScore=(Score)getScore.get(0); // check that there is a score
currScore.updateScore(1); //add 1 to the current score
}
int x = Greenfoot.getRandomNumber(27)+1; //random x co-ordinate
int y = Greenfoot.getRandomNumber(27)+1; //random y co-ordinate
getWorld().addObject(new Food(), x, y); //add new object at a random co-ordinate
addSnakeBody(); //addSnakeBody actor
}
}
private void addSnakeBody() //
{
if(nextSnakeBody==null){ //if there is no SnakeBody
int currX = getX(); //get current x co-ordinate
int currY = getY(); // get current y co-ordinate
nextSnakeBody=new SnakeBody((Actor)this, true); //newSnakeBody is a SnakeBody actor
getWorld().addObject(nextSnakeBody, lastx, lasty); //add SnakeBody at the last location of SnakeHead
}
else nextSnakeBody.addSnakeBody(); // if there is a SnakeBody add another
}
boolean canMove(int x,int y)
{
List foundWall = getWorld().getObjectsAt(x,y, Wall.class); //canMove is false if a Wall is found
if(!foundWall.isEmpty ())return false; //if Wall found return false and stop otherwise continue
List foundSnakeHead = getWorld().getObjectsAt(x,y, SnakeHead.class); //canMove is false if a SnakeHead is found
if(!foundSnakeHead.isEmpty ())return false; //if SnakeHead found return false and stop otherwise continue
List foundSnakeBody = getWorld().getObjectsAt(x,y, SnakeBody.class); //canMove is false if a SnakeBody is found
if(!foundSnakeBody.isEmpty ())return false; //if SnakeBody found return false and stop otherwise continue
return true; //this must be at the end – if canMove got this far it didn't find any of the listed objects.
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List; // import java for lists
/**
* Write a description of class SnakeBody here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SnakeBody extends Actor
{
public int offsetX = 0; //global variable storing the random X direction (0)
public int offsetY = 0; //global variable storing the random Y direction (0)
public SnakeBody lastSnakeBody=null; //there is no last SnakeBody
public SnakeHead lastSnakeHead=null; //there is no last SnakeHead
public int lastx; //variable for storing the last x co-ordinate
public int lasty; //variable for storing the last x co-ordinate
boolean joinedtoHead;
public SnakeBody (Actor thisBody, boolean isSnakeHead)
{
if(isSnakeHead)
{
lastSnakeHead=(SnakeHead)thisBody;
joinedtoHead=isSnakeHead;
setLocation(lastSnakeHead.lastx, lastSnakeHead.lasty);
}
else
{
lastSnakeBody=(SnakeBody)thisBody;
setLocation(lastSnakeBody.lastx, lastSnakeBody.lasty);
}
}
public void act()
{
lastx=getX();
lasty=getY();
move();
growSnake();
}
private void growSnake() //the SnakeBody will follow behind the head by using the current x and y of the head
{
int offsetX=0;
int offsetY=0;
int currX = getX(); //get current x co-ordinate
int currY = getY(); // get current y co-ordinate
List getSnakeHead=getWorld().getObjects(SnakeHead.class); //get the SnakeHead actor from the world.
if(!getSnakeHead.isEmpty ()) //if there is no SnakeHead
{
SnakeHead thisSnakeHead=(SnakeHead)getSnakeHead.get(0); //is there a SnakeHead in the current location?
offsetX=thisSnakeHead.offsetX; //the x co-ordinate of SnakeHead
offsetY=thisSnakeHead.offsetY; //the y co-ordinate of SnakeHead
}
setLocation(currX+offsetX, currY+offsetY); //
}
private void move() //
{
if(joinedtoHead)
{
setLocation(lastSnakeHead.lastx, lastSnakeHead.lasty);
}
else
{
setLocation(lastSnakeBody.lastx, lastSnakeBody.lasty);
}
}
public void addSnakeBody()
{
if(lastSnakeBody==null){ //if there is no SnakeBody
int currX = getX(); //get current x co-ordinate
int currY = getY(); // get current y co-ordinate
lastSnakeBody=new SnakeBody((Actor)this, false); //newSnakeBody is a SnakeBody actor
getWorld().addObject(lastSnakeBody, currX-offsetX, currY-offsetY); //add SnakeBody at location of SnakeHead - 1
}
else lastSnakeBody.addSnakeBody(); // if there is a SnakeBody add another
}
boolean canMove(int x,int y)
{
List foundWall = getWorld().getObjectsAt(x,y, Wall.class); //canMove is false if a Wall is found
if(!foundWall.isEmpty ())return false; //if Wall found return false and stop otherwise continue
List foundSnakeHead = getWorld().getObjectsAt(x,y, SnakeHead.class); //canMove is false if a SnakeHead is found
if(!foundSnakeHead.isEmpty ())return false; //if SnakeHead found return false and stop otherwise continue
List foundSnakeBody = getWorld().getObjectsAt(x,y, SnakeBody.class); //canMove is false if a SnakeBody is found
if(!foundSnakeBody.isEmpty ())return false; //if SnakeBody found return false and stop otherwise continue
return true; //this must be at the end – if canMove got this far it didn't find any of the listed objects.
}
}