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.
Here is the code for the Ball.
Here is the code for the SuperPaddle class (underneath it is the paddles)
Here is the code for the paddle for PlayerOne.
Here is my code for the paddle for PlayerTwo.
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.
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));
}
}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;
}
}
}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);
}
}
}
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);
}
}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");
}
}
}

