I am making a game for a school project and I want the world to automatically reset when the player character dies. I don't know how to go about doing this and would appreciate any help you could offer.


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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * The Player charecter * * @author Austin Prokopishin * @version 1.0 */ public class Player extends Actor { private int gemsCollected = 0 ; private int counter = 0 ; private int deaths = 0 ; GifImage gifImage = new GifImage( "spaceshipV2.gif" ); /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage(gifImage.getCurrentImage()); //resetting the world if (deaths == 1 ) { Greenfoot.setWorld( new MyWorld()); } //interacting with gems/crystals if (foundGem()) { collectGem(); } //movement if (Greenfoot.isKeyDown( "left" )) { setLocation( getX() - 3 , getY() ); } if (Greenfoot.isKeyDown( "right" )) { setLocation( getX() + 3 , getY() ); } if (Greenfoot.isKeyDown( "up" )) { setLocation( getX() , getY() - 3 ); } if (Greenfoot.isKeyDown( "down" )) { setLocation( getX() , getY() + 3 ); } //rotate if (Greenfoot.isKeyDown( "d" )) { setRotation (getRotation() + 3 ); } if (Greenfoot.isKeyDown( "a" )) { setRotation (getRotation() - 3 ); } //fire lasers if (Greenfoot.isKeyDown( "space" )) { counter++; } if (counter > 10 ) { counter = 0 ; } if (counter == 5 ) { Greenfoot.playSound( "lasersound.wav" ); fire(); } if (isTouching(alien. class )) { removeTouching(alien. class ); death(); deaths++; } } public boolean foundGem() { Actor gem = getOneObjectAtOffset( 0 , 0 , Crystal. class ); if (gem != null ) { return true ; } else { return false ; } } public void collectGem() { Actor Gem = getOneObjectAtOffset( 0 , 0 , Crystal. class ); if (Gem != null ) { getWorld().removeObject(Gem); gemsCollected = gemsCollected + 1 ; } } private void fire() { LazerBolt bolt = new LazerBolt(); World myWorld = getWorld(); myWorld.addObject(bolt, 0 , 0 ); bolt.setLocation(getX(), getY()); bolt.setRotation(getRotation()); } public void death() { getWorld().addObject ( new explosion2(), getX(), getY()); getWorld().removeObject( this ); } } |
1 2 3 4 | if (getObjects(Player. class ).isEmpty() && getObjects(explosion2. class ).isEmpty()) { Greenfoot.setWorld( new MyWorld()); } |
1 2 3 4 | if (getObjects(Player. class ).isEmpty() && getObjects(explosion2. class ).isEmpty()) { Greenfoot.setWorld( new MyWorld()); } |
1 | getWorld().removeObjects(getWorld().getObjects(alien. class )); |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * The game world. * * @author Austin Prokopishin * @version 1.0 */ public class MyWorld extends World { GreenfootSound backgroundMusic = new GreenfootSound( "8-Bit Dubstep IV.mp3" ); private int lives = 3 ; private boolean dead; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 1600x770 cells with a cell size of 1x1 pixels. super ( 1600 , 770 , 1 ); populate(); backgroundMusic.playLoop(); } /** * Method act * */ public void act() { //check for game over if (lives == 0 ) { Greenfoot.setWorld( new GameOverScreen()); } if (getObjects(Player. class ).isEmpty() && getObjects(explosion2. class ).isEmpty()) { Greenfoot.setWorld( new MyWorld()); lives--; dead = true ; } if (getObjects(alien. class ).isEmpty() && dead != true ) { addObject( new alien(), 1551 , 35 ); addObject( new alien(), 1556 , 77 ); addObject( new alien(), 1488 , 45 ); addObject( new alien(), 30 , 732 ); addObject( new alien(), 42 , 663 ); addObject( new alien(), 94 , 726 ); } } public void populate(){ int x; int y; int x2; int y2; for ( int i = 0 ; i < 5 ; i++) { x = ( int )(Math.random() * 1600 ); y = ( int )(Math.random() * 770 ); x2 = ( int )(Math.random() * 1600 ); y2 = ( int )(Math.random() * 770 ); addObject( new Asteroid1(), x, y); addObject( new Asteroid2(), x2, y2); } addObject( new Player(), 800 , 385 ); addObject( new alien(), 1551 , 35 ); addObject( new alien(), 1556 , 77 ); addObject( new alien(), 1488 , 45 ); addObject( new alien(), 30 , 732 ); addObject( new alien(), 42 , 663 ); addObject( new alien(), 94 , 726 ); } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * The enemy ship that chases the player. * * @author Austin Prokopishin * @version 1.0 */ public class alien extends Actor { GifImage gifImage = new GifImage( "AlienShip.gif" ); /** * Act - do whatever the alien wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage(gifImage.getCurrentImage()); goTo(); if (isTouching(LazerBolt. class )) { removeTouching(LazerBolt. class ); death(); } } public void goTo() { Player player = (Player)getWorld().getObjects(Player. class ).get( 0 ); turnTowards(player.getX(),player.getY()); move( 2 ); } public void death() { getWorld().addObject ( new explosion3(), getX(), getY()); getWorld().removeObject( this ); } } |