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

2020/12/1

Highscore

4
5
6
7
8
danpost danpost

2020/12/7

#
NewbJava wrote...
This is what my code is now the only issue I have is that the high score does not save when you play again
What codes are in PlayAgain class?
NewbJava NewbJava

2020/12/7

#
import greenfoot.*;  


public class Playagain extends World
{

   
    public Playagain()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(296, 398, 1); 
    } 
     public void act() 
    {  
        if(Greenfoot.isKeyDown("enter")) 
        { 
            Greenfoot.setWorld(new MyWorld()); 
        
        }
        if(Greenfoot.isKeyDown("b")) 
        { 
            Greenfoot.setWorld(new Directions());
        }
} 
}
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
This is what my code is now the only issue I have is that the high score does not save when you play again
What codes are in PlayAgain class?
this is what is in my play agian class.
danpost danpost

2020/12/7

#
Change MyWorld to MeteorsWorld on line 17.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
Change MyWorld to MeteorsWorld on line 17.
The score does not reset when you click "enter" to play again.
danpost danpost

2020/12/7

#
NewbJava wrote...
The score does not reset when you click "enter" to play again.
Right, I was going to tell you to do something to fix that; but, the editing of the post failed and I lost it and got deterred from it by another issue you had. At end of MeteorsWorld constructor, add the following line:
adjustScore(-score);
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
The score does not reset when you click "enter" to play again.
Right, I was going to tell you to do something to fix that; but, the editing of the post failed and I lost it and got deterred from it by another issue you had. At end of MeteorsWorld constructor, add the following line:
adjustScore(-score);
Ok that fixed the problem thank you. There is one more thing I would like to ask help on?
NewbJava NewbJava

2020/12/7

#
import greenfoot.*;  

public class Rocket extends Actor
{
    private int xdistance=0,ydistance=0; 
    boolean fire = true; 
    private boolean toRemove=false;
     
    public void addedToWorld(World MyWorld) 
    { 
       xdistance=getX(); 
       ydistance=getY(); 
    } 
     
    public void move() 
    { 
      double rx=xdistance-getX(); 
      double ry=ydistance-getY(); 
      double r=Math.sqrt(rx*rx+ry*ry); 
      int b=5; 
      int posx=0,posy=0;
      if(r>b)  
      {  
        posx=(int)(getX()+b*rx/r);  
        posy=(int)(getY()+b*ry/r);
        } else{  
            posx=xdistance; 
            posy=ydistance; 
        } 
        setLocation(posx,posy); 
    } 
    
   
    public void act() 
    {
      if(Greenfoot.mouseMoved(null))  
      { 
          MouseInfo mouse=Greenfoot.getMouseInfo(); 
          xdistance=mouse.getX(); 
          ydistance=mouse.getY(); 
        } 
       move(); 
       fireLasars(); 
       gameOver();  
        
        
       
        
    }
    public void fireLasars()  
    {  
        if(Greenfoot.isKeyDown("a") && fire == true ) 
        { 
            getWorld().addObject(new Lasars(),getX() - 30,getY());  
            fire = false; 
        } 
        else if (!Greenfoot.isKeyDown("a")) 
        { 
            fire = true; 
        }
}   
public void gameOver() 
{ 
   Actor meteor = getOneIntersectingObject(Meteors.class); 
        if(meteor != null) 
        { 
        getWorld().removeObject(this);  
         
         }   
       
         
 }   
 
 
}
NewbJava NewbJava

2020/12/7

#
is there a way to have a method which says that if the rocket does not move for two second the screen will switch to the game over screen.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
The score does not reset when you click "enter" to play again.
Right, I was going to tell you to do something to fix that; but, the editing of the post failed and I lost it and got deterred from it by another issue you had. At end of MeteorsWorld constructor, add the following line:
adjustScore(-score);
is there a way to have a method which says that if the rocket does not move for two second the screen will switch to the game over screen.
danpost danpost

2020/12/7

#
NewbJava wrote...
is there a way to have a method which says that if the rocket does not move for two second the screen will switch to the game over screen.
Add an int timer field to Rocket class. In if block in act method, add:
timer = 120;
After if block, add:
else
{
    if (--timer == 0)
    {
        getWorld().removeObject(this);
        return;
    }
}
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
is there a way to have a method which says that if the rocket does not move for two second the screen will switch to the game over screen.
Add an int timer field to Rocket class. In if block in act method, add:
timer = 120;
After if block, add:
else
{
    if (--timer == 0)
    {
        getWorld().removeObject(this);
        return;
    }
}
import greenfoot.*;  

public class Rocket extends Actor
{
    private int xdistance=0,ydistance=0, timer; 
    boolean fire = true; 
    private boolean toRemove=false;
     
    public void addedToWorld(World MyWorld) 
    { 
       xdistance=getX(); 
       ydistance=getY(); 
    } 
     
    public void move() 
    { 
      double rx=xdistance-getX(); 
      double ry=ydistance-getY(); 
      double r=Math.sqrt(rx*rx+ry*ry); 
      int b=5; 
      int posx=0,posy=0;
      if(r>b)  
      {  
        posx=(int)(getX()+b*rx/r);  
        posy=(int)(getY()+b*ry/r);
        } else{  
            posx=xdistance; 
            posy=ydistance; 
        } 
        setLocation(posx,posy); 
    } 
    
   
    public void act() 
    {
      if(Greenfoot.mouseMoved(null))  
      { 
          MouseInfo mouse=Greenfoot.getMouseInfo(); 
          xdistance=mouse.getX(); 
          ydistance=mouse.getY();  
          timer = 120;   
        }  
       else
        {
            if (--timer == 0)
            {
        getWorld().removeObject(this);
        return;
         }
            }
        
       
       move(); 
       fireLasars(); 
       gameOver();  
        
        
       
        
    }
    public void fireLasars()  
    {  
        if(Greenfoot.isKeyDown("a") && fire == true ) 
        { 
            getWorld().addObject(new Lasars(),getX() - 30,getY());  
            fire = false; 
        } 
        else if (!Greenfoot.isKeyDown("a")) 
        { 
            fire = true; 
        }
}   
public void gameOver() 
{ 
   Actor meteor = getOneIntersectingObject(Meteors.class); 
        if(meteor != null) 
        { 
        getWorld().removeObject(this);  
         
         }   
       
         
 }   
 
 
}
NewbJava NewbJava

2020/12/7

#
this is what I have. There seems to be something wrong since the game does not switch to the game over screen.
NewbJava NewbJava

2020/12/7

#
to clarify it doesn't switch over after two seconds and the game just runs without the timer working
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
is there a way to have a method which says that if the rocket does not move for two second the screen will switch to the game over screen.
Add an int timer field to Rocket class. In if block in act method, add:
timer = 120;
After if block, add:
else
{
    if (--timer == 0)
    {
        getWorld().removeObject(this);
        return;
    }
}
to clarify it doesn't switch over after two seconds and the game just runs without the timer working
There are more replies on the next page.
4
5
6
7
8