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

2012/5/16

Displaying Images

Matt Matt

2012/5/16

#
What code should I useif i just want to display a game over image at the end of the game
erdelf erdelf

2012/5/16

#
danpost danpost

2012/5/16

#
@Matt, that class code is flawed, so read the whole discussion carefully. I will re-post the corrected code, here (shortly).
danpost danpost

2012/5/16

#
I believe this will work (but it is not tested) Create a class (sub-class of Actor) called 'GameOver' and copy/paste the following into it (replacing what was there).
import Greenfoot.*;
import java.awt.Color;

public class GameOver extends Actor
{
    String msgTxt;
    World world;

    public GameOver(World w)
    {
        this("GAME OVER", w);
    }

    public GameOver(String txt, World w)
    {
        msgTxt = txt;
        world = w;
        updateImage();
    }

    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(world.getWidth(), world.getHeight()); // or whatever size you want the object
        image.setColor(Color.cyan); // color of your choice
        image.fill();
        GreenfootImage txtImg = new GreenfootImage(msgTxt, 36, Color.black, new Color(0, 0, 0, 0)); // colors and font size of your choice
        image.drawImage(txtImg, (image.getWidth() - txtImg.getWidth()) / 2, (image.getHeight() - txtImg.getHeight) / 2);
        // whatever else you might want to do for the image
        setImage(image);
    }
}
You add it from the world class with
addObject(new GameOver(this), getWidth() / 2, getHeight() / 2);
// above for center screen (you can adjust location, if you desire)
//             or
// for something other than 'GAME OVER' as text
addObject(new GameOver("YOU WIN", this), getWidth() / 2, getHeight() / 2);
IsVarious IsVarious

2012/5/22

#
All that code, when you could of just created an image which would be used as your "game over" screen, and add that object to the world after a certain action has been taken. in other words, create a class which will be the image you want to display, then when the action has been preformed, you simply call "addObject(new GameOver.class, x,y);
danpost danpost

2012/5/22

#
@IsVarious, that is exactly what this code does, but it has a bit of flexibility (in that the message does not neccessarily have to be 'GAME OVER').
IsVarious IsVarious

2012/5/22

#
Nice, although your approach may work better, I think sometimes simpler is easier for new programmers to grasp. In any event, two thumbs up :)
Matt Matt

2012/5/22

#
Hi, Im gonna give both methods a go, . I dont know how I would use the second method for the Winner sign as it needs to appear after a particular actor has hit a particular co-ordinate, thankyou for your help.
           if (getX() == 30 && getY() == 19)
        {           
            List getSCORE = getWorld(). getObjects (SCORE.class);
            if (!getSCORE.isEmpty())
            {
                SCORE currScore= (SCORE) getSCORE.get(0);
                currScore.updateScore(20);
            }   
            {
            addObject(new LevelComplete.class(),10,10);
            }
            Greenfoot.stop();  
        }
This however doesnt work, something to do with identifiers
danpost danpost

2012/5/22

#
It might have to do with line 10, which probably should be:
addObject(new LevelComplete(), 10, 10);
Also lines 9 and 11 need removed.
You need to login to post a reply.