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

2020/11/25

GameOver

1
2
NewbJava NewbJava

2020/11/25

#
How do I make it so that once the rocket hits a meteor the game will end and switch to a gameover screen?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class MyWorld extends World
{
      
    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()
    {
       if(Greenfoot.getRandomNumber(15)<1)
       {
       addMeteor(); 
       }
         
    
    public void addMeteor()
    {
     addObject(new Meteors(),getWidth()-1,Greenfoot.getRandomNumber(getHeight()));
    }
    private void prepare()
    
        Rocket Rocket = new Rocket();
        addObject(Rocket, 100,200);
        Rocket.setLocation(0,200); 
         
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
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();
         
    }
    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;
        }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Lasars extends Actor
{
     public boolean toRemove=false;
    public void act()
    {
    lasarMove(); 
    removeLasars(); 
     
    }
    public void lasarMove()
    
        setLocation(getX() + 5,getY());
          
    }
    public void removeLasars()
    {
     if (this.isAtEdge() == true)
        {
           World world;
           world = getWorld();
           world.removeObject(this);
           return;
    }
    
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Obstacles extends Actor
{
     protected boolean toRemove=false;
    public void act()
    {
          
    }   
    public void move()
    {
      setLocation(getX()-10,getY());   
      Actor actor=getOneIntersectingObject(Rocket.class);
    }  
    public void remove()
    {
        if (toRemove || isAtEdge())
        {
           World world;
           world = getWorld();
           world.removeObject(this);
           return;
    }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)
        {
        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;
     
}  
 
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class Explosion extends Actor
{
    private int time = 5;
    public void addedToWorld(World MyWorld)
    
        GreenfootImage image=new GreenfootImage(100,100);
        image.setColor(new Color(255,255,0,180));
        image.fillOval(0,0,image.getWidth()-1,image.getHeight()-1);
        image.fillOval(20,20,image.getWidth()-41,image.getHeight()-41);
        setImage(image);
        time=5;
     
    }
    public void act()
    {
       if(time>0) time--;
       else getWorld().removeObject(this);
        
    }   
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class Animate extends Actor
{
    public int xaxisV=0, yaxisV=0,rotation=0;
     
    public Animate(GreenfootImage img)
    
        GreenfootImage image =new GreenfootImage(10,10);
        image.drawImage(img,-Greenfoot.getRandomNumber(img.getWidth()),-Greenfoot.getRandomNumber(img.getHeight()));
        setImage(image);
    }
         
    public void addedToWorld(World MyWorld)
    
        xaxisV=-5+Greenfoot.getRandomNumber(10);
        yaxisV=-5+Greenfoot.getRandomNumber(10);
        rotation=-10+Greenfoot.getRandomNumber(20);
        if(xaxisV==0)
        {
            xaxisV=1;
        }
        if(yaxisV==0)
        {
            yaxisV=1;
        }
    }
     
    public void act()
    {
        setLocation(getX()+xaxisV,getY()+yaxisV);
        setRotation(getRotation()+rotation);
        if(getX()<-200 || getY()<-200 || getX()>getWorld().getWidth()+200 || getY()>getWorld().getHeight()+200)
        {
            getWorld().removeObject(this);
        }
         
    }   
  
}
danpost danpost

2020/11/25

#
NewbJava wrote...
How do I make it so that once the rocket hits a meteor the game will end and switch to a gameover screen?
If rocket hits meteor, remove rocket. If rocket not in world, set game over world.
NewbJava NewbJava

2020/11/25

#
danpost wrote...
NewbJava wrote...
How do I make it so that once the rocket hits a meteor the game will end and switch to a gameover screen?
If rocket hits meteor, remove rocket. If rocket not in world, set game over world.
Sorry I am a little new at java so can you tell me how to write the code and were exactly should it be placed
NewbJava NewbJava

2020/11/25

#
danpost wrote...
NewbJava wrote...
How do I make it so that once the rocket hits a meteor the game will end and switch to a gameover screen?
If rocket hits meteor, remove rocket. If rocket not in world, set game over world.
I added this piece of code into the rocket class. I was able to make the rocket remove from world after being hit by a meteor, but now I am wondering how to switch to a gameover screen.
1
2
3
4
5
6
7
8
9
10
public void gameOver()
{
   Actor meteor = getOneIntersectingObject(Meteors.class);
        if(meteor != null)
        {
        getWorld().removeObject(this);
        }
         
 
}
danpost danpost

2020/11/25

#
NewbJava wrote...
I added this piece of code into the rocket class. I was able to make the rocket remove from world after being hit by a meteor, but now I am wondering how to switch to a gameover screen.
The second part is done by your game world.
NewbJava NewbJava

2020/11/25

#
danpost wrote...
NewbJava wrote...
I added this piece of code into the rocket class. I was able to make the rocket remove from world after being hit by a meteor, but now I am wondering how to switch to a gameover screen.
The second part is done by your game world.
So what should I put if i wanted to set a gameover image after the rocket is removed from world?
danpost danpost

2020/11/25

#
NewbJava wrote...
So what should I put if i wanted to set a gameover image after the rocket is removed from world?
Ask if any rockets are in world. If not, create and set a new game over world active.
NewbJava NewbJava

2020/11/25

#
danpost wrote...
NewbJava wrote...
So what should I put if i wanted to set a gameover image after the rocket is removed from world?
Ask if any rockets are in world. If not, create and set a new game over world active.
How I would I ask if there are any rockets in world in code?
danpost danpost

2020/11/25

#
NewbJava wrote...
How I would I ask if there are any rockets in world in code?
By comparing this to zero:
1
int numRockets = getObjects(Rocket.class).size();
or using this as the condition:
1
boolean noRockets = getObjects(Rocket.class).isEmpty();
NewbJava NewbJava

2020/11/26

#
danpost wrote...
NewbJava wrote...
How I would I ask if there are any rockets in world in code?
By comparing this to zero:
1
int numRockets = getObjects(Rocket.class).size();
or using this as the condition:
1
boolean noRockets = getObjects(Rocket.class).isEmpty();
How do i f
1
2
3
4
5
6
7
8
9
10
public void Switchscreen()
 {
     boolean noRockets = getObjects(Rocket.class).isEmpty();
     if (noRockets == true)
     {
        Greenfoot.setWorld(new Playagain());
 
 
   }
}
ix the error on getObjects
NewbJava NewbJava

2020/11/26

#
^^How do I fix the error?*
danpost danpost

2020/11/26

#
NewbJava wrote...
How do i fix the error on getObjects
Where did you put that? Show class codes.
NewbJava NewbJava

2020/11/26

#
danpost wrote...
NewbJava wrote...
How do i fix the error on getObjects
Where did you put that? Show class codes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
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(); 
       Switchscreen();
         
        
         
    }
    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); 
        Greenfoot.stop();
         }  
        
          
 }  
 public void Switchscreen()
 {
     boolean noRockets = getObjects(Rocket.class).isEmpty();
     if (noRockets == true)
     {
        Greenfoot.setWorld(new Playagain());
  
  
   }
}
  
}
danpost danpost

2020/11/26

#
danpost wrote...
The second part is done by your game world.
The Rocket class is not your game world class.
NewbJava NewbJava

2020/11/26

#
danpost wrote...
danpost wrote...
The second part is done by your game world.
The Rocket class is not your game world class.
So is this the correct location.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class MyWorld extends World
{
      
    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()
    {
       if(Greenfoot.getRandomNumber(20)<1)
       {
       addMeteor(); 
       }
         
    
    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); 
         
    }
    public void Switchscreen()
 {
     boolean noRockets = getObjects(Rocket.class).isEmpty();
     if (noRockets == true)
     {
        Greenfoot.setWorld(new Playagain());
  
  
   }
}
}
There are more replies on the next page.
1
2