This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if ball goes off the top side (side of table) | |
if (ball.position.y <= -fieldHeight/2) | |
{ | |
ballDirY = -ballDirY; | |
} | |
// if ball goes off the bottom side (side of table) | |
if (ball.position.y >= fieldHeight/2) | |
{ | |
ballDirY = -ballDirY; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if ball goes off the 'left' side (Player's side) | |
if (ball.position.x <= -fieldWidth/2) | |
{ | |
// CPU scores | |
score2++; | |
// update scoreboard HTML | |
document.getElementById("scores").innerHTML = score1 + "-" + score2; | |
// reset ball to center |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We lerp the scale back to 1 | |
// this is done because we stretch the paddle at some points | |
// stretching is done when paddle touches side of table and when paddle hits ball | |
// by doing this here, we ensure paddle always comes back to default size | |
paddle2.scale.y += (1 - paddle2.scale.y) * 0.2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in case the Lerp function produces a value above max paddle speed, we clamp it | |
if (Math.abs(paddle2DirY) <= paddleSpeed) | |
{ | |
paddle2.position.y += paddle2DirY; | |
} | |
// if the lerp value is too high, we have to limit speed to paddleSpeed | |
else | |
{ | |
// if paddle is lerping in +ve direction | |
if (paddle2DirY > paddleSpeed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Lerp towards the ball on the y plane | |
paddle2DirY = (ball.position.y - paddle2.position.y) * difficulty; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// move left | |
if (Key.isDown(Key.A)) | |
{ | |
// if paddle is not touching the side of table | |
// we move | |
if (paddle1.position.y < fieldHeight * 0.45) | |
{ | |
paddle1DirY = paddleSpeed * 0.5; | |
} | |
// else we don't move and stretch the paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
var Key = { | |
_pressed: {}, | |
A: 65, | |
W: 87, | |
D: 68, | |
S: 83, | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// move left | |
if (Key.isDown(Key.A)) | |
{ | |
// code to move paddle left | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setup() | |
{ | |
draw(); | |
} | |
function draw() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Game</title> | |
<style> | |
body { | |
background-color: black; | |
} | |
#gameCanvas { | |
background-color: black; |
NewerOlder