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

2013/11/2

Help with this Code!!

tartags11 tartags11

2013/11/2

#
Posted this before but no one commented. Really need help. Been trying for two weeks to get this working but no success. So please help!! So I am trying to get bet() to run until a bet is selected, and then no longer run. However, it won't work. Even if the value changes to 1, 2, 3, or 4, bet doesn't run. If i try to run bet() outside of an if statement, it works but adds infinite amount of horses until Greenfoot.stop;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
public class RaceWorld extends World  
{    
          
    /** 
     * Constructor for objects of class RaceWorld. 
     *  
     */  
    public RaceWorld()  
    {      
        // Create a new world with 1200x600 cells with a cell size of 1x1 pixels.  
        super(1200, 600, 1);  
        addObject( new BetWindow(), 570, 440);  
        addObject( new HorseBet(), 1125, 490);  
        addObject( new Money(), 1110, 35);  
    }  
      
    public void act()  
    {  
        int value = ((HorseBet) getObjects(HorseBet.class).get(0)).getValue();  
        bet();  
        if ( value == 0)  
        {  
            bet();  
        }  
        else  
        {  
            //nothing  
        }  
    }  
      
    public void bet()  
    {  
        int value = ((HorseBet) getObjects(HorseBet.class).get(0)).getValue();  
        if ( value == 1)  
        {  
            removeObjects(getObjects(BetWindow.class));  
            addObject( new Horse1(), 50, 80);  
            addObject( new Horse2(), 50, 152);  
            addObject( new Horse3(), 50, 222);  
            addObject( new Horse4(), 50, 296);  
        }  
        else if ( value == 2)  
        {  
            removeObjects(getObjects(BetWindow.class));  
            addObject( new Horse1(), 50, 80);  
            addObject( new Horse2(), 50, 152);  
            addObject( new Horse3(), 50, 222);  
            addObject( new Horse4(), 50, 296);  
        }  
        else if ( value == 3)  
        {  
            removeObjects(getObjects(BetWindow.class));  
            addObject( new Horse1(), 50, 80);  
            addObject( new Horse2(), 50, 152);  
            addObject( new Horse3(), 50, 222);  
            addObject( new Horse4(), 50, 296);  
        }  
        else if ( value == 4)  
        {  
            removeObjects(getObjects(BetWindow.class));  
            addObject( new Horse1(), 50, 80);  
            addObject( new Horse2(), 50, 152);  
            addObject( new Horse3(), 50, 222);  
            addObject( new Horse4(), 50, 296);  
        }  
        else  
        {  
            //nothing  
        }  
    }  
      
    public void started()  
    {  
        //code  
    }  
}  
danpost danpost

2013/11/2

#
I think you want an 'act' method more like this:
public void act()
{
    if (!getObjects(BetWindow.class).isEmpty()) bet();
}
and a 'bet' method like this:
if (((HorseBet) getObjects(HorseBet.class).get(0)).getValue() != 0)
{
    removeObject(getObjects(BetWindow.class));
    addObject(new Horse1(), 50, 80);
    addObject(new Horse2(), 50, 152);
    addObject(new Horse3(), 50, 224);
    addObject(new Horse4(), 50, 296);
}
tartags11 tartags11

2013/11/3

#
Thank you so much!!! It works!!! Now I'm trying to change it where if a horses X value is greater than or equal to a certain value, a screen pops up saying what horse wins, the horses are removed from the screen, money is added to the money value, and if space bar is pressed, the screen will go away and would bring up the original game screen (bet window) with the money value as the new value. If anyone can guide me in the right direction on this, please tell me. Thanks!!
taky123 taky123

2013/11/3

#
the score works but then when i die the scoreboard appears and gives me a hundred points and that's not what i have help i don't have much time. scoreboard import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Font; import java.util.Calendar; /** * The ScoreBoard is used to display results on the screen. It can display some * text and several numbers. * * @author M Kolling * @version 1.0 */ public class ScoreBoard extends Actor { public static final float FONT_SIZE = 48.0f; public static final int WIDTH = 400; public static final int HEIGHT = 300; /** * Create a score board with dummy result for testing. */ public ScoreBoard() { this(100); } /** * Create a score board for the final result. */ public ScoreBoard(int score) { makeImage("Game Over", "Score: ", score); } /** * Make the score board image. */ private void makeImage(String title, String prefix, int score) { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(255,255,255, 128)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 128)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); image.drawString(title, 60, 100); image.drawString(prefix + score, 60, 200); setImage(image); } }
danpost danpost

2013/11/3

#
You are probably not passing a score to the new ScoreBoard object when you create it. That is, you are probably using 'new ScoreBoard()' instead of 'new ScoreBoard(score)'.
tartags11 tartags11

2013/11/4

#
Put this in your world:
public void act()
    {
        endOfGame();
    }
    private void endOfGame()
    {
        //whatever the value variable is
        if (value == 1) // or any number
        {
            gameOver ("Game Over");
        }
    }
    public void gameOver(String message) 
    {
        int value = ((Counter) getObjects(Counter.class).get(0)).getValue();
        addObject (new ScoreBoard ("Game Over", value), 350, 350);
        Greenfoot.stop();
    }
I recommend using a counter to keep score. If you are using something else, then just replace counter with the name of that class and anything else that is needed.
You need to login to post a reply.