In my game, I need a scoring system but I'm not entirely sure how to create one. Could somebody please give me some advice. Thanks.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static final RED = 0 , BLUE = 1 ; // continue for more players private int [] scores = new int [ 2 ]; // size to number of players public int getScore( int player) { return scores[player); } public void addScore( int player, int amount) { scores[player] += amount; } public int [] getScores() { return scores; } |
1 2 3 4 5 6 7 8 | // get a single score int redScore = ((MyWorld)getWorld()).getScore(MyWorld.RED); // add to a score ((MyWorld)getWorld()).addScore(MyWorld.BLUE, 10 ); // or get both scores int finalScores = ((MyWorld)getWorld()).getScores(); |
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 | private int score = 0 ; public int time = 0 ; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 1100 , 600 , 1 ); GreenfootImage back = getBackground(); back.setColor(Color.BLACK); back.fill(); prepare(); showScore(); score++; } public void act() { countTime(); } public void countTime() { time++; } public void addScore( int points) { score = score + points; showScore(); } public void showScore() { showText( "Ship 1: " + score, 75 , 25 ); } |
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 | public void Stuff() { boolean kill = true ; if (kill) if (isTouching(UFO. class )) { kill = false ; removeTouching(UFO. class ); MyWorld world = (MyWorld)getWorld(); getWorld().addObject( new Explosion(), getX(), getY()); getWorld().removeObject( this ); Greenfoot.playSound( "Explosion copy.wav" ); world.addScore( 50 ); } if (kill) if (isTouching(Arwing. class )) { kill = false ; removeTouching(Arwing. class ); MyWorld world = (MyWorld)getWorld(); getWorld().addObject( new Explosion(), getX(), getY()); getWorld().removeObject( this ); Greenfoot.playSound( "Explosion copy.wav" ); world.addScore(- 50 ); } if (kill) if (isTouching(UFO0. class )) { kill = false ; removeTouching(UFO0. class ); MyWorld world = (MyWorld)getWorld(); getWorld().addObject( new Explosion(), getX(), getY()); getWorld().removeObject( this ); Greenfoot.playSound( "Explosion copy.wav" ); world.addScore( 50 ); } if (kill) if (isTouching(Mario. class )) { kill = false ; removeTouching(Mario. class ); MyWorld world = (MyWorld)getWorld(); getWorld().addObject( new Explosion(), getX(), getY()); getWorld().removeObject( this ); world.addScore( 200 ); Greenfoot.playSound( "Mario 64 Falling Death Scream (320 kbps).mp3" ); } if (kill) if (isTouching(Mario2. class )) { kill = false ; removeTouching(Mario2. class ); MyWorld world = (MyWorld)getWorld(); getWorld().addObject( new Explosion(), getX(), getY()); getWorld().removeObject( this ); world.addScore( 200 ); Greenfoot.playSound( "Mario 64 Falling Death Scream (320 kbps).mp3" ); } if (kill) if (isTouching(Arwing1. class )) { kill = false ; removeTouching(Arwing1. class ); MyWorld world = (MyWorld)getWorld(); getWorld().addObject( new Explosion(), getX(), getY()); getWorld().removeObject( this ); Greenfoot.playSound( "Explosion copy.wav" ); world.addScore(- 50 ); } } |
1 2 3 4 5 6 7 8 9 10 11 | private int score; public int getScore() { return score; } public void addScore( int amt) { score += amt; } |
1 2 3 4 5 | // getting score int score = ((MyWorld)getWorld()).getScore(); // changing score ((MyWorld)getWorld()).addScore( 10 ); |
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 | public Bullet() { GreenfootImage g = new GreenfootImage( "beeper.png" ); g.scale( 50 , 50 ); setImage(g); } public void act() { int score = ((MyGame)getWorld()).getScore(); Actor t; t = getOneObjectAtOffset( 0 , 0 , Igloo. class ); setLocation(getX() + 50 , getY()); if (t!= null ) { ((MyGame)getWorld()).addScore( 1 ); World w = getWorld(); w.removeObject(t); getWorld().removeObject( this ); } else if (isAtEdge()) { getWorld().removeObject( this ); } } |