but im wondering what to put in between the brackets. basically what i want is for one red worm to appear in a random place on screen.
What you had before looks fine. Why do you think it's wrong?
if (counter.getValue() == 20)
{
getWorld().addObject(new RedWorm(), Greenfoot.getRandomNumber(1000), Greenfoot.getRandomNumber(800));
} import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Crab here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Crab extends Animals
{
/**
* Act - do whatever the Crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private Counter counter;
public Crab(Counter pointCounter)
{
counter = pointCounter;
}
public void act()
{
moveAndTurn();
eatWorm();
}
private void moveAndTurn()
{
if (Greenfoot.isKeyDown ("space"))
move (4);
if (Greenfoot.isKeyDown ("right"))
turn (3);
if (Greenfoot.isKeyDown ("left"))
turn (-3);
}
private void eatWorm()
{
Actor Worm;
Worm = getOneObjectAtOffset (0,0,Worm.class);
if(Worm != null)
{
int x = Greenfoot.getRandomNumber(getWorld().getWidth());
int y = Greenfoot.getRandomNumber(getWorld().getHeight());
Worm.setLocation(x, y);
counter.add(1);
Greenfoot.playSound ("eating.wav");
}
Actor RedWorm;
RedWorm = getOneObjectAtOffset (0,0,RedWorm.class);
if (RedWorm != null)
{
World world;
world = getWorld ();
world.removeObject (RedWorm);
counter.add(5);
Greenfoot.playSound ("eating.wav");
}
if (counter.getValue() == 20)
{
getWorld().addObject(new RedWorm(), Greenfoot.getRandomNumber(1000), Greenfoot.getRandomNumber(800));
}
if (counter.getValue () == 50)
{
Greenfoot.stop();
Greenfoot.playSound ("fanfare.wav");
getWorld().addObject(new GameWon(), getWorld().getWidth()/2, getWorld().getHeight()/2);
}
}
}
if (counter.getValue()>=20)
private void eatWorm()
{
int addScore = 0; // to track any scoring
Actor worm = getOneObjectAtOffset (0,0,Worm.class);
if(worm != null)
{
int x = Greenfoot.getRandomNumber(getWorld().getWidth());
int y = Greenfoot.getRandomNumber(getWorld().getHeight());
worm.setLocation(x, y);
Greenfoot.playSound ("eating.wav");
addScore += 1;
}
worm = getOneObjectAtOffset (0,0,RedWorm.class);
if (worm != null)
{
getWorld().removeObject (RedWorm);
Greenfoot.playSound ("eating.wav");
addScore += 5;
}
if (addScore == 0) return; // exit method if no score
int previousScore = counter.getValue();
counter.add(addScore);
if (previousScore+addScore >= 20)
{
int x = Greenfoot.getRandomNumber(1000);
int y = Greenfoot.getRandomNumber(800);
getWorld().addObject(new RedWorm(), x , y);
}
if (previousScore+addScore >= 50)
{
Greenfoot.stop();
Greenfoot.playSound ("fanfare.wav");
getWorld().addObject(new GameWon(), getWorld().getWidth()/2, getWorld().getHeight()/2);
}
}getWorld().removeObject (worm);