Created
July 3, 2019 13:52
-
-
Save DhairyaLGandhi/95129ec5e7e22733f1311179fa43aff7 to your computer and use it in GitHub Desktop.
Pong - Javascript
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Pong</title> | |
<script src="http://code.jquery.com/jquery-latest.min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
<!-- Basic styling, centering the canvas --> | |
<style> | |
canvas{ | |
position: absolute; | |
margin: auto; | |
top:60px; | |
bottom:0; | |
right:0; | |
left:0; | |
} | |
#scoreBoard{ | |
position: absolute; | |
margin: auto; | |
top:0; | |
bottom:640px; | |
right:720px; | |
left:20px; | |
} | |
</style> | |
</head> | |
<body> | |
<canvas id = "scoreBoard" width="500" height = "100"> | |
<p>HELLLLLLLLLLO<p> | |
</canvas> | |
<script> | |
$(document).ready(function(){ | |
var WIDTH = 700 | |
var HEIGHT = 600 | |
var pi = Math.PI | |
var canvas | |
var ctx | |
var keystate | |
var upArrow = 38; | |
var downArrow = 40; | |
var computerCount = 0; | |
var playerCount = 0; | |
var player = { | |
x: null, | |
y: null, | |
width: 20, | |
height: 100, | |
speed:14, | |
/** | |
* Update the position depending on pressed keys | |
*/ | |
update: function() { | |
if(keystate[upArrow]){ | |
this.y-=this.speed; | |
} | |
if(keystate[downArrow]){ | |
this.y+=this.speed; | |
}}, | |
/** | |
* Draw the player paddle to the canvas | |
*/ | |
draw: function() { | |
ctx.fillRect(this.x, this.y, this.width, this.height); | |
} | |
} | |
/** | |
* The ai paddle | |
* | |
* @type {Object} | |
*/ | |
var ai = { | |
x: null, | |
y: null, | |
width: 20, | |
height: 100, | |
intellegence :0.1, | |
/** | |
* Update the position depending on the ball position | |
*/ | |
update: function() { | |
var desty = ball.y-(this.height - ball.side)*0.5 | |
this.y += (desty-this.y)*ai.intellegence; | |
}, | |
draw: function() { | |
ctx.fillRect(this.x, this.y, this.width, this.height); | |
} | |
} | |
/** | |
* The ball object | |
* | |
* @type {Object} | |
*/ | |
var ball = { | |
x: null, | |
y: null, | |
vel:null, | |
side: 20, | |
speed:15, | |
update: function() { | |
this.x += this.vel.x; | |
this.y += this.vel.y; | |
if(0>this.y || this.y+this.side>HEIGHT){ | |
var offset = this.vel.y<0 ? 0-this.y : HEIGHT-(this.y+this.side); | |
this.y+=offset | |
this.vel.y*=-1; | |
} | |
var AABBIntersect = function(ax, ay, aw, ah, bx, by, bw, bh) { | |
return ax < bx+bw && ay < by+bh && bx < ax+aw && by < ay+ah; | |
}; | |
var pdle = this.vel.x<0 ? player : ai; | |
if(AABBIntersect(pdle.x,pdle.y,pdle.width,pdle.height, this.x, this.y, this.side, this.side)){ | |
this.x = pdle===player ? player.x + player.width : ai.x - this.side; | |
var n = (this.y+this.side-pdle.y)/(pdle.height+this.side) | |
var phi = 0.25*pi*(2*n - 1) // pi/4 = 45 | |
this.x = pdle===player ? player.x+player.width : ai.x - this.side; | |
var n = (this.y+this.side - pdle.y)/(pdle.height+this.side); | |
var phi = 0.25*pi*(2*n - 1); // pi/4 = 45 | |
this.vel.x = (pdle===player ? 1 : -1)*this.speed*Math.cos(phi); | |
this.vel.y = this.speed*Math.sin(phi); | |
} | |
if(ball.x<0){ | |
computerCount +=1; | |
ball.vel.x -=5; | |
ball.x = (WIDTH - ball.side) / 2; | |
ball.y = (HEIGHT - ball.side) / 2; | |
} | |
if(ball.x>WIDTH){ | |
playerCount +=1; | |
ball.vel.x -=5; | |
ball.x = (WIDTH - ball.side) / 2; | |
ball.y = (HEIGHT - ball.side) / 2; | |
} | |
if(playerCount==5){ | |
ball.speed = 20; | |
} | |
else if(playerCount==10){ | |
ai.intellegence = 0.15; | |
} | |
else if(playerCount==15){ | |
ball.speed = 25; | |
} | |
else if(playerCount==20){ | |
ai.intellegence = 0.2; | |
} | |
}, | |
// reset the ball when ball outside of the canvas in the | |
draw: function() { | |
ctx.fillRect(this.x, this.y, this.side, this.side); | |
} | |
} | |
function score(){ | |
} | |
/** | |
* Starts the game | |
*/ | |
function main() { | |
// create, initiate and append game canvas | |
canvas = document.createElement("canvas"); | |
canvas.width = WIDTH; | |
canvas.height = HEIGHT; | |
ctx = canvas.getContext("2d"); | |
document.body.appendChild(canvas); | |
canvas2 = document.createElement("canvas"); | |
canvas2.setAttribute("id", "scoreBoard"); | |
canvas2.width = 200; | |
canvas2.height = 100; | |
ctx2 = canvas2.getContext("2d"); | |
document.body.appendChild(canvas2); | |
keystate = {}; | |
document.addEventListener("keydown" , function(event){ | |
keystate[event.keyCode] = true; | |
}) | |
document.addEventListener("keyup" , function(event){ | |
delete keystate[event.keyCode]; | |
}) | |
init(); | |
var loop = function() { | |
update(); | |
draw(); | |
window.requestAnimationFrame(loop, canvas); | |
}; | |
window.requestAnimationFrame(loop, canvas); | |
} | |
/** | |
* Initatite game objects and set start positions | |
*/ | |
function init() { | |
player.x = player.width; | |
player.y = (HEIGHT - player.height) / 2; | |
ai.x = WIDTH - (player.width + ai.width); | |
ai.y = (HEIGHT - ai.height) / 2; | |
ball.x = (WIDTH - ball.side) / 2; | |
ball.y = (HEIGHT - ball.side) / 2; | |
ball.vel = { | |
x:ball.speed, | |
y: 0 | |
} | |
} | |
/** | |
* Update all game objects | |
*/ | |
function update() { | |
ball.update(); | |
player.update(); | |
ai.update(); | |
} | |
function draw(){ | |
ctx.fillStyle = "black"; | |
ctx.fillRect(0,0,WIDTH,HEIGHT); | |
ctx.save(); | |
ctx2.fillStyle = "black"; | |
ctx2.fillRect(0,0,WIDTH, 100); | |
ctx2.save(); | |
ctx2.fillStyle = "red"; | |
ctx2.font = "bold 40px monaco"; | |
ctx2.fillText(String(computerCount),60,100); | |
ctx2.fillText("VS",100,100); | |
ctx2.fillText(String(playerCount),160,100); | |
ctx2.save(); | |
ctx.fillStyle = "white"; | |
ball.draw(); | |
player.draw(); | |
ai.draw(); | |
var w=4; | |
var x= (WIDTH-w)*0.5; | |
var y=2; | |
var step = HEIGHT/20; | |
while(y<HEIGHT){ | |
ctx.fillRect(x,y+step*0.25,w,step*0.5) | |
y+=step; | |
} | |
ctx.restore(); | |
} | |
// start and run the game | |
main(); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment