Hi everyone!
Having BIG time problem producing an Snake game in Greenfoot. The problem is that I want to expand and take the snakes body segments from an Array and I have no clue where to start or who to ask.
---------------------------------
I have this in my world:
SnakeHead snakeExtension = new SnakeHead ;
snakeExtension = new SnakeHead();
And I wrote this in my constructor for my SnakeHead class.
public SnakeHead()
{
}
What kind of parameters do the Constructor for the SnakeHEad class want?
------------------------------------------------------------------
WORLD CLASS:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class SnakeWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SnakeWorld extends World
{
SnakeHead snakeExtension = new SnakeHead ;
/**
* Constructor for objects of class SnakeWorld.
*
*/
public SnakeWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(80, 80, 10);
addObject( new SnakeHead(), 10, 10);
addObject( new Food(), 16, 16);
GreenfootImage gi = new GreenfootImage(this.getWidth(),this.getHeight());
for (int i = 0; i < gi.getWidth(); i += 10) {
gi.drawLine(i,0,i,gi.getHeight());
}
for (int i = 0; i < gi.getHeight(); i += 10) {
gi.drawLine(0, i, gi.getWidth(),i);
}
snakeExtension = new SnakeHead();
snakeExtension = new SnakeHead();
snakeExtension = new SnakeHead();
this.setBackground(gi);
getSnakeExtension();
}
public Actor getSnakeExtension()
{
return snakeExtension;
}
}
CLASS SNAKE:
public class SnakeHead extends Actor
{
int location = 1;
int foodOnScreen = 1;
int score = 0; //HIGHSCOREN
/**
* Act - do whatever the SnakeHead wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public SnakeHead()
{
}
public void act()
{
Actor snakeExtension = ((SnakeWorld)getWorld()).getSnakeExtension();
//dead();
checkForFoodCollide();
foodSpawn();
if(location == 1) //Right
{
setLocation(getX() + 3, getY());
if(Greenfoot.isKeyDown("up"))
{
location = 2;
setLocation(getX() -3, getY());
}
else if(Greenfoot.isKeyDown("down"))
{
location = 3;
setLocation(getX() -3, getY());
}
}
if(location == 2) //Up
{
setLocation(getX(), getY() - 3);
if(Greenfoot.isKeyDown("left"))
{
location = 4;
setLocation(getX(), getY() + 3);
}
else if(Greenfoot.isKeyDown("right"))
{
location = 1;
setLocation(getX(), getY() + 3);
}
}
if(location == 3) //down
{
setLocation(getX(), getY()+ 3);
if(Greenfoot.isKeyDown("left"))
{
location = 4;
setLocation(getX(), getY()-3);
}
else if(Greenfoot.isKeyDown("right"))
{
location = 1;
setLocation(getX(), getY()-3);
}
}
if(location == 4) //left
{
setLocation(getX() - 3, getY());
if(Greenfoot.isKeyDown("up"))
{
location = 2;
setLocation(getX() + 3, getY());
}
else if(Greenfoot.isKeyDown("down"))
{
location = 3;
setLocation(getX() + 3, getY());
}
}
}
public void mover()
{
}
public void dead()
{
if(getY() <= 0)
{
Greenfoot.stop();
setLocation(getX(), 1);
}
else if(getY() > 29)
{
Greenfoot.stop();
setLocation(getX(), 28);
}
else if(getX() < 0)
{
Greenfoot.stop();
setLocation(1, getY());
}
else if(getX() > 29)
{
Greenfoot.stop();
setLocation(28, getY());
}
}
//Det här metodanropet betyder att ormen hela tiden skall leta efter en pizza bit
public void checkForFoodCollide()
{
if ( whenCollide(Food.class) )
{
eat(Food.class);
}
}
//Det här är metoden för vad som händer när ormen hittar en pizza bit och för vad som händer just då.
//Samt vad just den här metoden skall retunera. Hade jag inte haft denna metoden här så hade jag aldrig fått
//Ut värdet som jag använder i metoden under denna.
public boolean whenCollide(Class eatStuff)
{
Actor pizza = getOneObjectAtOffset(0, 0, eatStuff);
return pizza != null;
}
//Det här är metoden för vad som sak hända med pizza biten såfort den äts upp.
public void eat(Class eatStuff)
{
Actor pizza = getOneObjectAtOffset(0, 0, eatStuff);
if(pizza != null)
{
getWorld().removeObject(pizza);
foodOnScreen = foodOnScreen - 1;
score++;
//snakeExtension = new Body();
}
}
//Här spawnar jag maten till en random location.. Inte mycket att beskriva egentligen.
public void foodSpawn()
{
int spawnTheFoodX = Greenfoot.getRandomNumber(60);
int spawnTheFoodY = Greenfoot.getRandomNumber(60);
if(foodOnScreen == 0)
{
getWorld().addObject(new Food(), spawnTheFoodX + 3 , spawnTheFoodY + 3);
foodOnScreen ++;
}
}
}