Skip to content

Instantly share code, notes, and snippets.

@bruno-de-queiroz
Created March 28, 2014 19:52
Show Gist options
  • Select an option

  • Save bruno-de-queiroz/9841653 to your computer and use it in GitHub Desktop.

Select an option

Save bruno-de-queiroz/9841653 to your computer and use it in GitHub Desktop.
game{ background:lightgrey; border:1px solid #CCC; height:400px; width:400px; position:relative; display:block;}
scene { display:block; position:relative; height:100%; min-width:100%;}
pipe { position:absolute; display:inline-block; background:green}
bird { position:absolute; bottom:0; transition: all 1s ease-in; height:10%; width:10%; background:black; border-radius:20px; }
bird:before { content:''; width:50%; height:50%; border-radius:10px; display:inline-block; background:green; position:absolute; left:0; top:50%; margin-top:-25%;}
bird:after { content:''; width:50%; height:50%; border-radius:10px; display:inline-block; background:green; position:absolute; right:-25%; top:50%; margin-top:-25%;}
bird.top { transition:all .3s ease-in-out; }
bird.initial { bottom:50%; margin-bottom:-5px; }
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-git2.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<game>
<bird class="initial"></bird>
<scene>
</scene>
</game>
</body>
</html>
document.createElement('game');
document.createElement('bird');
var bird = $('bird'),
game = $('game'),
scene = $('scene'),
jumpHeightPercent = 20,
pipeWidthPercent = 15,
time = Math.round(game.height()/100)/10,
birdPercent = bird.height()/game.height() * 100,
t = 0;
window.addEventListener('keypress', function( e ) {
if( bird.offset().top > 0){
var gameHeight = game.height(),
bottom = 100 - (bird.offset().top/gameHeight *100),
next = (bottom + jumpHeightPercent) > 100 ? bottom + (100 - bottom) : bottom + jumpHeightPercent,
dropTime = time + ( time * (next/100));
if( e.keyCode == 32 ){
bird.removeAttr( 'style' ).removeClass().addClass( 'top' ).css({
bottom : (next - birdPercent) + "%"
});
clearTimeout( t );
t = setTimeout(function() {
bird.removeClass( 'top' ).removeAttr( 'style' );
bird.css('-webkit-transition', 'all '+ dropTime + 's ease-in');
}, 300);
}
}
});
function getRandomPosition(){
var position = [ 'top', 'bottom' ];
return position[ Math.round( Math.random() * (position.length-1) ) ];
}
function getRandomPipeHeight(){
var maxPipeHeight = 100 - birdPercent;
return Math.round(Math.random() * maxPipeHeight);
}
function createRandomPipes(){
var pipe = document.createElement('pipe'),
pipePosition = getRandomPosition();
$( pipe ).css({
'width' : pipeWidthPercent + '%',
'height' : getRandomPipeHeight() + '%',
pipePosition : '0'
});
scene.append( pipe );
}
function buildScene(){
var pipesPerScene =
createRandomPipes();
}
buildScene();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment