This site requires JavaScript, please enable it in your browser!
Greenfoot back
Jackermann
Jackermann wrote ...

2013/9/15

Worms reappear??

Jackermann Jackermann

2013/9/15

#
I'm in the crabs and worms scenario, and I'm trying to make it so my worms reappear after being eaten. I need it so the location, timing, and chances of a worm reappearing are completely random - I was thinking about 30% chance of them coming back... I already have the "addObject( new Worm(), (Greenfoot.getRandomNumber(560)), (Greenfoot.getRandomNumber(560)) );" to make them appear randomly, I just need to get the rest!
Jackermann Jackermann

2013/9/15

#
Also, the code for the "CrabWorld" is: import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorld extends World { /** * 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); } public void populateWorld() { addObject( new Crab(), (Greenfoot.getRandomNumber(560)), (Greenfoot.getRandomNumber(560)) ); addObject( new Crab1(), (Greenfoot.getRandomNumber(560)), (Greenfoot.getRandomNumber(560)) ); addObject( new Crab2(), (Greenfoot.getRandomNumber(560)), (Greenfoot.getRandomNumber(560)) ); } public void populateWorms() { int worm = 0; while(worm < 10){ addObject( new Worm(), (Greenfoot.getRandomNumber(560)), (Greenfoot.getRandomNumber(560)) ); worm = worm + 1; } } }
Jackermann Jackermann

2013/9/15

#
The code for the crab is currently: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; } public void act() { lookForWorm(); switchImage(); move(); randomTurn(); turnAtEdge(); } public void turnAtEdge() { if ( atWorldEdge() ) { turn(30); } } public void randomTurn() { if (Greenfoot.getRandomNumber(100) > 90) { turn(Greenfoot.getRandomNumber(90)-45); } } public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } public void lookForWorm() { if ( canSee(Worm.class) ) { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten == 20) { Greenfoot.playSound("fanfare.wav"); Greenfoot.stop(); } } } }
Gevater_Tod4711 Gevater_Tod4711

2013/9/15

#
You already have the code to add the worms at random places. To give them a chance to be readded of about 30% you can use
if (Greenfoot.getRandomNumber(100) < 30) {
    //readd the worm;
}
But I don't realy get what you mean by random timing. Do you want the respawned worm to appear after some time? Or do you want worms to appear randomly no matter whether they're eaten? However, you can use the code like this:
//in your world class;
public void readdWorm() {
    if (Greenfoot.getRandomNumber(100) < 30) {
        addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560));
    }
}
//in your crab world:
public void lookForWorm() {
    //...
    eat(Worm.class);
    ((CrabWorld) getWorld()).readdWorm();
    Greenfoot.playSound("slurp.wav");
    //...
}
You need to login to post a reply.