Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                    <div id="track">

      <div class="dividers-first">
        <div class="divider one">
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
        </div>
        <div class="divider two">
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
        </div>
      </div>
      <div class="dividers-second">
        <div class="divider one">
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
        </div>
        <div class="divider two">
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
        </div>
      </div>
      <div id="myCar">
        <img src="https://goo.gl/MeGfUc">
      </div>

      <div id="first-row-to-avoid">
        <div id="carToAvoid1"><img src="https://goo.gl/oVsjP2" ></div>
        <div id="carToAvoid2"><img src="https://goo.gl/oVsjP2" ></div>
        <div id="carToAvoid3"><img src="https://goo.gl/oVsjP2" ></div>
     </div>

     <div id="second-row-to-avoid">
       <div id="carToAvoid1"><img src="https://goo.gl/oVsjP2" ></div>
       <div id="carToAvoid2"><img src="https://goo.gl/oVsjP2" ></div>
       <div id="carToAvoid3"><img src="https://goo.gl/oVsjP2" ></div>
    </div>


      <i id="start-btn" class="fa fa-play-circle"></i>

      <div id='result'>
       <div id="score">SCORE</div>
       <i class="fa fa-repeat fa-5x"></i>
      </div>
    </div>

              
            
!

CSS

              
                body{
  margin:0;
  background-color: #E5E7FF;
}

/* track for car racing */
#track {
  width: 350px;
  height: 600px;
  margin: 0 auto;
  position: relative;
  background-color: #FFBB2D;
  overflow: hidden;
}
/* the divider lines */

.dividers-first{
  width: 100%;
  height: 100%;
  position: absolute;
  top: -115%;
}

.dividers-second{
  width: 100%;
  height: 100%;
  position: absolute;
  top: -210%;
}
.divider {
  width: 1.4285%;
  position: absolute;
  top: 0;
  height: 100%;
}
.divider .line{
  width: 100%;
  height: 12.5%;
  background-color: #7F6A3D;
  margin-top: 20px;
}
.one{
  left: 32.5555%;
}
.two{
  left: 65.5555%;
}

/* car to drive */
#myCar{
  width: 15.714%;
  height: 18.0%;
  position: absolute;
  bottom:-25%;
  left: 42.5%;
  transition: left 0.4s ease 0s;
}
#myCar img{
  width: 100%;
}

/* car to avoid */
#first-row-to-avoid{
  width: 100%;
  height: 18%;
  position: absolute;
  top: -22%;
  left: 0;
}
#second-row-to-avoid{
  width: 100%;
  height: 18%;
  position: absolute;
  top: -75%;
  left: 0;
}
#carToAvoid1, #carToAvoid2, #carToAvoid3 {
  width: 15.714%;
  height: 100%;
  position: absolute;
  overflow: visible;

}
#carToAvoid1{
  left: 8.0%;
  display: none;
}
#carToAvoid2{
  left: 41%;
}
#carToAvoid3{
  left: 75%;
  display: none;
}

#carToAvoid1 img, #carToAvoid2 img, #carToAvoid3 img{
  width: 100%;
}



/* start button */
#start-btn{
  width: 100px;
  height: 100px;
  border-radius: 100%;
  font-size: 105px;
  color: #441c1B;
  text-align: center;
  position: absolute;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
  top: 45%;
  cursor: pointer;
  animation: 6s hover infinite;

}
@keyframes hover{
  0%{ color: #441c1B;
  }
  50%{  color: #a3a;
  }
}

/* score and restart button */
#result{
  position: absolute;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
  margin-top: 250px;
  width: 170px;
  display: none;
}

#score{
  width: 100%;
  font: 44px/1.5 Sans;
  text-align: center;
}
#result i{
  margin-left: 50px;
  color: #FF7F20;
  margin-top: 70px;
  cursor: pointer;
}
#result i:hover{
  margin-left:53px;
  color: #dF5F00;  
}

              
            
!

JS

              
                $(document).ready(function() {

  /* DOM to varaibles */
   var track = $("#track");
   var btn = $("#start-btn");
   var myCar = $("#myCar");
   var dividers_first = $(".dividers-first");
   var dividers_second = $(".dividers-second");

   /* Other variables */
   var speed = 10;
   var track_height = track.height();
   var track_width = track.width();
   var myCar_position_left = parseInt(myCar.css('left'));
   var myCar_position_bottom = parseInt(myCar.css('bottom'));
   var score_count = 0;
   /* start the game */

   btn.click(function() {

    /* Hide the btn */
    btn.hide();

    /* bring myCar to the track */
     var start_the_car = setInterval(function() {

       if (myCar_position_bottom > 0.05*track_height){
           clearInterval(start_the_car);
           myCar_position_bottom = ParseInt(myCar.css('bottom'));
       }

       myCar.css('bottom',myCar_position_bottom+=3);

     } , 40 );

     /* Divider starts to show */
     var dividers_first_current_position = parseInt(dividers_first.css('top'));
     var dividers_second_current_position = parseInt(dividers_second.css('top'));
     
     var divider_speed = 4;

     var dividers_moving = setInterval(function() {

        if(dividers_first_current_position >= track_height){
            dividers_first_current_position = parseInt(dividers_second.css('top')) - dividers_second.height() + 18;
            dividers_first.css( 'top' , dividers_first_current_position);
        }

      else  if(dividers_second_current_position >= track_height){
            dividers_second_current_position = parseInt(dividers_first.css('top')) - dividers_first.height() + 18;
            dividers_second.css( 'top' , dividers_second_current_position);
        }


        dividers_first.css('top' , dividers_first_current_position+= divider_speed );
        dividers_second.css('top' , dividers_second_current_position+= divider_speed );


     } , 20 );


     /* car controller starts after 3 seconds*/
     var horizontal_shift = track.width()/3 ;
     var vertical_shift = 4;
     var myCar_height = parseInt(myCar.css('height'));

     setTimeout( function() {
         $(document).on('keydown' , function(e) {
             if( e.which === 37)                     /* on press left */
             {
                if ( myCar_position_left - horizontal_shift > 0 )
                   myCar.css('left',myCar_position_left-= horizontal_shift);
             }
             else if( e.which === 39)                 /* on press right */
             {
                 if ( track_width - (myCar_position_left + horizontal_shift) > 0 )
                   myCar.css('left', myCar_position_left+= horizontal_shift);
             }
             else if(e.which === 38)                /* on press up */
             {
                if( track_height - myCar_position_bottom - myCar_height > 0)
                   myCar.css( 'bottom' , myCar_position_bottom+= vertical_shift);
             }
             else if(e.which === 40)                 /* on press down */
             {
               if( myCar_position_bottom > 0)
                  myCar.css( 'bottom' ,myCar_position_bottom-= vertical_shift );
             }
         });

     },3000);


    /* Cars to avoid */
    var car_to_avoid_block1 = $("#first-row-to-avoid");
    var car_to_avoid_block1_height = car_to_avoid_block1.height();
    var car_to_avoid_block1_current_position = parseInt(car_to_avoid_block1.css('top'));
    var car_to_avoid_block1_car1 = $('#first-row-to-avoid #carToAvoid1');
    var car_to_avoid_block1_car2 = $('#first-row-to-avoid #carToAvoid2');
    var car_to_avoid_block1_car3 = $('#first-row-to-avoid #carToAvoid3');

    var car_to_avoid_block2 = $('#second-row-to-avoid');
    var car_to_avoid_block2_height = car_to_avoid_block2.height();
    var car_to_avoid_block2_current_position = parseInt(car_to_avoid_block2.css('top'));
    var car_to_avoid_block2_car1 = $('#second-row-to-avoid #carToAvoid1');
    var car_to_avoid_block2_car2 = $('#second-row-to-avoid #carToAvoid2');
    var car_to_avoid_block2_car3 = $('#second-row-to-avoid #carToAvoid3');

    var cars_to_avoid_speed = 3;
    var random ;
    var lvl_count = 1;   /* the levels */


	
    setTimeout(function(){
          cars_to_avoid = setInterval(function() {

           if (collision(myCar,car_to_avoid_block1_car1) || collision(myCar,car_to_avoid_block1_car2) || collision(myCar,car_to_avoid_block1_car3) || collision(myCar,car_to_avoid_block2_car1) || collision(myCar,car_to_avoid_block2_car2) || collision(myCar,car_to_avoid_block2_car3))
              {
                   stopTheGame();
                  
              }
          else {

            if( lvl_count < 2 )
             {

                 if( car_to_avoid_block1_current_position > track_height)
                    {

                      car_to_avoid_block1_car1.hide();
                      car_to_avoid_block1_car2.hide();
                      car_to_avoid_block1_car3.hide();
                      random = Math.floor(Math.random()*3);

                      switch(random){
                           case 0: car_to_avoid_block1_car1.show();
                                   break;
                           case 1: car_to_avoid_block1_car2.show();
                                   break;
                           case 2: car_to_avoid_block1_car3.show();
                                   break;
                           default: alert("error");
                      }

                      car_to_avoid_block1.css('top',car_to_avoid_block1_current_position=-car_to_avoid_block1_height-35);

                      lvl_count++;
                    }

                 car_to_avoid_block1.css('top' , car_to_avoid_block1_current_position+=cars_to_avoid_speed);


             }
            else if(lvl_count >= 2 && lvl_count <= 5)
              {

                if( car_to_avoid_block1_current_position > track_height)
                   {
                     if (lvl_count == 3)
                       { cars_to_avoid_speed++;
                         divider_speed++;
                       }

                     car_to_avoid_block1_car1.hide();
                     car_to_avoid_block1_car2.hide();
                     car_to_avoid_block1_car3.hide();
                     random = Math.floor(Math.random()*3);

                     switch(random){
                          case 0: car_to_avoid_block1_car1.show();
                                  car_to_avoid_block1_car2.show();
                                  break;
                          case 1: car_to_avoid_block1_car2.show();
                                  car_to_avoid_block1_car3.show();
                                  break;
                          case 2: car_to_avoid_block1_car3.show();
                                  car_to_avoid_block1_car1.show();
                                  break;
                          default: alert("error");
                     }

                     car_to_avoid_block1.css('top',car_to_avoid_block1_current_position=-car_to_avoid_block1_height-35);

                     lvl_count++;
                   }

                car_to_avoid_block1.css('top' , car_to_avoid_block1_current_position+=cars_to_avoid_speed);


              }
           else if(lvl_count > 5 && lvl_count <=8)
             {


                if (car_to_avoid_block1_current_position > track_height && car_to_avoid_block2_current_position > track_height)
                  {
                      if (lvl_count == 7)
                        { cars_to_avoid_speed++;
                          divider_speed++;
                        }

                      car_to_avoid_block2_car1.hide();
                      car_to_avoid_block2_car2.hide();
                      car_to_avoid_block2_car3.hide();
                      random = Math.floor(Math.random()*3);

                      switch(random){
                              case 0: car_to_avoid_block2_car1.show();
                                      car_to_avoid_block2_car2.show()
                                      break;
                              case 1: car_to_avoid_block2_car2.show();
                                      car_to_avoid_block2_car3.show();
                                      break;
                              case 2: car_to_avoid_block2_car3.show();
                                      car_to_avoid_block2_car1.show();
                                      break;
                                      default: alert("error");
                       }



                      car_to_avoid_block1_car1.hide();
                      car_to_avoid_block1_car2.hide();
                      car_to_avoid_block1_car3.hide();
                      random = Math.floor(Math.random()*3);

                      switch(random){
                             case 0: car_to_avoid_block1_car1.show();
                                     break;
                             case 1: car_to_avoid_block1_car2.show();
                                     break;
                             case 2: car_to_avoid_block1_car3.show();
                                     break;
                             default: alert("error");
                      }

                     car_to_avoid_block1.css('top' , car_to_avoid_block1_current_position = -123 );
                     car_to_avoid_block2.css('top' , car_to_avoid_block2_current_position = -(369+Math.floor(Math.random()*180))) ;

                     lvl_count++;
                  }

                  car_to_avoid_block1.css('top' , car_to_avoid_block1_current_position+=cars_to_avoid_speed);
                  car_to_avoid_block2.css('top' , car_to_avoid_block2_current_position+=cars_to_avoid_speed);


              }

              else if(lvl_count > 8 && lvl_count <=12)
              {


                if (car_to_avoid_block1_current_position > track_height && car_to_avoid_block2_current_position > track_height)
                  {
                      if (lvl_count == 10)
                        {cars_to_avoid_speed++;
                         divider_speed++;
                        }

                      car_to_avoid_block2_car1.hide();
                      car_to_avoid_block2_car2.hide();
                      car_to_avoid_block2_car3.hide();
                      random = Math.floor(Math.random()*3);

                      switch(random){
                              case 0: car_to_avoid_block2_car1.show();
                                      car_to_avoid_block2_car2.show()
                                      break;
                              case 1: car_to_avoid_block2_car2.show();
                                      car_to_avoid_block2_car3.show();
                                      break;
                              case 2: car_to_avoid_block2_car3.show();
                                      car_to_avoid_block2_car1.show();
                                      break;
                                      default: alert("error");
                       }



                      car_to_avoid_block1_car1.hide();
                      car_to_avoid_block1_car2.hide();
                      car_to_avoid_block1_car3.hide();
                      random = Math.floor(Math.random()*3);

                      switch(random){
                             case 0: car_to_avoid_block1_car1.show();
                                     car_to_avoid_block1_car2.show();
                                     break;
                             case 1: car_to_avoid_block1_car2.show();
                                     car_to_avoid_block1_car3.show();
                                     break;
                             case 2: car_to_avoid_block1_car3.show();
                                     car_to_avoid_block1_car1.show();
                                     break;
                             default: alert("error");
                      }


                     car_to_avoid_block1.css('top' , car_to_avoid_block1_current_position = -143 );
                     car_to_avoid_block2.css('top' , ( car_to_avoid_block2_current_position = -(389+Math.abs(Math.floor(Math.random()*180 ) ) ) ) ) ;
                     lvl_count++;
                  }

                  car_to_avoid_block1.css('top' , car_to_avoid_block1_current_position+=cars_to_avoid_speed);
                  car_to_avoid_block2.css('top' , car_to_avoid_block2_current_position+=cars_to_avoid_speed);

              }
              else if(lvl_count > 12 )
              {


                if (car_to_avoid_block1_current_position > track_height && car_to_avoid_block2_current_position > track_height)
                  {
                      if (lvl_count%2==0)
                        {cars_to_avoid_speed++;
                         divider_speed++;
                        }

                      car_to_avoid_block2_car1.hide();
                      car_to_avoid_block2_car2.hide();
                      car_to_avoid_block2_car3.hide();
                      random = Math.floor(Math.random()*3);

                      switch(random){
                              case 0: car_to_avoid_block2_car1.show();
                                      car_to_avoid_block2_car2.show()
                                      break;
                              case 1: car_to_avoid_block2_car2.show();
                                      car_to_avoid_block2_car3.show();
                                      break;
                              case 2: car_to_avoid_block2_car3.show();
                                      car_to_avoid_block2_car1.show();
                                      break;
                                      default: alert("error");
                       }



                      car_to_avoid_block1_car1.hide();
                      car_to_avoid_block1_car2.hide();
                      car_to_avoid_block1_car3.hide();
                      random = Math.floor(Math.random()*3);

                      switch(random){
                             case 0: car_to_avoid_block1_car1.show();
                                     car_to_avoid_block1_car2.show();
                                     break;
                             case 1: car_to_avoid_block1_car2.show();
                                     car_to_avoid_block1_car3.show();
                                     break;
                             case 2: car_to_avoid_block1_car3.show();
                                     car_to_avoid_block1_car1.show();
                                     break;
                             default: alert("error");
                      }

                     car_to_avoid_block1.css('top' , car_to_avoid_block1_current_position = -143);
                     car_to_avoid_block2.css('top' ,car_to_avoid_block2_current_position = -(395+Math.floor(Math.random()*50 ) ) );
                     lvl_count++;
                  }

                  car_to_avoid_block1.css('top' , car_to_avoid_block1_current_position+=cars_to_avoid_speed);
                  car_to_avoid_block2.css('top' , car_to_avoid_block2_current_position+=cars_to_avoid_speed);

              }

            }

         },20);


    } , 6000);




    /* Game score */
    setTimeout( function(){

      count_the_score =  setInterval(function(){
            score_count+= 5;
        } , 4000 );

    } , 6000 );


	
    /* Check for collision between cars*/
    function collision($div1, $div2){
       var x1 = $div1.offset().left;
       var y1 = $div1.offset().top;
       var h1 = $div1.outerHeight(true);
       var w1 = $div1.outerWidth(true);
       var b1 = y1 + h1;
       var r1 = x1 + w1;
       var x2 = $div2.offset().left;
       var y2 = $div2.offset().top;
       var h2 = $div2.outerHeight(true);
       var w2 = $div2.outerWidth(true);
       var b2 = y2 + h2;
       var r2 = x2 + w2;

       if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;
          return true;
       }
	   
	   
	
    /* function for stopping the game */
    function stopTheGame(){

        clearInterval(window.cars_to_avoid);      /* window used as cars_to_avoid is local and cannot be accessed*/
        clearInterval(dividers_moving);
        clearInterval(window.count_the_score);
                
        myCar.hide();
        car_to_avoid_block1.hide();
	      car_to_avoid_block2.hide();
	     	dividers_first.hide();
	 	    dividers_second.hide();	
		
		
       $('#result').css('display','block');
	     $('#result #score').html("SCORE</br>" + score_count);
       
	   
	    $('#result i').click(function() {
           location.reload(window.location.href);
		   });
                           
	   
    }	   


   });



});

              
            
!
999px

Console