Skip to content

Commit 5432f96

Browse files
committed
Week 8 make the ninja run
1 parent 4b6d7f0 commit 5432f96

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

www/css/style.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ div.cylinder .bottom {
118118

119119
img#ninja {
120120
width: 80px;
121+
/* 4. Setting the Height */
122+
height: 120px;
121123
margin-top: -120px;
122124
position: absolute;
123125
}

www/js/jumpy.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ var ninja;
55
var screenWidth = screen.width;
66
var runBy = 10;
77

8+
89
function startGame() {
910
cylinder = $("#cylinder");
1011
ninja = $("#ninja");
1112
ninja.click(function() {
1213
run();
1314
});
14-
jump(10);
15+
jump(10);
1516
}
1617

1718
function jump() {
@@ -40,9 +41,36 @@ function run() {
4041
var newLeftPosition = ninjaLeft+runBy;
4142
console.log(newLeftPosition);
4243
ninja.css('left', newLeftPosition+"px");
43-
44-
run();
4544

45+
if(newLeftPosition > screenWidth) {
46+
switchDirection();
47+
setTimeout(run, 20);
48+
}
49+
else if (newLeftPosition < 0) {
50+
stop();
51+
}
52+
else {
53+
if(isCollision()) {
54+
alert('Game over');
55+
56+
} else {
57+
setTimeout(run, 20);
58+
}
59+
}
60+
61+
}
62+
63+
64+
function switchDirection() {
65+
runBy = runBy * -1;
66+
ninja.attr('src','img/ninjas-red.png');
67+
}
68+
69+
function stop() {
70+
ninja.css('left', "0px");
71+
ninja.attr('src','img/ninjas-green.png');
72+
runBy = runBy * -1;
73+
4674
}
4775

4876
startGame();

0 commit comments

Comments
 (0)