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.
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);
}
}if (getObjects(Player.class).isEmpty() && getObjects(explosion2.class).isEmpty())
{
Greenfoot.setWorld(new MyWorld());
}if (getObjects(Player.class).isEmpty() && getObjects(explosion2.class).isEmpty())
{
Greenfoot.setWorld(new MyWorld());
}getWorld().removeObjects(getWorld().getObjects(alien.class));
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);
}
}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);
}
}