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

2020/1/27

Adding a changing Scoreboard

Carbon_Raven Carbon_Raven

2020/1/27

#
Ok. So yes, there are multiple discussions already but I still cant find help for my excat problem. I try to create a working score that changes. I tried it with showText but that stays and I want a changing Score without 442 Score Objects after the first Level. My project is programming Space Invaders. Here is my Score Class code:
import java.awt.Font;
import greenfoot.*;
public class Score extends Actor
{
    // instance variables - replace the example below with your own
    private static int score = 0;
    public static GreenfootImage display = new GreenfootImage(400,80);
    private int pre = 0;
    public Score(){
        score = 0;
        setImage(new GreenfootImage("space1.jpg"));
        // display.setFont(new Font("Courier New", Font.PLAIN, 20));
        // display.drawString(convert(),400,80);
        display.drawString("Banana",400,80);
    }
    
    public void act(){
        if(pre != score){
            pre = score;
            changeScore();
        }
    }
    
    public static int getScore(){
      return score;
    }
    
    public static void addToScore(int x){
      score = score + x;
    }  
    
    public static void resetScore(int x){
        score = 0;
    }
    
    private static String convert(){
        String scoreS = "Score: " + score;
        return scoreS;
    }
    
    private void changeScore(){
        return;
    }
}
Yes, changeScore is empty. I need to change that. My world Class:
public ClassicWorld()
    {    
        // Creation of the Classic Map
        super(672, 720, 1); 
        removeObjects(getObjects(null));  // remove all existing objects
        addObject(new Player(),336,670);
        addObject(new LivesCounter(),450,40);
        addObject(new LivesCounter(),500,40);
        addObject(new house(), 350,640);
        addObject(new Score(),400,80);
        Score.addToScore(-1000);
        setupClassic(800); 
    }
No other Method refers to the Score class in the World class My Alien Class:
public void act() 
    {
        //die
        if(getY()>600){ 
            ClassicWorld World = getWorldOfType(ClassicWorld.class);
            World.showText("Aliens reached Earth",336,400);
            World.endGame();
            return;
        }
        
        //move    
        if(isDead == false){
            Actor bullet = getOneIntersectingObject(Bullet.class);
            if (bullet != null) {
                getWorld().removeObject(bullet);
                isDead = true;
                setImage(new GreenfootImage("AlienClassicDead.png"));
                int points = 10 + type*10;
                Score.addToScore(points); //SCORE!!!!!
                deathTimer.mark();
            }
            if(time.millisElapsed() > speed){
                move();
                time.mark();
                if(Greenfoot.getRandomNumber(180) == 1){
                    shoot();
                }
            }
        }
        else{
                if(deathTimer.millisElapsed() < 400){
                    if(time.millisElapsed() > speed){
                        move();
                        time.mark();
                    }
                }
                else{
                    List alienlist = getWorld().getObjects(Alien.class);
                    aliencounter = alienlist.size();
                    aliencounter --;
                    if(aliencounter==0){
                        World w = getWorld();
                        if(0<=type&&type<=2){
                            ClassicWorld cw = getWorldOfType(ClassicWorld.class);
                            cw.reset("CW",level);
                        }
                        else{}
                        w.removeObject(this);
                        }
                        else{
                            getWorld().removeObject(this);
                        }
                }
            }
        
        //--------TESTING----------//
            
    }  
If you need more information, just ask. Hope you can help, Raven
Carbon_Raven Carbon_Raven

2020/1/27

#
Edit: the setupClassic refers to score :facepalm: Here's the code:
public void setupClassic(int speed)
    {
        Score.addToScore(1000);
        System.out.println(Score.getScore());
        //-------------------------------------------------------------//
        List<LivesCounter> lives = getObjects(LivesCounter.class);
        if(lives.size()<8){
            int yFak = 1;
            int calcF =(lives.size()+1);
            if(calcF>4){
                yFak ++;
                calcF-=4;
            }
            int calcX = 400 + calcF*50;
            int calcY = yFak*40;
            addObject(new LivesCounter(),calcX,calcY);
        }
        //-------------------------------------------------------------//
        addObject(new Alien(2,speed),52,160);
        addObject(new Alien(2,speed),92,160);
        addObject(new Alien(2,speed),132,160);
        addObject(new Alien(2,speed),172,160);
        addObject(new Alien(2,speed),212,160);
        addObject(new Alien(2,speed),252,160);
        addObject(new Alien(2,speed),292,160);
        addObject(new Alien(2,speed),332,160);
        addObject(new Alien(2,speed),372,160);
        addObject(new Alien(2,speed),412,160);
        addObject(new Alien(2,speed),452,160);
        //----//
        addObject(new Alien(1,speed),52,200);
        addObject(new Alien(1,speed),92,200);
        addObject(new Alien(1,speed),132,200);
        addObject(new Alien(1,speed),172,200);
        addObject(new Alien(1,speed),212,200);
        addObject(new Alien(1,speed),252,200);
        addObject(new Alien(1,speed),292,200);
        addObject(new Alien(1,speed),332,200);
        addObject(new Alien(1,speed),372,200);
        addObject(new Alien(1,speed),412,200);
        addObject(new Alien(1,speed),452,200);
        //----//
        addObject(new Alien(1,speed),52,240);
        addObject(new Alien(1,speed),92,240);
        addObject(new Alien(1,speed),132,240);
        addObject(new Alien(1,speed),172,240);
        addObject(new Alien(1,speed),212,240);
        addObject(new Alien(1,speed),252,240);
        addObject(new Alien(1,speed),292,240);
        addObject(new Alien(1,speed),332,240);
        addObject(new Alien(1,speed),372,240);
        addObject(new Alien(1,speed),412,240);
        addObject(new Alien(1,speed),452,240);
        //----//
        addObject(new Alien(0,speed),52,280);
        addObject(new Alien(0,speed),92,280);
        addObject(new Alien(0,speed),132,280);
        addObject(new Alien(0,speed),172,280);
        addObject(new Alien(0,speed),212,280);
        addObject(new Alien(0,speed),252,280);
        addObject(new Alien(0,speed),292,280);
        addObject(new Alien(0,speed),332,280);
        addObject(new Alien(0,speed),372,280);
        addObject(new Alien(0,speed),412,280);
        addObject(new Alien(0,speed),452,280);
        //----//
        addObject(new Alien(0,speed),52,320);
        addObject(new Alien(0,speed),92,320);
        addObject(new Alien(0,speed),132,320);
        addObject(new Alien(0,speed),172,320);
        addObject(new Alien(0,speed),212,320);
        addObject(new Alien(0,speed),252,320);
        addObject(new Alien(0,speed),292,320);
        addObject(new Alien(0,speed),332,320);
        addObject(new Alien(0,speed),372,320);
        addObject(new Alien(0,speed),412,320);
        addObject(new Alien(0,speed),452,320);
    }
danpost danpost

2020/1/27

#
In changeScore, create a text GreenfootImage object and set it to the image of the Score actor.
You need to login to post a reply.