Hello, I need some help with my code. I am trying to make the Score Counter go up every time my fish eats a cherry. I am not sure if I am putting the code in the correct place or not. I would greatly appreciate an explanation if possible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Counter here. * * @author (your name) * @version (a version number or a date) */ public class Counter extends Actor { int score = 0 ; /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage( new GreenfootImage( "Score : " + score, 24 , Color.GREEN, Color.BLACK)); } public void addScore() { score++; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Fish1 here. * * @author (your name) * @version (a version number or a date) */ public class Fish extends Player { public int cherriesEaten; public Fish() { cherriesEaten = 0 ; } /** * Act - do whatever the Fish1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (foundCherry()) { eatCherry(); } if (Greenfoot.isKeyDown( "left" )) { setLocation(getX() - 10 , getY()); } if (Greenfoot.isKeyDown( "right" )) { setLocation(getX() + 5 , getY()); } if (Greenfoot.isKeyDown( "up" )) { setLocation(getX(), getY() - 5 ); } if (Greenfoot.isKeyDown( "down" )) { setLocation(getX(), getY() + 5 ); } } public boolean foundCherry() { Actor cherry = getOneObjectAtOffset( 0 , 0 , Cherry. class ); if (cherry != null ) { return true ; } else { return false ; } } public void eatCherry() { Actor cherry = getOneObjectAtOffset( 0 , 0 , Cherry. class ); if (cherry != null ) { World myWorld = getWorld(); getWorld().removeObject(cherry); Ocean ocean = (Ocean)myWorld; Counter counter = ocean.getCounter(); counter.addScore(); } } public int getCherriesEaten() { return cherriesEaten; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * An Ocean Experience * * @author Gabe Piper * @version 1.0.1 */ public class Ocean extends World { Counter counter = new Counter(); public Ocean() { super ( 600 , 400 , 1 , false ); } public Counter getCounter() { return counter; } public void act() { if (Greenfoot.getRandomNumber( 150 ) < 1 ) { addObject( new Seal(), getWidth(), Greenfoot.getRandomNumber(getHeight())); } if (Greenfoot.getRandomNumber( 100 ) < 1 ) { addObject( new Cherry(), getWidth(), Greenfoot.getRandomNumber(getHeight())); } } } |