added a game over and restart functionality
This commit is contained in:
parent
c5913a2f47
commit
8826d007dd
@ -2,9 +2,11 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Gregory Crawford</title>
|
<title>Gregory Crawford</title>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<link rel="stylesheet" href="style.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
@ -2,13 +2,19 @@
|
|||||||
<html>
|
<html>
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>Basic Pong HTML Game</title>
|
<title>Pong Game</title>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<link rel="stylesheet" href="style.css">
|
<link rel="stylesheet" href="style.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<canvas width="750" height="585" id="game"></canvas>
|
<canvas width="750" height="585" id="game"></canvas>
|
||||||
<script src="scripts.js"></script>
|
<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>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
20
scripts.js
20
scripts.js
@ -54,9 +54,29 @@ function collides(obj1, obj2) {
|
|||||||
obj1.y + obj1.height > obj2.y;
|
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
|
// game loop
|
||||||
function loop() {
|
function loop() {
|
||||||
|
if (leftScore < 7 && rightScore < 7) {
|
||||||
requestAnimationFrame(loop);
|
requestAnimationFrame(loop);
|
||||||
|
} else if (leftScore >= 7 || rightScore >= 7) {
|
||||||
|
gameOver();
|
||||||
|
}
|
||||||
context.clearRect(0,0,canvas.width,canvas.height);
|
context.clearRect(0,0,canvas.width,canvas.height);
|
||||||
|
|
||||||
// move paddles by their velocity
|
// move paddles by their velocity
|
||||||
|
11
style.css
11
style.css
@ -9,3 +9,14 @@ body {
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.button {
|
||||||
|
background: blue;
|
||||||
|
color: white;
|
||||||
|
padding: 15px 20px;
|
||||||
|
font-size: 32px;
|
||||||
|
font-family: Arial, Helvetica, sans-serif;
|
||||||
|
text-transform: uppercase;
|
||||||
|
cursor: pointer;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user