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

2020/12/1

Highscore

5
6
7
8
danpost danpost

2020/12/7

#
NewbJava wrote...
this is what I have. There seems to be something wrong since the game does not switch to the game over screen.
The timer starts as soon as the rocket stops moving. That is, the rocket must move first.
NewbJava NewbJava

2020/12/7

#
Ok I fixed it thank you for your help.
NewbJava NewbJava

2020/12/8

#
danpost wrote...
NewbJava wrote...
there is an error on line 8 that "constructor MyWorld in class MyWorld can not be applied to given type"
You seem to have removed the other constructor from MyWorld. Refer back and restore it back into the class. Also, you change the super call in the main MyWorld constructor. Restore it, also. Then, add the following 3rd constructor to the class:
public MyWorld(int w, int h, int c, boolean b)
{
    super(w, h, c, b);
}
I already finished my code but what is the purpose of this constructor?
danpost danpost

2020/12/9

#
NewbJava wrote...
<< Code Omitted >> I already finished my code but what is the purpose of this constructor?
It is added just in case you decide to extend the class with an unbounded world (or, rather, just in the event you want to use the other World class constructor).
NewbJava NewbJava

2020/12/9

#
danpost wrote...
NewbJava wrote...
<< Code Omitted >> I already finished my code but what is the purpose of this constructor?
It is added just in case you decide to extend the class with an unbounded world (or, rather, just in the event you want to use the other World class constructor).
import greenfoot.*;  
 
public class MyWorld extends World // focuses on displaying of the score 
{ 
    static Actor scoreDisplay;        // all static variable because they rest once the game is finished 
    static Actor highScoreDisplay;
    public static int score;
    static int highScore;  
    
      
    public MyWorld() // screen size and sets variable values, 
    {    
         
        super(600, 400, 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 trial) // score resseting and high score being saved for every round and being replaced if score is largr than high score 
    {
        score += trial;
        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));
            highScoreDisplay.setImage(img);
        }
    }
     
    public void act() // getting from the MeteorsWorld() method in the sub class 
    {  
        Greenfoot.setWorld(new MeteorsWorld()); 
        
        
         
    }   
    
   
    
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MeteorsWorld extends MyWorld // location of starting objects , meteor location and spawning, and qualifications of game over  
{

    
    public MeteorsWorld() // size of screen, methods within the class
    {    
        
        super(600, 400, 1);  
        prepare(); 
        adjustScore(-score);
        
        
        
        
    } 
    public void addMeteor() // places a meteor all the way to left - 1 and at a random y-value 
    { 
     addObject(new Meteors(),getWidth()-1,Greenfoot.getRandomNumber(getHeight()));
    }
    private void prepare() // Location of rocket and where score and high score are located. 
    {  
        Rocket Rocket = new Rocket(); 
        addObject(Rocket,100,200); 
        Rocket.setLocation(70,200);  
        addObject(scoreDisplay,55,380); 
        addObject(highScoreDisplay,525,25);
         
         
    } 
    public void Switchscreen() // defining when the game is over 
 { 
     boolean noRockets = getObjects(Rocket.class).isEmpty(); 
     if (noRockets == true) 
     { 
        Greenfoot.setWorld(new Playagain()); 
  
  
   } 
}   
public void act() // Switchscreen() method and the frequency in which a meteor is spawned 
{ 
    Switchscreen(); 
        if(Greenfoot.getRandomNumber(20)<1) // Greenfoot.getRandomNumber(20) must be zero to spawn 
       {
       addMeteor();  
       }  
    }












}
NewbJava NewbJava

2020/12/9

#
danpost wrote...
NewbJava wrote...
<< Code Omitted >> I already finished my code but what is the purpose of this constructor?
It is added just in case you decide to extend the class with an unbounded world (or, rather, just in the event you want to use the other World class constructor).
What would be the purpose of the code in MyWorld in lines 32-35.
danpost danpost

2020/12/9

#
NewbJava wrote...
What would be the purpose of the code in MyWorld in lines 32-35.
That constructor, along with the one including the boolean parameter, have a two-fold purpose. The main purpose is to avoid the main constructor (lines 11 to 28) which would cause a reset of the high score. The other is to make extending the class a simple matter of changing what the class extends. It also allows for extending World types to be of different dimensions (as well as bound states).
You need to login to post a reply.
5
6
7
8