I tried to add a counter but I cant pass the referens to my snake cuz it already have a parameter what should I do?
i want to remove all objects in the world but I want to call the method in my snake class
public void addedToWorld(World world)
{
if (!"head".equals(segment)) return;
counter = new Counter(); // may have to adjust constructor call
world.addObject(counter, 100, 100); //wherever
}getWorld().removeObjects(getWorld().getObjects(null));
if (getWorld() == null) return;
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Playground here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Playground 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;
/**
* Constructor for objects of class Playground.
*
*/
public Playground()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
direction = 0;
eaten = false;
gameState = 0;
}
/**
* Game starts from here.
*/
public void act()
{
checkKeys();
gameStates();
if(eaten)
{
updateFoodLocation();// updates the location of the food;
}
int previousLocationX = snakeBody[0].getX();
int previousLocationY = snakeBody[0].getY();
snakeBody[0].move(20); // moves one step
if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY) // Checks if the snake hits walls
{
for(int i=1; i<snakeBody.length; i++)
{
previousLocationX = snakeBody[i].getX();
previousLocationY = snakeBody[i].getY();
snakeBody[i].move(20); // entire body moves one step
}
if(snakeBody[0].getX()==food.getX() && snakeBody[0].getY()==food.getY()) // Checks if the snake eats food
{
growSnake(previousLocationX, previousLocationY);
updateFoodLocation();
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());
{
gameState = -1;
}
}
}
else
{
gameState = -1;
}
}
/**
* 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);
}
private void updateFoodLocation()
{
int x = 0;
int y = 0;
boolean overlap = true;
eaten = false;
while(overlap)
{
x = Greenfoot.getRandomNumber(getWidth());
y = Greenfoot.getRandomNumber(getHeight());
for( int i = 1; 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 checkKeys()
{
if(Greenfoot.isKeyDown("right")) // checks if right key is pressed
{
if(direction==1 || direction==3)
direction = 0;
}
else if(Greenfoot.isKeyDown("down")) // checks if down key is pressed
{
if(direction==0 || direction==2)
direction = 1;
}
else if(Greenfoot.isKeyDown("left")) // checks if left key is pressed
{
if(direction==1 || direction==3)
direction = 2;
}
else if(Greenfoot.isKeyDown("up")) // checks if up key is pressed
{
if(direction==0 || direction==2)
direction = 3;
}
}
public void gameStates()
{
if( gameState==0) // Initialization
{
setBackground("SnakeGame.jpg");
}
if(Greenfoot.getKey() !=null) // check if any key is pressed
{
gameState = 1; return;
}
else if ( gameState == 1) // Game Activate State
{
setBackground("bluerock.jpg");
food = new Food(); // initialization of the food
addObject(food, 0,0); // adding food actor to the world
score = new Counter("Score : "); // initialization of the score
addObject( score, 20, 4);// adding score
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);
}
updateFoodLocation();
gameState = 2;
return;
}
else if( gameState == -1) // game over
{
setBackground("GameOver.jpg");
addObject(score, 300, 8);
return;
}
}
}