Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="x-ua-compatible" content="ie=edge">
- <title>Flappy Bird</title>
- <meta name="description" content="">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
- <link rel="icon" href="">
- <style>
- * {
- font-family: sans-serif;
- font-size: 16px;
- width: 100%;
- height: 100%;
- padding: 0;
- margin: 0;
- }
- canvas {
- width: 100%;
- height: 99.6%;
- }
- </style>
- </head>
- <body>
- <canvas id="canvas"></canvas>
- <script>
- var canvas = document.getElementById("canvas");
- var ctx = canvas.getContext("2d");
- var winW = window.innerWidth;
- var winH = window.innerHeight;
- canvas.width = window.innerWidth;
- canvas.height = window.innerHeight;
- document.onkeydown = checkKey;
- var gameSpeed = 30;
- var x,y,score;
- var scored = false;
- var jumpH = winH/10;
- var gravity = winH/750;
- var maxGrav = 15;
- const pillarCount = 4;
- var pillarX = [];
- var pillarY1 = [];
- var pillarY2 = [];
- var pillarW = winW/(pillarCount*2.75+1);
- var pillarDist = winH/5;
- var ballR = jumpH/3.5;
- setup();
- function timeout() {
- setTimeout(function () {
- main();
- timeout();
- }, gameSpeed);
- };
- timeout();
- function setup()
- {
- x = winW/10;
- y = winH/2;
- score = 0;
- for(var i = 0; i < pillarCount; i++)
- {
- if(i === 0)
- {
- pillarX[i] = winW;
- }
- else
- {
- pillarX[i] = pillarX[(i-1)] + pillarW*4;
- }
- pillarY1[i] = Math.floor(Math.random() * ((winH-winH/5) - (winH/10) + 1)) + (winH/10);
- pillarY2[i] = pillarY1[i] + pillarDist;
- }
- }
- function main()
- {
- logic();
- move();
- draw();
- }
- function move()
- {
- if(gravity < maxGrav/2)
- {
- gravity *= 1.1;
- }
- else if(gravity < maxGrav)
- {
- gravity *= 1.05;
- }
- y += gravity;
- for(var i = 0; i < pillarCount; i++)
- {
- pillarX[i] -= 10;
- }
- }
- function logic()
- {
- if(y < 0 || y > winH)
- {
- alert("You died with a score of " + score);
- setup();
- }
- for(var i = 0; i < pillarCount; i++)
- {
- if(x >= pillarX[i] && x <= pillarX[i]+pillarW && y-ballR/1.5 > pillarY1[i] && y+ballR/1.5 < pillarY2[i])
- {
- if(!scored)
- {
- score++;
- scored = true;
- }
- }
- else if(x+ballR/1.5 >= pillarX[i] && x-ballR/1.5 <= pillarX[i]+pillarW && (y-ballR/1.5 < pillarY1[i] || y+ballR/1.5 > pillarY2[i]))
- {
- alert("You died with a score of " + score);
- setup();
- }
- if(scored)
- {
- if(x > pillarX[i]+pillarW)
- {
- scored = false;
- }
- }
- if(pillarX[i]+pillarW <= 0)
- {
- pillarX[i] += winW + pillarW*4;
- pillarY1[i] = Math.floor(Math.random() * ((winH-winH/5) - (winH/10) + 1)) + (winH/10);
- pillarY2[i] = pillarY1[i] + pillarDist;
- }
- }
- }
- function draw()
- {
- rect(0,0, winW,winH, '#0088CC');
- circle(x,y, ballR,'#CCCC00');
- for(var i = 0; i < pillarCount; i++)
- {
- rect(pillarX[i],0, pillarW,pillarY1[i],'#007700');
- rect(pillarX[i],pillarY2[i], pillarW,(winH-pillarY2[i]),'#007700');
- }
- ctx.fillStyle = 'red';
- ctx.font = 'normal bold 42px sans-serif';
- ctx.textAlign = 'center';
- ctx.fillText(score,winW/2,winH/15);
- }
- function rect(x,y, width,height, color)
- {
- ctx.fillStyle = color;
- ctx.fillRect(x,y, width,height);
- }
- function circle(cX,cY, radius, color)
- {
- ctx.fillStyle = color;
- ctx.beginPath();
- ctx.arc(cX,cY, radius,0,Math.PI*2,true);
- ctx.fill();
- }
- function checkKey(e)
- {
- var keynum;
- if(window.event) { // IE
- keynum = e.keyCode;
- } else if(e.which){ // Netscape/Firefox/Opera
- keynum = e.which;
- }
- switch(keynum)
- {
- case 32:
- gravity = 0;
- j = Math.floor(jumpH/2);
- (function theLoop (i) {
- setTimeout(function () {
- y -= 2;
- if (--i) {
- theLoop(i);
- }
- }, 1);
- })(j);
- setTimeout(function () { gravity = winH/750; }, j);
- break;
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement