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

2015/2/27

Help with Score Milestone

GabrahamCAPS GabrahamCAPS

2015/2/27

#
So essentially what I am trying to do is have it to where when you have a score of 20 in-game, seahorses start spawning along with seals and cherries making the game harder. When you pass a score of 20, then you are able to eat both cherries and seals with cherries being 1 point and seals being 2 points. Now, the problem I am having right now is with my world class. I don't exactly know how to take my score integer from my counter class and send it over to my world class where i will then create an if statement using the score integer. I have thought possibly putting the if statement in my counter class rather than my world class. If so, please let me know.Here is my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * An Ocean Experience
 * 
 * @author Gabe Piper
 * @version 1.0.1
 */
public class Ocean extends World
{
    Counter score = getScore();
    Counter counter = new Counter();
    public Ocean()
    {    
        super(600, 400, 1, false); 
        addObject(counter, 546, 37);
        addObject(new Fish(), 51, 192);
    }
        
    public void act()
    {
        if(canAddCherry()) 
        {
            addObject (new Cherry(), getWidth(), Greenfoot.getRandomNumber(getHeight()));
        }
        if(canAddSeal())
        {
            addObject (new Seal(), getWidth(), Greenfoot.getRandomNumber(getHeight()));
        }
        
    }
    
    public void levelUp()
    {
       
    }
    
    public boolean canAddCherry()
    {
        return getObjects(Cherry.class).size() < 2;
    }
    
    public boolean canAddSeal()
    {
        return getObjects(Seal.class).size() < 2;
    }
    
    public boolean canAddSeahorse()
    {
        return getObjects(Seahorse.class).size() < 1;
    }
       
    public Counter getCounter()
    {
        return counter;
    }
    
    public Counter getScore()
    {
        return score;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    public int score = 0;
    public Counter()
    {
    }
    
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }    
    
    public void addScore()
    {
        score++;
    }
    
    public void subtractScore()
    {
        score--;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Seahorse here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Seahorse extends Actor
{
    public Seahorse()
    {
    }
    
    public void act() 
    {
       setLocation(getX() - 6, getY());
     if (foundPlayer())
     {
          eatPlayer();
     }
     
      if (isOffScreen())
     {
          getWorld().removeObject(this);
     }   
    }    
    
        public boolean isOffScreen()
{
     if (getX() < 0 - getImage().getWidth()/2 || 
         getX() > getWorld().getWidth() + getImage().getWidth()/2 ||
         getY() < 0 - getImage().getHeight()/2 ||
         getY() > getWorld().getHeight() + getImage().getHeight()/2)
     {
          return true;
     }
     return false;
}

    public boolean foundPlayer()
    {
        Actor player = getOneObjectAtOffset(0, 0, Player.class);
        if(player != null) {
            return true;
        }
        else {
            return false;
        }
    }
    
    public void eatPlayer()
    {
    Actor player = getOneObjectAtOffset(0, 0, Player.class);
        if(player != null) {
            World myWorld = getWorld();
            Scoreboard scoreboard = new Scoreboard();
            myWorld.addObject(scoreboard, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(player);
            Greenfoot.stop();
        }
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Seal here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Seal extends Actor
{
    
    /**
     * Act - do whatever the Seal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - 6, getY());
     if (foundPlayer())
     {
          eatPlayer();
     }
     
      if (isOffScreen())
     {
          getWorld().removeObject(this);
     }
}

public boolean foundPlayer()
    {
        Actor player = getOneObjectAtOffset(0, 0, Player.class);
        if(player != null) {
            return true;
        }
        else {
            return false;
        }
    }
    
    public boolean isOffScreen()
{
     if (getX() < 0 - getImage().getWidth()/2 || 
         getX() > getWorld().getWidth() + getImage().getWidth()/2 ||
         getY() < 0 - getImage().getHeight()/2 ||
         getY() > getWorld().getHeight() + getImage().getHeight()/2)
     {
          return true;
     }
     return false;
}

public void eatPlayer()
{
    Actor player = getOneObjectAtOffset(0, 0, Player.class);
    if(player != null) {
        World myWorld = getWorld();
        Scoreboard scoreboard = new Scoreboard();
        myWorld.addObject(scoreboard, myWorld.getWidth()/2, myWorld.getHeight()/2);
        myWorld.removeObject(player);
        Greenfoot.stop();
    }
}
}
danpost danpost

2015/2/27

#
Lines 58 through 61 of your Ocean class above should be in you Counter class instead (that is where the 'score' field is). Also, you should remove line 11 of the Ocean class. Line 50 in the Ocean class can be expanded on to include the score check:
return counter.getScore() >= 20 && getObjects(Seahorse.class).isEmpty();
GabrahamCAPS GabrahamCAPS

2015/3/4

#
danpost wrote...
Lines 58 through 61 of your Ocean class above should be in you Counter class instead (that is where the 'score' field is). Also, you should remove line 11 of the Ocean class. Line 50 in the Ocean class can be expanded on to include the score check:
return counter.getScore() >= 20 && getObjects(Seahorse.class).isEmpty();
Okay, thank you so far. However, their are still errors. The lines 58 through 61 I moved over to the Counter class are giving me the error incompatible types. Not sure what is up but here is the code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    public int score = 0;
    public Counter()
    {
    }
    
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }    
    
    public void addScore()
    {
        score++;
    }
    
    public void subtractScore()
    {
        score--;
    }
    
    public Counter getScore()
    {
        return score;
    }
}
danpost danpost

2015/3/4

#
In line 36, you are declaring the 'getScore' method to return a Score object, but you are not doing that; you are returning 'score', which is an int value.
GabrahamCAPS GabrahamCAPS

2015/3/4

#
danpost wrote...
In line 36, you are declaring the 'getScore' method to return a Score object, but you are not doing that; you are returning 'score', which is an int value.
So I should write return int score but that is popping up another error saying that their should be a .class.
Super_Hippo Super_Hippo

2015/3/4

#
No, you should change the return type to 'int'.
GabrahamCAPS GabrahamCAPS

2015/3/4

#
Super_Hippo wrote...
No, you should change the return type to 'int'.
public Counter getScore()
    {
        return int score;
    }
Like this? This does not work.
Super_Hippo Super_Hippo

2015/3/4

#
public int /* <-- this is the return type*/ getScore()
{
    return score;
}
GabrahamCAPS GabrahamCAPS

2015/3/4

#
Super_Hippo wrote...
public int /* <-- this is the return type*/ getScore()
{
    return score;
}
Thank you very much.
You need to login to post a reply.