Skip to content

Commit a883122

Browse files
committed
Added scoring and collision
1 parent 5432f96 commit a883122

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

www/css/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ img#ninja {
124124
position: absolute;
125125
}
126126

127+
div#score {
128+
position: absolute;
129+
right: 5%;
130+
top: 10%;
131+
color: yellow;
132+
font-size: 50px;
133+
}
134+
127135

128136

129137

www/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<div class="cloud c4"></div>
1818
</div>
1919

20+
<div id="score">
21+
</div>
22+
2023
<div id="cylinder" class="cylinder">
2124
<div class="top"></div>
2225
<div class="middle"></div>

www/js/jumpy.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ var jumpBy = 10;
44
var ninja;
55
var screenWidth = screen.width;
66
var runBy = 10;
7-
7+
var score;
88

99
function startGame() {
1010
cylinder = $("#cylinder");
1111
ninja = $("#ninja");
1212
ninja.click(function() {
1313
run();
1414
});
15+
score = 10;
1516
jump(10);
1617
}
1718

@@ -48,6 +49,8 @@ function run() {
4849
}
4950
else if (newLeftPosition < 0) {
5051
stop();
52+
score = score * 2;
53+
updateScore();
5154
}
5255
else {
5356
if(isCollision()) {
@@ -57,7 +60,26 @@ function run() {
5760
setTimeout(run, 20);
5861
}
5962
}
63+
}
6064

65+
function isCollision() {
66+
var ninjaPosition = ninja.offset();
67+
var cylinderPosition = cylinder.offset();
68+
var ninjaLeft = ninjaPosition.left;
69+
var ninjaRight = ninjaLeft+80;
70+
71+
var cylinderLeft = cylinderPosition.left;
72+
var cylinderRight = cylinderLeft + 50;
73+
var cylinderGap = screenHeight - (cylinderPosition.top+350);
74+
75+
if(ninjaRight > cylinderLeft
76+
&& ninjaLeft < cylinderRight
77+
&& cylinderGap < 150) {
78+
return true;
79+
} else {
80+
return false;
81+
}
82+
6183
}
6284

6385

@@ -73,4 +95,8 @@ function stop() {
7395

7496
}
7597

98+
function updateScore() {
99+
$("#score").html(score);
100+
}
101+
76102
startGame();

0 commit comments

Comments
 (0)