Hello, I am programming the asteroids game from chapter 7, and cannot for the life of me get my score board to count at all. It shows up, but the score does not increase. Here is what I have in my Score board, Counter, Space and Bullet class. I have tried many different discussions but none have solved my problem. Please respond immediately.
Counter:
---------------------------------------------------------------------------------------
Scoreboard:
-----------------------------------------------------------------------------------
Space:
----------------------------------------------------------------------------------------------
Bullet:
-----------------------------------------------------------------------------------------------------
If you find what is wrong please respond immediately. Time is of the essence.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Graphics; /** * Counter that displays a text and number. * * @author Michael Kolling * @version 1.0.1 */ public class Counter extends Actor { private static final Color textColor = new Color(255, 180, 150); private int value = 0; private int target = 0; private String text; private int stringLength; public Counter() { this(""); } public Counter(String prefix) { text = prefix; stringLength = (text.length() + 2) * 10; setImage(new GreenfootImage(stringLength, 16)); GreenfootImage image = getImage(); image.setColor(textColor); updateImage(); } public void act() { if(value < target) { value++; updateImage(); } else if(value > target) { value--; updateImage(); } } public void add(int score) { target += score; } public int getValue() { return value; } /** * Make the image */ private void updateImage() { GreenfootImage image = getImage(); image.clear(); image.drawString(text + value, 1, 12); } }
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Font; import java.util.Calendar; /** * The ScoreBoard is used to display results on the screen. It can display some * text and several numbers. * * @author M Kolling * @version 1.0 */ 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); } }
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.util.List; /** * Space. Something for rockets to fly in. * * @author Michael Kolling * @version 1.0 */ public class Space extends World { private Counter scoreCounter; private int startAsteroids = 3; List<Asteroid> asteroids = this.getObjects(Asteroid.class); public Space() { super(1200, 800, 1); GreenfootImage background = getBackground(); background.setColor(Color.BLACK); background.fill(); createStars(300); Rocket rocket = new Rocket(); addObject(rocket, getWidth()/2 + 100, getHeight()/2); addAsteroids(startAsteroids); scoreCounter = new Counter("Score: "); addObject(scoreCounter, 60, 380); Explosion.initializeImages(); ProtonWave.initializeImages(); } /** * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ private void addAsteroids(int count) { for(int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); } } private void createStars(int number) { GreenfootImage background = getBackground(); for(int i=0; i < number; i++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); int color = 120 - Greenfoot.getRandomNumber(100); background.setColor(new Color(color,color,color)); background.fillOval(x, y, 2, 2); } } public void countScore() { scoreCounter.add(25); } public void gameOver() { ScoreBoard board = new ScoreBoard(); addObject(board,getWidth()/2, getHeight()/2); } }
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A bullet that can hit asteroids. * * @author Poul Henriksen */ 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); } } }