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

2016/7/3

Worlds in Pong

Octavius Octavius

2016/7/3

#
Hello, I am having a problem with my WinScreen in my Pong game. I have 3 modes. One player mode, two player mode, and a story mode. In one and two player mode, if you win, a screen pops up that says that you've won. In story mode though, I want a button to pop up and tell me "Next Level" instead. The problem is that when you score 10 points, the gameOver() method is called and the WinScreen pops up. How could I prevent this from happening (only in Story Mode) and show a clickable "Next Level" button instead? Here is my PongWorld code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PongWorld extends World
{
    Scoreboard scoreboard;
    Ball ball;
    PlayerTwo playertwo;
    PlayerThree playerthree;
    PlayerFour playerfour;
    PlayerFive playerfive;
    PlayerSix playersix;
    StoryModeAnimation storyMode;
    GreenfootSound sound = new GreenfootSound("menu.mp3");
    public int xVector = (Greenfoot.getRandomNumber(13)-6);
    public int yVector = (Greenfoot.getRandomNumber(13)-6);
    public int player1Score;
    public int player2Score;
    Scoreboard score;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public PongWorld()
    {    
        super(800, 600, 1);

        this.ball = ball;

        GreenfootImage background = getBackground();
        background.setColor(new Color(0,0,0));
        background.fill();

        sound.play();
        addObject(new Title(), 400, 150);
        addObject(new Button("One Player Mode"), 400, 300);
        addObject(new Button("Two Player Mode"), 400, 400);
        addObject(new Button("Story Mode"), 400, 500);
    }
    public void onePlayerMode()
    {
        sound.stop();
        removeObjects(getObjects(Button.class));
        addObject(new Button("Easy Difficulty"), 400, 250);
        addObject(new Button("Medium Difficulty"), 400, 350);
        addObject(new Button("Hard Difficulty"), 400, 450);
        addObject(new Button("BEAST MODE"), 400, 550);
    }
    public void onePlayerModeEasy()
    {
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playerthree = playerthree;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerThree(ball, playerthree), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }
    public void onePlayerModeMedium()
    {
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playerfour = playerfour;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerFour(ball, playerfour), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }
    public void onePlayerModeHard()
    {
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playertwo = playertwo;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerTwo(ball, playertwo), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }
    public void onePlayerModeBeast()
    {
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playerfive = playerfive;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerFive(ball, playerfive), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }
    public void twoPlayerMode()
    {
        sound.stop();
        GreenfootSound sound = new GreenfootSound("menu.mp3");
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        Ball ball = new Ball(scoreboard);
        addObject(scoreboard, 400, 50);
        this.ball = ball;
        addObject(new Ball(scoreboard), 400, 300);
        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerTwo(), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }

    public void storyMode()
    {
        sound.stop();
        GreenfootSound sound = new GreenfootSound("storymodeintro.mp3");
        sound.play();
        addObject(new StoryModeAnimation(), 400, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
        addObject(new Button("Continue"), 400, 500);
    }
    
    public void levelOne()
    {
        sound.stop();
        Greenfoot.playSound("intense.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playerthree = playerthree;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerThree(ball, playerthree), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
        removeObjects(getObjects(StoryModeAnimation.class));
        if (player1Score > 9)
        {
            removeObjects(getObjects(WinScreen.class));
            addObject(new ButtonNext("Next Level"), 400, 300);
        }
    }
    
    public void levelTwo()
    {
        sound.stop();
        Greenfoot.playSound("intense.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playersix = playersix;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerSix(ball, playersix), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
        removeObjects(getObjects(StoryModeAnimation.class));
    }
    
    public void gameOver()
    {
        int player1 = ball.player1Score;
        int player2 = ball.player2Score;
        this.ball = ball;
        GreenfootSound sound = new GreenfootSound("intense.mp3");
        sound.stop();
        removeObjects(getObjects(Ball.class));
        removeObjects(getObjects(Scoreboard.class));
        removeObjects(getObjects(SuperPaddle.class));
        removeObjects(getObjects(Button.class));
        if (ball.player1Score > ball.player2Score)
        {
            addObject(new WinScreen("Player One Wins!"), 400, 300);
        } else {
            addObject(new WinScreen("Player Two Wins!"), 400, 300);
        }
    }
}
Here is my Ball code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    public int xVector = (Greenfoot.getRandomNumber(13)-6);
    public int yVector = (Greenfoot.getRandomNumber(13)-6);
    public int player1Score;
    public int player2Score;
    Scoreboard score;
    public Ball(Scoreboard scoreboard)
    {
        GreenfootImage image = new GreenfootImage(38, 38);
        image.setColor(new Color(255,255,255));
        image.fillOval(0,0,38,38);
        setImage(image);
        score = scoreboard;
        player1Score = 0;
        player2Score = 0;

        while (xVector == 0 || yVector == 0)
        {
            xVector = Greenfoot.getRandomNumber(11) - 5;
            yVector = Greenfoot.getRandomNumber(11) - 5;
        }
    }

    public void scorePoints()
    {
        if (getX() < 10)
        {
            player2Score++;
            if (player2Score > 9)
            {
                PongWorld world = (PongWorld) getWorld();
                world.gameOver();
            } else {
                score.updateScore("Player One: " + player1Score, "Player Two: " + player2Score);
                setLocation(400, 300);
                Greenfoot.delay(50);
                xVector = (Greenfoot.getRandomNumber(13)-6);
                yVector = (Greenfoot.getRandomNumber(13)-6);
            }
        } else {
            if (getX() > 790)
            {
                player1Score++;
                if (player1Score > 9)
                {
                    PongWorld world = (PongWorld) getWorld();
                    world.gameOver();
                } else {
                    score.updateScore("Player One: " + player1Score, "Player Two: " + player2Score);
                    setLocation(400, 300);
                    Greenfoot.delay(50);
                    xVector = (Greenfoot.getRandomNumber(13)-6);
                    yVector = (Greenfoot.getRandomNumber(13)-6);
                }
            }
        }
    }

    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setLocation(getX(), getY());
        setLocation(getX() + xVector, getY() + yVector);
        wallCollision();
        scorePoints();
        paddleCollision();

        while (xVector == 0 || yVector == 0)
        {
            xVector = Greenfoot.getRandomNumber(11) - 5;
            yVector = Greenfoot.getRandomNumber(11) - 5;
        }
    }

    public void paddleCollision()
    {
        Actor PlayerOne = getOneIntersectingObject(SuperPaddle.class);
        Actor paddle1 = getOneIntersectingObject(PlayerOne.class);
        Actor paddle2 = getOneIntersectingObject(PlayerTwo.class);
        Actor paddle3 = getOneIntersectingObject(PlayerThree.class);
        Actor paddle4 = getOneIntersectingObject(PlayerFour.class);
        Actor paddle5 = getOneIntersectingObject(PlayerFive.class);
        Actor paddle6 = getOneIntersectingObject(PlayerSix.class);
        if (PlayerOne != null)
        {
            if (xVector < 0) 
            {
                setLocation (PlayerOne.getX() + 30, getY());
            }
            if (xVector > 0)
            {
                setLocation (PlayerOne.getX() - 30, getY());
            }
            xVector = -xVector;
            if (paddle1 != null)
            {
                xVector = xVector + 1;
                yVector = yVector + (Greenfoot.getRandomNumber(4));
            }
            if (paddle2 != null)
            {
                xVector = xVector - 1;
                yVector = yVector - (Greenfoot.getRandomNumber(4));
            }
            if (paddle3 != null)
            {
                xVector = xVector - 1;
                yVector = yVector - (Greenfoot.getRandomNumber(4));
            }
            if (paddle4 != null)
            {
                xVector = xVector - 1;
                yVector = yVector - (Greenfoot.getRandomNumber(4));
            }
            if (paddle5 != null)
            {
                xVector = xVector - 3;
                yVector = yVector - (Greenfoot.getRandomNumber(4));
            }
            if (paddle6 != null)
            {
                xVector = xVector - 10;
                yVector = yVector - (Greenfoot.getRandomNumber(4));
            }
        }
    }

    public void wallCollision()
    {
        if (getX() < 2 || getX() > 798)
        {
            xVector = -xVector;
        }
        if (getY() < 2 || getY() > 598)
        {
            yVector = -yVector;
        }
    }
}
Here is my "Next Level" Button code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Button here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ButtonNext extends Actor
{
    String mode;
    Ball ball;
    PlayerTwo playertwo;
    public ButtonNext(String mode)
    {
        this.mode = mode;

        GreenfootImage image = new GreenfootImage(800, 600);
        image.setColor(Color.WHITE);
        image.fill();
        image.setColor(Color.BLACK);
        Font font = new Font("Helvetica", Font.BOLD, 30);
        image.setFont(font);
        image.drawString(mode, 25, 40);
        setImage(image);
    }

    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this) && mode == "Next Level")
        {
            PongWorld world = (PongWorld) getWorld();
            world.levelTwo();
            GreenfootSound sound = new GreenfootSound("menu.mp3");
            sound.stop();
            Greenfoot.delay(50);
        }
    }
}
Here is my WinScreen code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;

/**
 * Write a description of class WinScreen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class WinScreen extends Actor
{
    /**
     * Act - do whatever the WinScreen wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }
    public WinScreen(String winner)
    {
        GreenfootImage img = new GreenfootImage(800, 600);
        img.setColor(Color.WHITE);
        img.fill();
        Font font = new Font("Helvetica", Font.BOLD, 30);
        img.setFont(font);
        img.setColor(Color.BLACK);
        img.drawString(winner, 275, 250);
        img.drawString("Press Reset to Play Again!", 225, 350);
        setImage(img);
        Greenfoot.playSound("menu.mp3");
    }
}
Thank you.
danpost danpost

2016/7/3

#
Just add to the condition for displaying the win screen the following condition: getWorld().getObjects(StoryModeAnimation.class).isEmpty() You can combine the two conditions using the AND operator '&&'.
Octavius Octavius

2016/7/3

#
I tried that... Like this??
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PongWorld extends World
{
    Scoreboard scoreboard;
    Ball ball;
    PlayerTwo playertwo;
    PlayerThree playerthree;
    PlayerFour playerfour;
    PlayerFive playerfive;
    PlayerSix playersix;
    StoryModeAnimation storyMode;
    GreenfootSound sound = new GreenfootSound("menu.mp3");
    public int xVector = (Greenfoot.getRandomNumber(13)-6);
    public int yVector = (Greenfoot.getRandomNumber(13)-6);
    public int player1Score;
    public int player2Score;
    Scoreboard score;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public PongWorld()
    {    
        super(800, 600, 1);

        this.ball = ball;

        GreenfootImage background = getBackground();
        background.setColor(new Color(0,0,0));
        background.fill();

        sound.play();
        addObject(new Title(), 400, 150);
        addObject(new Button("One Player Mode"), 400, 300);
        addObject(new Button("Two Player Mode"), 400, 400);
        addObject(new Button("Story Mode"), 400, 500);
    }
    public void onePlayerMode()
    {
        sound.stop();
        removeObjects(getObjects(Button.class));
        addObject(new Button("Easy Difficulty"), 400, 250);
        addObject(new Button("Medium Difficulty"), 400, 350);
        addObject(new Button("Hard Difficulty"), 400, 450);
        addObject(new Button("BEAST MODE"), 400, 550);
    }
    public void onePlayerModeEasy()
    {
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playerthree = playerthree;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerThree(ball, playerthree), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }
    public void onePlayerModeMedium()
    {
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playerfour = playerfour;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerFour(ball, playerfour), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }
    public void onePlayerModeHard()
    {
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playertwo = playertwo;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerTwo(ball, playertwo), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }
    public void onePlayerModeBeast()
    {
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playerfive = playerfive;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerFive(ball, playerfive), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }
    public void twoPlayerMode()
    {
        sound.stop();
        GreenfootSound sound = new GreenfootSound("menu.mp3");
        sound.stop();
        Greenfoot.playSound("game.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        Ball ball = new Ball(scoreboard);
        addObject(scoreboard, 400, 50);
        this.ball = ball;
        addObject(new Ball(scoreboard), 400, 300);
        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerTwo(), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }

    public void storyMode()
    {
        sound.stop();
        GreenfootSound sound = new GreenfootSound("storymodeintro.mp3");
        sound.play();
        addObject(new StoryModeAnimation(), 400, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
        addObject(new Button("Continue"), 400, 500);
    }
    
    public void levelOne()
    {
        sound.stop();
        Greenfoot.playSound("intense.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playerthree = playerthree;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerThree(ball, playerthree), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
        removeObjects(getObjects(StoryModeAnimation.class));
        if (player1Score > 9)
        {
            removeObjects(getObjects(WinScreen.class));
            addObject(new ButtonNext("Next Level"), 400, 300);
        }
    }
    
    public void levelTwo()
    {
        sound.stop();
        Greenfoot.playSound("intense.mp3");
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 50);
        Ball ball = new Ball(scoreboard);
        addObject(ball, 400, 300);

        this.ball = ball;
        this.playersix = playersix;

        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerSix(ball, playersix), 770, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
        removeObjects(getObjects(StoryModeAnimation.class));
    }
    
    public void gameOver()
    {
        int player1 = ball.player1Score;
        int player2 = ball.player2Score;
        this.ball = ball;
        GreenfootSound sound = new GreenfootSound("intense.mp3");
        sound.stop();
        removeObjects(getObjects(Ball.class));
        removeObjects(getObjects(Scoreboard.class));
        removeObjects(getObjects(SuperPaddle.class));
        removeObjects(getObjects(Button.class));
        if (ball.player1Score > ball.player2Score && getWorld().getObjects(StoryModeAnimation.class).isEmpty())
        {
            addObject(new WinScreen("Player One Wins!"), 400, 300);
        } else {
            addObject(new WinScreen("Player Two Wins!"), 400, 300);
        }
    }
}
that was in the PongWorld. When I try that, I get an error message saying "cannot find symbol - method getWorld()". How do I fix this?
Octavius Octavius

2016/7/3

#
public void gameOver()
    {
        int player1 = ball.player1Score;
        int player2 = ball.player2Score;
        this.ball = ball;
        GreenfootSound sound = new GreenfootSound("intense.mp3");
        sound.stop();
        removeObjects(getObjects(Ball.class));
        removeObjects(getObjects(Scoreboard.class));
        removeObjects(getObjects(SuperPaddle.class));
        removeObjects(getObjects(Button.class));
        if (ball.player1Score > ball.player2Score && getWorld().getObjects(StoryModeAnimation.class).isEmpty())
        {
            addObject(new WinScreen("Player One Wins!"), 400, 300);
        } else {
            addObject(new WinScreen("Player Two Wins!"), 400, 300);
        }
    }
danpost danpost

2016/7/3

#
Octavius wrote...
< Code Omitted > that was in the PongWorld. When I try that, I get an error message saying "cannot find symbol - method getWorld()". How do I fix this?
Sorry. Remove 'getWorld().' from the beginning of that code.
You need to login to post a reply.