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

2014/1/19

How do i get my scoreboard to show the correct score in asteroids.

xnefoo xnefoo

2014/1/19

#
Hi. I am having troubles getting my scoreboard at the end of my game to display the correct score. The scoreboard shows up when I die. My Rocket Class.
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
 
/**
 * A rocket that can be controlled by the arrowkeys: up, left, right.
 * The gun is fired by hitting the 'space' key. 'z' releases a proton wave.
 *
 * @author Poul Henriksen
 * @author Michael Kolling
 *
 * @version 1.0
 */
public class Rocket extends SmoothMover
{
    private static final int gunReloadTime = 5;         // The minimum delay between firing the gun.
    private static final int protonReloadTime = 550;
     
    private int reloadDelayCount;               // How long ago we fired the gun the last time.
    private int protonDelayCount;
     
    private GreenfootImage rocket = new GreenfootImage("rocket.png");   
    private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
    private boolean touched = false;
 
    /**
     * Initilise this rocket.
     */
    public Rocket()
    {
        reloadDelayCount = 5;
        protonDelayCount = 550;
        addForce (new Vector(13, 0.3));
    }
 
    /**
     * Do what a rocket's gotta do. (Which is: mostly flying about, and turning,
     * accelerating and shooting when the right keys are pressed.)
     */
    public void act()
    {
        checkKeys();
        move();
        reloadDelayCount++;
        checkCollision();
        protonDelayCount++;
    }
 
    /**
     * Check if we colide with a asteroid.
     */
    public void checkCollision()
    {
        Actor a = getOneIntersectingObject(Asteroid.class);
        if (a != null)
        {
            Space space = (Space) getWorld();
            space.addObject(new Explosion(), getX(), getY());
            space.removeObject(this);
            space.gameOver();
        }
    }
 
    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys()
    {
        ignite(Greenfoot.isKeyDown("up"));
 
        if (Greenfoot.isKeyDown("space"))
        {
            fire();
        }
 
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation(getRotation() -5);
        }
 
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(getRotation() +5);
        }
         
        if (Greenfoot.isKeyDown("z"))
        {
            startProtonWave();
        }
 
    }
 
    /**
     * Fire a bullet if the gun is ready.
     */
    private void fire()
    {
        if (reloadDelayCount >= gunReloadTime)
        {
            Bullet bullet = new Bullet (getMovement().copy(), getRotation());
            getWorld().addObject (bullet, getX(), getY());
            bullet.move ();
            reloadDelayCount = 0;
        }
    }
     
    /**
     * Fire proton wave if available
     */
    private void startProtonWave()
    {
        if  (protonDelayCount >= protonReloadTime)
        {
            ProtonWave wave = new ProtonWave();
            getWorld().addObject (wave, getX(), getY());
            protonDelayCount = 0;
        }
     
    }
 
    private void ignite(boolean boosterOn)
    {
        if(boosterOn)
        {
            setImage (rocketWithThrust);
            addForce (new Vector (getRotation(), 0.3));
        }
        else
        {
            setImage(rocket);
        }
 
    }
 
}
My Space class.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
import java.awt.Color;
 
/**
 * 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;
 
    public Space()
    {
        super(600, 400, 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);
        }
    }
 
 
    /**
     * Create Stars
     */
    public 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);
        }
    }
 
    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver()
    {
        addObject(new ScoreBoard(), getWidth()/2, getHeight()/2);
    }
 
    public Counter getCounter()
    {
 
        return scoreCounter;
    }
}
Thanks!
danpost danpost

2014/1/19

#
You are showing all your acquired classes except the one that is for the object you need help with.
xnefoo xnefoo

2014/1/19

#
Ooops. My scoreboard class.
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, 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 = 24.0f;
    public static final int WIDTH = 200;
    public static final int HEIGHT = 150;
     
    /**
     * 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(125,125,125, 64));
        image.fillRect(0, 0, WIDTH, HEIGHT);
        image.setColor(new Color(0, 0, 0, 64));
        image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
        Font font = image.getFont();
        font = font.deriveFont(FONT_SIZE);
        image.setFont(font);
        image.setColor(Color.ORANGE);
        image.drawString(title, 30, 50);
        image.drawString(prefix + score, 30, 100    );
        setImage(image);
    }
}
danpost danpost

2014/1/19

#
xnefoo wrote...
Ooops. My scoreboard class.
No. You mean mik's (Michael Kolling) ScoreBoard class. Look at line 30. You should be able to do something with that.
xnefoo xnefoo

2014/1/20

#
Sorry. But i still don't see what's wrong. Could it possible be because of line 24 because the score ends up at 100 no matter what at the end of the game.
danpost danpost

2014/1/20

#
xnefoo wrote...
Sorry. But i still don't see what's wrong. Could it possible be because of line 24 because the score ends up at 100 no matter what at the end of the game.
In part, yes. Look at what it says in the comment above that constructor (the code where line 24 resides). Now, look at what it says above line 30.
You need to login to post a reply.