This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mace55555
Mace55555 wrote ...

2013/11/1

Can't get scoreboard working!!

Mace55555 Mace55555

2013/11/1

#
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:
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);  
    }  
}  
--------------------------------------------------------------------------------------- Scoreboard:
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);
    }
}
----------------------------------------------------------------------------------- Space:
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);  
    }

}
---------------------------------------------------------------------------------------------- Bullet:
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);
        }
    }
}
----------------------------------------------------------------------------------------------------- If you find what is wrong please respond immediately. Time is of the essence.
erdelf erdelf

2013/11/1

#
well, ur error seems, that u are calling the blank constructor of the scoreboard in line 69 of ur space class, change it to
ScoreBoard board = new ScoreBoard(scoreCounter.getValue());
Mace55555 Mace55555

2013/11/1

#
That did not appear to be the problem. Anything else?
erdelf erdelf

2013/11/1

#
is the counter itself increasing`?
Mace55555 Mace55555

2013/11/1

#
No it is not. Both the counter during the game and the scoreboard at the end do not increase.
erdelf erdelf

2013/11/1

#
I think i know the problem, may I see ur asteroid class?
Mace55555 Mace55555

2013/11/1

#
Sure: Asteroid:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A rock in space
 * 
 * @author Poul Henriksen
 */
public class Asteroid extends SmoothMover
{
    /** Size of this asteroid */
    private int size;
    
    /** When the stability reaches 0 the asteroid will explode */
    private int stability;
    
    
    public Asteroid()
    {
        this(50);
    }
    
    public Asteroid(int size)
    {
        super(new Vector(Greenfoot.getRandomNumber(360), 2));
        setSize(size);
    }
    
    public Asteroid(int size, Vector speed)
    {
        super(speed);
        setSize(size);
    }
    
    public void act()
    {         
        move();
        intersecting();
    }

    /**
     * Set the size of this asteroid. Note that stability is directly
     * related to size. Smaller asteroids are less stable.
     */
    public void setSize(int size) 
    {
        stability = size;
        this.size = size;
        GreenfootImage image = getImage();
        image.scale(size, size);
    }

    /**
     * Return the current stability of this asteroid. (If it goes down to 
     * zero, it breaks up.)
     */
    public int getStability() 
    {
        return stability;
    }
    
    /**
     * Hit this asteroid dealing the given amount of damage.
     */
    public void hit(int damage) {
        stability = stability - damage;
        if(stability <= 0) 
            breakUp ();         
    }
    
    /**
     * Break up this asteroid. If we are still big enough, this will create two
     * smaller asteroids. If we are small already, just disappear.
     */
    private void breakUp() 
    {
        Greenfoot.playSound("Explosion.wav");
        
        if(size <= 16) 
        {
            getWorld().removeObject(this);
        }
        else 
        {
            int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getMovement().getLength();
            Vector speed1 = new Vector(r + 60, l * 1.2);
            Vector speed2 = new Vector(r - 60, l * 1.2);        
            Asteroid a1 = new Asteroid(size/2, speed1);
            Asteroid a2 = new Asteroid(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move();
            a2.move();
        
            getWorld().removeObject(this);
        }
    }
    
    public void intersecting() {
        Actor rocket = getOneIntersectingObject(Rocket.class);
        if(rocket != null) {
            Actor Explosion = new Explosion();
            getWorld().addObject(Explosion,getX(), getY());
            Actor ScoreBoard = new ScoreBoard();
            getWorld().addObject(ScoreBoard,(578),(406));
            Greenfoot.playSound("MetalExplosion.wav");
            getWorld().removeObject(rocket);
            getWorld().removeObject(this);
        }
    }
}
erdelf erdelf

2013/11/1

#
oh ok, I see ur problem, u are never really increasing the counter. In the asteroid class, in the method hit, u should add the method call to the countScore method of the space class and something else bothers me in ur code, in the intersecting method u are creating the scoreboard, but wasnt that the purpose of the gameover method in the space class`?
Mace55555 Mace55555

2013/11/1

#
OK I think I understand. But could you possibly put some code for me to put in? I'm not really sure on the specifics.
erdelf erdelf

2013/11/1

#
sure I see that i should rewrite the breakup instead of the hit method, so put this in the first line of the method
((Space)getWorld()).countScore();
Mace55555 Mace55555

2013/11/1

#
Sorry for being like this, but should I replace a certain line of code, or create a new method? It's close to midnight here and the stress is killing me, so my brain really isn't working so well. Explain it to me like someone who has never coded before.
erdelf erdelf

2013/11/1

#
five hours after that for me, but ok, same method, but dont replace a line, place it before line 76
Mace55555 Mace55555

2013/11/1

#
YES!!! So the Counter while playing goes up, but the scoreboard score stays at the score 100 no matter what. Anymore ideas?
erdelf erdelf

2013/11/1

#
sure, give me a sec... replace the lines 104 and 105 in the asteroid class with
((Space)getWorld()).gameOver();
Mace55555 Mace55555

2013/11/1

#
THANK YOU!!! IT WOOOOOOOOOORKS!!!!!!!!!!! YOU HAVE NO IDEA HOW HAPPY I AM RIGHT NOW!!!!! IT'S WAY TO LATE NOW, BUT I SWEAR TO GOD I WILL RETURN THE FAVOR ONE DAY!!!! THANK YOU!!!!!!!
You need to login to post a reply.