<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Flappy Bird</title> <link rel="stylesheet" href="style.css"> </head> <body> <canvas id="canvas"></canvas> <script src="flappybird.js"></script> </body> </html> // Get the canvas element var canvas = document.getElementById("canvas"); // Set the canvas dimensions var width = canvas.width = window.innerWidth; var height = canvas.height = window.innerHeight; // Get the canvas context var ctx = canvas.getContext("2d"); // Set the game variables var bird = { x: width / 2, y: height / 2, radius: 20, speed: 5, gravity: 1, jump: 20, alive: true }; var pipes = []; var pipeGap = 150; var pipeFrequency = 100; var score = 0; // Game loop function gameLoop() { // Clear the canvas ctx.clearRect(0, 0, width, height); // Move and draw the bird moveBird(); drawBird(); // Move and draw the pipes movePipes(); drawPipes(); // Check for collisions checkCollisions(); // Update the score updateScore(); // Check if the bird is alive if (bird.alive) { // Call the game loop again requestAnimationFrame(gameLoop); } else { // Game over alert("Game Over"); } } // Move the bird function moveBird() { // Apply gravity bird.y += bird.gravity; // Jump when spacebar is pressed window.addEventListener("keydown", function (event) { if (event.keyCode === 32) { bird.y -= bird.jump; } }); // Check if the bird is offscreen if (bird.y > height || bird.y < 0) { bird.alive = false; } } // Draw the bird function drawBird() { ctx.fillStyle = "yellow"; ctx.beginPath(); ctx.arc(bird.x, bird.y, bird.radius, 0, Math.PI * 2); ctx.closePath(); ctx.fill(); } // Move the pipes function movePipes() { // Add a new pipe every pipeFrequency frames if (frames % pipeFrequency === 0) { var pipe = { x: width, y: Math.floor(Math.random() * (height - pipeGap)), width: 50, height: pipeGap }; pipes.push(pipe); } // Move the pipes to the left for (var i = 0; i < pipes.length; i++) { pipes[i].x -= bird.speed; } // Remove the pipes that are offscreen if (pipes[0].x + pipes[0].width < 0) { pipes.shift(); } } // Draw the pipes function drawPipes() { for (var i = 0; i < pipes.length; i++) { ctx.fillStyle = "green"; ctx.fillRect(pipes[i].x, pipes[i].y, pipes[i].width, pipes[i].height); ctx.fillRect(pipes[i].x, pipes[i].y + pipes[i].height + pipeGap, pipes[i].width, height - pipes[i].y - pipes[i].height - pipeGap); } } // Check for collisions function checkCollisions() { // Check for bird-pipe collisions for (var i = 0; i < pipes.length; i++) { if (bird.x + bird.radius > pipes[i].x && bird.x - bird.radius < pipes[i].x + pipes[i].width && bird.y + bird.radius > pipes[i].y && bird.y - bird.radius < pipes[i].y + pipes[i].height) { bird.alive = false; } if (bird.x + bird.radius > pipes[i].x && bird.x - bird.radius < pipes[i].x + pipes[i].width && bird.y + bird.radius > pipes[i].y + pipes[i].height + pipeGap && bird.y - bird.radius < height) { bird.alive = false; } } } // Update the score function updateScore() { for (var i = 0; i < pipes.length; i++) { if (pipes[i].x + pipes[i].width < bird.x && !pipes[i].scored) { score++; pipes[i].scored = true; } } ctx.fillStyle = "black"; ctx.font = "30px Arial"; ctx.fillText("Score: " + score, 10, 50); } // Start the game loop var frames = 0; gameLoop();
Write, Run & Share HTML code online using OneCompiler's HTML online Code editor for free. It's one of the robust, feature-rich online Code editor for HTML language, running on the latest version HTML5. Getting started with the OneCompiler's HTML compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as HTML
. You can also specify the stylesheet information in styles.css
tab and scripts information in scripts.js
tab and start coding.
HTML(Hyper Text Markup language) is the standard markup language for Web pages, was created by Berners-Lee in the year 1991. Almost every web page over internet might be using HTML.
<!DOCTYPE html>
<html>
and ends with </html>
<h1>
to <h6>
where <h1>
is the highest important heading and <h6>
is the least important sub-heading.<p>..</p>
tag.<a>
tag.
<a href="https://onecompiler.com/html">HTML online compiler</a>
<img>
tag, where src
attribute consists of image name.<button>..</button>
tag<ul>
for unordered/bullet list and <ol>
for ordered/number list, and the list items are defined in <li>
.<a href="https://onecompiler.com/html">HTML online compiler</a>
CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.
Below is a sample style sheet which displays heading in green and in Candara font with padding space of 25px.
body{
padding: 25px;
}
.title {
color: #228B22;
font-family: Candara;
}
<table>
tag.<tr>
tag<th>
tag<td>
tag<caption>
tag<script>
is the tag used to write scripts in HTML<script src="script.js"></script>