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

2020/3/4

Chapter 2 - Little Crab (Differences between the book and the scenarios!!!)

pazi2000 pazi2000

2020/3/4

#
Hi there, If I open chapter 2 the little crab scenario, there is no "Animal" subclass??? If i put the "move();" function into the crab class like its written on page 37, there is an error. So i cant use the book for school purposes... Any help for that massive problem?
Just_in Just_in

2020/3/4

#
Little crab: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * This class defines a crab. Crabs live on the beach. They like sand worms * (very yummy, especially the green ones). * * Version: 5 * * In this version, the crab behaves as before, but we add animation of the * image. */ public class Crab extends Actor { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; } /** * Act - do whatever the crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeypress(); move(5); lookForWorm(); switchImage(); } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ public void checkKeypress() { if (Greenfoot.isKeyDown("left")) { turn(-4); } if (Greenfoot.isKeyDown("right")) { turn(4); } } /** * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. If we have * eaten eight worms, we win. */ public void lookForWorm() { if ( isTouching(Worm.class) ) { removeTouching(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten == 8) { Greenfoot.playSound("fanfare.wav"); Greenfoot.stop(); } } } } Lobster: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A lobster. Lobsters live on the beach. They like to eat crabs. (Well, in our game * they do...) * * Version: 2 * * The lobster walks around randomly. If it runs into a crab it eats it. * In this version, we have added a sound effect, and the game stops when * a lobster eats the crab. */ public class Lobster extends Actor { /** * Do whatever lobsters do. */ public void act() { turnAtEdge(); randomTurn(); move(5); lookForCrab(); } /** * Check whether we are at the edge of the world. If we are, turn a bit. * If not, do nothing. */ public void turnAtEdge() { if ( isAtEdge() ) { turn(17); } } /** * Randomly decide to turn from the current direction, or not. If we turn * turn a bit left or right by a random degree. */ public void randomTurn() { if (Greenfoot.getRandomNumber(100) > 90) { turn(Greenfoot.getRandomNumber(90)-45); } } /** * Try to pinch a crab. That is: check whether we have stumbled upon a crab. * If we have, remove the crab from the game, and stop the program running. */ public void lookForCrab() { if ( isTouching(Crab.class) ) { removeTouching(Crab.class); Greenfoot.playSound("au.wav"); Greenfoot.stop(); } } } CrabWorld: import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage /** * The CrabWorld is the place where crabs and other creatures live. * It creates the initial population. */ 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); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Crab crab = new Crab(); addObject(crab, 231, 203); Worm worm = new Worm(); addObject(worm, 445, 137); Worm worm2 = new Worm(); addObject(worm2, 454, 369); Worm worm3 = new Worm(); addObject(worm3, 368, 466); Worm worm4 = new Worm(); addObject(worm4, 129, 488); Worm worm5 = new Worm(); addObject(worm5, 254, 388); Worm worm6 = new Worm(); addObject(worm6, 106, 334); Worm worm7 = new Worm(); addObject(worm7, 338, 112); Worm worm8 = new Worm(); addObject(worm8, 150, 94); Worm worm9 = new Worm(); addObject(worm9, 373, 240); Worm worm10 = new Worm(); addObject(worm10, 509, 55); Lobster lobster = new Lobster(); addObject(lobster, 334, 65); Lobster lobster2 = new Lobster(); addObject(lobster2, 481, 481); Lobster lobster3 = new Lobster(); addObject(lobster3, 79, 270); } } Hope I can help you
danpost danpost

2020/3/4

#
On page 37, I do not see:
move();
but I do see:
move(5);
You need to login to post a reply.