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

2014/5/9

Score counter not working.

1
2
Bntyhntr501 Bntyhntr501

2014/5/9

#
I need help on trying to get my score counter to work I have looked at many other forums for this and still nothing. I am trying to make it when I pick up a Goldasteroid the score will add up 5 and if I destroy an Alienship the score will add 10 points here is what I have: 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); } } 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); } } Redship: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Redship here. * * @author (your name) * @version (a version number or a date) */ public class Redship extends SmoothMover { public int score; private static final int gunReloadTime = 15; private int reloadDelayCount; public Redship() { reloadDelayCount = 5; //addForce(new Vector(13, 0.3)); } /** * Act - do whatever the Redship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { //move(3); checkKeyPress(); reloadDelayCount++; //score(); lookForGoldastroid(); }//end act /** * Checks for a pressed key, if this is true it will exeacute an action. */ public void checkKeyPress() { if(Greenfoot.isKeyDown("d"))//right { turn(4); }//end if if(Greenfoot.isKeyDown("a"))//left { turn(-4); }//end if if(Greenfoot.isKeyDown("s"))//slow down { move(-2); }//end if if(Greenfoot.isKeyDown("w"))//speed up { move(1); }//end if if(Greenfoot.isKeyDown("space")) { fire(); }//end if }//end checkKeyPress /** * Fires a laser if ready to fire. */ public void fire() { if(reloadDelayCount >= gunReloadTime) { Laser laser = new Laser (getMovement().copy(), getRotation()); getWorld().addObject (laser, getX(), getY()); laser.move (); reloadDelayCount = 0; }//end if }//end fire /** * Check to see if we are touching a Goldastroid. * If so then pick it up and add 50 points to score. */ public void lookForGoldastroid() { if (canSee(Goldastroid.class)) { pickUp(Goldastroid.class); score++; getWorld().addObject(new Goldastroid(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700)); }//end if }//end lookForGoldastroid }//end class Laser: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Laser here. * * @author (your name) * @version (a version number or a date) */ public class Laser extends SmoothMover { private static final int damage = 1; private int life = 90; /** * Laser will destroy Alienship if it hits them. */ public void act() { if(life <= 0) { getWorld().removeObject(this); } else { life--; move(10); checkAlienshipHit(); }//end if/else }//end hitAlienship public Laser(Vector speed, int rotation) { setRotation(rotation); addForce(new Vector(rotation, 15)); }//end Laser public void removeAtEdge() { if ( atWorldEdge() ) { getWorld().removeObject(this); } }//end removeAtEdge /** * Checks if laser has hit Alienship */ public void checkAlienshipHit() { Alienship alienship = (Alienship) getOneIntersectingObject(Alienship.class); if(alienship != null) { getWorld().removeObject(this);// remove laser alienship.hit(damage); }//end if }//end checkAlienshipHit public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getHeight() - 20) return true; else return false; }//end atWorldEdge }//end class Aienship: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Alienship here. * * @author (your name) * @version (a version number or a date) */ public class Alienship extends Aliens { private int health; private int stability; private int spawnTimer = 10; /** * Act - do whatever the Alienship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { turnAtEdge(); randomTurn(); //move(2); lookForRedship(); runSpawnTimer(); }//end act public void healthLevel() { health = 1; stability = health; this.health = health; }//end health private void runSpawnTimer() { spawnTimer = (spawnTimer+1)%1000; if(spawnTimer== 0) spawnAlienship(); }//end runSpawnTimer private void spawnAlienship() { getWorld().addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700)); } public void turnAtEdge() { if(atWorldEdge()) { turn(Greenfoot.getRandomNumber(10)); }//end if }//end turnAtEdge public void randomTurn() { if(Greenfoot.getRandomNumber(100) > 90) { turn(Greenfoot.getRandomNumber(90)-45); }//end if }//end randomTurn public void lookForRedship() { if(canSee(Redship.class)) { destroy(Redship.class); Greenfoot.playSound("Blast.wav"); Greenfoot.stop(); } } public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getHeight() - 20) return true; else return false; //end if/else }//end atWorldEdge public int getStability() { return stability; } public void hit(int damage) { stability = stability - damage; if(stability <= 0) destroy (); }//end hit public void destroy() { if(health <= 0) { ((SpaceWorld)getWorld()).countScore(); getWorld().addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700)); getWorld().addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700)); getWorld().removeObject(this); } }//end destory } Goldasteroid: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Goldastroid here. * * @author (your name) * @version (a version number or a date) */ public class Goldasteroid extends Space { /** * Act - do whatever the Goldasteroid wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { turn(3); }//end act }
danpost danpost

2014/5/9

#
I think you have shown every method you have except for SmoothMover (which is not necessary) and your SpaceWorld class (which, do you not think is necessary? it has the 'countScore' method in it plus shows the creation of the Counter). Please post the entire SpaceWorld class. This time, use the 'code' tag below the 'Post a reply' box to insert (copy/paste) your code into your post.
Bntyhntr501 Bntyhntr501

2014/5/9

#
Sorry for the late response, I was able to finally figure out the counter I am now having a problem with wanting to display the scoreboard at the end of the game if you die by an alien. I am able to make it show up on the screen but I am unable to change the score section to display what it is I got for a score. Also I am using the scoreboard image that was used in Greenfoot scenarios Chapter 7. This is the code I have. Alienship:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Alienship here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Alienship extends Aliens
{
    private int health;
    private int stability;
    private int spawnTimer = 10;
     
 
    /**
     * Act - do whatever the Alienship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        turnAtEdge();
        randomTurn();
        move(2);
        lookForRedship();
         
        runSpawnTimer();
         
    }//end act
     
    public void healthLevel()
    {
        health = 1;
        stability = health;
        this.health = health;
    }//end health
     
    private void runSpawnTimer()
    {
        spawnTimer = (spawnTimer+1)%400;
        if(spawnTimer == 0) spawnAlienship();
    }//end runSpawnTimer
     
    private void spawnAlienship()
    {
        getWorld().addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
    }//end spawnAlienship
     
     
    public void turnAtEdge()
    {
        if(atWorldEdge())
        {
            turn(Greenfoot.getRandomNumber(10));
        }//end if
    }//end turnAtEdge
     
    public void randomTurn()
    {
        if(Greenfoot.getRandomNumber(100) > 90)
        {
            turn(Greenfoot.getRandomNumber(90)-45);
        }//end if
    }//end randomTurn
     
     
     
    public void lookForRedship()
    {
        if(canSee(Redship.class))
        {
            destroy(Redship.class);
            getWorld().addObject(new ScoreBoard(), 400, 400);
            Greenfoot.playSound("Blast.wav");
            Greenfoot.stop();
        }//end if
    }//end lookForRedship
     
        public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
             
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
        //end if/else
    }//end atWorldEdge
     
    public int getStability()
    {
        return stability;
    }
     
    public void hit(int damage)
    {
        stability = stability - damage;
        if(stability <= 0)
            destroy ();        
    }//end hit
     
    public void destroy()
    {
        if(health <= 0)
       {
                   
         getWorld().removeObject(this);
         
       }//end if
 
    }//end destory
     
 
     
 
}//end class
ScoreBoard:
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
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);
    }
}
danpost danpost

2014/5/9

#
Line 73 of the Alienship class is creating the scoreboard with 'new ScoreBoard()'. You need to supply the final score to the new scoreboard object by putting the final score in the parenthesis of 'new ScoreBoard( /* here */ )'.
Bntyhntr501 Bntyhntr501

2014/5/9

#
Ok what is it that I should be putting in it, I can't seem to figure that out.
danpost danpost

2014/5/9

#
Bntyhntr501 wrote...
Ok what is it that I should be putting in it, I can't seem to figure that out.
I cannot say without seeing your SpaceWorld class code.
Bntyhntr501 Bntyhntr501

2014/5/9

#
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MetalWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class SpaceWorld extends World
{
     
     
     
    /**
     * Constructor for objects of class MetalWorld.
     *
     */
    public SpaceWorld()
    {   
         
        super(800, 800, 1);
         
        populateWorld();
         
         
    }//end SpaceWorld
     
     
    public void populateWorld()
    {
        Counter counter = new Counter("Score: ");
        addObject(counter, 50, 790);
         
         
         
         
        Redship redship = new Redship(counter);
        addObject(redship, 400, 400);
         
         
         
         
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        
         
        addObject(new Goldasteroid(),Greenfoot.getRandomNumber(650), Greenfoot.getRandomNumber(650));
         
 
 
         
    }//end populateWorld
     
     
 
     
 
 
 
}//end World
danpost danpost

2014/5/9

#
Bntyhntr501 wrote...
< Code Omitted >
That cannot be the entire class. Where are the other method(s), like the 'countScore' method (which I know you must have) and possibly others related to the score counter?
Bntyhntr501 Bntyhntr501

2014/5/9

#
On line 31 it is pretty much the method for the counter if that is what you are looking for but I didn't make a method for the countScore or anything like that.
danpost danpost

2014/5/9

#
Ok. There is a way to access the Counter object with the way you have it set up (as is), with this:
1
2
3
4
5
6
7
8
9
public Counter getCounter()
{
    Counter counter = null;
    if (!getObjects(Counter.class).isEmpty())
    {
        counter = (Counter)getObjects(Counter.class).get(0);
    }
    return counter;
}
Bear in mind that a method like this will only work if there is, at most, only one object of the type looking for in the world or it does not matter which object of that type is returned. Add this method to your SpaceWorld class. In your Alienship class, in the 'destroy' method, add the following line:
1
((SpaceWorld)getWorld()).getCounter().add(10);
and change line 73 in the same class to:
1
getWorld().addObject(new ScoreBoard(((SpaceWorld)getWorld().getCounter().getValue()), 400, 400);
Bntyhntr501 Bntyhntr501

2014/5/9

#
Ok I did that and when I hit compile it gives "cannot find sumbol - method getCounter()".
danpost danpost

2014/5/9

#
The 'getCounter' method goes in your SpaceWorld class. Since the lines in the Alienship class assume that a Counter object will always be in the world anytime the 'getCounter' method is called, you could change the 'getCounter' method to this:
1
2
3
4
public Counter getCounter()
{
    return (Counter)geObjects(Counter.class).get(0);
}
Bntyhntr501 Bntyhntr501

2014/5/9

#
I put that into my SpaceWorld and it still gives that same error.
Bntyhntr501 Bntyhntr501

2014/5/9

#
Do I have to put this method in an act method?
danpost danpost

2014/5/9

#
Bntyhntr501 wrote...
Do I have to put this method in an act method?
No. It is a method in itself. You should not (yet) have an act method in your world class. If it is still not compiling and giving 'cannot find' or something, post the SpaceWorld class as it now is.
There are more replies on the next page.
1
2