File tree Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,8 @@ div.cylinder .bottom {
118
118
119
119
img # ninja {
120
120
width : 80px ;
121
+ /* 4. Setting the Height */
122
+ height : 120px ;
121
123
margin-top : -120px ;
122
124
position : absolute;
123
125
}
Original file line number Diff line number Diff line change @@ -5,13 +5,14 @@ var ninja;
5
5
var screenWidth = screen . width ;
6
6
var runBy = 10 ;
7
7
8
+
8
9
function startGame ( ) {
9
10
cylinder = $ ( "#cylinder" ) ;
10
11
ninja = $ ( "#ninja" ) ;
11
12
ninja . click ( function ( ) {
12
13
run ( ) ;
13
14
} ) ;
14
- jump ( 10 ) ;
15
+ jump ( 10 ) ;
15
16
}
16
17
17
18
function jump ( ) {
@@ -40,9 +41,36 @@ function run() {
40
41
var newLeftPosition = ninjaLeft + runBy ;
41
42
console . log ( newLeftPosition ) ;
42
43
ninja . css ( 'left' , newLeftPosition + "px" ) ;
43
-
44
- run ( ) ;
45
44
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
+
46
74
}
47
75
48
76
startGame ( ) ;
You can’t perform that action at this time.
0 commit comments