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

2020/12/1

Highscore

3
4
5
6
7
8
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
On 16 and 17
You probably still have not created an Actor subclass called SimpleActor.
should I create one now?
danpost danpost

2020/12/7

#
NewbJava wrote...
should I create one now?
Well, yeah. You should have created one long ago.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
should I create one now?
Well, yeah. You should have created one long ago.
ok there are no errors in the Myworld and MeteorsWorld classes now but there are errors in the Meteor class.
NewbJava NewbJava

2020/12/7

#
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

#
line 16 because there is no Scoreboard class and the getScoreboard() method has been removed from the MyWorld class.
danpost danpost

2020/12/7

#
Remove lines 5, 19, 20 and 21. Change line 22 to:
MyWorld.adjustScore(1);
NewbJava NewbJava

2020/12/7

#
danpost wrote...
Remove lines 5, 19, 20 and 21. Change line 22 to:
MyWorld.adjustScore(1);
ok there are no errors anymore in that class
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;  
    
      
    public MyWorld()
    {    
         
        super(600, 400, 1);   
         
        scoreDisplay = new SimpleActor();
        highScoreDisplay = new SimpleActor();
        score = -1;
        highScore = -1;
        adjustScore(1);
         
         
         
          
           
          
    }    
    
    public MyWorld(int w, int h, int c, boolean b)
{
    super(w, h, c, b);
}
    
    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));
            highScoreDisplay.setImage(img);
        }
    }
     
    public void act() 
    {  
        Greenfoot.setWorld(new MeteorsWorld()); 
        
        
         
    }    
    
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MeteorsWorld extends MyWorld
{

    
    public MeteorsWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);  
        prepare(); 
        
    } 
    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(scoreDisplay,55,380); 
        addObject(highScoreDisplay,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
{
    
    public void act() 
    {  
        
        move(); 
        meteor();
        remove(); 
        
    }   
    public void meteor() 
    {  
        Actor lasars = getOneIntersectingObject(Lasars.class); 
        if(lasars != null) 
        {   
        
        MyWorld.adjustScore(1); 
        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;
    
}   

} 
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SimpleActor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SimpleActor extends Actor
{
    /**
     * Act - do whatever the SimpleActor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
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;  
    
      
    public MyWorld()
    {    
         
        super(600, 400, 1);   
         
        scoreDisplay = new SimpleActor();
        highScoreDisplay = new SimpleActor();
        score = -1;
        highScore = -1;
        adjustScore(1);
         
         
         
          
           
          
    }    
    
    public MyWorld(int w, int h, int c, boolean b)
{
    super(w, h, c, b);
}
    
    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));
            highScoreDisplay.setImage(img);
        }
    }
     
    public void act() 
    {  
        Greenfoot.setWorld(new MeteorsWorld()); 
        
        
         
    }    
    
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MeteorsWorld extends MyWorld
{

    
    public MeteorsWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);  
        prepare(); 
        
    } 
    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(scoreDisplay,55,380); 
        addObject(highScoreDisplay,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
{
    
    public void act() 
    {  
        
        move(); 
        meteor();
        remove(); 
        
    }   
    public void meteor() 
    {  
        Actor lasars = getOneIntersectingObject(Lasars.class); 
        if(lasars != null) 
        {   
        
        MyWorld.adjustScore(1); 
        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;
    
}   

} 
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SimpleActor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SimpleActor extends Actor
{
    /**
     * Act - do whatever the SimpleActor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
NewbJava NewbJava

2020/12/7

#
this is what I have now
NewbJava NewbJava

2020/12/7

#
danpost wrote...
Remove lines 5, 19, 20 and 21. Change line 22 to:
MyWorld.adjustScore(1);
what is the next step?
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;  
    
      
    public MyWorld()
    {    
         
        super(600, 400, 1);   
         
        scoreDisplay = new SimpleActor();
        highScoreDisplay = new SimpleActor();
        score = -1;
        highScore = -1;
        adjustScore(1);
         
         
         
          
           
          
    }    
    
    public MyWorld(int w, int h, int c, boolean b)
{
    super(w, h, c, b);
}
    
    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));
            highScoreDisplay.setImage(img);
        }
    }
     
    public void act() 
    {  
        Greenfoot.setWorld(new MeteorsWorld()); 
        
        
         
    }    
    
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MeteorsWorld extends MyWorld
{

    
    public MeteorsWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);  
        prepare();   
        
        
        
        
    } 
    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(scoreDisplay,55,380); 
        addObject(highScoreDisplay,525,25);
         
         
    } 
    public void Switchscreen() 
 { 
     boolean noRockets = getObjects(Rocket.class).isEmpty(); 
     if (noRockets == true) 
     { 
        Greenfoot.setWorld(new Playagain()); 
  
  
   } 
}   
public void act() 
{ 
    Switchscreen(); 
        if(Greenfoot.getRandomNumber(20)<1) 
       {
       addMeteor();  
       }  
    }












}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class SimpleActor extends Actor
{
    /**
     * Act - do whatever the SimpleActor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
import greenfoot.*;  

public class Meteors extends Obstacles
{
    
    public void act() 
    {  
        
        move(); 
        meteor();
        remove(); 
        
    }   
    public void meteor() 
    {  
        Actor lasars = getOneIntersectingObject(Lasars.class); 
        if(lasars != null) 
        {   
        
        MyWorld.adjustScore(1); 
        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/7

#
NewbJava wrote...
what is the next step?
You could work on the title screen or build on the game.. Your choice. However, if you have OCD, you might want to have something in the background of MyWorld; so, working on the title screen might be considered next. Add a new method in MyWorld called titleScreen. Then call the method at the end of the main MyWorld constructor. For now, add the following lines into the titleScreen method:
GreenfootImage img = new GreenfootImage("METEORS", 84, Color.GRAY, new Color(0, 0, 0, 0));
getBackground().drawImage(img, 400-img.getWidth()/2, 40);
NewbJava NewbJava

2020/12/7

#
This is what my code is now the only issue I have is that the high score does not save when you play again
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
what is the next step?
You could work on the title screen or build on the game.. Your choice. However, if you have OCD, you might want to have something in the background of MyWorld; so, working on the title screen might be considered next. Add a new method in MyWorld called titleScreen. Then call the method at the end of the main MyWorld constructor. For now, add the following lines into the titleScreen method:
GreenfootImage img = new GreenfootImage("METEORS", 84, Color.GRAY, new Color(0, 0, 0, 0));
getBackground().drawImage(img, 400-img.getWidth()/2, 40);
This is what my code is now the only issue I have is that the high score does not save when you play again.
There are more replies on the next page.
3
4
5
6
7
8