Tail Code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Tail here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Tail extends Actor
{
/**
* Act - do whatever the Tail wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int w,g,b;
int count=0;
public Tail(int w, int g, int b)
{
this.w=w;
this.b=b;
this.g=g;
getImage().setColor(new Color(w,g,b));
getImage().fillRect(0,0,40,40);
}
public void act()
{
count++;
if (count >60)
getWorld().removeObject(this);
// Add your action code here.
}
}
Snake Code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int w, g, b, player;
int speed = 2;
int count=0;
public Player(int player,int w,int b)
{
setRotation(180);
this.w = w;
this.b = b;
this.g = g;
getImage().setColor(new Color(w,g,b));
getImage().fillRect(0,0,40,40);
}
public void act()
{
count++;
getWorld().addObject(new Tail(w, b, g), getX(), getY());
move(speed);
if(this.player == 0){
if(Greenfoot.isKeyDown("right"))
setRotation(0);
if(Greenfoot.isKeyDown("left"))
setRotation(180);
if(Greenfoot.isKeyDown("up"))
setRotation(270);
if(Greenfoot.isKeyDown("down"))
setRotation(90);
if(isTouching(Food.class)) {
removeTouching(Food.class);
MyWorld world = (MyWorld)getWorld();
world.addFood();
}
}
}
}
World Code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
/**
* Constructor for objects of class MyWorld.
*
*/
Player bluePlayer = new Player(0,255,0);
Player whitePlayer = new Player(255,0,0);
int count=0;
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
getBackground().setColor(Color.BLACK);
getBackground().fill();
addObject(bluePlayer, 450, 300);
addObject(whitePlayer, 450,100);
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addFood();
}
public void addFood()
{
count++;
int x = Greenfoot.getRandomNumber(400);
int y = Greenfoot.getRandomNumber(400);
addObject(new Food(), x+1, y+1);
}
}
Food Code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Food here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Food extends Actor
{
/**
* Act - do whatever the Food wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int count=0;
public void act()
{
count++;
if(count > 400)
getWorld().removeObject(this);
}
}