I created a game where a seal eats all the grapes in the ocean. The seal must avoid the birds to be eaten. My concern is how can I reset the score to zero each time without clicking the compile button? I need this project finished by Tuesday.
For the ocean as background:
public class ocean extends World
{
/**
* Constructor for objects of class ocean.
*
*/
public ocean()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
addObject(new seal(), 25, getHeight()/2);
addObject(new grapes(), getWidth() - 40, getHeight()/2);
addObject( new grapes(), (Greenfoot.getRandomNumber(450)), (Greenfoot.getRandomNumber(450)));
addObject( new grapes(), (Greenfoot.getRandomNumber(450)), (Greenfoot.getRandomNumber(450)));
addObject(new score(), 40, getHeight()/2);
populateWorld();
}
public void populateWorld()
{
for (int i=1; i<4; i++)
{
addObject( new bird(), (Greenfoot.getRandomNumber(450)), (Greenfoot.getRandomNumber(450))) ;
}
for (int i= 1; i< 10; i++)
{
addObject( new grapes(), (Greenfoot.getRandomNumber(500)), (Greenfoot.getRandomNumber(500)));
}
}
}
The Seal for the user:
public static int score = 0;
public void act()
{
checkForArrowKeys(5);
}
public void checkForArrowKeys (int adjust)
{
if ( Greenfoot.isKeyDown("left") )
{
setLocation(getX() - adjust, getY());
}
if ( Greenfoot.isKeyDown("right") )
{
setLocation(getX() + adjust, getY());
}
if ( Greenfoot.isKeyDown("down") )
{
setLocation(getX() , getY()+ adjust);
}
if ( Greenfoot.isKeyDown("up") )
The Bird to eat Seal:
public class bird extends Actor
{
/**
* Act - do whatever the bird wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
randomMove( 5 );
tryToCatch( seal.class );
}
public void randomMove (int maxAmount)
{
setLocation(getX() + (-maxAmount + Greenfoot.getRandomNumber(2 * maxAmount + 1)) , getY() + (-maxAmount + Greenfoot.getRandomNumber(2 * maxAmount + 1)) );
}
public void tryToCatch(Class clss)
{
Actor actor = getOneObjectAtOffset(0,0,clss);
if ( actor !=null )
{
getWorld().removeObject (actor);
}
}
The Grapes:
public class grapes extends Actor
{
/**
* Act - do whatever the grapes wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private boolean flag = false;
public void act()
{
checkForseal();
}
public void checkForseal()
{
seal theseal = null;
theseal = (seal)getOneIntersectingObject(seal.class);
if(theseal !=null)
{
if (flag == false)
{
seal.score++;
flag = true;
getWorld().removeObject(this);
}
}
}
}
And Finally my Score (the purple ball):
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;
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.
*/
public void act()
{
updatescore();
}
public void updatescore()
{
GreenfootImage image = new GreenfootImage(145, 35);
image.setColor(new Color(0, 0, 0, 160));
image.fillRect(0, 0, 145, 35);
Font font = new Font ("Arial", Font.PLAIN, 30);
image.setFont(font);
image.setColor(Color.WHITE);
image.drawString("score: " + seal.score, 5, 27);
setImage(image);
}
}

