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

2012/5/28

How to end the game once a co-ordinate has been reached

1
2
3
DarrenM2012 DarrenM2012

2012/5/28

#
I'm trying to figure out how the game will end once a co-ordinate has been reached or if that's possible, what you have to do in my game is collect the keys from mummies guarding them, once all they keys have been retrieved the doors will open allowing you to escape. Basically I'm trying to figure out how it will end once the player has walked into the exit(removed doors).
MatheMagician MatheMagician

2012/5/28

#
You can have the world add a scoreboard (the balloon game demo shows you how to do this), or post the highscores which is explained in the helicopter game if your game has players collect points. Or you could have the world write "You won" or something like that. Post whatever you would like to do and we could be more helpful.
danpost danpost

2012/5/28

#
Its your scenario, you decide how it will end. Unless you are being vague, in that you need like a 'Game Over' display or something. Explain what you want, in detail, so we know how to respond.
DarrenM2012 DarrenM2012

2012/5/28

#
Sorry about that, I'm planning to make an image saying You win. So I would like that to display, sorry I didn't mention it earlier.
danpost danpost

2012/5/28

#
You need to create a new sub-class of Actor, and give it the image you want to display, and then add it to the world. You can create the image in a number of programs or you can create it programatically (or a little of both, using the drawImage method in the GreenfootImage class). It may sound a bit strange, but I create most of my text images in 'Microsoft Excel' and then use the 'Snipping Tool' to capture the image created.
MatheMagician MatheMagician

2012/5/28

#
If you have already made the image you could just make an actor and set its image to your image. You could then put this bit of code it your Character act cycle.
if(getX() == 3&&getY() ==3)
{
       getWorld().addObject(new Scoreboard(), getWorld().getWidth()/2,getWorld().getHeight()/2 );
       Greenfoot.stop();
}
MatheMagician MatheMagician

2012/5/28

#
Or instead you could have the world set its image to that by putting this in the Character class
if(getX() == 3&&getY() ==3)  
{  
       ((name of your games world here) getWorld()).board();
       Greenfoot.stop();  
} 
And then in the world class put
import java.awt.Color;
import java.awt.Font;


public static final float FONT_SIZE = 48.0f;
public void board()
{
         Font font = getBackground().getFont();
         font = font.deriveFont(FONT_SIZE);
         getBackground().setFont(font);
         getBackground().setColor(Color.red);
         getBackground().drawText("You win!",getWidth()/2,getHeight()/2);

}
This will draw the text on the world.
DarrenM2012 DarrenM2012

2012/5/28

#
what would I have to put if there's two sets of co-ordinations it could be.
danpost danpost

2012/5/28

#
@MatheMagician, with the code you provided, one would have to hit those exact coordinates. Better would be using '<=', at least as far as those particular coordinates are concerned. @DarrenM2012, you could box in the area that the character would have to be to win the game with coordinate comparisons, or create the 'You Win' object at the start of the game, or level (giving it a transparent image of a size covering the strike zone for winning the game, and placing it there. Then its act method could check for the character intersecting it, and when so, set the image to the 'You Win' image and relocate itself.
MatheMagician MatheMagician

2012/5/28

#
if(getX() == 3 && getY()==3)
{
     //whatever code you choose
     Greenfoot.stop();
}
else if(getX() == 2 && getY()==4)
{
      //whatever code you choose
      Greenfoot.stop();
}
danpost danpost

2012/5/28

#
DarrenM2012 wrote...
what would I have to put if there's two sets of co-ordinations it could be.
Are you saying there are two possible winning exits?
MatheMagician MatheMagician

2012/5/28

#
Sorry, I posted before I read danpost's post. Here is the code modified
DarrenM2012 DarrenM2012

2012/5/28

#
Are you saying there are two possible winning exits?
Yes but they are next to each other. I don't know why I put two doors in, only really need one. hehe.
MatheMagician MatheMagician

2012/5/28

#
if(getX() >= 3 && getX() <=5 && getY()>=3 && getY() <= 5)  
{  
     //whatever code you choose  
     Greenfoot.stop();  
}  
else if(getX() >= 2 && getX() <= 5 && getY()>=4 && getY() <= 6)  
{  
      //whatever code you choose  
      Greenfoot.stop();  
}  
MatheMagician MatheMagician

2012/5/28

#
Sorry I keep on posting while you guys are so I am a step behind. This should work Darren:
There are more replies on the next page.
1
2
3