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

2020/12/1

Highscore

1
2
3
4
NewbJava NewbJava

2020/12/1

#
I am not sure what to put in the highscore class
public class Scoreboard extends Actor
{ 
    int score = 0; 
    /**
     * Act - do whatever the Scoreboard 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.RED,Color.BLACK)); 
       
    }   
    public void addscore() 
    {  
        score++; 
        
        
    }
}
public class highscore extends Scoreboard
{
   int highscore = 0;  
    public void act() 
    {
        setImage(new GreenfootImage("High Score: " + score,20,Color.RED,Color.BLACK));  
        
        if(score > highscore) 
        {  
           
          
        } 
         
    } 
  
    
}
public class MyWorld extends World
{ 
     Scoreboard scoreboard = new Scoreboard();  
     highscore highscore = new highscore(); 
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);   
        prepare();   
        
         
          
         
    } 
    public void act() 
    {  
       Switchscreen();
       if(Greenfoot.getRandomNumber(20)<1) 
       {
       addMeteor();  
       } 
       
        
    }   
    
    public Scoreboard getScoreboard() 
    {  
        return scoreboard; 
        
        
        
        
    }   
    
    public highscore gethighscore() 
    { 
        return highscore; 
      
    }
    
    public void addMeteor() 
    { 
     addObject(new Meteors(),getWidth()-1,Greenfoot.getRandomNumber(getHeight()));
    }
    private void prepare() 
    {  
        Rocket Rocket = new Rocket(); 
        addObject(Rocket,100,200); 
        Rocket.setLocation(70,200);  
        addObject(scoreboard,55,380); 
        addObject(highscore,525,25); 
        
    } 
    public void Switchscreen() 
 { 
     boolean noRockets = getObjects(Rocket.class).isEmpty(); 
     if (noRockets == true) 
     { 
        Greenfoot.setWorld(new Playagain()); 
 
 
   } 
}
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Meteors extends Obstacles
{
   
    public void act() 
    {  
        
        move(); 
        Hancur();
        remove(); 
        
    }   
    public void Hancur() 
    {  
        Actor lasars = getOneIntersectingObject(Lasars.class); 
        if(lasars != null) 
        {   
        World MyWorld = getWorld(); 
        MyWorld myWorld = (MyWorld)MyWorld;  
        Scoreboard scoreboard = myWorld.getScoreboard(); 
        scoreboard.addscore(); 
        getWorld().removeObject(lasars);
        for(int i=0;i<10;i++) 
        { 
            int posx=-20+Greenfoot.getRandomNumber(40);  
            int posy=-20+Greenfoot.getRandomNumber(40);
            getWorld().addObject(new Animate(getImage()),getX()+posx,getY()+posy); 
        } 
        getWorld().addObject(new Explosion(),getX(),getY());  
        toRemove=true;
    
}   

} 
}
danpost danpost

2020/12/2

#
You have a Scoreboard object; and then you have a highscore object. Both of these objects have their own individual score field (because highscore extends Scoreboard, it creates a Scoreboard object with the added attributes and behavior given in the highscore class). This is probably not what you want -- that is, highscore should probably not extend Scoreboard. As currently coded, when you go to your PlayAgain world, you lose all information regarding any earlier worlds. That means that during any game, the high score value will always be just whatever the current score is. To retain the high score through different worlds, you will need to either pass it along to each world or increase the scope of the field containing the value or the reference field of the object holding the field. I know how I would approach this. Unfortunately, your set up if far from what I would have that it would be a major re-structuring of our codes. In essence, however, I would have both int fields, score and highscore, in my game world class; have one method to update score and test and update high score when needed; and use simple actor objects to display the stats.
NewbJava NewbJava

2020/12/3

#
danpost wrote...
You have a Scoreboard object; and then you have a highscore object. Both of these objects have their own individual score field (because highscore extends Scoreboard, it creates a Scoreboard object with the added attributes and behavior given in the highscore class). This is probably not what you want -- that is, highscore should probably not extend Scoreboard. As currently coded, when you go to your PlayAgain world, you lose all information regarding any earlier worlds. That means that during any game, the high score value will always be just whatever the current score is. To retain the high score through different worlds, you will need to either pass it along to each world or increase the scope of the field containing the value or the reference field of the object holding the field. I know how I would approach this. Unfortunately, your set up if far from what I would have that it would be a major re-structuring of our codes. In essence, however, I would have both int fields, score and highscore, in my game world class; have one method to update score and test and update high score when needed; and use simple actor objects to display the stats.
import greenfoot.*;  

public class MyWorld extends World
{ 
     Scoreboard scoreboard = new Scoreboard();  
     Highscore highscore = new Highscore(); 
    public MyWorld()
    {    
        
        super(600, 400, 1, false);   
        prepare();   
        
         
          
         
    } 
    public void act() 
    {  
       Switchscreen();
       if(Greenfoot.getRandomNumber(20)<1) 
       {
       addMeteor();  
       } 
       
        
    }   
    
    public Scoreboard getScoreboard() 
    {  
        return scoreboard; 
        
        
        
        
    }    
    
   

    
    public void addMeteor() 
    { 
     addObject(new Meteors(),getWidth()-1,Greenfoot.getRandomNumber(getHeight()));
    }
    private void prepare() 
    {  
        Rocket Rocket = new Rocket(); 
        addObject(Rocket,100,200); 
        Rocket.setLocation(70,200);  
        addObject(scoreboard,55,380); 
        addObject(highscore,525,25); 
        
    } 
    public void Switchscreen() 
 { 
     boolean noRockets = getObjects(Rocket.class).isEmpty(); 
     if (noRockets == true) 
     { 
        Greenfoot.setWorld(new Playagain()); 
 
 
   } 
}
}
import greenfoot.*;  


public class Scoreboard extends Actor
{ 
    public int score = 0; 
    /**
     * Act - do whatever the Scoreboard 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.RED,Color.BLACK)); 
       
    }   
    public void addscore() 
    {  
        score++; 
        
        
    }
}
import greenfoot.*;  


public class Highscore extends Actor
{
   int highscore = 0;  
    public void act() 
    {
        setImage(new GreenfootImage("High Score: " + highscore,20,Color.RED,Color.BLACK));  
        
         
        {  
          
          
        } 
         
    } 
  
    
}  
NewbJava NewbJava

2020/12/3

#
I have now made the Highscore class a separate actor. What should I need in my world class and HIghscore class for the Highscore class to work and for it to be saved?
danpost danpost

2020/12/3

#
NewbJava wrote...
I have now made the Highscore class a separate actor. What should I need in my world class and HIghscore class for the Highscore class to work and for it to be saved?
Are you looking to maintain session high scores (no UserInfo storage) or retain all time high score (UserInfo storage)?
NewbJava NewbJava

2020/12/3

#
danpost wrote...
NewbJava wrote...
I have now made the Highscore class a separate actor. What should I need in my world class and HIghscore class for the Highscore class to work and for it to be saved?
Are you looking to maintain session high scores (no UserInfo storage) or retain all time high score (UserInfo storage)?
Only session high score.
danpost danpost

2020/12/3

#
NewbJava wrote...
Only session high score.
Okay. Probably the best way to set things up is with a MyWorld skeleton of:
import greenfoot.*;

public class MyWorld extends World
{
    static Inside inside;
    static Outside outside;
    static int score;
    static Actor scoreDisplay;
    
    public MyWorld()
    {
        super(800, 600, 1);
        score = 0;
        scoreDisplay = new SimpleActor();
        adjustScore(0);
        outside = new Outside();
        inside = null;
    }
    
    public MyWorld(int w, int h, int c)
    {
        super(w, h, c);
    }
    
    static void adjustScore(int amt)
    {
        score += amt;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.BLACK, new Color(0, 0, 0, 0));
        scoreDisplay.setImage(img);
    }
    
    public void act()
    {
        Greenfoot.setWorld(outside);
    }
}
with SimpleActor class as follows:
public class SimpleActor extends greenfoot.Actor{}
and with both Inside (or Indoors) and Outside (or Outdoors) extending MyWorld. From any Actor subclass, you can use:
MyWorld.adjustScore(<< amount >>);
Just add scoreDisplay object (already created) into your worlds (in constructors or prepare methods of extending classes.
NewbJava NewbJava

2020/12/3

#
danpost wrote...
NewbJava wrote...
danpost wrote...
NewbJava wrote...
I have now made the Highscore class a separate actor. What should I need in my world class and HIghscore class for the Highscore class to work and for it to be saved?
Are you looking to maintain session high scores (no UserInfo storage) or retain all time high score (UserInfo storage)?
Only session high score.
Okay. Probably the best way to set things up is with a MyWorld skeleton of:
import greenfoot.*;

public class MyWorld extends World
{
    static Inside inside;
    static Outside outside;
    static int score;
    static Actor scoreDisplay;
    
    public MyWorld()
    {
        super(800, 600, 1);
        score = 0;
        scoreDisplay = new SimpleActor();
        adjustScore(0);
        outside = new Outside();
        inside = null;
    }
    
    public MyWorld(int w, int h, int c)
    {
        super(w, h, c);
    }
    
    static void adjustScore(int amt)
    {
        score += amt;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.BLACK, new Color(0, 0, 0, 0));
        scoreDisplay.setImage(img);
    }
    
    public void act()
    {
        Greenfoot.setWorld(outside);
    }
}
with SimpleActor class as follows:
public class SimpleActor extends greenfoot.Actor{}
and with both Inside (or Indoors) and Outside (or Outdoors) extending MyWorld. From any Actor subclass, you can use:
MyWorld.adjustScore(<< amount >>);
Just add scoreDisplay object (already created) into your worlds (in constructors or prepare methods of extending classes.
What do inside and outside represent?
NewbJava NewbJava

2020/12/3

#
This might be a dumb question but what would the SimpleActor class do and what will be included in that class? And what class exactly would my scoreDisplay object be located in?
danpost danpost

2020/12/3

#
NewbJava wrote...
What do inside and outside represent? This might be a dumb question but what would the SimpleActor class do and what will be included in that class? And what class exactly would my scoreDisplay object be located in?
Blimy. I got confused with a different discussion thread. Sorry.
NewbJava NewbJava

2020/12/3

#
danpost wrote...
NewbJava wrote...
What do inside and outside represent? This might be a dumb question but what would the SimpleActor class do and what will be included in that class? And what class exactly would my scoreDisplay object be located in?
Blimy. I got confused with a different discussion thread. Sorry.
Oh no problems.
NewbJava NewbJava

2020/12/3

#
danpost wrote...
NewbJava wrote...
What do inside and outside represent? This might be a dumb question but what would the SimpleActor class do and what will be included in that class? And what class exactly would my scoreDisplay object be located in?
Blimy. I got confused with a different discussion thread. Sorry.
Are you still able to answer my question? thank you.
danpost danpost

2020/12/3

#
Still, the skeleton has some good points to it. Here is a more complete remake:
import greenfoot.*;
 
public class MyWorld extends World
{
    static Actor scoreDisplay;
    static Actor highScoreDisplay;
    static int score;
    static int highScore;
     
    public MyWorld()
    {
        super(800, 600, 1);
        scoreDisplay = new SimpleActor();
        highScoreDisplay = new SimpleActor();
        score = -1;
        highScore = -1;
        adjustScore(1);
    }
     
    public MyWorld(int w, int h, int c)
    {
        super(w, h, c);
    }
     
    static void adjustScore(int amt)
    {
        score += amt;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.RED, new Color(0, 0, 0, 0));
        scoreDisplay.setImage(img);
        if (score > highScore)
        {
            highScore = score;
            img = new GreenfootImage("High score: "+highScore, 24, Color.RED, new Color(0, 0, 0, 0));
            highScore.setImage(img);
        }
    }
     
    public void act()
    {
        Greenfoot.setWorld(new MeteorWorld());
    }
}
Using same SimpleActor class, which is used to create the actors that display the score and high score. It is just a generic Actor class. It creates actors that have no additional or changed attributes. Move game code to a MeteorWorld class that extends MyWorld. Delete Scoreboard and HighScore classes.
NewbJava NewbJava

2020/12/6

#
danpost wrote...
Still, the skeleton has some good points to it. Here is a more complete remake:
import greenfoot.*;
 
public class MyWorld extends World
{
    static Actor scoreDisplay;
    static Actor highScoreDisplay;
    static int score;
    static int highScore;
     
    public MyWorld()
    {
        super(800, 600, 1);
        scoreDisplay = new SimpleActor();
        highScoreDisplay = new SimpleActor();
        score = -1;
        highScore = -1;
        adjustScore(1);
    }
     
    public MyWorld(int w, int h, int c)
    {
        super(w, h, c);
    }
     
    static void adjustScore(int amt)
    {
        score += amt;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.RED, new Color(0, 0, 0, 0));
        scoreDisplay.setImage(img);
        if (score > highScore)
        {
            highScore = score;
            img = new GreenfootImage("High score: "+highScore, 24, Color.RED, new Color(0, 0, 0, 0));
            highScore.setImage(img);
        }
    }
     
    public void act()
    {
        Greenfoot.setWorld(new MeteorWorld());
    }
}
Using same SimpleActor class, which is used to create the actors that display the score and high score. It is just a generic Actor class. It creates actors that have no additional or changed attributes. Move game code to a MeteorWorld class that extends MyWorld. Delete Scoreboard and HighScore classes.
So do I need to make a subclass of MyWorld?
NewbJava NewbJava

2020/12/6

#
Also what would be my SimpleActor?
There are more replies on the next page.
1
2
3
4