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

2014/12/7

Having trouble leveling up

cnb8428 cnb8428

2014/12/7

#
In the first level i hit the 25 percent mark and it repopulates the world but then as soon as i hit one Boid it repopulates again and the same thing happens over and over. Im trying to make the game so after i finish level 1 it requires more boids to be hit in order to reach the next level. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; /** * A world for the boids. * * @author Poul Henriksen * @version 2.0 */ public class Sky extends World { /** * Counter Object Used to keep the score */ private Counter scoreCounter; private Timer gameTimer; private int numberOfBoids; private double percentageToWin; private int level = 1; /** * Constructor for objects of class Sky. * */ public Sky() { super(800, 600, 1); /** * Create a new scoreCounter object and then add it to the world */ scoreCounter = new Counter("Score: "); addObject(scoreCounter, 55, 580); getBackground().setColor(new Color(220,220,100)); getBackground().fill(); levelUp(); } public void levelUp() { /** * add a timer to the world */ level = 1; gameTimer = new Timer("Time Remaining: "); addObject(gameTimer, 100, 560); numberOfBoids = 100; percentageToWin = 0.25; populateTrees(100); populateBoids(numberOfBoids); /** * Make a Shooter */ Shooter myShooter = new Shooter(); addObject(myShooter, getWidth()/2, getHeight()/2); } public void clearWorld() { Greenfoot.delay(120); removeObject(gameTimer); removeObjects(getObjects(Boid.class)); removeObjects(getObjects(Tree.class)); removeObjects(getObjects(Shooter.class)); } public void populateBoids(int number) { for(int i=0; i < number; i++) { int x = (int) (Math.random() * getWidth()); int y = (int) (Math.random() * getHeight()); Boid b = new Boid(); addObject(b, x, y); } } public void populateTrees(int number) { // Size of block in pixels int blockSize = 70; // Trees per block int treesPrBlock = 10; // How close together trees in a block are. Higher number, the closer they are. int blockCoherence = 1; for(int block = 0; block < number / treesPrBlock; block++) { int x = Greenfoot.getRandomNumber(getWidth()/blockSize) * blockSize; int y = Greenfoot.getRandomNumber(getHeight()/blockSize) * blockSize; for(int t = 0; t < treesPrBlock; t++) { int dx = Greenfoot.getRandomNumber(blockSize/blockCoherence); int dy = Greenfoot.getRandomNumber(blockSize/blockCoherence); Tree b = new Tree(); addObject(b, x + dx, y + dy); } } } /** * This method is called when the game is over to display the final score. */ public void gameOver() { addObject(new ScoreBoard(scoreCounter.getValue()), getWidth()/2, getHeight()/2); levelUp(); } /** * Add a value to the current score counter object */ public void countScore(int input) { scoreCounter.add(input); /** * The game is over if we hit percentageToWin number of the boids */ if(scoreCounter.getValue() >= numberOfBoids*percentageToWin) { clearWorld(); levelUp(); } } public void act() { } }
cnb8428 cnb8428

2014/12/7

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;


/**
 * A world for the boids.
 * 
 * @author Poul Henriksen 
 * @version 2.0
 */
public class Sky extends World
{
    /**
     * Counter Object Used to keep the score
     */
    private Counter scoreCounter;
    private Timer gameTimer;
    private int numberOfBoids;
    private double percentageToWin;


    private int level = 1;


    
    
    
    /**
     * Constructor for objects of class Sky.
     * 
     */
    public Sky()
    {    
        super(800, 600, 1);
        
        
      
        /**
         * Create a new scoreCounter object and then add it to the world
         */
        scoreCounter = new Counter("Score: ");
        addObject(scoreCounter, 55, 580);
        
                      
        getBackground().setColor(new Color(220,220,100));
        
        getBackground().fill();
       
        
        levelUp();
        
        
        
    }
    
    
    
    public void levelUp()
    {
        /**
         * add a timer to the world
         */
        level = 1;
        gameTimer = new Timer("Time Remaining: ");
        addObject(gameTimer, 100, 560);
        
        numberOfBoids = 100;
        percentageToWin = 0.25;

        
        
        

        populateTrees(100);
        populateBoids(numberOfBoids);
        
        
        
        
        
       
        
        
        
        
        /**
         * Make a Shooter
         */
        
        Shooter myShooter = new Shooter();
        addObject(myShooter, getWidth()/2, getHeight()/2);
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    }
    
    
    
    
    public void clearWorld()
    {
        Greenfoot.delay(120);
        removeObject(gameTimer);
        removeObjects(getObjects(Boid.class));
        removeObjects(getObjects(Tree.class));
        removeObjects(getObjects(Shooter.class));
        
        
    }
    
    
    public void populateBoids(int number) {
        for(int i=0; i < number; i++) {            
             int x = (int) (Math.random() * getWidth());          
             int y = (int) (Math.random() * getHeight());
             Boid b = new Boid();
             addObject(b, x, y);
        }
    }
    
    public void populateTrees(int number) {
        // Size of block in pixels
        int blockSize = 70; 
        // Trees per block
        int treesPrBlock = 10;
        // How close together trees in a block are. Higher number, the closer they are.
        int blockCoherence = 1;
        for(int block = 0; block < number / treesPrBlock; block++) {           
             int x = Greenfoot.getRandomNumber(getWidth()/blockSize) * blockSize;   
             int y = Greenfoot.getRandomNumber(getHeight()/blockSize) * blockSize;  
             for(int t = 0; t < treesPrBlock; t++) {
                 int dx = Greenfoot.getRandomNumber(blockSize/blockCoherence);
                 int dy = Greenfoot.getRandomNumber(blockSize/blockCoherence);
                 Tree b = new Tree();
                 addObject(b, x  + dx, y + dy);                
             }    
        }        
    }
    
    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver() 
    {
        addObject(new ScoreBoard(scoreCounter.getValue()), getWidth()/2, getHeight()/2);
        levelUp();
       
        
    }
    
    /**
     * Add a value to the current score counter object
     */
    public void countScore(int input)
    {
        scoreCounter.add(input);
        /**
         * The game is over if we hit percentageToWin number of the boids
         */
        if(scoreCounter.getValue() >= numberOfBoids*percentageToWin)
        {
            
            clearWorld();
            levelUp();

            
            
            
            
            

        
        
        }
        

    }
    
    
    
    public void act()
    {
        
        
     
    }
    
    
}
danpost danpost

2014/12/7

#
Line 172 is the one that detects level changing. You are comparing the current score with the product of the number of boids and the percentage to win. For level 2, you will still be using the score counter, as is; so, you need to adjust one of the fields to have a new goal to reach. Unfortunately, just upping the percentage to win will not be enough. It would have to be multiplied by the level number just to keep the same number of boids to win (this is just to keep up with the score each level). To introduce a higher percentage to win with each successive level, more will have to be done). The percentage to win will need a slight adjustment before being multiplied by the level. This adjustment will need to be some factor of one less than the level number so there will be no adjustment at level one. It could end up something like this:
if (scoreCounter.getValue() >= numberOfBoids*(percentageToWin+0.05*(level-1))*level)
Using 100 for numberOfBoids and 0.25 for percentageToWin, the winning scores per level will be: (1) 25 points for the first 25 boids (2) 60 points for 35 more boids (3) 105 points for 45 more boids (4) 160 points for 55 more boids Looks like this will increase the number of boids required to win by 10 each level.
danpost danpost

2014/12/7

#
If the game will be beatable at level 9 then you will have to incorporate another way to exit the levels. Maybe just asking if no boids are in the world is enough.
if (scoreCounter.getValue() >= numberOfBoids*(percentageToWin+0.05*(level-1))*level || getObjects(Boid.class).isEmpty())
You could at that point then just increase the number of boids per level.
You need to login to post a reply.