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

2014/7/9

Please help - implementing different scoring based on...

1
2
blangley83 blangley83

2014/7/9

#
I am trying to implement a different score depending on if my asteroid is completely cleared or of it has been split into smaller pieces. I have reviewed the forum and saw a few similar discussions, however I do not understand how to do this. In the asteroid game, I have it set to score 25 points every time an asteroid is broken up or cleared. How can I make it so that a cleared asteroid will yield 25 points while if it is split up it will only give you 10. I have tried putting it into the if statement but not sure why it is not working. thanks for your help.
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() 
    {
        ((Space)getWorld()).countScore();
        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());
            ((Space)getWorld()).gameOver();
            Greenfoot.playSound("MetalExplosion.wav");
            getWorld().removeObject(rocket);
            getWorld().removeObject(this);
        }
    }
}
MusicPenguin MusicPenguin

2014/7/9

#
it seems all you'll have to do is place the score setting variable into the respective methods that are already built. initialize a quantifying variable like int or double, whichever you want to use for your score keeping and add to it whenever such method gets called.. ex:
public int score = 0;
at line 82 add this:
score += 10;
and at line 96:
score += 25;
danpost danpost

2014/7/9

#
Unfortunately, it will not be as easy as MusicPenguin made it seem. With what was given, as soon as the asteroid is destroyed (removed from the world -- whether smaller asteroid were added in place of it or not) the score held by that asteroid is also destroyed. The score field will either need to be placed in your world class or it would need to be made a 'static' field (a class field -- instead of an instance field).
blangley83 blangley83

2014/7/9

#
I do not understand. So take my score field and add it to the world class? When you mean, with what was given, do you mean with the code i provided or with what music penguin suggested? Thanks sorry if this is hard for me to understand, it is a very fast paced intro computer science course. constantly on google and re-looking up terms as they are not all embedded in my brain just yet. Thanks for bearing with me
danpost danpost

2014/7/9

#
Sorry for the confusion. I was referring to the help code suggested by MusicPenguin. It seems you already have your score field (or object) in the Space class and a method which I believe needs to be modified to accommodate variable point scoring. Please post the 'countScore' method that you have in your Space class.
blangley83 blangley83

2014/7/9

#
No problem, i am easily confused :)
public void countScore()
    {
        scoreCounter.add(25);
    }
yes, I added this method like so as prior instructions for the teacher had us add 25 points for every asteroid. a couple of assignments later she wants us to implement the 25/10 scoring. I am assuming an if statement in the countScore method is needed? maybe I am completely off... thanks for the help music and dan.
danpost danpost

2014/7/9

#
What needs to be done is that the hard-coded '25' need to be changed to a local variable. The value of this variable will need to be passed to the method when called and this method must be modified to accept that value. An int parameter is in order -- call it 'int change'. Then line 3 would be 'scoreCounter.add(change);'. After making these modifications, you will need to go back to the Asteroid class and insert the scoring values in the method calls. The Classes trail in the Java tutorials might be of some help. Escpecially the pages on 'Defining methods' and 'Passing Information to a Method or a Constructor'.
blangley83 blangley83

2014/7/9

#
How do I hardcode the scoring values into those methods now? Is it done in the if statement? Do I implement the new int parameter you just suggested? Do I define change in the Space class only? or the asteroid class as well? Still really confused.
danpost danpost

2014/7/9

#
You do not define 'change' as a field in either class. You only declare it as a local variable brought in by way of the parameter of the method. The hard-coded values will be included in your calls to your 'countScore' method in the Asteroid class as like in the following:
((Space)getWorld()).countScore(25);
// or
((Space)getWorld()).countScore(10);
blangley83 blangley83

2014/7/9

#
Sorry, my use of define and declare mean the same thing to me but clearly are completely different. I do know what you mean, but it still leaves me confused how to code it. This did not work, which I was hoping i understood what you meant:
    private void breakUp() 
    {
        ((Space)getWorld()).countScore();
        Greenfoot.playSound("Explosion.wav");
        
        if(size <= 16) 
        {
            getWorld().removeObject(this);
           scoreCounter.add(10);
        }
        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();
          scoreCounter.add(25);
            getWorld().removeObject(this);
        }
blangley83 blangley83

2014/7/9

#
ohhh, would this work
Space.scoreCounter.add(25);
and also, in my space class making the counter static (since it gave me an error)
  static Counter scoreCounter; 
EDIT: - I tried what you suggested, i arrived at new errors. Posting them shortly
danpost danpost

2014/7/10

#
Actually, that would work -- at least for the first time after compiling. But, you will have a problem after resetting. The score would carry over to the new game. Leave the Counter as non-static, use calls in your Asteroid class as I gave above and replace your 'countScore' method in your Space class with this:
public void countScore(int change)
{
    scoreCounter.add(change);
}
blangley83 blangley83

2014/7/10

#
This is the new code with what you suggested:
     private void breakUp() 
    {
        ((Space)getWorld()).countScore();
        Greenfoot.playSound("Explosion.wav");
        
        if(size <= 16) 
        {
            getWorld().removeObject(this);
       ((Space)getWorld()).countScore(10);
        }
        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();
        ((Space)getWorld()).countScore(25);
            getWorld().removeObject(this);
        }
    }
and am arriving at the error code saying: method countScore in class Space cannot be applied to given types;
blangley83 blangley83

2014/7/10

#
YES, that is what happened. I got it to add the score correctly, but it would not be correct when it displayed the score... meaning the score would show 100 but in bottom left may show 116. I am assuming it has something to do with your last post edit: sorry this post came as you must have been typing your last one, thanks so much dan going to try it now
danpost danpost

2014/7/10

#
blangley83 wrote...
This is the new code with what you suggested: < Code Omitted > and am arriving at the error code saying: method countScore in class Space cannot be applied to given types;
You did not specify which line you received that error on (line 3 or line 9). Actually you can remove line 3 as it does absolutely nothing in itself. After modifying the 'countScore' method as I gave above, the errors should go away.
There are more replies on the next page.
1
2