var ballX = 200;
var ballY = 200;
var ballR = 10;
ellipseMode(RADIUS);
ellipse(ballX, ballY, ballR, ballR);
var ball = { // one variable "ball" to store multiple properties
x: 100, // colon in between property name and value
y: 200, // comma after each property name:value
r: 10 // no comma after last property
};
ellipseMode(RADIUS);
ellipse(ball.x, ball.y, ball.r, ball.r);
// Use multiple variables
var ballX = 200;
var ballY = 200;
var ballR = 10;
var drawBall = function (x, y, r) {
noStroke();
fill(255,0,0);
ellipseMode(RADIUS);
ellipse(x, y, r, r);
};
// call function with 3 arguments
drawBall(ballX, ballY, ballR);
// Use object with multiple properties
var ball = {
x: 100,
y: 200,
r: 10
};
var drawBall = function (ball) { // ball object
noStroke();
fill(255,0,0);
ellipseMode(RADIUS);
ellipse(ball.x, ball.y, ball.r, ball.r);
};
// pass 1 object argument with 3 properties
drawBall(ball);
// --- Define game objects and their properties
var ball = {
x: width/2, // middle of stage width
y: height/2, // middle of stage height
r: 10,
xVelocity: 0,
yVelocity: 0
};
var player1 = { // left paddle
x: 30,
y: height/2,
score: 0
};
var player2 = { // right paddle
x: width-30,
y: height/2,
score: 0
};
var paddle = { // shared by players
h: 30, // paddle height
w: 7, // paddle width
speed: 6
};
var YELLOW = color(255,255,0); // global color constant
rectMode(RADIUS); // global draw modes
ellipseMode(RADIUS);
var drawField = function () {
var x = width/2;
var y = height/2;
background(27, 97, 41); // green background
stroke(245, 240, 240); // white color for lines
strokeWeight(4);
line(x, 0, x, height); // vertical midline
noFill();
ellipse(x, y, 20, 20); // small circle
ellipse(x, y, 100, 100); // large circle
rect(x, y, 195, 195); // bounding rectangle
};
var draw = function () { // the Animation "Loop"
drawField();
};
var YELLOW = color(255,255,0); // global color constant
var drawField = function () { ... };
var drawScores = function() {
var offset = 50;
textAlign(CENTER);
fill(YELLOW); // text color
textSize(30);
text(player1.score, width/2 - offset, offset);
text(player2.score, width/2 + offset, offset);
};
var draw = function () { // the Animation "Loop"
drawField();
drawScores(); // add
};
var drawBall = function () {
noStroke();
fill(255, 0, 0);
ellipse(ball.__, ball.__, ball.__, ball.__);
};
var drawPaddles = function () {
noStroke();
fill(YELLOW);
rect(player1.__, player1.__, paddle.__, paddle.__, 10);
rect(player2.__, player2.__, paddle.__, paddle.__, 10);
};
// The Animation "Loop"
var draw = function () {
drawField();
drawScores();
drawPaddles(); // add
drawBall(); // add
};
// --- Define keycode constants
var KEY_W = "W".charCodeAt();
var KEY_S = "S".charCodeAt();
// -------------- Functions -----------------------------------
// Functions to capture multiple key presses at the same time.
// usage example:
// if (keys[UP]) { // arrow keycodes: UP, DOWN, LEFT, RIGHT
// ...
// }
var keys = []; // array of keys pressed
var keyPressed = function () {
keys[keyCode] = true; // set to true when key with keycode is pressed
};
var keyReleased = function () {
keys[keyCode] = false; // set to false when key with keycode is released
};
var movePaddles = function () {
// W,S keys controls player1
if (keys[KEY_W]) {
player1.y = player1.y - paddle.speed;
}
if (keys[KEY_S]) {
player1.y = player1.y + paddle.speed;
}
// UP, DOWN keys controls player2
if (keys[UP]) {
player2.y = _________ ;
}
if (keys[DOWN]) {
player2.y = _________ ;
}
};
var draw = function () {
drawField();
drawPaddles();
drawBall();
movePaddles(); // add
};
var movePaddles = function () {
// W,S keys controls player1
if (keys[KEY_W]) {
player1.y = max(player1.y - paddle.speed, paddle.h);
}
if (keys[KEY_S]) {
player1.y = min( ______ , _____);
}
// UP, DOWN keys controls player2
if (keys[UP]) {
player2.y = max( ______ , _____);
}
if (keys[DOWN]) {
player2.y = min( ______ , _____);
}
};
var draw = function () {
drawField();
drawPaddles();
drawBall();
movePaddles(); // add
};
// --- Define global game state constants
var GAME_PAUSED = 1;
var GAME_ACTIVE = 2;
var GAME_OVER = 3;
var gameState = GAME_PAUSED; // starting state
// initialize ball and player positions, and game state
var resetBall = function () {
ball.x = 200;
ball.y = 200;
ball.xVelocity = 1.5 + random(0,1); // random initial velocity
ball.yVelocity = 2 + random(0,1);
player1.y = 200;
player2.y = 200;
gameState = GAME_PAUSED; // pause game
};
var draw = function () { ... };
resetBall(); // initialize outside draw()
var moveBallAndCheckBoundaries = function () {
// move ball
ball.x += _________;
ball.y += _________;
// Check for Goal
if (ball.x < ball.r) { // left boundary
_______.score += 1;
resetBall();
} else if (ball.x > width - ______) { // right boundary
_______.score += 1;
resetBall();
}
// Bounce the ball against the top and bottom boundaries (Y coordinates)
if (ball.y < ______ || ball.y > _______ ) {
ball.yVelocity = _____________; // reverse Y direction
}
};
var showMessage = function () {
textSize(22);
fill(YELLOW); // text color
if (gameState === GAME_PAUSED) {
text("W, A UP, DOWN", 200, 300);
text("left paddle right paddle", 200, 330);
text("SPACE to start!", 200, 370);
} else if (gameState === GAME_OVER) {
if (player1.score > player2.score) {
text("Player 1 Wins!", 100, 80);
} else {
text("Player 2 wins!", 300, 80);
}
text("SPACE to restart!", 200, 350);
}
};
var draw = function() {
...
if (gameState === GAME_ACTIVE) {
moveBallAndCheckBoundaries();
} else {
showMessage();
}
};var draw = function() {
...
if (gameState === GAME_ACTIVE) {
moveBallAndCheckBoundaries();
} else {
showMessage();
}
if (keys[32]) { // keycode=32 for SPACE
if (gameState === GAME_PAUSED) {
gameState = GAME_ACTIVE;
} else if (gameState === GAME_OVER) {
gameState = GAME_ACTIVE;
player1.score = 0; // clear scores
player2.score = 0;
}
}
// Check scores for end of game
if (player1.score === 3 || player2.score === 3) {
gameState = GAME_OVER;
}
};
var playerHitsBall = function (player, ball) {
var dx = abs(ball.x - player.x);
var dy = abs(ball.y - player.y);
return dx < (paddle.w + ball.r) &&
dy < (paddle.h + ball.r) ;
};
Type in the code and proceed.
Review later if you do not understand
var bounceOnPaddles = function () {
// check for collision with player1
if (playerHitsBall(player1, ball)) {
ball._______ = -(ball.________); // reverse direction
}
// check for collision with player2
if (playerHitsBall(player2, ball)) {
ball._______ = -(ball.________); // reverse direction
}
};
var draw = function() {
...
movePaddles();
bounceOnPaddles(); // add
...
};
Please enter Classically Pong for Exercise Name