How to Make a Simple Ping Pong Game with Javascript
In this article I will share how to make a game, namely the game ping pong . I got this game script code in Javascript Game Code .
<html></html><br />
<head></head><br />
<title>Making ping pong game</title><br />
<style type="text/css"></p>
<p>
//taruh file cssnya here </p>
<p>
</style><br />
</div>
<body></body><br />
//taruh file htmlnya disini<br />
<script type="text/javascript"></p>
<p>
//js file here</p>
<p>
</script>
Copy the following html, css, javascript code:
<canvas height="400" id="pong" width="600"></canvas><br />
Kode CSS<br />
body{<br />
background-color: dimgray;<br />
}<br />
#pong{<br />
border: 2px solid #FFF;<br />
position: absolute;<br />
margin :auto;<br />
top:0;<br />
right:0;<br />
left:0;<br />
bottom:0;<br />
}<br />
Kode JAVASCRIPT<br />
// select canvas element<br />
const canvas = document.getElementById("pong");<br />
// getContext of canvas = methods and properties to draw and do a lot of thing to the canvas<br />
const ctx = canvas.getContext('2d');<br />
// load sounds<br />
let hit = new Audio();<br />
let wall = new Audio();<br />
let userScore = new Audio();<br />
let comScore = new Audio();<br />
hit.src = "sounds/hit.mp3";<br />
wall.src = "sounds/wall.mp3";<br />
comScore.src = "sounds/comScore.mp3";<br />
userScore.src = "sounds/userScore.mp3";<br />
// Ball object<br />
const ball = {<br />
x : canvas.width/2,<br />
y : canvas.height/2,<br />
radius : 10,<br />
velocityX : 5,<br />
velocityY : 5,<br />
speed : 7,<br />
color : "WHITE"<br />
}<br />
// User Paddle<br />
const user = {<br />
x : 0, // left side of canvas<br />
y : (canvas.height - 100)/2, // -100 the height of paddle<br />
width : 10,<br />
height : 100,<br />
score : 0,<br />
color : "WHITE"<br />
}<br />
// COM Paddle<br />
const com = {<br />
x : canvas.width - 10, // - width of paddle<br />
y : (canvas.height - 100)/2, // -100 the height of paddle<br />
width : 10,<br />
height : 100,<br />
score : 0,<br />
color : "WHITE"<br />
}<br />
// NET<br />
const net = {<br />
x : (canvas.width - 2)/2,<br />
y : 0,<br />
height : 10,<br />
width : 2,<br />
color : "WHITE"<br />
}<br />
// draw a rectangle, will be used to draw paddles<br />
function drawRect(x, y, w, h, color){<br />
ctx.fillStyle = color;<br />
ctx.fillRect(x, y, w, h);<br />
}<br />
// draw circle, will be used to draw the ball<br />
function drawArc(x, y, r, color){<br />
ctx.fillStyle = color;<br />
ctx.beginPath();<br />
ctx.arc(x,y,r,0,Math.PI*2,true);<br />
ctx.closePath();<br />
ctx.fill();<br />
}<br />
// listening to the mouse<br />
canvas.addEventListener("mousemove", getMousePos);<br />
function getMousePos(evt){<br />
let rect = canvas.getBoundingClientRect();<br />
<br />
user.y = evt.clientY - rect.top - user.height/2;<br />
}<br />
// when COM or USER scores, we reset the ball<br />
function resetBall(){<br />
ball.x = canvas.width/2;<br />
ball.y = canvas.height/2;<br />
ball.velocityX = -ball.velocityX;<br />
ball.speed = 7;<br />
}<br />
// draw the net<br />
function drawNet(){<br />
for(let i = 0; i <= canvas.height; i+=15){<br />
drawRect(net.x, net.y + i, net.width, net.height, net.color);<br />
}<br />
}<br />
// draw text<br />
function drawText(text,x,y){<br />
ctx.fillStyle = "#FFF";<br />
ctx.font = "75px fantasy";<br />
ctx.fillText(text, x, y);<br />
}<br />
// collision detection<br />
function collision(b,p){<br />
p.top = p.y;<br />
p.bottom = p.y + p.height;<br />
p.left = p.x;<br />
p.right = p.x + p.width;<br />
<br />
b.top = b.y - b.radius;<br />
b.bottom = b.y + b.radius;<br />
b.left = b.x - b.radius;<br />
b.right = b.x + b.radius;<br />
<br />
return p.left < b.right && p.top < b.bottom && p.right > b.left && p.bottom > b.top;<br />
}<br />
// update function, the function that does all calculations<br />
function update(){<br />
<br />
// change the score of players, if the ball goes to the left "ball.x<0 ball.x="" computer="" else="" if="" win=""> canvas.width" the user win</0><br />
if( ball.x - ball.radius < 0 ){<br />
com.score++;<br />
comScore.play();<br />
resetBall();<br />
}else if( ball.x + ball.radius > canvas.width){<br />
user.score++;<br />
userScore.play();<br />
resetBall();<br />
}<br />
<br />
// the ball has a velocity<br />
ball.x += ball.velocityX;<br />
ball.y += ball.velocityY;<br />
<br />
// computer plays for itself, and we must be able to beat it<br />
// simple AI<br />
com.y += ((ball.y - (com.y + com.height/2)))*0.1;<br />
<br />
// when the ball collides with bottom and top walls we inverse the y velocity.<br />
if(ball.y - ball.radius < 0 || ball.y + ball.radius > canvas.height){<br />
ball.velocityY = -ball.velocityY;<br />
wall.play();<br />
}<br />
<br />
// we check if the paddle hit the user or the com paddle<br />
let player = (ball.x + ball.radius < canvas.width/2) ? user : com;<br />
<br />
// if the ball hits a paddle<br />
if(collision(ball,player)){<br />
// play sound<br />
hit.play();<br />
// we check where the ball hits the paddle<br />
let collidePoint = (ball.y - (player.y + player.height/2));<br />
// normalize the value of collidePoint, we need to get numbers between -1 and 1.<br />
// -player.height/2 < collide Point < player.height/2<br />
collidePoint = collidePoint / (player.height/2);<br />
<br />
// when the ball hits the top of a paddle we want the ball, to take a -45degees angle<br />
// when the ball hits the center of the paddle we want the ball to take a 0degrees angle<br />
// when the ball hits the bottom of the paddle we want the ball to take a 45degrees<br />
// Math.PI/4 = 45degrees<br />
let angleRad = (Math.PI/4) * collidePoint;<br />
<br />
// change the X and Y velocity direction<br />
let direction = (ball.x + ball.radius < canvas.width/2) ? 1 : -1;<br />
ball.velocityX = direction * ball.speed * Math.cos(angleRad);<br />
ball.velocityY = ball.speed * Math.sin(angleRad);<br />
<br />
// speed up the ball everytime a paddle hits it.<br />
ball.speed += 0.1;<br />
}<br />
}<br />
// render function, the function that does al the drawing<br />
function render(){<br />
<br />
// clear the canvas<br />
drawRect(0, 0, canvas.width, canvas.height, "#000");<br />
<br />
// draw the user score to the left<br />
drawText(user.score,canvas.width/4,canvas.height/5);<br />
<br />
// draw the COM score to the right<br />
drawText(com.score,3*canvas.width/4,canvas.height/5);<br />
<br />
// draw the net<br />
drawNet();<br />
<br />
// draw the user's paddle<br />
drawRect(user.x, user.y, user.width, user.height, user.color);<br />
<br />
// draw the COM's paddle<br />
drawRect(com.x, com.y, com.width, com.height, com.color);<br />
<br />
// draw the ball<br />
drawArc(ball.x, ball.y, ball.radius, ball.color);<br />
}<br />
function game(){<br />
update();<br />
render();<br />
}<br />
// number of frames per second<br />
let framePerSecond = 50;<br />
//call the game function 50 times every 1 Sec<br />
let loop = setInterval(game,1000/framePerSecond);
Click Save and click View
If you have questions, criticisms and suggestions, please comment below.
0 Response to "How to Make a Simple Ping Pong Game with Javascript "
Post a Comment