|
| 1 | +/** |
| 2 | + * @name Ping Pong |
| 3 | + * @description This is a game inspired by one of the oldest arcade video |
| 4 | + * games: Atari's |
| 5 | + * <a href="https://en.wikipedia.org/wiki/Pong" target="_blank">Pong</a>. |
| 6 | + * In this two-player game, each player controls a paddle, represented by a |
| 7 | + * white rectangle. The W and S keys move the paddle on the left up and down, |
| 8 | + * and the up and down arrow keys move the paddle on the right up and down. |
| 9 | + * Players score points by bouncing the ball, represented by a white square, |
| 10 | + * past the edge of the opponent's side of the canvas. |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +let paddleLeftX = 20; |
| 15 | +let paddleLeftY = 200; |
| 16 | + |
| 17 | +let paddleRightX = 380; |
| 18 | +let paddleRightY = 200; |
| 19 | + |
| 20 | +let paddleSpeed = 2; |
| 21 | +let paddleHeight = 80; |
| 22 | +let paddleWidth = 10; |
| 23 | + |
| 24 | +let leftScore = 0; |
| 25 | +let rightScore = 0; |
| 26 | + |
| 27 | +let ballPosX = 200; |
| 28 | +let ballPosY = 200; |
| 29 | +let ballSpeedX = 0; |
| 30 | +let ballSpeedY = 0; |
| 31 | +let ballSize = 10; |
| 32 | + |
| 33 | +function setup() { |
| 34 | + createCanvas(400, 400); |
| 35 | + |
| 36 | + // Draw rectangles from their center |
| 37 | + // This makes it easier to check whether the ball is above or below the |
| 38 | + // center of a paddle |
| 39 | + rectMode(CENTER); |
| 40 | + fill(255); |
| 41 | + noStroke(); |
| 42 | + textSize(40); |
| 43 | + textAlign(CENTER); |
| 44 | + |
| 45 | + // Start paused |
| 46 | + noLoop(); |
| 47 | + describe( |
| 48 | + 'Two narrow white rectangles and a white square representing the paddles and ball in a game of ping pong. The player scores are displayed in the upper corners, and initially text reads "Click to start"' |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +function draw() { |
| 53 | + background(0); |
| 54 | + |
| 55 | + // Draw the paddles |
| 56 | + rect(paddleLeftX, paddleLeftY, paddleWidth, paddleHeight); |
| 57 | + rect(paddleRightX, paddleRightY, paddleWidth, paddleHeight); |
| 58 | + |
| 59 | + // Draw the ball |
| 60 | + square(ballPosX, ballPosY, ballSize); |
| 61 | + |
| 62 | + // Draw the score |
| 63 | + text(leftScore, width * 0.25, height * 0.1); |
| 64 | + text(rightScore, width * 0.75, height * 0.1); |
| 65 | + |
| 66 | + // Move the ball using its current speed |
| 67 | + ballPosX += ballSpeedX; |
| 68 | + ballPosY += ballSpeedY; |
| 69 | + |
| 70 | + // Store coordinates of the left paddle's collision area edges |
| 71 | + let leftCollisionLeft = paddleLeftX - paddleWidth / 2 - ballSize / 2; |
| 72 | + let leftCollisionRight = paddleLeftX + paddleWidth / 2 + ballSize / 2; |
| 73 | + let leftCollisionTop = paddleLeftY - paddleHeight / 2 - ballSize / 2; |
| 74 | + let leftCollisionBottom = paddleLeftY + paddleHeight / 2 + ballSize / 2; |
| 75 | + |
| 76 | + // If the ball is colliding with the left paddle |
| 77 | + if ( |
| 78 | + ballPosX >= leftCollisionLeft && |
| 79 | + ballPosX <= leftCollisionRight && |
| 80 | + ballPosY >= leftCollisionTop && |
| 81 | + ballPosY <= leftCollisionBottom |
| 82 | + ) { |
| 83 | + // Reverse the ball's horizontal speed |
| 84 | + ballSpeedX = -ballSpeedX; |
| 85 | + |
| 86 | + // Change the ball's vertical speed so it appears to bounce off the paddle |
| 87 | + ballSpeedY = (ballPosY - paddleLeftY) / 20; |
| 88 | + } |
| 89 | + |
| 90 | + // Store coordinates of the right paddle's collision area edges |
| 91 | + let rightCollisionLeft = paddleRightX - paddleWidth / 2 - ballSize / 2; |
| 92 | + let rightCollisionRight = paddleRightX + paddleWidth / 2 + ballSize / 2; |
| 93 | + let rightCollisionTop = paddleRightY - paddleHeight / 2 - ballSize / 2; |
| 94 | + let rightCollisionBottom = paddleRightY + paddleHeight / 2 + ballSize / 2; |
| 95 | + |
| 96 | + // If the ball is colliding with the right paddle |
| 97 | + if ( |
| 98 | + ballPosX >= rightCollisionLeft && |
| 99 | + ballPosX <= rightCollisionRight && |
| 100 | + ballPosY >= rightCollisionTop && |
| 101 | + ballPosY <= rightCollisionBottom |
| 102 | + ) { |
| 103 | + // Reverse the ball's horizontal speed |
| 104 | + ballSpeedX = -ballSpeedX; |
| 105 | + |
| 106 | + // Change the ball's vertical speed so it appears to bounce off the paddle |
| 107 | + ballSpeedY = (ballPosY - paddleRightY) / 20; |
| 108 | + } |
| 109 | + |
| 110 | + // If the ball is beyond the left edge |
| 111 | + if (ballPosX < 0) { |
| 112 | + // Give the right player a point |
| 113 | + rightScore += 1; |
| 114 | + resetBall(); |
| 115 | + |
| 116 | + // Otherwise if the ball is beyond the right edge |
| 117 | + } else if (ballPosX > width) { |
| 118 | + // Give the left player a point |
| 119 | + leftScore += 1; |
| 120 | + resetBall(); |
| 121 | + |
| 122 | + // Otherwise if the ball is hitting the top or bottom edge |
| 123 | + } else if (ballPosY < 0 || ballPosY > height) { |
| 124 | + // Reverse its vertical speed |
| 125 | + ballSpeedY = -ballSpeedY; |
| 126 | + } |
| 127 | + |
| 128 | + // Store whether W and S keys are pressed |
| 129 | + let leftDownPressed = keyIsDown(83); |
| 130 | + let leftUpPressed = keyIsDown(87); |
| 131 | + |
| 132 | + // Store how much the left paddle will move |
| 133 | + let leftMove = 0; |
| 134 | + |
| 135 | + if (leftDownPressed === true) { |
| 136 | + leftMove += paddleSpeed; |
| 137 | + } |
| 138 | + if (leftUpPressed === true) { |
| 139 | + leftMove -= paddleSpeed; |
| 140 | + } |
| 141 | + |
| 142 | + // Prevent the paddle from moving off screen |
| 143 | + paddleLeftY = constrain( |
| 144 | + paddleLeftY + leftMove, |
| 145 | + paddleHeight / 2, |
| 146 | + height - paddleHeight / 2 |
| 147 | + ); |
| 148 | + |
| 149 | + // Store whether up and down arrow keys are pressed |
| 150 | + let rightDownPressed = keyIsDown(DOWN_ARROW); |
| 151 | + let rightUpPressed = keyIsDown(UP_ARROW); |
| 152 | + |
| 153 | + // Store how much the right paddle will move |
| 154 | + let rightMove = 0; |
| 155 | + |
| 156 | + if (rightDownPressed === true) { |
| 157 | + rightMove += paddleSpeed; |
| 158 | + } |
| 159 | + if (rightUpPressed === true) { |
| 160 | + rightMove -= paddleSpeed; |
| 161 | + } |
| 162 | + |
| 163 | + // Prevent the paddle from moving off screen |
| 164 | + paddleRightY = constrain( |
| 165 | + paddleRightY + rightMove, |
| 166 | + paddleHeight / 2, |
| 167 | + height - paddleHeight / 2 |
| 168 | + ); |
| 169 | + |
| 170 | + // Show 'Click to start' if game is paused |
| 171 | + if (isLooping() === false) { |
| 172 | + text('Click to start', width / 2, height / 2 - 20); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +// Reset ball to center of canvas with random speed |
| 177 | +function resetBall() { |
| 178 | + ballPosX = width / 2; |
| 179 | + ballPosY = height / 2; |
| 180 | + ballSpeedX = random([-3, 3]); |
| 181 | + ballSpeedY = random([-1, 1]); |
| 182 | +} |
| 183 | + |
| 184 | +function mousePressed() { |
| 185 | + if (isLooping() === false) { |
| 186 | + resetBall(); |
| 187 | + loop(); |
| 188 | + } |
| 189 | +} |
0 commit comments