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

2018/12/10

How to end a game when a specific score is reached?

hmhmhn hmhmhn

2018/12/10

#
ive got a simple two player pong game that I need to display a gameover image on when one player hits 8 points, I want to add code into the world class to do this but I don't know what code to add??
danpost danpost

2018/12/10

#
hmhmhn wrote...
ive got a simple two player pong game that I need to display a gameover image on when one player hits 8 points, I want to add code into the world class to do this but I don't know what code to add??
What code are you working with and what have you tried?
Nate2002 Nate2002

2018/12/10

#
Try this:

if (score == 8) 
        {
            music.stop();

            Greenfoot.setWorld(new P1Win());
            
        }


Put this in your act method and replace P1Win with the name of your new world in which it will show the message that says you won.
Nate2002 Nate2002

2018/12/10

#
If you don't have music comment the "music.stop()" out.
hmhmhn hmhmhn

2018/12/11

#
this is my code for my world class (pongWorld)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PongWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PongWorld  extends World
{
    private Paddle paddle;
    public Paddle paddle1;
    public Paddle paddle2;
    public Counter score1;
    public Counter score2;

    /**
     * Constructor for objects of class PongWorld.
     * 
     */
    public PongWorld()
    {    
        // Create a new world with 20x20 cells with a cell size of 10x10 pixels.
        super(700, 500, 1);
        
        
        score1=new Counter();
        score2= new Counter();
        addObject (score1, 50, 35);
        addObject(score2,50,465);

        paddle1 = new Paddle();
        addObject ( paddle1, getWidth() / 2, getHeight() - 40);
        paddle2 = new Paddle("a","s");
        addObject ( paddle2, getWidth() / 2, getHeight() - 450);

        addObject(new Ball(), 350, 250);
        
        GreenfootImage bg = new GreenfootImage("pink.jpg");
        bg.scale(getWidth(), getHeight());
        setBackground(bg);
    }

    public void scoreTop()
    {
        score1.add(1);

    }

    public void scoreBottom()
    {
        score2.add(1);

    }

    public void act()
    {
      
    }
}
and this is the code for my counter class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael Kölling 
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
and my ball class is pretty simple: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ball here. * * @author (your name) * @version (a version number or a date) */ public class Ball extends Actor { private int hSpeed=3, vSpeed=5; public static int score1=0; public void act() { movement(); getWorld().addObject(new Trail(),getX(),getY()); Actor paddle = getOneIntersectingObject(Paddle.class); if( paddle!=null) { vSpeed=-vSpeed; } } public void movement() { setLocation(getX()+hSpeed,getY()+vSpeed); if(getX()<0) { vSpeed=-vSpeed; } if(getY()<5) { ( (PongWorld) getWorld()).scoreBottom(); reset(); } if(getX()>getWorld().getWidth()-5) { hSpeed=-hSpeed; } if(getY()>=getWorld().getHeight()-5) { ((PongWorld)getWorld()).scoreTop(); reset(); } } public void reset() { int ballStartX = getWorld().getWidth()/2; int ballStartY = getWorld().getHeight()/2; setLocation(ballStartX, ballStartY); } } I just don't know how to make the game stop at 8 points, because ive used strings for the score im guessing I have to do it in an act method in the world class??
danpost danpost

2018/12/11

#
hmhmhn wrote...
<< Code Omitted >> I just don't know how to make the game stop at 8 points, because ive used strings for the score im guessing I have to do it in an act method in the world class??
I am not sure what you mean by "ive used strings for the score". The getValue method of the Counter class returns an int value which is a score. Best places to check for game over is when you add to the scores (lines 47 and 53 of your PongWorld class).
You need to login to post a reply.