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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Flappy Bird</title>
	<script type="text/javascript">
		var ctx;
		var cwidth = 400;
		var cheight = 600;
		var objects = [];
		var birdIndex = 0;
		var ver1 = 30;
		var ver2;
		var ver3;
		var ver4;
		var gravity = 2;
		var pipe_height = 200;
		var velocity = 20;
		var tid;
		var score = 0;
		var scores = 0;
		var isScores = 15;
		var isScore = false;

		var birds = [
			"https://wx3.sinaimg.cn/thumb150/7f08cdcfgy1fudypyxmhqg201600wjr5.gif", "https://wx2.sinaimg.cn/thumb150/7f08cdcfgy1fudypyxiwug201500vjr5.gif", "https://wx1.sinaimg.cn/thumb150/7f08cdcfgy1fudypyx2omg201500vjr5.gif"];

		var back = new Background(0, 0, 400, 600, "https://wx3.sinaimg.cn/mw690/7f08cdcfgy1fudypyytaoj20k00zkaah.jpg");
		var up_pipe = new UpPipe(0, 0, 100, 200, "https://zengxiaoluan.com/wp-content/uploads/2018/08/pipe.png");
		var down_pipe = new DownPipe(0, 400, 100, 200, "https://zengxiaoluan.com/wp-content/uploads/2018/08/pipe.png");
		var ground = new Background(0, 550, 400, 200, "https://wx1.sinaimg.cn/mw690/7f08cdcfgy1fudypyymiqj20nc07t0sk.jpg");
		var bird = new Bird(80, 300, 40, 40, birds);

		objects.push(back);
		objects.push(up_pipe);
		objects.push(down_pipe);
		objects.push(ground);
		objects.push(bird);

		function UpPipe(x, y, width, height, img_src) {
			this.px = x;
			this.py = y;
			this.pwidth = width;
			this.pheight = height;
			this.img_src = img_src;
			this.draw = drawUpPipe;
		}

		function DownPipe(x, y, width, height, img_src) {
			this.px = x;
			this.py = y;
			this.pwidth = width;
			this.pheight = height;
			this.img_src = img_src;
			this.draw = drawDownPipe;
		}

		function drawUpPipe() {
			var image = new Image();
			image.src = this.img_src;
			ctx.drawImage(image, 150, 500, 150, 800, this.px, this.py, this.pwidth, this.pheight);
		}

		function drawDownPipe() {
			var image = new Image();
			image.src = this.img_src;
			ctx.drawImage(image, 0, 500, 150, 500, this.px, this.py, this.pwidth, this.pheight);
		}

		function Background(x, y, width, height, img_src) {
			this.bgx = x;
			this.bgy = y;
			this.bgwidth = width;
			this.bgheight = height;
			var image = new Image();
			image.src = img_src;
			this.img = image;
			this.draw = drawbg;
		}

		function drawbg() {
			ctx.drawImage(this.img, this.bgx, this.bgy, this.bgwidth, this.bgheight);
		}

		function Bird(x, y, width, height, img_srcs) {
			this.bx = x;
			this.by = y;
			this.bwidth = width;
			this.bheight = height;
			this.imgs = img_srcs;
			this.draw = drawbird;
		}

		function drawbird() {
			birdIndex++;
			var image = new Image();
			image.src = this.imgs[birdIndex % 3];
			ctx.drawImage(image, this.bx, this.by, this.bwidth, this.bheight);
		}

		function calculator() {
			if(bird.by+bird.bheight>ground.bgy ||
				((bird.bx+bird.bwidth>up_pipe.px)&&(bird.by>up_pipe.py)&&(bird.bx+bird.bwidth<up_pipe.px+up_pipe.pwidth)&&(	bird.by<up_pipe.py+up_pipe.pheight))||
				((bird.bx+bird.bwidth>up_pipe.px)&&(bird.by>up_pipe.py)&&(bird.bx+bird.bwidth<up_pipe.px+up_pipe.pwidth)&&(	bird.by<up_pipe.py+up_pipe.pheight))||
				((bird.bx>down_pipe.px)&&(bird.by>down_pipe.py)&&(bird.bx<down_pipe.px+down_pipe.pwidth)&&(bird.by<down_pipe.py+down_pipe.pheight))||
				((bird.bx>down_pipe.px)&&(bird.by+bird.bheight>down_pipe.py)&&(bird.bx<down_pipe.px+down_pipe.pwidth)&&(bird.by+bird.bheight<down_pipe.py+down_pipe.pheight))){
				clearInterval(tid);
				ctx.fillStyle = "rgb(255,255,255)";
				ctx.font = "30px Accent";
				ctx.fillText("You got "+score+"!",110,300)
				return;
			}

			ver2 = ver1 + gravity;
			bird.by += (ver2 + ver1) * 0.1;
			// bird.by = ver1

			if (up_pipe.px + up_pipe.pwidth > 0) {
				up_pipe.px -= velocity;
				down_pipe.px -= velocity;
			} else {
				up_pipe.px = 400;
				down_pipe.px = 400;
				up_pipe.pheight = 100 + Math.random() * 200;
				down_pipe.py = up_pipe.pheight + pipe_height;
				down_pipe.pheight = 600 - down_pipe.py;
				isScore = true;
			}

			if (isScore && bird.bx > up_pipe.px + up_pipe.pwidth) {
				score += 1;
				scores = score;
				isScore = false;
				ctxs();
				if (score > 0 && score % 10 === 0) {
					velocity++;
				}
			}

			if (score > 0) {
				score % 10 !== 0 ? ctx.fillText(score, 180, 100) : ctx.fillText("Great!" + score, 120, 100);
			}
		}

		function drawall() {
			ctx.clearRect(0, 0, cwidth, cheight);
			var i;
			for (i = 0; i < objects.length; i++) {
				objects[i].draw();
			}
			calculator();
		}

		function keyup(e) {
			var e = e || event;
			var currKey = e.keyCode || e.which || e.charCode;
			switch (currKey) {
				case 32:
					bird.by -= 80;
					break;
			}
		}

		function init() {
			ctx = document.getElementById('canvas').getContext('2d');
			document.onkeyup = keyup;
			tid = setInterval(drawall, 80);
		}
	</script>
</head>

<body onload="init();">
	<canvas id="canvas" width="400" height="600" style="margin-left:100px;">
		Your browser is not support canvas!
	</canvas>
</body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console