I am working on a project where the rocket(player) can shoot bullets to damage asteroids in the world. The score is supposed to increase by 100 every time a bullet hits an asteroid but I am having trouble getting it to work. How do I make it so that whenever an asteroid gets hit, it adds 100 points to the score so when the game ends and the scoreboard is displayed, the current score from the game is displayed?
There are my codes
public class ScoreBoard extends Actor { public static final float FONT_SIZE = 48.0f; public static final int WIDTH = 400; public static final int HEIGHT = 300; /** * Create a score board with dummy result for testing. */ public ScoreBoard() { this(100); } /** * Create a score board for the final result. */ public ScoreBoard(int score) { makeImage("Game Over", "Score: ", score); } /** * Make the score board image. */ private void makeImage(String title, String prefix, int score) { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(255,255,255, 128)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 128)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); image.drawString(title, 60, 100); image.drawString(prefix + score, 60, 200); setImage(image); } }
public class Bullet extends SmoothMover { /** The damage this bullet will deal */ private static final int damage = 16; /** A bullet looses one life each act, and will disappear when life = 0 */ private int life = 30; public Bullet() { } public Bullet(Vector speed, int rotation) { super(speed); setRotation(rotation); addForce(new Vector(rotation, 15)); Greenfoot.playSound("EnergyGun.wav"); } /** * The bullet will damage asteroids if it hits them. */ public void act() { if(life <= 0) { getWorld().removeObject(this); } else { life--; move(); checkAsteroidHit(); } } /** * Check whether we have hit an asteroid. */ private void checkAsteroidHit() { Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class); if (asteroid != null) { getWorld().removeObject(this); asteroid.hit(damage); } } }