added a game over and restart functionality
This commit is contained in:
parent
c5913a2f47
commit
8826d007dd
@ -2,9 +2,11 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Gregory Crawford</title>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<a href="pong.html">Play pong here!</a>
|
||||
<button class="button" type="button" onclick="window.location.href='pong.html'">Start Game</button>
|
||||
<script src = "scripts.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
@ -2,13 +2,19 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Basic Pong HTML Game</title>
|
||||
<title>Pong Game</title>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<canvas width="750" height="585" id="game"></canvas>
|
||||
<script src="scripts.js"></script>
|
||||
<div id="gameover" hidden="true">
|
||||
<h1>Game Over</h1>
|
||||
</div>
|
||||
<div id="restart" hidden="true">
|
||||
<button class="button" type="button" onclick = "restartGame()">Restart Game</a>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
20
scripts.js
20
scripts.js
@ -54,9 +54,29 @@ function collides(obj1, obj2) {
|
||||
obj1.y + obj1.height > obj2.y;
|
||||
}
|
||||
|
||||
function gameOver() {
|
||||
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||
let restartGame = document.getElementById("restart");
|
||||
let gameOverText = document.getElementById("gameover");
|
||||
restartGame.hidden = false;
|
||||
gameOverText.hidden = false;
|
||||
}
|
||||
|
||||
function restartGame() {
|
||||
leftScore = 0;
|
||||
rightScore = 0;
|
||||
let restartGame = document.getElementById("restart");
|
||||
restartGame.hidden = true;
|
||||
requestAnimationFrame(loop);
|
||||
}
|
||||
|
||||
// game loop
|
||||
function loop() {
|
||||
if (leftScore < 7 && rightScore < 7) {
|
||||
requestAnimationFrame(loop);
|
||||
} else if (leftScore >= 7 || rightScore >= 7) {
|
||||
gameOver();
|
||||
}
|
||||
context.clearRect(0,0,canvas.width,canvas.height);
|
||||
|
||||
// move paddles by their velocity
|
||||
|
Loading…
Reference in New Issue
Block a user