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
MatheMagician MatheMagician

2012/5/28

#
if(getX() >= 3 && getX() <=5 && getY() ==3)    
{    
     //whatever code you choose    
     Greenfoot.stop();    
}
DarrenM2012 DarrenM2012

2012/5/28

#
Thanks for the help. much appreciated.
danpost danpost

2012/5/28

#
@DarrenM2012, what is the cell-size of your world (the third parameter in the 'super(int, int, int)' statement in your world class?
DarrenM2012 DarrenM2012

2012/5/28

#
(20, 16, 30)
danpost danpost

2012/5/28

#
OK, that means if either of the two win location is occupied, then display the 'You Win' text object. MatheMagician had assumed that you cell-size was not pixel-sized, but you had not informed us of that fact until I had asked. You, obviously, cannot go with the idea of placing the transparent 'You Win' covering the exits. We are left with brute detection (which is not so bad, in this case):
if (getX() == winX1 && getY() == winY1 || getX() == winX2 && getX() == winY2) 
{
    // insert code to execute when game is over here
}
MatheMagician MatheMagician

2012/5/28

#
Yeah, I assumed since you only had the mummies in your other discussion page cover three cells, it would be pretty ridiculous to have a mummy cover just three pixels of space.
DarrenM2012 DarrenM2012

2012/5/28

#
I'm having trouble trying to get the world to display text as suggested by MatheMagician. In the character class I have:
      if(getX() == 17 && getY()==15)  
    {  
         getWorld()).board(); 
         Greenfoot.stop();  
    }  
    else if(getX() == 18 && getY()==15)  
    {  
          getWorld()).board(); 
          Greenfoot.stop();  
    }  
in the world subclass I have
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);  
      
    }  
The following line has an error when compiling
    public static final float FONT_SIZE = 48.0f;  
MatheMagician MatheMagician

2012/5/28

#
Sorry, could you post your entire world's code and I will fix it. I think I know what went wrong (and it was my fault).
DarrenM2012 DarrenM2012

2012/5/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
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);  
      
    }  

/**
 * A Background.
 * 
 * @author () 
 * @version (02/05/2012)
 */
public class background  extends World
{

    
    public background()
    {    
        super(20, 16, 30);// 
        setBackground("background2.jpg");
        populate();//add objects to background
    }
    
void populate()//add objects to the background area
{
    addObject(new Wall(),0,0);
    addObject(new Wall(),0,1);
    addObject(new Wall(),0,2);
    addObject(new Wall(),0,3);
    addObject(new Wall(),0,4);
    addObject(new Wall(),0,5);
    addObject(new Wall(),0,6);
    addObject(new Wall(),0,7);
    addObject(new Wall(),0,8);
    addObject(new Wall(),0,9);
    addObject(new Wall(),0,10);
    addObject(new Wall(),0,11);
    addObject(new Wall(),0,12);
    addObject(new Wall(),0,13);
    addObject(new Wall(),0,14);
    addObject(new Wall(),0,15);
    addObject(new Wall(),0,16);
    addObject(new Wall(),0,17);
    addObject(new Wall(),0,18);
    addObject(new Wall(),0,19);
    addObject(new Wall(),1,0);
    addObject(new Wall(),2,0);
    addObject(new Wall(),3,0);
    addObject(new Wall(),4,0);
    addObject(new Wall(),5,0);
    addObject(new Wall(),6,0);
    addObject(new Wall(),7,0);
    addObject(new Wall(),8,0);
    addObject(new Wall(),9,0);
    addObject(new Wall(),10,0);
    addObject(new Wall(),11,0);
    addObject(new Wall(),12,0);
    addObject(new Wall(),13,0);
    addObject(new Wall(),14,0);
    addObject(new Wall(),15,0);
    addObject(new Wall(),16,0);
    addObject(new Wall(),17,0);
    addObject(new Wall(),18,0);
    addObject(new Wall(),19,0);
    addObject(new Wall(),19,1);
    addObject(new Wall(),19,2);
    addObject(new Wall(),19,3);
    addObject(new Wall(),19,4);
    addObject(new Wall(),19,5);
    addObject(new Wall(),19,6);
    addObject(new Wall(),19,7);
    addObject(new Wall(),19,8);
    addObject(new Wall(),19,9);
    addObject(new Wall(),19,10);
    addObject(new Wall(),19,11);
    addObject(new Wall(),19,12);
    addObject(new Wall(),19,13);
    addObject(new Wall(),19,14);
    addObject(new Wall(),19,15);
    addObject(new Wall(),19,16);
    addObject(new Wall(),19,17);
    addObject(new Wall(),19,18);
    addObject(new Wall(),19,19);
    addObject(new Wall(),1,19);
    addObject(new Wall(),2,19);
    addObject(new Wall(),3,19);
    addObject(new Wall(),4,19);
    addObject(new Wall(),5,19);
    addObject(new Wall(),6,19);
    addObject(new Wall(),7,19);
    addObject(new Wall(),8,19);
    addObject(new Wall(),9,19);
    addObject(new Wall(),10,19);
    addObject(new Wall(),11,19);
    addObject(new Wall(),12,19);
    addObject(new Wall(),13,19);
    addObject(new Wall(),14,19);
    addObject(new Wall(),15,19);
    addObject(new Wall(),16,19);
    addObject(new Character(),1,1);
    addObject(new Key(),18,10);
    addObject(new Door(),17,17);
    addObject(new Door(),18,17);
    addObject(new InsideWallR(),18,11);
    addObject(new InsideWallR(),18,9);
    addObject(new InsideWall(),8,7);
    addObject(new Key(),9,7);
    addObject(new InsideWall(),10,7);
    addObject(new Mummie2(9,8),9,8);
    addObject(new Mummie2(9,6),9,6);
    addObject(new InsideWall(),5,1);
    addObject(new Key(),6,1);
    addObject(new InsideWall(),7,1);
    addObject(new Mummie2(6,2),6,2);
    addObject(new InsideWall(),10,1);
    addObject(new Key(),11,1);
    addObject(new InsideWall(),12,1);
    addObject(new Mummie2(11,2),11,2);
    addObject(new InsideWallRRR(),2,14);
    addObject(new Key(),3,14);
    addObject(new InsideWallRRR(),4,14);
    addObject(new Mummie2(3,13),3,13);
    addObject(new InsideWall(),15,1);
    addObject(new Key(),16,1);
    addObject(new InsideWall(),17,1);
    addObject(new Mummie2(16,2),16,2);
    addObject(new InsideWallR(),18,4);
    addObject(new Key(),18,5);
    addObject(new InsideWallR(),18,6);
    addObject(new Mummie1(),17,5);
    addObject(new InsideWallRR(),1,4);
    addObject(new Key(),1,5);
    addObject(new InsideWallRR(),1,6);
    addObject(new InsideWallRR(),1,9);
    addObject(new Key(),1,10);
    addObject(new InsideWallRR(),1,11);
    addObject(new Mummie1(),2,5);   
    addObject(new Mummie1(),2,10);
    addObject(new InsideWallRRR(),7,14);
    addObject(new Key(),8,14);
    addObject(new InsideWallRRR(),9,14);
    addObject(new InsideWallRRR(),12,14);
    addObject(new Key(),13,14);
    addObject(new InsideWallRRR(),14,14);
    addObject(new Mummie2(8,13),8,13);
    addObject(new Mummie2(13,13),13,13);
    addObject(new Mummie1(),17,10);
    
    
 
    
    
    
}
}















MatheMagician MatheMagician

2012/5/28

#
I'm so sorry, I was not specific enough about where to put the code. You put the imports in right, but put the rest inside of public class background, i.e.
import greenfoot.*;
import java.awt.Color;    
import java.awt.Font; 

/** 
 * A Background. 
 *  
 * @author ()  
 * @version (02/05/2012) 
 */  
public class background  extends World  
{  
  
      
    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);    
        
    }  
	public background()  
    {      
        super(20, 16, 30);//   
        setBackground("background2.jpg");  
        populate();//add objects to background  
    }  
      
void populate()//add objects to the background area  
{  
    addObject(new Wall(),0,0);  
    addObject(new Wall(),0,1);  
    addObject(new Wall(),0,2);  
    addObject(new Wall(),0,3);  
    addObject(new Wall(),0,4);  
    addObject(new Wall(),0,5);  
    addObject(new Wall(),0,6);  
    addObject(new Wall(),0,7);  
    addObject(new Wall(),0,8);  
    addObject(new Wall(),0,9);  
    addObject(new Wall(),0,10);  
    addObject(new Wall(),0,11);  
    addObject(new Wall(),0,12);  
    addObject(new Wall(),0,13);  
    addObject(new Wall(),0,14);  
    addObject(new Wall(),0,15);  
    addObject(new Wall(),0,16);  
    addObject(new Wall(),0,17);  
    addObject(new Wall(),0,18);  
    addObject(new Wall(),0,19);  
    addObject(new Wall(),1,0);  
    addObject(new Wall(),2,0);  
    addObject(new Wall(),3,0);  
    addObject(new Wall(),4,0);  
    addObject(new Wall(),5,0);  
    addObject(new Wall(),6,0);  
    addObject(new Wall(),7,0);  
    addObject(new Wall(),8,0);  
    addObject(new Wall(),9,0);  
    addObject(new Wall(),10,0);  
    addObject(new Wall(),11,0);  
    addObject(new Wall(),12,0);  
    addObject(new Wall(),13,0);  
    addObject(new Wall(),14,0);  
    addObject(new Wall(),15,0);  
    addObject(new Wall(),16,0);  
    addObject(new Wall(),17,0);  
    addObject(new Wall(),18,0);  
    addObject(new Wall(),19,0);  
    addObject(new Wall(),19,1);  
    addObject(new Wall(),19,2);  
    addObject(new Wall(),19,3);  
    addObject(new Wall(),19,4);  
    addObject(new Wall(),19,5);  
    addObject(new Wall(),19,6);  
    addObject(new Wall(),19,7);  
    addObject(new Wall(),19,8);  
    addObject(new Wall(),19,9);  
    addObject(new Wall(),19,10);  
    addObject(new Wall(),19,11);  
    addObject(new Wall(),19,12);  
    addObject(new Wall(),19,13);  
    addObject(new Wall(),19,14);  
    addObject(new Wall(),19,15);  
    addObject(new Wall(),19,16);  
    addObject(new Wall(),19,17);  
    addObject(new Wall(),19,18);  
    addObject(new Wall(),19,19);  
    addObject(new Wall(),1,19);  
    addObject(new Wall(),2,19);  
    addObject(new Wall(),3,19);  
    addObject(new Wall(),4,19);  
    addObject(new Wall(),5,19);  
    addObject(new Wall(),6,19);  
    addObject(new Wall(),7,19);  
    addObject(new Wall(),8,19);  
    addObject(new Wall(),9,19);  
    addObject(new Wall(),10,19);  
    addObject(new Wall(),11,19);  
    addObject(new Wall(),12,19);  
    addObject(new Wall(),13,19);  
    addObject(new Wall(),14,19);  
    addObject(new Wall(),15,19);  
    addObject(new Wall(),16,19);  
    addObject(new Character(),1,1);  
    addObject(new Key(),18,10);  
    addObject(new Door(),17,17);  
    addObject(new Door(),18,17);  
    addObject(new InsideWallR(),18,11);  
    addObject(new InsideWallR(),18,9);  
    addObject(new InsideWall(),8,7);  
    addObject(new Key(),9,7);  
    addObject(new InsideWall(),10,7);  
    addObject(new Mummie2(9,8),9,8);  
    addObject(new Mummie2(9,6),9,6);  
    addObject(new InsideWall(),5,1);  
    addObject(new Key(),6,1);  
    addObject(new InsideWall(),7,1);  
    addObject(new Mummie2(6,2),6,2);  
    addObject(new InsideWall(),10,1);  
    addObject(new Key(),11,1);  
    addObject(new InsideWall(),12,1);  
    addObject(new Mummie2(11,2),11,2);  
    addObject(new InsideWallRRR(),2,14);  
    addObject(new Key(),3,14);  
    addObject(new InsideWallRRR(),4,14);  
    addObject(new Mummie2(3,13),3,13);  
    addObject(new InsideWall(),15,1);  
    addObject(new Key(),16,1);  
    addObject(new InsideWall(),17,1);  
    addObject(new Mummie2(16,2),16,2);  
    addObject(new InsideWallR(),18,4);  
    addObject(new Key(),18,5);  
    addObject(new InsideWallR(),18,6);  
    addObject(new Mummie1(),17,5);  
    addObject(new InsideWallRR(),1,4);  
    addObject(new Key(),1,5);  
    addObject(new InsideWallRR(),1,6);  
    addObject(new InsideWallRR(),1,9);  
    addObject(new Key(),1,10);  
    addObject(new InsideWallRR(),1,11);  
    addObject(new Mummie1(),2,5);     
    addObject(new Mummie1(),2,10);  
    addObject(new InsideWallRRR(),7,14);  
    addObject(new Key(),8,14);  
    addObject(new InsideWallRRR(),9,14);  
    addObject(new InsideWallRRR(),12,14);  
    addObject(new Key(),13,14);  
    addObject(new InsideWallRRR(),14,14);  
    addObject(new Mummie2(8,13),8,13);  
    addObject(new Mummie2(13,13),13,13);  
    addObject(new Mummie1(),17,10);  
      
      
   
      
      
      
}  
}
DarrenM2012 DarrenM2012

2012/5/28

#
Yes thanks that's all fine. now there's an issue with the get world and board thing within the character, sorry I only really know the basics.
      if(getX() == 17 && getY()==15)  
    {  
         getWorld()).board(); 
         Greenfoot.stop();  
    }  
    else if(getX() == 18 && getY()==15)  
    {  
          getWorld()).board(); 
          Greenfoot.stop();  
    }  
MatheMagician MatheMagician

2012/5/28

#
You appear to have miscopied my code: It is supposed to say:
if(getX() == 17 && getY()==15)    
{    
     ((background)getWorld()).board();   
     Greenfoot.stop();    
}    
else if(getX() == 18 && getY()==15)    
{    
      ((background)getWorld()).board();   
      Greenfoot.stop();    
} 
You said:
if(getX() == 17 && getY()==15)    
{    
     getWorld()).board();   
     Greenfoot.stop();    
}    
else if(getX() == 18 && getY()==15)    
{    
      getWorld()).board();   
      Greenfoot.stop();    
} 
DarrenM2012 DarrenM2012

2012/5/28

#
Sorry to be a annoying, but whilst compiling an error occurred within the background world subclass regarding .draw text.
cannot find symbol - method drawText.
DarrenM2012 DarrenM2012

2012/5/28

#
Sorry to be annoying, but whilst compiling an error occurred within the background world subclass regarding .draw text. do I have to declare it?
cannot find symbol - method drawText.
danpost danpost

2012/5/28

#
I believe the correct method name is 'drawString'.
There are more replies on the next page.
1
2
3