Skip to content

Instantly share code, notes, and snippets.

Created July 20, 2016 18:13
Show Gist options
  • Save anonymous/60f2bf204ba325a0308ed2b7362243d3 to your computer and use it in GitHub Desktop.
Save anonymous/60f2bf204ba325a0308ed2b7362243d3 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/yozafiz
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script>
/*
* Author: Ivan Jiang
*
* A happy flappy bird built with HTML5 Canvas.
*
*/
var JS_FLAPPYBIRD = {};
JS_FLAPPYBIRD = (function() {
var ms = 500,
sceneHeight = 300,
sceneWidth = 200,
blockSize = 10,
spaceHeight = 30;
var context;
var init = function() {
var $canvas = $('<canvas id="flappybird" />')
.appendTo('body')
.attr('width', sceneWidth + 'px')
.attr('height', sceneHeight + 'px');
var canvas = $canvas[0];
context = canvas.getContext('2d');
context.fillStyle = '#97EFD5'; // May be a sexy color
context.fillRect(0, (sceneHeight - blockSize) / 2, blockSize, blockSize);
this.gameLoop(context);
}
var gameLoop = function() {
var randomH = parseInt(Math.random() * sceneHeight),
high = randomH - spaceHeight / 2, // high point
low = randomH + spaceHeight / 2; // low point
context.clearRect(0, 0, sceneWidth, sceneHeight);
// Make a new wall from the right side.
context.fillStyle = 'red';
context.fillRect(sceneWidth - blockSize, 0, blockSize, high);
context.fillRect(sceneWidth - blockSize, low, blockSize, sceneHeight - low);
this.bird.draw(context);
setTimeout(gameLoop, ms);
}
return {
init: init,
gameLoop: gameLoop,
sceneHeight: sceneHeight,
blockSize: blockSize,
}
})();
JS_FLAPPYBIRD.bird = (function() {
var blockSize = JS_FLAPPYBIRD.blockSize,
sceneHeight = JS_FLAPPYBIRD.sceneHeight;
var y = (sceneHeight - blockSize) / 2;
var jump = function() {
y += blockSize;
};
var draw = function(context) {
--y;
context.clearRect(0, 0, blockSize, sceneHeight);
};
})();
JS_FLAPPYBIRD.init();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment