This site requires JavaScript, please enable it in your browser!
SportingLife7 wrote ...

2021/1/9

Little Crab Scenario

SportingLife7

2021/1/9

```CRAB WORLD
import greenfoot.*;  // (Actor, World, Greenfoot, GreenfootImage)

public class CrabWorld extends World
{
private int i;
/**
* Create the crab world (the beach). Our world has a size
* of 560x560 cells, where every cell is just 1 pixel.
*/
public CrabWorld()
{
super(560, 560, 1);
populateWorld();
}
public void populateWorld()
{
int i = 0;
addObject( new Crab(), 150,100);
addObject( new Elephant(), 150,300);

addObject( new Lobster(), 400,100);
addObject( new Lobster(), 400,300);
addObject( new Lobster(), 400,500);
addObject( new Worm(), 300,50);
for (i=0; i<9; i++)
{
addObject (new Worm(), Greenfoot.getRandomNumber (561), Greenfoot.getRandomNumber (561));
}

}
}
```
``` ANIMAL
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.
*
* @author Michael Kolling
* @version 1.0
*/
public class Animal extends Actor
{
private static final double WALKING_SPEED = 5.0;

/**
* Constructor for Animal - nothing to do.
*/
public Animal()
{
}

/**
* 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);
}
}
}```
```CRAB CLASS
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
* This class defines a crab. Crabs live on the beach.
*/
public class Crab extends Animal
{
private GreenfootImage image1;
private GreenfootImage image2;
private int wormsEaten;
/**
* Create a crab and initialize both images
*/
public Crab()
{
image1 = new GreenfootImage("crab.png");
image2 = new GreenfootImage("crab2.png");
setImage(image1);
wormsEaten=0;
}
public void act()
{
move();
lookForWorm();
lookForElephant();
checkKeyPress();
switchImage();
}
/**
* Switch the images of the Crab to make it appear as if the Crab is moving it's legs.
* If the current image is image1 switch it to image2 and vice versa.
*/
public void switchImage()
{
if(getImage()==image1)
{
setImage(image2);
}
else
{
setImage (image1);
}
}
/**
* check whether we have stumbled upon a worm.
* If we have, eat it. If not, do nothing.
*/

public void lookForWorm()
{
if ( canSee(Worm.class) )
{
eat(Worm.class);
Greenfoot.playSound("slurp.wav");
wormsEaten=wormsEaten+1;
if(wormsEaten==8)
{
Greenfoot.playSound ("fanfare.wav");
Greenfoot.stop();
}
}
}
public void lookForElephant()
{
if ( canSee(Elephant.class) )
{
eat(Elephant.class);
Greenfoot.playSound("Elephant Sound Effect - Trumpet.wav");
Greenfoot.stop();
}
}
/**
* if the crab runs into the edge of the world, he turns 17 degrees -
* need this because if not he will stay there forever asking what he's
* doing with his life.
*/

public void turnAtEdge()
{
if ( atWorldEdge() )
{
turn(17);
}
}
/**
* causes the crab to turn based off of key presses
*/
public void checkKeyPress()
{
if (Greenfoot.isKeyDown("left"))
{
turn(-5);
}
if (Greenfoot.isKeyDown("right"))
{
turn(5);
}
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-2);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+2);
}
}
}```
```ELEPHANT CLASS
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
* This class defines an elephant. elephants vacation on the beach.
*/
public class Elephant extends Animal
{
private GreenfootImage image1;
private GreenfootImage image2;
private int wormsEaten;
/**
* Create an Elephant and initialize both images
*/
public Elephant()
{
image1 = new GreenfootImage("elephant.png");
image2 = new GreenfootImage("elephant2.png");
setImage(image1);
wormsEaten=0;
}
public void act()
{
move();
lookForWorm();
lookForCrab();
checkKeyPress();
switchImage();
}
/**
* Switch the images of the Elephant to make it appear as if the Elephant is moving it's legs.
* If the current image is image1 switch it to image2 and vice versa.
*/
public void switchImage()
{
if(getImage()==image1)
{
setImage(image2);
}
else
{
setImage (image1);
}
}
/**
* check whether we have stumbled upon a worm.
* If we have, eat it. If not, do nothing.
*/

public void lookForWorm()
{
if ( canSee(Worm.class) )
{
eat(Worm.class);
Greenfoot.playSound("slurp.wav");
wormsEaten=wormsEaten+1;
if(wormsEaten==8)
{
Greenfoot.playSound ("fanfare.wav");
Greenfoot.stop();
}
}
}
public void lookForCrab()
{
if ( canSee(Crab.class) )
{
eat(Crab.class);
Greenfoot.playSound("Hmm Yummy.wav");
Greenfoot.stop();
}
}
/**
* if the elephant runs into the edge of the world, he turns 17 degrees -
* need this because if not he will stay there forever asking what he's
* doing with his life.
*/

public void turnAtEdge()
{
if ( atWorldEdge() )
{
turn(17);
}
}
/**
* causes the elephant to turn based off of key presses
*/
public void checkKeyPress()
{
if (Greenfoot.isKeyDown("a"))
{
turn(-5);
}
if (Greenfoot.isKeyDown("d"))
{
turn(5);
}
if (Greenfoot.isKeyDown("w"))
{
setLocation(getX(), getY()-2);
}
if (Greenfoot.isKeyDown("s"))
{
setLocation(getX(), getY()+2);
}
}
}```
```LOBSTER CLASS
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Lobster here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lobster extends Animal
{
private GreenfootImage image1;
private GreenfootImage image2;
/**
* create a lobster and initialize both images
*/
public Lobster()
{
image1 = new GreenfootImage("lobster.png");
image2 = new GreenfootImage("lobster2.png");
setImage(image1);
}
/**
* Act - do whatever the Lobster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
randomTurn();
turnAtEdge();
move();
lookForCrab();
lookForElephant();
switchImage();
}
/**
* check whether we have stumbled upon a crab.
* If we have, eat it. If not, do nothing.
*/

public void lookForCrab()
{
if ( canSee(Crab.class) )
{
eat(Crab.class);
Greenfoot.playSound("au.wav");
Greenfoot.stop();
}
}
public void lookForElephant()
{
if ( canSee(Elephant.class) )
{
eat(Elephant.class);
Greenfoot.playSound("au.wav");
Greenfoot.stop();
}
}
/**
* Switch the image of the lobster to make it appear as if the lobster is moving.
* If the current image is image1 switch it to image2 and vice versa.
*/
public void switchImage()
{
if(getImage()==image1)
{
setImage(image2);
}
else
{
setImage(image1);
}
}
/**
* causes the lobster to randomly turn so that it does not walk straight
*/

public void randomTurn()
{
if (Greenfoot.getRandomNumber(100) < 10 )
{
turn( Greenfoot.getRandomNumber(90)-45);
}
}
/**
* if the lobster runs into the edge of the world, he turns 17 degrees.
*/

public void turnAtEdge()
{
if ( atWorldEdge() )
{
turn(17);
}
}
}```
```WORM CLASS
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Worm here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Worm extends Animal
{
/**
* Act - do whatever the Worm wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
}```
SportingLife7

2021/1/9

I'm trying to code so that: Every time that a worm gets eaten by either crab or elephant then a new worm takes it place in a random location. Their can only be a max of 10 worms in the world. I want this to keep happening until the Greenfoot.stop code takes place. I know I have to use a while loop and a variable but I don't know how, what, or where.
danpost

2021/1/9

SportingLife7 wrote...
Every time that a worm gets eaten by either crab or elephant then a new worm takes it place in a random location. Their can only be a max of 10 worms in the world. I want this to keep happening until the Greenfoot.stop code takes place. I know I have to use a while loop and a variable but I don't know how, what, or where.
Incorrect ... not a while loop nor a variable. HOW: if worm count is less than 10, spawn random worm; WHERE: CrabWorld act method;
SportingLife7

2021/1/9

Could you please write it in code for me, i'd really appreciate it.
danpost

2021/1/9

SportingLife7 wrote...
Could you please write it in code for me, i'd really appreciate it.
The HOW part pretty much says it all. Worm count would be:
`int wormCount = getObjects(Worm.class).size();`
SportingLife7

2021/1/10

Thank you so much, I really appreciate it (it worked!).
You need to login to post a reply.