cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div class="welcomePop">
  <div class="textWrap">
    <h2>Welcome, human.</h2>
    <p>Use your spacebar to keep me alive</p>
    <div class="cta ctaStartGame">Start the game</div>
  </div>
</div>
<div class="restartGamePop">
  <div class="textWrap">
    <h2>Humans...</h2>
    <div class="cta ctaRestartGame">Play again</div>
  </div>
</div>

<div id="stage">
  <div id="credits"><span>0</span> <p>credits</p></div>
  <div id="ceiling" class="enemy"></div>
  <div id="bird"><img src="http://thomaspodgro.com/pen/ufo.png" alt="" /></div>
  <div id="ground" class="enemy"></div>
</div>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Montserrat:400,700');

* {
  margin:0;
  padding:0;
  outline:none;
  list-style:none;
  text-decoration:none;
  box-sizing:border-box;
  color:#111;
  background: transparent;
  border:none;
}

body {
  color: #fff;
  background: #111;
  font-family: 'Montserrat', sans-serif;
}

h1, h2, h3 {
  font-family: 'Montserrat', sans-serif;
  font-weight: 700;
}

#stage{
  width: 100%;
  height: 100vh;
  background:url(https://dl.dropboxusercontent.com/s/tsx10smmmcwegyr/test_background_game.jpg) repeat-x;
  background-position: 0 bottom;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
  animation: bgAnim 5s linear infinite;
}

#bird{
  width: 100px;
  height: 100px;
  border-radius: 50%;
  position: absolute;
  top:8%;
  left: 8%;
  transition: 1s all linear;
}

#bird.goDown {
  top: calc(100% - 100px);
  transition: 1s all ease-in;
}
#bird.birdMove {
  top: -100px;
  transition: 0.7s all linear;
}

#bird:after {
  content:"";
  background: url('http://thomaspodgro.com/pen/fire.png') no-repeat center center;
  background-size: contain;
  height: 50px;
  width: 25px;
  display: block;
  position: absolute;
  left: 0;right: 0;
  margin: auto;
  bottom: 0;
  top: 120%;
  z-index: 0;
  transition: 0.3s all linear;
  transform: scale(0);
  -webkit-transform: scale(0);
  -moz-transform: scale(0);
  transform-origin: top;
  -webkit-transform-origin: top;
  -moz-transform-origin: top;
}

#bird.birdMove:after {
  transform: scale(1);
  -webkit-transform: scale(1);
  -moz-transform: scale(1);
}
#bird img {
  width: 100%;
}



.enemy{
  position: absolute;
  height:50px;
  width: 50px;
}

#ceiling.enemy{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 5px;
  background-color: red;
}

#ground.enemy{
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 5px;
  background-color: red;
}

.item.enemy {
  width: 100px; 
  height: 100px;
}
.item.enemy:after {
  content: "";
  position: absolute;
  height: 200%;
  width: 200%;
  background: url(https://dl.dropboxusercontent.com/s/b62iv37npwonxht/smash.png) no-repeat center center;
  background-size: contain;
  transform: translate(-25%,-25%);
}

.item.gold {
  width: 100px;
  height: 100px;
  position: absolute;
}
.item.gold.goldFound {
  transform: scale(0);
  -webkit-transform: scale(0);
  -moz-transform: scale(0);
  transition: 0.3s all ease-out;
  -webkit-transition: 0.3s all ease-out;
  -moz-transition: 0.3s all ease-out;
}
.item.gold:after {
  content: "";
  position: absolute;
  height: 200%;
  width: 200%;
  background:url(https://dl.dropboxusercontent.com/s/2e14tq4rq8oddwr/gold.png) no-repeat center center;
  background-size: contain;
  transform: translate(-25%,-25%);
}

.textWrap {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  text-align: center;
}

.textWrap h2 {
  font-size: 72px;
  margin-bottom: 30px;
  color: #fff;
}
.textWrap p {
  margin-bottom: 40px;
  color: #fff;
  font-size: 30px;
}

.welcomePop, .restartGamePop {
  position: absolute;
  top: 0;left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  background: rgba(0,0,0,0.7);
  display: none;
}

.cta {
  display: inline-block;
  background: red;
  padding:20px 50px;
  color: #fff;
  text-transform: uppercase;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

#credits {
  position: absolute;
  top: 30px;
  left: 0;right: 0;
  margin: auto;
  font-size: 30px;
  width: 100%;
  text-align: center;
}
#credits p, #credits span {
  color: #fff;
  display: inline-block;
}


@keyframes bgAnim {
  0% {
    background-position: 0 bottom;
  }
  100%{
    background-position: -1920px bottom;
  }
}

-webkit-@keyframes bgAnim {
  0% {
    background-position: 0 bottom;
  }
  100%{
    background-position: -1920px bottom;
  }
}

-moz-@keyframes bgAnim {
  0% {
    background-position: 0 bottom;
  }
  100%{
    background-position: -1920px bottom;
  }
}
            
          
!
            
              var gameplaying = false;
var hitDetect;
var enemyInt;
var stage = $('#stage');
var gameCredits;

showWelcome();
$('#stage').css({'animation-play-state':'paused'});

$('.ctaRestartGame').click(function(){
  $('.item').remove();
  $('#bird').removeClass().attr('style', '');
  $('.restartGamePop').fadeOut();
  setTimeout(function(){
    $('#stage').css({'animation-play-state':'running'});
    gameplaying = true;
    gravitypull();
    hitDetect = setInterval(charHit, 5);
    enemyInt = setInterval(createItems, 600);
  },500);
});

$('.ctaStartGame').click(function(){
  $('.welcomePop').fadeOut();
  setTimeout(function(){
    $('#stage').css({'animation-play-state':'running'});
    gameplaying = true;
    gravitypull();
    hitDetect = setInterval(charHit, 5);
    enemyInt = setInterval(createItems, 600);
  },500);
});

$(document).keydown( function(e){

  if(e.keyCode == 32){
    if(gameplaying){
      $('#bird').removeClass('goDown').addClass('birdMove');
    }
  }

}); //end keydown

$(document).keyup(function(evt) {
  if (evt.keyCode == 32) {
    if(gameplaying){
      $('#bird').removeClass('birdMove').addClass('goDown');
    }
  }
});//end keyup

function gravitypull(){

  if(gameplaying){
    $('#bird').removeClass('birdMove').addClass('goDown');
  }
}


function charHit(){
  //check for enemies
  $('.enemy').each( function(){
    if( rectHit($('#bird'), $(this)) ){
      var birdPos = $('#bird').position().top;
      //remove enemies
      $(this).stop();
      $('#bird').css('top', birdPos);
      gameplaying = false;
      clearInterval(hitDetect);
      clearInterval(enemyInt);
      showRestartGame();
      $('#stage').css({'animation-play-state':'paused'});
    }
  });
  
  //check for credits
  $('.gold').each( function(){
    if( rectHit($('#bird'), $(this)) ){
      $(this).addClass('goldFound');
      //$('#bird').css('top', birdPos);
      updateCredits();
      setTimeout(function(){
        $(this).remove();
      },300);
    }
  });
}

function updateCredits() {
  gameCredits = Number($('#credits span').html());
  $('#credits span').html(gameCredits + 1);
}

function createItems(){
  //console.log('createItems');
  //create enemy
  var item = $('<div>').addClass('item');
  //decide if this is an enemy 1 or 2
  if(Math.random() > 0.3){
    item.addClass('enemy');
  }else{
    item.addClass('gold');        
  }

  //starting location for my enemies
  var eleft = stage.width() + 50;
  var etop = Math.random() * (stage.height()-50); //0 to (gameheight-50);

  item.css({top:etop, left:eleft});

  //add to stage:
  stage.append(item);

  //move enemy from right to left    
  item.animate({left: -100}, 3000, 'linear', function(){
    //callback function runs after animation is complete
    $(this).remove();

  });//end animate    

}//end createItems()    


//hit detect:
function rectHit(rectone, recttwo){
  //console.log('recthit called');
  var r1 = $(rectone);
  var r2 = $(recttwo);

  var r1x = r1.offset().left;
  var r1w = r1.width();
  var r1y = r1.offset().top;
  var r1h = r1.height();

  var r2x = r2.offset().left;
  var r2w = r2.width();
  var r2y = r2.offset().top;
  var r2h = r2.height();

  if( r1y+r1h < r2y ||
     r1y > r2y+r2h ||
     r1x > r2x+r2w ||
     r1x+r1w < r2x ){
    //console.log('recthit-false')
    return false;
  }else{
    //console.log('recthit-true')
    return true;
  }

}//end function 

function showWelcome() {
  $('.welcomePop').fadeIn();
}
function showRestartGame() {
  $('.restartGamePop').fadeIn();
}
            
          
!
999px
Loading ..................

Console