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 {
118118
119119img # ninja {
120120 width : 80px ;
121+ /* 4. Setting the Height */
122+ height : 120px ;
121123 margin-top : -120px ;
122124 position : absolute;
123125}
Original file line number Diff line number Diff line change @@ -5,13 +5,14 @@ var ninja;
55var screenWidth = screen . width ;
66var runBy = 10 ;
77
8+
89function startGame ( ) {
910 cylinder = $ ( "#cylinder" ) ;
1011 ninja = $ ( "#ninja" ) ;
1112 ninja . click ( function ( ) {
1213 run ( ) ;
1314 } ) ;
14- jump ( 10 ) ;
15+ jump ( 10 ) ;
1516}
1617
1718function 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
4876startGame ( ) ;
You can’t perform that action at this time.
0 commit comments