I am current making a snake game. Can someone please help me create a score counter? I want the score to increase by one point when the snake eats a piece of food. When the score reaches to 15, the game is over and the user wins.
Here is the code:
MyWorld-
Tail:
Food:
Head:
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
{
public MyWorld()
{
super(30, 20, 20); //30 width, 30 height, 20x20 pixels
GreenfootImage img= new GreenfootImage (20,20);
img.drawRect(0, 0, 19, 19); //draw rectangle
setBackground(img);
addHead(); //add 'head' to world
addFood(); //adding food to world
addFood();
}
public void addHead()
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new Head(), x, y); //random placement of 'head'
}
public void addFood()
{
int x = Greenfoot.getRandomNumber(getWidth()); //random x co-ordinate
int y = Greenfoot.getRandomNumber(getHeight()); //random y co-ordinate
addObject(new Food(), x, y); //random placement of food
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; //import colour class to set image as black
/**
* Write a description of class Tail here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Tail extends Actor
{
private int age = 0; //initial age of tail
private int lifeSpan;
public Tail(int lifeSpan)
{
GreenfootImage img = new GreenfootImage (20,20);
img.fill(); //no colour specified- black
setImage(img);
this.lifeSpan = lifeSpan; //this=tail
}
public void act()
{
if (age == lifeSpan) {
getWorld().removeObject(this);
}
age++;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; //import colour class to set image as red
/**
* Write a description of class Food here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Food extends Actor
{
public Food()
{
GreenfootImage img = new GreenfootImage(20,20);
img.setColor(Color.RED);
img.fill();
setImage(img);
}
public void act()
{
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; //import colour class to set image as black
/**
* Write a description of class Head here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Head extends Actor
{
private final int NORTH = 270;
private final int SOUTH = 90;
private final int EAST = 0;
private final int WEST = 180;
private final int SPEED = 10;
private int counter = 0;
private int foodEaten = 0;
public Head()
{
GreenfootImage img = new GreenfootImage (20,20);
img.fill(); //no colour specified- black
setImage(img);
setRotation(Greenfoot.getRandomNumber(4)*90); //gets a random number(0,1,2,3) and multiplies by 90 to get private final int)
}
public void act()
{
moveRandom();
getKeys();
if(isTouching(Food.class)) { //checks to see if actor is touching any other object
removeTouching(Food.class); //removes object from class that the actor is touching
foodEaten++;
MyWorld world = (MyWorld)getWorld();
world.addFood();
}
if ( facingEdge() ) {
Greenfoot.stop(); //stop game if facing edge of world
}
}
private boolean facingEdge()
{
switch(getRotation()) {
case EAST: return getX()==getWorld().getWidth()-1;
case SOUTH: return getY()==getWorld().getHeight()-1;
case WEST: return getX()==0; //if x co-ordinate is o
case NORTH: return getY()==0; //if y co-ordinate is o
}
return false;
}
public void moveRandom()
{
counter ++; //counter + 1
if(counter==SPEED) { //speed = 10
getWorld().addObject(new Tail(foodEaten*SPEED), getX(), getY());
move(1); //move one cell = 20x20 pixels
counter=0;
}
}
public void getKeys()
{
if (Greenfoot.isKeyDown("right")) {
setRotation(EAST);
}
if (Greenfoot.isKeyDown("left")) {
setRotation(WEST);
}
if (Greenfoot.isKeyDown("up")) {
setRotation(NORTH);
}
if (Greenfoot.isKeyDown("down")) {
setRotation(SOUTH);
}
}
}