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

2016/8/22

Trying to make the Shark chase the Crab & You Win Sign

SkyCodes99 SkyCodes99

2016/8/22

#
Hello, I would like to get the shark character in my game to be smarter and actually chase the Crab. Also, I would like to have a "You Win" appear after the Crab has eaten all the Worm Charcters in the game.
import greenfoot.*;  // (Actor, World, Greenfoot, GreenfootImage)

public class CrabWorld extends World
{
    private int wormsEaten;
    /**
     * Create the crab world (the beach). Our world has a size 
     * of 700x700 cells, where every cell is just 1 pixel.
     */
    public CrabWorld() 
    {
        super(750, 750, 1);
        setPaintOrder(Crab.class, Worm.class, Shark.class);
        populate();
        randomWorms(5);
    }

public void populate()
{
    
    addObject (new Crab (), 200, 320);
    addObject (new Shark (), 650, 650); 
}

 public void randomWorms(int howMany)
    {
        for(int i=0; i<howMany; i++) {
            Worm worm = new Worm();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(worm, x, y);
        }
    }
    }

Shark Code:

import greenfoot.*;

/**
 * Write a description of class Shark here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shark extends Actor
{
     private int crabsEaten;
    /**
     * Act - do whatever the Shark wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-2);
        Actor crab = (Actor)getWorld().getObjects(Crab.class).get(0);
        turnTowards(Crab.getX(),Crab.getY());
           
                if(foundWorm()) {
            eatWorm();
        }
    }
    public boolean atWorldEdge()
    {
       Actor shark = getOneObjectAtOffset(0, 0, CrabWorld.class);
        if(CrabWorld.class != null) {
            return true;
        }
        else {
            return false;
        }
    }
    public boolean foundWorm()
    {
        Actor worm = getOneObjectAtOffset(0, 0, Crab.class);
        if(worm != null) {
            return true;
        }
        else {
            return false;
        }
    }
public void eatWorm()
    {
        Actor worm = getOneObjectAtOffset(0, 0, Crab.class);
        if(worm != null) {
            // eat the leaf...
            getWorld().removeObject(worm);
            crabsEaten = crabsEaten + 1; 
        }
    }
    public int getcrabsEaten()
    {
        return crabsEaten;
    }
}

Crab Code:

import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * This class defines a crab. Crabs live on the beach. */ public class Crab extends Animal { private int wormsEaten; public Crab() { wormsEaten = 10; } public void act() { if (Greenfoot.isKeyDown("a")) { turn(-3); } if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("d")) { turn (3); } if (Greenfoot.isKeyDown("right")) { turn(3); } if (Greenfoot.isKeyDown("w")) { move (3); } if (Greenfoot.isKeyDown("up")) { move (3); } if (Greenfoot.isKeyDown("s")) { move (-3); } if (Greenfoot.isKeyDown("down")) { move (-3); } if(foundWorm()) { eatWorm(); } } public boolean foundWorm() { Actor worm = getOneObjectAtOffset(0, 0, Worm.class); if(worm != null) { return true; } else { return false; } } public void eatWorm() { Actor worm = getOneObjectAtOffset(0, 0, Worm.class); if(worm != null) { // eat the leaf... getWorld().removeObject(worm); wormsEaten = wormsEaten + 1; } } public int getwormsEaten() { return wormsEaten; } }
danpost danpost

2016/8/22

#
SkyCodes99 wrote...
I would like to get the shark character in my game to be smarter and actually chase the Crab.
What have you tried as far a this? If nothing, what information do you think the shark would need to accomplish this? and, what steps do you think would be involved?
Also, I would like to have a "You Win" appear after the Crab has eaten all the Worm Charcters in the game.
If the Crab has eaten all the worms, then no worms would be left in the world. Add an act method in your CrabWorld class. Have it check to see if no worms are in the world, and if so, show the "You Win" message and stop the scenario.
You need to login to post a reply.