Skip to content

Commit c021ff2

Browse files
committed
Added Game Over Box, its Styles & Design Preview #Day38
1 parent 4a5845c commit c021ff2

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed

flappy-birds-game/README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
1. Use <canvas> tags to create a canvas for the game.
66
2. Inline CSS used to align the canvas & heading to the center of the page.
77
3. Use onload property in <body> that enables the execution of draw() once the page's elements finish loading.
8-
4. Bind game.js file using <script> tags.
8+
4. Add div for game-over, including the score heading & Restart button.
9+
5. Bind game.js file using <script> tags.
910

1011
## JavaScript Code
1112

@@ -17,10 +18,13 @@
1718
6. Create an array of pipes[], to draw multiple pipes on the canvas, by updating the x & y coordinates using Math.random().
1819
7. Use coordinates of the bird and pipes to check for collision of these elements.
1920
1. If the bird's position on x-axis is equivant to the position of pipe or in-between the width of pipe, and
20-
2. if the bird's position on y-axis is greater than or equal to the position of North pipe, OR if it is less than or equal to the position of South pipe
21-
3. Check if the bird reaches the foreGround.
21+
2. If the bird's position on y-axis is greater than or equal to the position of North pipe, OR if it is less than or equal to the position of South pipe
22+
3. If the bird reaches the foreGround.
23+
4. If the bird crosses/reaches the top of canvas box.
2224
8. Increase score once the pipe crosses the bird without collision.
2325
9. Display the updated score using fillText() on the canvas.
26+
10. Add Score Over pop-in box, using a game-wrapper in HTML, and display it at the time of collision, with a button to restart the game.
27+
11. Neccessary to use return statement on collision to stop the game loop.
2428

2529

2630
## Credits

flappy-birds-game/game.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ let draw = () => {
5050
// check collision of elements
5151
if (birdState.x + bird.width >= pipe.x && birdState.x <= pipe.x + pipeNorth.width
5252
&& (birdState.y <= pipe.y + pipeNorth.height || birdState.y + bird.height >= pipe.y + gap)
53-
|| birdState.y + bird.height >= canvasContainer.height - foreGround.height) {
54-
location.reload();
53+
|| birdState.y + bird.height >= canvasContainer.height - foreGround.height
54+
|| birdState.y + bird.height <= 0) {
55+
document.querySelector(".game-over").style.display = "block";
56+
document.getElementById("scores").textContent += score;
57+
return;
5558
}
5659
// increase score when pipe crosses the position of bird
5760
if (pipe.x === 5) {

flappy-birds-game/index.html

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>FlappyBird</title>
8+
<link rel="stylesheet" href="styles.css">
89
</head>
910

1011
<body style="display: grid; justify-content: center; align-items: center; color: green;" onload="draw()">
1112
<h3 style="text-align: center;">Play Flappy Bird Game Here</h3>
12-
<canvas id="myCanvas" width="288" height="512">Your browser doesn't include support for the canvas element.</canvas>
13+
<div class="game-wrapper">
14+
<canvas id="myCanvas" width="288" height="512">Your browser doesn't include support for the canvas
15+
element.</canvas>
16+
<div class="game-over">
17+
<h2>GAME OVER</h2>
18+
<h3 id="scores">Score: </h3>
19+
<button class="btn" onclick="window.location.reload()">restart</button>
20+
</div>
21+
</div>
1322
<script src="game.js"></script>
1423
</body>
1524

flappy-birds-game/styles.css

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
.game-wrapper {
2+
flex: 1;
3+
display: flex;
4+
justify-content: center;
5+
align-items: center;
6+
background: rgb(22, 22, 22);
7+
}
8+
9+
.game-over {
10+
display: none;
11+
position: absolute;
12+
background: yellowgreen;
13+
color: red;
14+
padding: 20px 50px;
15+
box-shadow: 0 2px 10px rgba(235, 89, 5, 0.2);
16+
border-radius: 10px;
17+
text-align: center;
18+
animation: pop-in 1s;
19+
font-family: Verdana;
20+
}
21+
22+
.btn {
23+
border: 2px solid #ee9e26;
24+
border-radius: 3px;
25+
box-shadow: 0 2px rgba(0, 0, 0, 0.15);
26+
background: linear-gradient(
27+
to bottom,
28+
#fff 0%,
29+
#fff 49%,
30+
#f5f5f5 50%,
31+
#eee 100%
32+
);
33+
padding: 10px 40px;
34+
font-family: 14px Verdana;
35+
text-transform: capitalize;
36+
}
37+
38+
@keyframes pop-in {
39+
0% {
40+
opacity: 0;
41+
transform: translate(0, -50px);
42+
}
43+
10% {
44+
opacity: 1;
45+
}
46+
50% {
47+
transform: translate(0, 50px);
48+
}
49+
100% {
50+
transform: translate(0, 0);
51+
}
52+
}
53+

0 commit comments

Comments
 (0)