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

2014/4/30

Codes for making a Scorecounter

1
2
danpost danpost

2014/5/2

#
I do not get it. That does not look like a World 'super' call in your Level2 constructor. Above you show the object in a 'setWorld' method argument which must be a World object. Please explain.
Neymar.Jr Neymar.Jr

2014/5/2

#
The code I sent was the number of enemies speed etc that will be in each level, Is that what you meant by explain.
danpost danpost

2014/5/2

#
OK. Let us follow the 'score' value. Show the constructor of the LevelInfo class.
Neymar.Jr Neymar.Jr

2014/5/2

#
import java.awt.Color;
/**
 * Class for specifying a level's
 * information. Things that could be
 * added (depending on your game)
 * are things like terrain information,
 * spawn positions, etc.
 */
public class LevelInfo {
    private int totalEnemies;
    private int enemyMaxHealth;
    private int enemySpeed;
    private int enemyValue;
    private Color backgroundColor;
    
    public LevelInfo(int totalEnemies, int enemyMaxHealth, int enemySpeed,
            int enemyValue, Color backgroundColor) {
        this.totalEnemies = totalEnemies;
        this.enemyMaxHealth = enemyMaxHealth;
        this.enemySpeed = enemySpeed;
        this.enemyValue = enemyValue;
        this.backgroundColor = backgroundColor;
    }
    
    public int getTotalEnemies() {
        return totalEnemies;
    }
    
    public int getEnemyMaxHealth() {
        return enemyMaxHealth;
    }
    
    public int getEnemySpeed() {
        return enemySpeed;
    }
    
    public int getEnemyValue() {
        return enemyValue;
    }
    
    public Color getBackgroundColor() {
        return backgroundColor;
    }
}
danpost danpost

2014/5/2

#
I am sorry, I meant the constructor for the class that the Level2 class extends.
Neymar.Jr Neymar.Jr

2014/5/2

#
its okay and here u go
import greenfoot.*;
import java.awt.Color;
public class Level extends SpaceWorld {
    protected ScoreManager score;
    private LevelInfo info;
    private int enemiesSpawned;
    
    int minTime = 1;
    int maxTime =5;
    int timeRange = maxTime-minTime;
    int fps = 60;
    private int timer;
    
    
    
    

    public Level(LevelInfo info, ScoreManager score) {
        this.info = info;
        this.score = score;
        makeBackground();
        addPlayer();
        
        
        
        
        
        addStars(500);

        timer = fps*(minTime+Greenfoot.getRandomNumber(timeRange));
    }
    
   
    
    
    
    

    public void act() {
        if(shouldSpawnEnemy()) {
            spawnEnemy();
        }
        if(isPlayer1Dead()) {
            Greenfoot.setWorld(new EndScreen(score, false));
        }
         if(isPlayer2Dead()) {
            Greenfoot.setWorld(new EndScreen(score, false));
        }
        
         
        if (timer > 0)
        {
            timer--;
            if (timer == 0) 
            {
                int x = Greenfoot.getRandomNumber( getWidth() );
                int y = Greenfoot.getRandomNumber( getHeight() );
                addObject( new Bomb(),x,y);

            }
        }
    }
    
    
    
    public void addStars(int howMany)
    {
        for(int s=0; s<howMany; s++) {
            int x = Greenfoot.getRandomNumber( getWidth() );
            int y = Greenfoot.getRandomNumber( getHeight() );
            addObject( new Star(),x,y);

        }   
    }   

    private void makeBackground() {
        GreenfootImage img = new GreenfootImage(900,386);
        img.setColor(info.getBackgroundColor());

        String levelName = getClass().getCanonicalName();
        img.drawString(levelName, 5, getHeight() - 6);

    }
    private void addPlayer() {
        Player1 player1 = new Player1();
        addObject(player1, 66, 199);
        player1.setRotation(90);

        Player2 player2 = new Player2();
        addObject(player2, 256, 124);
        player2.setRotation(90);
        
        Counter counter = new Counter("Player 1 Score: ");
        addObject(counter, 474, 28);
        
        Counter2 counter2 = new Counter2("Player 2 Score: ");
        addObject(counter2, 474, 355);
        

    }
    private boolean shouldSpawnEnemy() {
        boolean enemiesLeftToSpawn = enemiesSpawned < info.getTotalEnemies();
        boolean randomChance = Greenfoot.getRandomNumber(100) < 6;
        return enemiesLeftToSpawn && randomChance;
    }

    private void spawnEnemy() {
        int x = getWidth();
        int y = Greenfoot.getRandomNumber(getHeight());
        Enemy enemy = new Enemy(score, info.getEnemyMaxHealth(), info.getEnemySpeed(), info.getEnemyValue());
        addObject(enemy, x, y);
        enemiesSpawned++;
    }

    protected boolean isLevelCompleted() {
        return enemiesSpawned == info.getTotalEnemies()
        && getObjects(Enemy.class).isEmpty();
    }

    protected boolean isPlayer1Dead() {
        return getObjects(Player1.class).isEmpty();
        
    }
    
    protected boolean isPlayer2Dead() {
        return getObjects(Player2.class).isEmpty();
        
    }
   
}
danpost danpost

2014/5/2

#
Where do you describe the dimensions of the Level2 world? where is the 'super' call to the World class constructor with the width, height, and cellsize of the world?
Neymar.Jr Neymar.Jr

2014/5/2

#
I placed it in my world class and made all the other classes that i made its sub class heres the code.
public SpaceWorld()
    {    
        super(900, 386, 1, false);
        
        

    }
danpost danpost

2014/5/2

#
I do not see where you are calling that from the Level subclass.
You need to login to post a reply.
1
2