// Cloned by Scott Brady on 25 Oct 2022 from World "PongLand (clone by K.Ellis)" by K.Ellis
// Please leave this clone trail here.
// Cloned by K.Ellis on 5 Oct 2019 from World "PongLand" by Michael Ryan
// Please leave this clone trail here.
// Cloned by Michael Ryan on 30 Sep 2019 from World "One Cube World (P5)" by Starter user
// Please leave this clone trail here.
const PADDLE_WIDTH = 25;
const PADDLE_HEIGHT = 175;
const PADDLE_DEPTH = 1;
const BALL_WIDTH = 20;
const BALL_HEIGHT = 20;
const BOUNCE_DIRECTIONS = [-3, -2, 0, 2, 3];
const SCORE_X_POSITION = (ABWorld.fullwidth() / 2) - 40;
const SCORE_Y_POSITION = 50;
var LeftPaddleXPosition = 200;
var LeftPaddleYPosition = (ABWorld.fullheight() - PADDLE_HEIGHT) / 2;
var RightPaddleXPosition = ABWorld.fullwidth() - PADDLE_WIDTH - 200;
var RightPaddleYPosition = (ABWorld.fullheight() - PADDLE_HEIGHT) / 2;
var BallXPosition = ABWorld.fullwidth() / 2;
var BallYPosition = ABWorld.fullheight() / 2;
var BallYDirection = 0;
var BallSpeed = 5;
var PlayerOneScore = 0;
var PlayerTwoScore = 0;
function setup()
{
// Note I removed WEBGL to make this 2D
createCanvas (ABWorld.fullwidth(), ABWorld.fullheight());
}
function resetBall()
{
BallXPosition = ABWorld.fullwidth() / 2;
BallYPosition = ABWorld.fullheight() / 2;
BallSpeed = 5;
}
function checkForHits()
{
// Left Paddle
if (
BallXPosition <= (LeftPaddleXPosition + PADDLE_WIDTH) &&
BallYPosition <= (LeftPaddleYPosition + PADDLE_HEIGHT) &&
BallYPosition >= LeftPaddleYPosition) {
BallSpeed *= -1.1;
// Random Angle
BallYDirection = BOUNCE_DIRECTIONS[Math.floor(Math.random()*BOUNCE_DIRECTIONS.length)];
}
// Right Paddle
if (
BallXPosition + BALL_WIDTH >= RightPaddleXPosition &&
BallYPosition <= (RightPaddleYPosition + PADDLE_HEIGHT) &&
BallYPosition >= RightPaddleYPosition) {
BallSpeed *= -1.1;
// Random Angle
BallYDirection = BOUNCE_DIRECTIONS[Math.floor(Math.random()*BOUNCE_DIRECTIONS.length)];
}
// Left Wall
if (BallXPosition <= 0) {
PlayerTwoScore++;
resetBall();
}
// Right Wall
if (BallXPosition >= ABWorld.fullwidth()) {
PlayerOneScore++;
resetBall();
}
// Top Wall
if (BallYPosition <= 0) {
BallYDirection = -1;
}
// Bottom Wall
if (BallYPosition + BALL_HEIGHT >= ABWorld.fullheight())
{
BallYDirection = 1;
}
}
function getUserInput()
{
// W - PlayerOne Up
if(keyIsDown(87) && LeftPaddleYPosition >= 0)
{
LeftPaddleYPosition -= 10;
}
// S - PlayerOne Down
if(keyIsDown(83) && (LeftPaddleYPosition + PADDLE_HEIGHT) <= ABWorld.fullheight() )
{
LeftPaddleYPosition += 10;
}
// UpArrow - PlayerTwo Up
if(keyIsDown(38) && RightPaddleYPosition >= 0)
{
RightPaddleYPosition -= 10;
}
// S - PlayerTwo Down
if(keyIsDown(40) && (RightPaddleYPosition + PADDLE_HEIGHT) <= ABWorld.fullheight())
{
RightPaddleYPosition += 10;
}
}
function draw()
{
background("black");
fill("white");
// Score
textSize(32);
text(PlayerOneScore + '\t:\t' + PlayerTwoScore, SCORE_X_POSITION, SCORE_Y_POSITION);
// Paddle 1
rect(LeftPaddleXPosition, LeftPaddleYPosition, PADDLE_WIDTH, PADDLE_HEIGHT);
// Paddle 2
rect(RightPaddleXPosition, RightPaddleYPosition, PADDLE_WIDTH, PADDLE_HEIGHT);
// Check for hit
checkForHits();
// Get User Input
getUserInput();
// Draw Ball
rect(BallXPosition, BallYPosition, BALL_WIDTH, BALL_HEIGHT);
// Move Ball
BallXPosition -= BallSpeed;
BallYPosition -= BallYDirection;
}