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

2017/11/28

how do I display a gameover screen if my actor falls below the last platform?

Brantgarron Brantgarron

2017/11/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class doodler here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class doodler  extends Actor
{
    float ys, xs;
    int x,y;
    boolean canMove;
    GreenfootImage left = new GreenfootImage("doodler.png");
    GreenfootImage right = new GreenfootImage("doodler.png");
    GreenfootImage shooting = new GreenfootImage("shooting.png");
    ground Ground = new ground();
    int scrollSpeed;
    int hits = 0;
    /**
     * Act - do whatever the doodler wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public doodler()
    {
        left.mirrorHorizontally();
        shooting.scale(25,40);
        canMove = true;
    }

    protected void addedToWorld(World world)
    {
        x = getX();
        y = getY();
    }

    public doodler(boolean moveable)
    {
        canMove = moveable;
    }

    public void act() 
    {
        x = getX();
        y = getY();

        ((doodleWorld) getWorld()).doodleX = getX();

        if(((doodleWorld) getWorld()).fall==false)
        {
            bounce();
        }
        if(canMove)
            keys();

        if(ys>11)
            ys = 10;

        setLocation(x+(int)xs, y+(int)ys);

        gravity();
        wrapAround();

        if(canMove & y < 400)
            scrollUp();

        if(y>400)
            fall();

        ((doodleWorld) getWorld()).height = hits;

        watch();
    }    

    public void gravity()
    {
        ys += 0.3f;
    }

    public void bounce()
    {
        ground Ground = (ground) getOneIntersectingObject(ground.class);
        if(Ground != null & ys > 0)
        {
            ys = -10;
            
            if(canMove & !Ground.hasBeenBounced)
            {
                Ground.hasBeenBounced=true;
                {
                    getWorld().addObject(new ground(), Greenfoot.getRandomNumber(300), 0);
                    getWorld().addObject(new ground(), Greenfoot.getRandomNumber(300), 70);
                }
            }
        }
    }

    public void keys()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            xs += 0.25f;
            setImage(right);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            xs -= 0.25f;
            setImage(left);
        }
        if(!Greenfoot.isKeyDown("left") & !Greenfoot.isKeyDown("right") & xs != 0)
        {
            xs = 0;
        }
        if(Greenfoot.isKeyDown("space") & getWorld().getObjects(ammo.class).isEmpty())
        {
            setImage(shooting);
            getWorld().addObject(new ammo(), x, y);
        }
    }

    public void wrapAround()
    {
        if(x>300)
        {
            setLocation(0, getY());
        }
        if(x<0)
        {
            setLocation(300,getY());
        }
    }

    public void scrollUp()
    {
        if(y<=200 & y>100)
        {
            ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
            ((doodleWorld) getWorld()).scroll = true;
            hits++;
        }
        else if(y<=100)
        {
            ((doodleWorld) getWorld()).scrollSpeed = (int) -ys*2;
            ((doodleWorld) getWorld()).scroll = true;
            hits++;
        }

        else
        {
            ((doodleWorld) getWorld()).scroll = false;
        }
    }

    public void fall()
    {
        ((doodleWorld) getWorld()).fall = true;
        ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
        
    }

    public void watch()
    {
        if(y<0 & getWorld().getObjects(arrow.class).isEmpty())
        {
            getWorld().addObject(new arrow(), getX(), 20);
        }
        if(y>0 & !getWorld().getObjects(arrow.class).isEmpty())
        {
            getWorld().removeObjects(getWorld().getObjects(arrow.class));
        }
    }
}
danpost danpost

2017/11/28

#
With the information given, the best I can do is to use something like this:
if (<< condition to show game over screen >>)
{
    Greenfoot.setWorld(new << game over world subclass name >>());
}
You just add a game over actor to display the screen, but then you may have to deal with the actions of the actors already in the world.
Brantgarron Brantgarron

2017/11/29

#
danpost wrote...
With the information given, the best I can do is to use something like this:
if (<< condition to show game over screen >>)
{
    Greenfoot.setWorld(new << game over world subclass name >>());
}
You just add a game over actor to display the screen, but then you may have to deal with the actions of the actors already in the world.
@danpost is there any more information I need to supply. What I am trying to do is have the game over be its own screen with nothing behind it displaying the score the user got
danpost danpost

2017/11/29

#
Brantgarron wrote...
is there any more information I need to supply. What I am trying to do is have the game over be its own screen with nothing behind it displaying the score the user got
Well, you need to create a subclass of World for the game over screen; also, you will need to add the final score text to that world (either onto the background or by use of an actor). Easiest way to accomplish that is to break line 3 above into the following:
World endWorld = new << whatever the game over world subclass name is >>();
Greenfoot.setWorld(endWorld);
so you can add the appropriate text to 'endWorld' right there.
You need to login to post a reply.