I am working on a new smoother 'follow-the-leader' movement code for a snake. Hope to have it up and running soon.
public boolean eats()
{
Food food = (Food) getOneIntersectingObject(Food.class);
if (food != null)
{
getWorld().removeObject(food);
return true;
}
return false;
}int newX = snakeBody[0].getX();
int newY = snakeBody[0].getY();
if (!getObjectsAt(newX, newY, Food.class).isEmpty())
{
removeObjects(getObjectsAt(newX, newY, Food.class));
growSnake();
}if (i == snakeBody.length - 1)
{
if (eaten)
{
eaten = false;
// add the new segment at coordinates (snakeBody[i].getX(), snakeBody[i].getY())
// add new segment to the array, etc.
}
}import greenfoot.*;
/**
* This is MyWorld class in which game runs.
*
* @author Chandrasekhar Thotakura
* @version 1.0.0
*/
public class MyWorld extends World
{
/**
* Defines the variables required in game.
*/
private Snake snakeBody[];
private Food food;
private Counter score;
private int direction;
private boolean eaten;
private int gameState;
private int speed;
private int speedX;
private int speedY;
int previousLocationX;
int previousLocationY;
/**
* Constructor for objects of class MyWorld.
*/
public MyWorld()
{
super(300, 450, 1); // constructor of the super class
direction = 0; // initialization
eaten = true;; // initialization
speed = 15;
speedX = speed;
speedY = 0;
setBackground("bluerock.jpg");
for (int i = 0; i < 1; i++)
{
int x = (Greenfoot.getRandomNumber(18) + 1) * 15 + 10;
int y = (Greenfoot.getRandomNumber(18) + 1) * 15 + 10;
addObject(new Food(), x, y);
}
score = new Counter("Score : "); // initialization of the food
addObject(score, 19*15, 8); // adding food actor to the world
snakeBody = new Snake[3]; // initialization of the Snake
for(int i=0; i<snakeBody.length; i++)
{
snakeBody[i] = new Snake(i==0);
addObject(snakeBody[i], 90+(snakeBody.length-i)*15, 30); // adding snake actor to the world
}
}
/**
* Game starts from here.
*/
public void act()
{
move();
checkKey();
}
/**
* This method will update the length of the Snake.
*/
private void growSnake(int x, int y)
{
Snake s = new Snake(false);
Snake oldSnake[] = new Snake[snakeBody.length];
for(int i=0; i<snakeBody.length; i++)
{
oldSnake[i] = snakeBody[i];
}
snakeBody = new Snake[snakeBody.length+1];
for(int i=0; i<snakeBody.length-1; i++)
{
snakeBody[i] = oldSnake[i];
}
snakeBody[snakeBody.length-1] = s;
addObject(snakeBody[snakeBody.length-1], x, y); // adds new snake body actor
}
/**
* This method will changes the place where the food is displayed.
*/
private void updateFoodLocation()
{
int x=0, y=0;
boolean overlap = true;
eaten = false;
while(overlap)
{
x = Greenfoot.getRandomNumber(getWidth()/15);
y = Greenfoot.getRandomNumber(getHeight()/15);
for(int i=0; i<snakeBody.length; i++)
{
// Condition to check food will not touch the snake
if(x!=snakeBody[i].getX() || y!=snakeBody[i].getY())
{
overlap = false;
break;
}
}
}
food.setLocation(x*15, y*15);
}
public void checkKey()
{
if(Greenfoot.isKeyDown("right")) // checks if right key is pressed
{
if(direction==1 || direction==3)
{ direction = 0;
speedX = speed;
speedY = 0;
}
}
else if(Greenfoot.isKeyDown("down")) // checks if down key is pressed
{
if(direction==0 || direction==2)
{direction = 1;
speedX = 0;
speedY = speed;
}
}
else if(Greenfoot.isKeyDown("left")) // checks if left key is pressed
{
if(direction==1 || direction==3)
{direction = 2;
speedX = -speed;
speedY = 0;
}
}
else if(Greenfoot.isKeyDown("up")) // checks if up key is pressed
{
if(direction==0 || direction==2)
{ direction = 3;
speedX = 0;
speedY = -speed;
}
}
}
private void move()
{
// save the location of the 'head' segment (segment in front)
// these are the 'goto' values for the next segment
previousLocationX = snakeBody[0].getX();
previousLocationY = snakeBody[0].getY();
// move head
snakeBody[0].setLocation(snakeBody[0].getX()+ speedX, snakeBody[0].getY()+speedY); // moves one step
// move body
if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY) // Checks if the snake hits walls
{
for(int i=1; i<snakeBody.length; i++)
{
int holdX = snakeBody[i].getX();
int holdY = snakeBody[i].getY();
snakeBody[i].setLocation(previousLocationX, previousLocationY);
// set 'goto' values for next segment
previousLocationX = holdX;
previousLocationY = holdY;
if( i == snakeBody.length-1)
{
if(eaten)
{
updateFoodLocation();// updates the location of the food
growSnake(previousLocationX, previousLocationY);
score.add(5);
}
}
}
for(int i=1; i<snakeBody.length; i++) // Checks if the snake eats itself
{
if(snakeBody[0].getX()==snakeBody[i].getX() && snakeBody[0].getY()==snakeBody[i].getY())
{
die();
}
}
}
else
{
die();
}
}
public void die()
{
setBackground("GameOver.jpg");
addObject(score, 19*15, 8);
}
}