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 ); } } } |