Hello, I am creating a Snake/Worm game and am just about finished, but I have a problem with it. I want the snake to begin as one square, but it starts as a rectangular shape composed of 6 squares.
Here is the code for the head:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Worm extends Actor
{
private final int size;
private static final int EAST = 0;
private static final int SOUTH = 90;
private static final int WEST = 180;
private static final int NORTH = 270;
private int counter = 0;
private int tailCounter = 0;
public Worm(final int s)
{
switch( Greenfoot.getRandomNumber(4) ) {
case 0:
setRotation( EAST );
break;
case 1:
setRotation(WEST);
break;
case 2:
setRotation(SOUTH);
break;
case 3:
setRotation(NORTH);
}
size = s;
GreenfootImage img = new GreenfootImage(size,size);
img.fill();
setImage(img);
setRotation(Greenfoot.getRandomNumber(4)*90);
}
public void act()
{
checkKeys();
moveForward();
tailCounter++;
if( atEdgeOfWorld() ) {
getWorld().removeObject(this);
Greenfoot.stop();
return;
}else{
Actor food = getOneIntersectingObject(Food.class);
if(food!=null) {
Tail.maxLife += 3;
getWorld().addObject(new Food(20),Greenfoot.getRandomNumber(40),Greenfoot.getRandomNumber(30));
getWorld().removeObject(food);
}
}
if(tailCounter==3) {
getWorld().addObject(new Tail(),getX(),getY());
tailCounter=0;
}
}
public void moveForward()
{
counter++;
if( counter == 3 ) {
move(1);
counter = 0;
}
}
public void checkKeys()
{
if( Greenfoot.isKeyDown("right") ) {
setRotation(EAST);
}
if( Greenfoot.isKeyDown("down") ) {
setRotation(SOUTH);
}
if( Greenfoot.isKeyDown("left") ) {
setRotation(WEST);
}
if( Greenfoot.isKeyDown("up") ) {
setRotation(NORTH);
}
}
private boolean atEdgeOfWorld()
{
return getX()<=-1 || getY()<=-1 || getX()>=getWorld().getWidth()+0|| getY()>=getWorld().getHeight()+0;
}
}
Here is the code for the tail:
import greenfoot.*;
public class Tail extends Actor
{
public static int maxLife = Ground.CELL_SIZE;
private int lifespan;
public Tail()
{
GreenfootImage img = new GreenfootImage(20,20);
img.drawRect(0,0,20,20);
img.fillRect(0,0,20,20);
setImage(img);
}
public void act()
{
lifespan++;
if (lifespan == maxLife) {
getWorld().removeObject(this);
}
}
}
Here is the code for the World:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ground here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ground extends World
{
public final static int CELL_SIZE = 20;
/**
* Constructor for objects of class Ground.
*
*/
public Ground()
{
super(40, 30, 20, false);
GreenfootImage img = new GreenfootImage(CELL_SIZE, CELL_SIZE);
img.drawRect(0, 0, CELL_SIZE-1, CELL_SIZE-1);
setBackground(img);
img.fill();
int x = Greenfoot.getRandomNumber(40);
int y = Greenfoot.getRandomNumber(30);
int a = Greenfoot.getRandomNumber(40);
int b = Greenfoot.getRandomNumber(30);
addObject(new Worm(CELL_SIZE), x, y );
addObject(new Food(CELL_SIZE), a, b);
}
}
Thank you very much!

