function Ball(options) { if (typeof(options) == 'undefined') options={}; var x = options.x !== undefined ? options.x : 0; var y = options.y !== undefined ? options.y : 0; var dx = options.dx !== undefined ? options.dx : 0; var dy = options.dy !== undefined ? options.dy : 0; var sx = options.sx !== undefined ? options.sx : 0; var sy = options.sy !== undefined ? options.sy : 0; var size = options.size !== undefined ? options.size : 0; function update(ctx, paddle) { // Get the start x and y for the ball. var aX = x; var aY = y; // Move the ball left or right. x += dx * sx; // Check to see if the ball hit the left wall. // If it did, bounce to the right. if (x <= (size / 2)) { x = size / 2; dx = 1; } // Check to see if the ball hit the right wall. // If it did, bounce to the left. if (x >= (ctx.canvas.width - (size / 2))) { x = (ctx.canvas.width - (size / 2)); dx = -1; } // Move the ball up or down. y += dy * sy; // Check to see if the ball hit the ceiling. // Bounce down if it did. if (y <= (size / 2)) { y = size / 2; dy = 1; } // Check to see if the ball hit the paddle. // Ball line aX, aY to x, y // Paddle line paddle.x - (paddle.size / 2), paddle.y to paddle.x + (paddle.size / 2), paddle.y // y = mx + b if (x >= paddle.getX1() && x <= paddle.getX2() && aY <= paddle.getY() && y >= paddle.getY()) { y = paddle.getY() - size; dy = -1; sx++; sy++; paddle.increaseDifficulty(); game.add1ToScore(); } // Check to see if the ball goes off the bottom // of the screen. If it did, game over! if (y >= (ctx.canvas.height - (size / 2))) { game.endGame(); } } function draw(ctx) { ctx.beginPath(); ctx.fillStyle = '#000000'; ctx.ellipse(x, y, size, size, 0, 0, 360); ctx.fill(); } return { 'x': x, 'y': y, 'dx': dx, 'dy': dy, 'sx': sx, 'sy': sy, 'size': size, 'update': update, 'draw': draw }; }