I have no clue on how to contine the counter score into different worlds.
here is my world code
here is my Counter code
and here is my player code
also here is my GameOverScreen
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class CrabWorld here. * * @author (your name) * @version (a version number or a date) */ public class Playerx1 extends World { public Counter counter; //save the counter in a field to get access from outside /** * Constructor for objects of class CrabWorld. * */ public Playerx1() { super ( 1000 , 660 , 1 ); prepare(); } public void act() { if (getObjects(Player_p1. class ).isEmpty()){ Greenfoot.setWorld( new GameOverScreen()); } if (getObjects(Peasant. class ).isEmpty()){ Greenfoot.setWorld( new level_2()); } } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { //when creating counter = new Counter(); addObject(counter, 54 , 624 ); Player_p1 player_p1 = new Player_p1(counter); addObject(player_p1, 240 , 304 ); |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * A simple counter with graphical representation as an actor on screen. * * @author mik * @version 1.0 */ public class Counter extends Actor { private static final Color transparent = new Color( 0 , 0 , 0 , 0 ); private GreenfootImage background; private int value; private int target; private int score = 0 ; //declare 'score' as private public int getScore() //to be able to access this field from outside this class { return score; } /** * Create a new counter, initialised to 0. */ public Counter() { background = getImage(); // get image from class value = 0 ; target = 0 ; updateImage(); } /** * Animate the display to count up (or down) to the current target value. */ public void act() { if (value < target) { value++; updateImage(); } else if (value > target) { value--; updateImage(); } } /** * Add a new score to the current counter value. */ public void add( int score) { target += score; } /** * Return the current counter value. */ public int getValue() { return value; } /** * Set a new counter value. */ public void setValue( int newValue) { target = newValue; value = newValue; updateImage(); } /** * Update the image on screen to show the current value. */ private void updateImage() { GreenfootImage image = new GreenfootImage(background); GreenfootImage text = new GreenfootImage( "Score : " + value, 22 , Color.GREEN, transparent); image.drawImage(text, (image.getWidth()-text.getWidth())/ 2 , (image.getHeight()-text.getHeight())/ 2 ); setImage(image); } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Player_p1 extends Actor { private Counter counter; public Player_p1(Counter pointCounter) { counter = pointCounter; } public void act() { moveAndTurn(); eat(); } public void moveAndTurn() { if (Greenfoot.isKeyDown( "a" )) { turn(- 7 ); } if (Greenfoot.isKeyDown( "d" )) { turn( 7 ); } if (Greenfoot.isKeyDown( "w" )) { move( 7 ); } if (Greenfoot.isKeyDown( "s" )) { move(- 7 ); } } public void eat() { Actor peasant; peasant = getOneObjectAtOffset( 0 , 0 , Peasant. class ); if (peasant != null ) { World world; world = getWorld(); world.removeObject(peasant); counter.add( 1 ); Greenfoot.playSound( "eating.wav" ); } } } |
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 | import greenfoot.*; /** * Write a description of class GameOverScreen here. * * @author (your name) * @version (a version number or a date) */ public class GameOverScreen extends World { /** * Constructor for objects of class GameOverScreen. * */ public GameOverScreen() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 1000 , 624 , 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() { GameOverText gameovertext = new GameOverText(); addObject(gameovertext, 309 , 175 ); gameovertext.setLocation( 300 , 176 ); gameovertext.setLocation( 490 , 197 ); Back_button back_button = new Back_button(); addObject(back_button, 494 , 456 ); back_button.setLocation( 479 , 451 ); } } |