Well, then. Here is the code:
//variable for the gamemode switch: 0 for menu, 1 for singleplayer, 2 for multiplayer
int gamemode = 0;
//repetition of the width and height variables to be able to use them in constructors
final int frameWidth = 1000;
final int frameHeight = 750;
void setup() {
size(1000, 750);
surface.setTitle("Pong");
}
PongGame pong = new PongGame();
void draw() {
background(#0B0C10);
switch(gamemode) {
case 0:
pong.displayMenu();
break;
case 1:
pong.singleplayer();
break;
case 2:
pong.multiplayer();
break;
}
}
void mousePressed() {
if (mouseX < frameWidth/2) {
gamemode = 1;
} else if (mouseX > frameWidth/2) {
gamemode = 2;
}
}
void keyPressed() {
if (keyCode == RETURN || keyCode == ENTER) {
gamemode = 0;
}
}
public class Ball {
private PVector position;
private PVector velocity;
private color colour;
private int extent;
private int radius;
public Ball(PVector initialPosition, PVector initialVelocity, int extent) {
this.position = initialPosition;
this.velocity = initialVelocity;
this.extent = extent;
this.radius = extent/2;
}
public void update() {
position.add(velocity);
fill(colour);
noStroke();
circle(position.x, position.y, extent);
}
public void bounceOff(float xBorderL, float xBorderR, float yBorderU, float yBorderD) {
if (position.x < (xBorderR + radius) && position.x > (xBorderL - radius) && position.y < (yBorderD + radius) && position.y > (yBorderU - radius)) {
velocity.x *= -1;
}
if (position.x < (xBorderR + radius) && position.x > (xBorderL - radius) && (position.y == yBorderD + radius || position.y == yBorderU - radius)) {
velocity.y *= -1;
}
}
public void bounce(int xMin, int xMax, int yMin, int yMax) {
if ((position.x < (xMin + radius) && velocity.x < 0) || (position.x > (xMax - radius) && velocity.x > 0)) {
velocity.x *= -1;
}
if ((position.y < (yMin + radius) && velocity.y < 0) || (position.y > (yMax - radius) && velocity.y > 0)) {
velocity.y *= -1;
}
}
public PVector getVelocity() {
return velocity;
}
public void setVelocity(PVector velocity) {
this.velocity = velocity;
}
public void setColor(color colour) {
this.colour = colour;
}
public PVector getPosition() {
return position;
}
public void setPosition(PVector position) {
this.position = position;
}
public int getRadius() {
return radius;
}
}
public class Paddle {
private PVector position;
private color colour;
private float[] size = new float[2];
public Paddle(PVector position, float[] size) {
this.size = size;
this.position = position;
}
public void update(PVector velocity, char upKey, char downKey) {
if (keyPressed && (key == upKey || key == Character.toLowerCase(upKey))) {
position.y += -velocity.y;
}
if (keyPressed && (key == downKey || key == Character.toLowerCase(downKey))) {
position.y += velocity.y;
}
fill(colour);
noStroke();
rect(position.x, position.y, size[0], size[1]);
}
public void update(PVector velocity, int upKey,int downKey) {
if (keyPressed && (keyCode == upKey)) {
position.y += -velocity.y;
}
if (keyPressed && (keyCode == downKey)) {
position.y += velocity.y;
}
fill(colour);
noStroke();
rect(position.x, position.y, size[0], size[1]);
}
public void setColor(color colour) {
this.colour = colour;
}
public float[] getSize() {
return size;
}
public PVector getPosition() {
return position;
}
}
public class PongGame {
private Ball ball = new Ball(new PVector(frameWidth/2, frameHeight/2), new PVector(3, 4), 25);
private Paddle paddle1 = new Paddle(new PVector(50, frameHeight/2), new float[]{20, 100});
private Paddle paddle2 = new Paddle(new PVector(frameWidth-50, frameHeight/2), new float[]{20, 100});
public Scoreboard scoreboardLeft = new Scoreboard(new PVector(50, 50), #C5C6C7);
public Scoreboard scoreboardRight = new Scoreboard(new PVector(frameWidth-50, 50), #C5C6C7);
public void singleplayer() {
scoreboardLeft.display();
ball.setColor(#66FCF1);
paddle1.setColor(#45A29E);
paddle1.update(new PVector(0, 3), 'W', 'S');
ball.bounceOff(paddle1.getPosition().x, paddle1.getPosition().x + paddle1.getSize()[0], paddle1.getPosition().y, paddle1.getPosition().y + paddle1.getSize()[1]);
ball.bounce(-25, frameWidth, 0, frameHeight);
if (ball.getPosition().x <= 0) {
ball.setPosition(new PVector(frameWidth/2, frameHeight/2));
ball.setVelocity(new PVector(ball.getVelocity().x * -1, ball.getVelocity().y));
scoreboardLeft.points -= 1;
}
ball.update();
}
public void multiplayer() {
scoreboardLeft.display();
scoreboardRight.display();
ball.setColor(#66FCF1);
paddle1.setColor(#45A29E);
paddle1.update(new PVector(0, 3), 'W', 'S');
paddle2.setColor(#45A29E);
paddle2.update(new PVector(0, 3), UP, DOWN);
ball.bounceOff(paddle1.getPosition().x, paddle1.getPosition().x + paddle1.getSize()[0], paddle1.getPosition().y, paddle1.getPosition().y + paddle1.getSize()[1]);
ball.bounceOff(paddle2.getPosition().x, paddle2.getPosition().x + paddle2.getSize()[0], paddle2.getPosition().y, paddle2.getPosition().y + paddle2.getSize()[1]);
ball.bounce(-25, frameWidth+25, 0, frameHeight);
if (ball.getPosition().x <= 0 || ball.getPosition().x >= frameWidth) {
ball.setPosition(new PVector(frameWidth/2, frameHeight/2));
ball.setVelocity(new PVector(ball.getVelocity().x * -1, ball.getVelocity().y));
if (ball.getPosition().x <= 0) {
scoreboardLeft.points -= 1;
} else if (ball.getPosition().x >= frameWidth) {
scoreboardRight.points -= 1;
}
}
ball.update();
}
public void displayMenu() {
fill(255);
stroke(255);
line(width/2, height/3, width/2, height);
textAlign(CENTER, CENTER);
textSize(50);
text("PONG", width/2, height/6);
textSize(20);
text("Start Singleplayer", width/4, height/1.6);
text("Start Multiplayer", width/1.3, height/1.6);
}
}
public class Scoreboard {
public int points = 0;
private PVector position;
private color colour;
public Scoreboard(PVector position, color colour) {
this.position = position;
this.colour = colour;
}
public void display() {
textAlign(TOP, LEFT);
fill(colour);
text("Points:", position.x, position.y);
text(points, position.x+75, position.y);
}
}
And, as I’ve mentioned before, style improvement suggestions are always welcome, too.
Thanks again
Ear1yT