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.
*
*/
private Score score;
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(640,470, 1);
addObject(new Snake(),321,216);
addObject(new Food(),Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(430));
addObject(new Score(), 320, 450);
}
public void spawnFood(){
addObject(new Food(),Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(430));
}
}
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. Thi s method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private GreenfootImage foods = new GreenfootImage;
private int random = Greenfoot.getRandomNumber(3);
public Food(){
for(int i = 0; i < foods.length; i++){
foods = new GreenfootImage("Food" + i + ".png");
}
setImage(foods);
}
public void act()
{
}
public GreenfootImage getFoodImage(int food){
if(food == 0){
return foods;
}
else if(food == 1){
return foods;
}
return foods;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Score here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Score extends Actor
{
/**
* Act - do whatever the Score wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int score;
public Score(){
updateImage();
}
public void act()
{
// Add your action code here.
}
public void changeScore(){
score++;
updateImage();
}
public void updateImage(){
setImage(new GreenfootImage("Score: " + score, 24, Color.RED, Color.WHITE));
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Snake here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Snake extends Actor
{
/**
* Act - do whatever the Snake wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private Food food;
private Score score;
private int speed;
private boolean check;
public Snake(){
GreenfootImage snake = getImage();
snake.scale(snake.getWidth() - 155, snake.getHeight() - 110);
setImage(snake);
}
public void act()
{
control();
check();
move(25);
}
public void control(){
if(Greenfoot.isKeyDown("right") && getRotation() != 0 && getRotation() != 180){
setRotation(0);
}
if(Greenfoot.isKeyDown("left") && getRotation() != 180 && getRotation() != 0){
setRotation(180);
}
if(Greenfoot.isKeyDown("up") && getRotation() != 270 && getRotation() != 90){
setRotation(270);
}
if(Greenfoot.isKeyDown("down") && getRotation() != 90 && getRotation() != 270){
setRotation(90);
}
}
public void check(){
if(isTouching(Food.class)){
removeTouching(Food.class);
((MyWorld)getWorld()).spawnFood();
score.changeScore();
}
}
}
I keep getting this error:
java.lang.NullPointerException
at Snake.check(Snake.java:49)
at Snake.act(Snake.java:28)
at greenfoot.core.Simulation.actActor(Simulation.java:567)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)