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

2020/12/1

Highscore

1
2
3
4
5
danpost danpost

2020/12/7

#
NewbJava wrote...
do I need to make a subclass of MyWorld?
Yes. Any class needing access to the scores should extend MyWorld. No need (or rather, best is not) to modify the MyWorld class from how it is given here -- other than making it a title screen (showing name of game,, author and maybe instructions and credits).
what would be my SimpleActor?
The actors that display the score and the high score would both be. See lines 13 and 14.
NewbJava NewbJava

2020/12/7

#
You can ignore my other discussion posts by the way
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
do I need to make a subclass of MyWorld?
Yes. Any class needing access to the scores should extend MyWorld. No need (or rather, best is not) to modify the MyWorld class from how it is given here -- other than making it a title screen (showing name of game,, author and maybe instructions and credits).
what would be my SimpleActor?
The actors that display the score and the high score would both be. See lines 13 and 14.
import greenfoot.*;  

public class MyWorld extends World
{ 
    static Actor scoreDisplay;
    static Actor highScoreDisplay;
    public static int score;
    static int highScore;  
    Scoreboard scoreboard = new Scoreboard();  
    HighScore highscore = new HighScore();
     
    public MyWorld()
    {    
        
        super(600, 400, 1, false);   
        prepare(); 
        scoreDisplay = new Scoreboard();
        highScoreDisplay = new HighScore(); 
        score = -1;
        highScore = -1;
        adjustScore(1); 
        
        
        
         
          
         
    }   
      
    
    static void adjustScore(int amt)
    {
        score += amt;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.RED, Color.BLACK);
        
        if (score > highScore)
        {
            highScore = score;
            img = new GreenfootImage("High score: "+highScore, 24, Color.RED, Color.BLACK);
            highScoreDisplay.setImage(img);
        }
    } 
    
    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.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HighScore here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HighScore extends Actor
{
    int highscore = 0;
    
    public void act() 
    {
        setImage(new GreenfootImage("High Score: " + highscore,20,Color.RED,Color.BLACK));  
         
       
    }     
    public void gamehighscore() 
    { 
        
        
        
    }
}
import greenfoot.*;  


public class Scoreboard extends Actor
{ 
    public static 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 Meteors extends Obstacles
{
   MyWorld thisGame; 
    public void act() 
    {  
        
        move(); 
        meteor();
        remove(); 
        
    }   
    public void meteor() 
    {  
        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;
    
}   

} 
}
NewbJava NewbJava

2020/12/7

#
These are what my classes look like now.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
do I need to make a subclass of MyWorld?
Yes. Any class needing access to the scores should extend MyWorld. No need (or rather, best is not) to modify the MyWorld class from how it is given here -- other than making it a title screen (showing name of game,, author and maybe instructions and credits).
what would be my SimpleActor?
The actors that display the score and the high score would both be. See lines 13 and 14.
What do I need to do from here in order for my high score to work.
danpost danpost

2020/12/7

#
NewbJava wrote...
These are what my classes look like now.
Remove HighScore and Scoreboard classes entirely from your project. Create another subclass of World called MeteorWorld. Edit it so that it extends MyWorld and then move (cutting from MyWorld) prepare, SwitchScreen and addMeteor methods to MeteorWorld.
NewbJava NewbJava

2020/12/7

#
import greenfoot.*;  

public class MyWorld extends World
{ 
    static Actor scoreDisplay;
    static Actor highScoreDisplay;
    public static int score;
    static int highScore;  
    Scoreboard scoreboard = new Scoreboard();  
    HighScore highscore = new HighScore();
     
    public MyWorld()
    {    
        
        super(600, 400, 1, false);   
        prepare(); 
        scoreDisplay = new Scoreboard();
        highScoreDisplay = new HighScore(); 
        score = -1;
        highScore = -1;
        adjustScore(1); 
        
        
        
         
          
         
    }   
      
    
    static void adjustScore(int amt)
    {
        score += amt;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.RED, Color.BLACK);
        
        if (score > highScore)
        {
            highScore = score;
            img = new GreenfootImage("High score: "+highScore, 24, Color.RED, Color.BLACK);
            highScoreDisplay.setImage(img);
        }
    } 
    
    public void act() 
    {  
       Switchscreen();
       if(Greenfoot.getRandomNumber(20)<1) 
       {
       addMeteor();  
       } 
       
        
    }    
   
    public Scoreboard getScoreboard() 
    {  
        return scoreboard; 
         
         
         
         
    }  
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MeteorWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MeteorWorld extends MyWorld
{
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 Meteors extends Obstacles
{
   MyWorld thisGame; 
    public void act() 
    {  
        
        move(); 
        meteor();
        remove(); 
        
    }   
    public void meteor() 
    {  
        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;
    
}   

} 
}
NewbJava NewbJava

2020/12/7

#
This is what I have now.
danpost danpost

2020/12/7

#
NewbJava wrote...
This is what I have now.
Where is your MeteorWorld constructor?
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
This is what I have now.
Where is your MeteorWorld constructor?
do you mean the classes act() or should I make another constructor called MeteorWorld()?
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
This is what I have now.
Where is your MeteorWorld constructor?
public class MeteorWorld extends MyWorld
{ 
  public MeteorWorld() 
  { 
      
      
    }
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()); 
 
 
   } 
}
}

danpost danpost

2020/12/7

#
NewbJava wrote...
do you mean the classes act() or should I make another constructor called MeteorWorld()?
It should have been there when you first created the class.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
danpost wrote...
NewbJava wrote...
This is what I have now.
Where is your MeteorWorld constructor?
do you mean the classes act() or should I make another constructor called MeteorWorld()?
It should have been there when you first created the class.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class MeteorWorld extends MyWorld
{

   
    public MeteorWorld()
    {
    }  
    
    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()); 
  
  
   } 
}
}
danpost danpost

2020/12/7

#
Where is the super call that should be in the constructor?
NewbJava NewbJava

2020/12/7

#
danpost wrote...
Where is the super call that should be in the constructor?
If it is a subclass of MyWorld it wouldn't have a super call.
There are more replies on the next page.
1
2
3
4
5