I am a complete beginner in Programming, and I am currently working on the little crab game. The project is due tomorrow, and as extra credit, we are supposed to add new levels once 8 worms are eaten. In the second level, you must eat 12 worms to win. I am having trouble changing the value of level (a field i set to 1 at the start), so that the program will display the outcome for another conditional statement when wormCount=12 and not 8. I'm sorry if that was really confusing. But if any one is willing to help, please do so! It will be greatly appreciated
import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage)
public class CrabWorld extends World
{
/**
* instantiates objects for Level 1
*/
public CrabWorld()
{
super(600, 560, 1);
System.out.println ("Player 1, you are the crab.");
System.out.println ("Your goal is to eat all the worms without being eaten by a lobster.");
System.out.println ("Continuously hold down the space bar to move, while using the right key to turn right, and the left key to turn left.");
System.out.println ("Player 2, you are the person.");
System.out.println ("Your goal is to catch the lobster without being stopped by the police.");
System.out.println ("Continuously hold down the s key to move, while using the d key to turn right, and the a key to turn left.");
System.out.println ("If Player 1 eats all the worms or if Player 2 catches the lobster, you will move on to the next level.");
System.out.println ("BUT if either of you gets eaten or caught, the game is over.");
//System.out.println ("You can also press the p button to pause the game.");
System.out.println ("Good luck!");
populateWorld();
}
public void act()
{
//checkForPause();
}
/**
* Create the crab world (the beach). Our world has a size
* of 600x560 cells, where every cell is just 1 pixel.
* 8 worms, 1 lobster, 1 person, 1 crab, 1 police, 1 circle to indicate 1 level
*/
public void populateWorld()
{
Level one =new Level();
addObject(one, 580,15);
Crab stanley=new Crab();
/*Greenfootmethod w/3 perameters object you drop into world
*name of object
*choose x coordinate for location
*choose y coordinate for location
* going down world=bigger width and height >> larger (x,y)
*/
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (stanley, ((int)(Math.random()*560)), ((int)(Math.random()*560)) );
Lobster tim=new Lobster();
addObject (tim, ((int)(Math.random()*560)), ((int)(Math.random()*560)) );
Person bob=new Person();
addObject (bob, ((int)(Math.random()*560)),((int)(Math.random()*560)));
Police cop=new Police();
addObject (cop, ((int)(Math.random()*560)),((int)(Math.random()*560)));
}
/*/**
* checks for pauses
*
private void checkForPause()
{
if (Greenfoot.isKeyDown("p"))
{
Greenfoot.delay(1);
}
}*/
/**
* Constructor for objects of class Level2.
*
*/
public void Level2()
{
populateWorld2();
}
/**
* Create the crab world (the beach). Our world has a size
* of 600x560 cells, where every cell is just 1 pixel.
* 12 worms, 2 lobsters, 1 person, 1 crab, 2 police, 2 circles to indicate 1 level
*/
public void populateWorld2()
{
Greenfoot.delay(10);
removeObjects(getObjects(null));
Level one =new Level();
addObject(one, 580,15);
Level two =new Level();
addObject(two, 550,15);
Crab stanley=new Crab();
addObject (stanley, ((int)(Math.random()*560)), ((int)(Math.random()*560)) );
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
Lobster tim=new Lobster();
addObject (tim, ((int)(Math.random()*560)), ((int)(Math.random()*560)) );
Lobster joe=new Lobster();
addObject (joe, ((int)(Math.random()*560)), ((int)(Math.random()*560)) );
Person bob=new Person();
addObject (bob, ((int)(Math.random()*560)),((int)(Math.random()*560)));
Police cop=new Police();
addObject (cop, ((int)(Math.random()*560)),((int)(Math.random()*560)));
Police cop2=new Police();
addObject (cop2, ((int)(Math.random()*560)),((int)(Math.random()*560)));
Greenfoot.start();
}
}
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;
/**
* Animal. This is the base class for all animals. In addition to the standard Actor
* methods, it provides the ability to move and turn.
*
* Olivia Higa
* 09/22/2014
*/
public class Animal extends Actor
{
private GreenfootImage gameover;
private GreenfootImage background2;
private int wormCount=0;
private int crabCount=0;
private int lobsterCount=0;
private int personCount=0;
private int level;
private static final double WALKING_SPEED = 5.0;
/**
* Constructor for Animal
*/
public Animal()
{
gameover= new GreenfootImage ("gameover.jpg");
background2= new GreenfootImage ("space1.jpg");
level=1;
}
/**
* Act - empty method. Animals have no default action.
*/
public void act()
{
}
/**
* Turn 'angle' degrees towards the right (clockwise).
*/
public void turn(int angle)
{
setRotation(getRotation() + angle);
}
/**
* Move forward in the current direction.
*/
public void move()
{
double angle = Math.toRadians( getRotation() );
int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
setLocation(x, y);
}
/**
* Test if we are close to one of the edges of the world. Return true is we are.
*/
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
}
/**
* Return true if we can see an object of class 'clss' right where we are.
* False if there is no such object here.
*/
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
/**
* Try to eat an object of class 'clss'. This is only successful if there
* is such an object where we currently are. Otherwise this method does
* nothing.
*/
public void eat(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
if(actor != null) {
getWorld().removeObject(actor);
}
}
/**
* If the crab reaches the edge of the world, turn 17 degrees.
*/
public void turnAtEdge ()
{
if (atWorldEdge())
{
turn(17);
}
}
/**
* Method for the lobster and police car that randomly chases the crab or person
*/
public void randomTurn()
{
if ((Math.random()*100)<10)
{
turn((int)(Math.random()*45));
}
}
/**
* Check whether up,down, or space key has been pressed by Player1.
* If it has, react accordingly
*/
public void checkKeypressPlayer1()
{
if (Greenfoot.isKeyDown("left"))
{
turn(-4);
}
if (Greenfoot.isKeyDown("right"))
{
turn(4);
}
if (Greenfoot.isKeyDown("space"))
{
move(4);
}
/* if (Greenfoot.isKeyDown("down"))
{
turn(180);
}
*/
}
/**
* Check whether a,d,or s key has been pressed by Player2.
* If it has, react accordingly.
* a= left, d=right, s= move
*/
public void checkKeypressPlayer2()
{
if (Greenfoot.isKeyDown("a"))
{
turn(-4);
}
if (Greenfoot.isKeyDown("d"))
{
turn(4);
}
if (Greenfoot.isKeyDown("s"))
{
move(4);
}
}
/**
* Check whether Player1 (crab) has stumbled upon a worm.
* If they have, eat it. If not, do nothing.
* wormCount increases by one, each time the crab eats it
*/
public void lookForWorm()
{
if (canSee(Worm.class))
{
eat(Worm.class);
Greenfoot.playSound ("slurp.wav");
wormCount++;
randomWorm();
checkForLevelUp();
endGame();
//Greenfoot.playSound("OUCH.wav");
}
}
/**
* adds a worm at a random times based on the wormCount
*/
public void randomWorm()
{
if (((int)(Math.random()*8))==wormCount)
{
getWorld().addObject (new Worm(),((int)(Math.random()*560)), ((int)(Math.random()*560)));
}
}
/**
* Check whether the lobster has stumbled upon a crab.
* If it has, eat it. If not, do nothing.
* the crabCount increases by one, each time a lobster eats it.
*/
public void lookForCrab()
{
if (canSee(Crab.class))
{
eat(Crab.class);
Greenfoot.playSound ("au.wav");
crabCount++;
checkForLevelUp();
endGame();
}
}
/**
* Check whether Player2 (Person) has stumbled upon a lobster.
* If they have, eat it. If not, do nothing.
* the lobsterCount increases by one, each time person2 eats it.
*/
public void lookForLobster()
{
if (canSee(Lobster.class))
{
eat(Lobster.class);
Greenfoot.playSound ("yummy.wav");
lobsterCount++;
checkForLevelUp();
endGame();
}
}
/**
* Check whether police has stumbled upon a person.
* If it has, "eat" it. If not, do nothing.
* the personCount increases by one, each time a police "eats" it.
*/
public void lookForPerson()
{
if (canSee(Person.class))
{
eat(Person.class);
Greenfoot.playSound ("SIREN2.WAV");
personCount++;
checkForLevelUp();
endGame();
}
}
/**
* Moves on to level 2 if all 8 worms are eaten by Player1, or if 1 lobster is caught by Player2.
* plays fanfare and changes background to level 2 setting
* Finishes game after level 2, if all 12 worms are eaten or if both the lobsters are caught.
* plays fanfare and ends game
*/
public void checkForLevelUp()
{
if (wormCount==12)
{
level++;
Greenfoot.delay(30);
Greenfoot.playSound ("chipquest.wav");
System.out.println ("Congratulations Player 1! You won!");
Greenfoot.stop();
}
else if (wormCount==88&level==1)
{
level++;
Greenfoot.delay(30);
Greenfoot.playSound ("chipquest.wav");
System.out.println ("Congratulations Player 1! You won!");
System.out.println ("Initiating Level "+level);
levelUp ();
Greenfoot.delay(100);
}
else if (lobsterCount==28&level==1 )
{
level++;
Greenfoot.delay(30);
Greenfoot.playSound ("chipquest.wav");
System.out.println ("Congratulations Player 1! You won!");
Greenfoot.stop();
}
else if(lobsterCount==1)
{
level++;
Greenfoot.delay(30);
Greenfoot.playSound ("chipquest.wav");
System.out.println ("Congratulation Player 2! You won!");
System.out.println ("Initiating Level "+level);
levelUp();
Greenfoot.delay(100);
}
}
/**
* End game if lobster eats crab, or if police car catched the person.
* Game over sign shows up
*/
public void endGame()
{
if (crabCount==1)
{
Greenfoot.playSound("gameover.wav");
System.out.println ("Sorry Player 1! Game over, you lose!");
Greenfoot.delay(30);
getWorld().setBackground(gameover);
getWorld().removeObjects(getWorld().getObjects(null));
Greenfoot.stop();
}
else if (personCount==1)
{
Greenfoot.playSound("gameover.wav");
System.out.println ("Sorry Player 2! Game over, you lose!");
Greenfoot.delay(30);
getWorld().setBackground(gameover);
getWorld().removeObjects(getWorld().getObjects(null));
Greenfoot.stop();
}
}
/**
* Initiate Level 2, if wormCount=8 or if lobstercount=1
*/
public void levelUp()
{
if (level==2)
{
getWorld().setBackground(background2);
((CrabWorld)getWorld()).Level2();
}
}
}

