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

2016/7/2

One Player Pong

Octavius Octavius

2016/7/2

#
Hello Greenfoot, I am working on a Pong game that I call "King Pong". I have many different modes, but the real problem lies in the 1 Player mode. When I play against the computer in one player mode, it is impossible to win, unless the ball goes through the opponents paddle (a glitch). I need some code that will fix the problem with the ball going through the paddle and I need to set a "speed limit" to the computer's paddle so that it is possible to pass the paddle by hitting at an angle. How could I do this? Here is my code for the World.
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;
    GreenfootSound sound = new GreenfootSound("menu.mp3");
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public PongWorld()
    {    
        super(800, 600, 1);
        
        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();
        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;
        
        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerTwo(ball), 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("Narration1.mp3");
        sound.play();
        addObject(new StoryModeAnimation(), 400, 300);
        removeObjects(getObjects(Title.class));
        removeObjects(getObjects(Button.class));
    }
    public void gameOver()
    {
        int PlayerOne = ball.player1Score;
        int PlayerTwo = ball.player2Score;

        if (PlayerOne > PlayerOne)
        {
            addObject(new WinScreen("Player One Wins!"), 400, 300);
        } else {
            addObject(new WinScreen("Player Two Wins!"), 400, 300);
        }
        removeObjects(getObjects(Ball.class));
        removeObjects(getObjects(Scoreboard.class));
        removeObjects(getObjects(SuperPaddle.class));
        removeObjects(getObjects(Button.class));
    }
}
Here is the code for the Ball.
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);
        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;
            }
            if (paddle2 != null)
            {
                xVector = xVector - 1;
            }
        }
    }

    public void wallCollision()
    {
        if (getX() < 2 || getX() > 798)
        {
            xVector = -xVector;
        }
        if (getY() < 2 || getY() > 598)
        {
            yVector = -yVector;
        }
    }
}
Here is the code for the SuperPaddle class (underneath it is the paddles)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class SuperPaddle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SuperPaddle extends Actor
{
    /**
     * Act - do whatever the SuperPaddle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //
    } 
    
    
    public void checkKeyPress(String upKey, String downKey, String leftKey, String rightKey)
    {
        if (Greenfoot.isKeyDown(upKey))
        {
            setLocation(getX(), getY()-8);
        }
        if (Greenfoot.isKeyDown(downKey))
        {
            setLocation(getX(), getY()+8);
        }
    }
}
Here is the code for the paddle for PlayerOne.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Paddle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerOne extends SuperPaddle
{
    /**
     * Act - do whatever the Paddle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeyPress("w", "s", "a", "d");
    }
    public PlayerOne()
    {
        GreenfootImage image = new GreenfootImage(10, 100);
        image.setColor(Color.WHITE);
        image.fill();
        setImage(image);
    }
}
Here is my code for the paddle for PlayerTwo.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Paddle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerTwo extends SuperPaddle
{
    Ball ball;
    /**
     * Act - do whatever the Paddle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        paddleMotion();
    } 

    public PlayerTwo()
    {
        GreenfootImage image = new GreenfootImage(10, 100);
        image.setColor(Color.WHITE);
        image.fill();
        setImage(image);
    }

    public PlayerTwo(Ball ball)
    {
        this.ball = ball;
        GreenfootImage image = new GreenfootImage(10, 100);
        image.setColor(Color.WHITE);
        image.fill();
        setImage(image);
    }

    private void paddleMotion()
    {
        if (ball != null)
        {
            if (ball.getX() < 150)
            {
                Greenfoot.delay(1);
            } else {
                setLocation(getX(), ball.getY());
            }
        } else {
            checkKeyPress("up", "down", "left", "right");
        }
    }
}
That is all the code that you will need. I have other components like the scoreboard and the title and buttons, but those are irrelevant. I know this is very long, but I want to make sure that you have all the information you need. Thank you.
Super_Hippo Super_Hippo

2016/7/2

#
Line 48 in the PlayerTwo class sets the paddle to the y-position of the ball. Instead, you could check if the paddle is above or below the ball and move in the direction of the ball. The glitch that the ball moves through the paddle could be caused by too small images for the paddle relative to the speed of the ball. So let's say the ball moves 10 cells each act-cycle and the paddle is only 5 cells thick. The ball could move "through it" without detecting it.
Octavius Octavius

2016/7/2

#
So, to check if the paddle is above or below the ball, would this work?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Paddle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerTwo extends SuperPaddle
{
    Ball ball;
    PlayerTwo playertwo;
    /**
     * Act - do whatever the Paddle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        paddleMotion();
    } 

    public PlayerTwo()
    {
        GreenfootImage image = new GreenfootImage(10, 100);
        image.setColor(Color.WHITE);
        image.fill();
        setImage(image);
    }

    public PlayerTwo(Ball ball, PlayerTwo playertwo)
    {
        this.ball = ball;
        this.playertwo = playertwo;
        GreenfootImage image = new GreenfootImage(10, 100);
        image.setColor(Color.WHITE);
        image.fill();
        setImage(image);
        if (ball.getY() < playertwo.getY())
        {
            setLocation(getX(), getY()-8);
        }
        if (ball.getY() > playertwo.getY())
        {
            setLocation(getX(), getY()+8);
        }
    }

    private void paddleMotion()
    {
        if (ball != null)
        {
            if (ball.getX() < 150)
            {
                Greenfoot.delay(1);
            }
        } else {
            checkKeyPress("up", "down", "left", "right");
        }
    }
}
That was PaddleTwo's code. Here is the 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;
    GreenfootSound sound = new GreenfootSound("menu.mp3");
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public PongWorld()
    {    
        super(800, 600, 1);
        
        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();
        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;
        
        addObject(new PlayerOne(), 30, 300);
        addObject(new PlayerTwo(ball, playertwo), 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));       
    }
    public void gameOver()
    {
        int PlayerOne = ball.player1Score;
        int PlayerTwo = ball.player2Score;

        if (PlayerOne > PlayerOne)
        {
            addObject(new WinScreen("Player One Wins!"), 400, 300);
        } else {
            addObject(new WinScreen("Player Two Wins!"), 400, 300);
        }
        removeObjects(getObjects(Ball.class));
        removeObjects(getObjects(Scoreboard.class));
        removeObjects(getObjects(SuperPaddle.class));
        removeObjects(getObjects(Button.class));
    }
}
The game says that there are no syntax errors, but when I try to load the One Player Mode, I get this error message:
java.lang.NullPointerException
	at PlayerTwo.<init>(PlayerTwo.java:40)
	at PongWorld.onePlayerMode(PongWorld.java:47)
	at Button.act(Button.java:36)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
Lastly, what code would I have to put in to set a speed-limit on the ball so that it doesn't go through the paddle? Thanks a lot.
Super_Hippo Super_Hippo

2016/7/2

#
You don't need to pass this 'playertwo' object. Since you are using the code in the PlayerTwo class, you can just use 'getY()' instead of 'playertwo.getY()'. The errors comes up because 'playertwo' is null (Pong World line 15) and you are trying to get the y coordinate of null. To the last question: you can decrease the speed of the ball or move half the way of its speed twice each act cycle and check for collision twice. (You can split it up more often if needed.)
You need to login to post a reply.