// Set up environment
gravity = 0
friction = 0
walls = false
// Create ball and paddles
let ball = circle(width / 2, height / 2, 50)
let player1 = rect(30, height / 2, 20, 100)
let player2 = rect(width - 30, height / 2, 20, 100)
// Keep track of score
let score1 = 0
let score2 = 0
display(100, 50, score1)
display(width - 100, 50, score2)
// Keep track of power moves
let player1PowerMoves = 2
let player2PowerMoves = 2
display(150, 50, player1PowerMoves)
display(width - 150, 50, player2PowerMoves)
// Start the game
function startGame() {
ball.stop()
ball.x = width / 2
ball.y = height / 2
let pushX = (random(0, 1) - 0.5) * random(10, 20)
let pushY = random(-5, 5)
ball.push(pushX, pushY)
}
on('click', startGame)
// Move the paddles
function movePaddle(key) {
if (key == 'w') {
player1.y = player1.y - 5
}
if (key == 's') {
player1.y = player1.y + 5
}
if (key == 'ArrowUp') {
player2.y = player2.y - 5
}
if (key == 'ArrowDown') {
player2.y = player2.y + 5
}
}
on('keydown', movePaddle)
// Check if ball has moved off screen
function checkBallPosition() {
if (ball.x > width) {
score1 = score1 + 1
startGame();
}
if (ball.x < 0) {
score2 = score2 + 1
startGame();
}
}
ball.on('move', checkBallPosition)
// Start "Extender" power move for Player 1
function startPlayer1Extender() {
if (player1PowerMoves > 0) {
player1.height = 200
setTimeout(endPlayer1Extender, 5000)
player1PowerMoves = player1PowerMoves - 1
}
}
// End "Extender" power move for Player 1
function endPlayer1Extender() {
player1.height = 100
}
on('a', startPlayer1Extender)
// Start "Extender" power move for Player 2
function startPlayer2Extender() {
if (player2PowerMoves > 0) {
player2.height = 200
setTimeout(endPlayer2Extender, 5000)
player2PowerMoves = player2PowerMoves - 1
}
}
on('arrowright', startPlayer2Extender)
// End "Extender" power move for Player 2
function endPlayer2Extender() {
player2.height = 100
}
// Start "Fire Ball" power move for Player 1
function startPlayer1FireBall() {
if (player1PowerMoves > 0) {
ball.speed = ball.speed * 2
ball.color = 'red'
setTimeout(endFireBall, 2000)
player1PowerMoves = player1PowerMoves - 1
}
}
on('d', startPlayer1FireBall)
// Start "Fire Ball" power move for Player 2
function startPlayer2FireBall() {
if (player2PowerMoves > 0) {
ball.speed = ball.speed * 2
ball.color = 'red'
setTimeout(endFireBall, 2000)
player2PowerMoves = player2PowerMoves - 1
}
}
on('arrowleft', startPlayer2FireBall)
// End "Fire Ball" power move
function endFireBall() {
ball.speed = ball.speed / 2
ball.color = 'default'
}