Javascript Pong Game
How do I make an javascript pong game?
What is a javascript pong game? How do you make a javascript pong game? This script and codes were developed by Tim on 31 October 2022, Monday.
Javascript Pong Game - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Javascript Pong Game</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <h1>Javascript Pong</h1>
<canvas id="myCanvas" width="480" height="320"></canvas>
<div>Controls: <br/> p1: A (up) & Z (down) <br/> p2: K (up) & M (down)
</div>
<button id="start-btn">Start Game</button> <script src="js/index.js"></script>
</body>
</html>
Javascript Pong Game - Script Codes CSS Codes
*{ margin: 0; padding: 0;
}
body{ display: flex; flex-direction: column; justify-content: center; align-items: center;
}
canvas{ background: #eee;
}
button{ font-size: 18px;
}
Javascript Pong Game - Script Codes JS Codes
//DOM references
const canvas = document.getElementById('myCanvas')
const ctx = canvas.getContext('2d')
const startButton = document.getElementById('start-btn')
//Game component constants
const player = { x:0, y:0, width:10, height: 50, upPressed: false, downPressed: false, speed: 2, score: 0,
}
const ball = { x:canvas.width/2, y:canvas.height/2, dx: 3, dy: 1, radius: 8, pause: true,
}
const maxScore = 3
const defaultMessage = 'Press start to begin'
const speedIncrease = 1.05
//Game components
var p1, p2, gameBall, message
var gameOver = true
//Game state functions
function newGame(){ p1 = Object.assign( {}, player, { x:0, y:(canvas.height-player.height)/2 } ) p2 = Object.assign( {}, player, { x:canvas.width-player.width, y:(canvas.height-player.height)/2 } ) gameBall = Object.assign({},ball) message = defaultMessage gameOver = false setTimeout(()=>{gameBall.pause = false},1000)
}
function newRound(){ //Reset ball gameBall = Object.assign({},ball) setTimeout(()=>{gameBall.pause = false},1000)
}
//Game update functions
function updateBall(){ if(gameBall.y + gameBall.dy < gameBall.radius || gameBall.y + gameBall.dy > canvas.height-gameBall.radius){ gameBall.dy = -(speedIncrease * gameBall.dy) } if(gameBall.x +gameBall.dx < gameBall.radius){ if(gameBall.y>p1.y && gameBall.y < p1.y + p1.height){ if(gameBall.y<p1.y+p1.height/2){ gameBall.dy = -Math.abs(speedIncrease * gameBall.dy) } else{ gameBall.dy = Math.abs(speedIncrease * gameBall.dy) } gameBall.dx = -(speedIncrease * gameBall.dx) } else{ p2.score++ if(p2.score >= maxScore){ message = 'Player 2 won!' gameOver = true } else{ newRound() } } } else if(gameBall.x + gameBall.dx > canvas.width-gameBall.radius){ if(gameBall.y>p2.y && gameBall.y < p2.y + p2.height){ if(gameBall.y<p2.y+p2.height/2){ gameBall.dy = -Math.abs(speedIncrease * gameBall.dy) } else{ gameBall.dy = Math.abs(speedIncrease * gameBall.dy) } gameBall.dx = -(speedIncrease * gameBall.dx) } else{ p1.score++ if(p1.score >= maxScore){ message = 'Player 1 won!' gameOver = true } else{ newRound() } } } gameBall.x += gameBall.dx gameBall.y += gameBall.dy
}
function updatePaddle(p){ if(p.upPressed && p.y - p.speed> 0){ p.y -= p.speed } else if(p.downPressed && p.y + p.speed < canvas.height-p.height){ p.y += p.speed }
}
//Drawing functions
function drawPaddle(p){ ctx.beginPath() ctx.rect(p.x,p.y,p.width,p.height) ctx.fillStyle = 'black' ctx.fill() ctx.closePath()
}
function drawBall(){ ctx.beginPath() ctx.arc(gameBall.x,gameBall.y,gameBall.radius,0,Math.PI*2) ctx.fillStyle = 'black' ctx.fill() ctx.closePath()
}
function drawScore(){ ctx.font = "16px Arial" ctx.fillStyle = 'black' ctx.textAlign = "center" ctx.fillText(p1.score+' - '+p2.score,canvas.width/2,20)
}
function drawMessage(message=defaultMessage){ ctx.font = "16px Arial" ctx.fillStyle = 'black' ctx.textAlign = "center" ctx.fillText(message,canvas.width/2,canvas.height/2)
}
//Canvas drawing function
function draw(){ //Start by clearing the canvas ctx.clearRect(0,0,canvas.width,canvas.height) if(!gameOver){ drawScore() drawPaddle(p1) drawPaddle(p2) drawBall() if(!gameBall.pause){ updateBall() } updatePaddle(p1) updatePaddle(p2) } else{ drawMessage(message) } requestAnimationFrame(draw)
}
//Events
function keyDownHandler(e){ //A if(e.keyCode === 65){ p1.upPressed = true } //Z else if(e.keyCode === 90){ p1.downPressed = true } //K if(e.keyCode === 75){ p2.upPressed = true } else if(e.keyCode === 77){ p2.downPressed = true }
}
function keyUpHandler(e){ //A if(e.keyCode === 65){ p1.upPressed = false } //Z else if(e.keyCode === 90){ p1.downPressed = false } //K if(e.keyCode === 75){ p2.upPressed = false } else if(e.keyCode === 77){ p2.downPressed = false }
}
//Listeners
document.addEventListener('keydown',keyDownHandler,false)
document.addEventListener('keyup',keyUpHandler,false)
startButton.addEventListener('click',newGame,false)
//Start the canvas drawing function
draw()
Developer | Tim |
Username | maytim |
Uploaded | October 31, 2022 |
Rating | 3 |
Size | 2,903 Kb |
Views | 24,288 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Phaser.io First Game Tutorial | 2,478 Kb |
Double Helix Animation - Jade and Sass | 2,497 Kb |
Javascript Spelling Game | 4,380 Kb |
Reddit API Gifs | 2,023 Kb |
Barbell | 5,192 Kb |
Pixi JS | 2,710 Kb |
Bot Land Rankings | 3,592 Kb |
Custom Multi Column Data Entry Select | 3,313 Kb |
A Pen by Tim | 2,359 Kb |
Game idea | 2,839 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
CSSOff 2013 Submission | Codewunder | 14,766 Kb |
Fixed with using Calc | Tomleo | 2,542 Kb |
Bloomberg Style Link Hover | Gil-- | 1,609 Kb |
Owl Carousel - jumpTo | OwlFonk | 2,553 Kb |
BabyStore | Pablo-Ai | 3,807 Kb |
Brian The CSS Bee | Jonitrythall | 3,922 Kb |
Sticky notes with CSS3 | HaiNguyen007 | 2,146 Kb |
Single element checkbox | Ivijaygupta | 1,996 Kb |
Image Stack Test SCSS | CalvinMorett | 2,799 Kb |
Social.svg.min | Larsenwork | 13,849 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!