<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flappy Bird</title>
<style>
    body {
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #70c5ce; /* Sky color */
    }
    #canvas {
        display: block;
        margin: 0 auto;
        background-color: #70c5ce; /* Sky color */
        border: 2px solid #000; /* Border for the canvas */
    }
</style>
</head>
<body>
<canvas id="canvas" width="400" height="600"></canvas>
<script>
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');

    const bird = {
        x: 50,
        y: canvas.height / 2,
        size: 25, // Size of the bird
        velocity: 0,
        gravity: 0.15 // Decreased gravity
    };

    let pipes = [];
    let score = 0;
    let passedPipe = false; // Track if bird has passed a pipe

    const pipeWidth = 50;
    const pipeGap = 200; // Increased distance between pipes
    const pipeSpeed = 2;
    const pipeColor = '#008000'; // Green color for pipes
    const pipeBorderColor = '#004d00'; // Darker color for depth

    let gameOver = false;

    function drawBird() {
        ctx.fillStyle = '#f0a200'; // Yellow color for bird
        ctx.fillRect(bird.x, bird.y, bird.size, bird.size);
        ctx.fillStyle = '#ff0000'; // Red color for bird's beak
        ctx.fillRect(bird.x + bird.size, bird.y + bird.size / 2 - 3, 5, 5);
        ctx.fillStyle = '#000'; // Black color for bird's eye
        ctx.fillRect(bird.x + bird.size / 2 + 5, bird.y + bird.size / 4, 5, 5);
    }

    function drawPipe(pipe) {
        ctx.fillStyle = pipeBorderColor;
        ctx.fillRect(pipe.x, pipe.y, pipe.width, pipe.height);
       
        ctx.fillStyle = pipeColor;
        ctx.fillRect(pipe.x + 5, pipe.y + 5, pipe.width - 10, pipe.height - 10);
       
        // Draw the distinctive feature of the pipe
        ctx.fillStyle = pipeBorderColor;
        ctx.fillRect(pipe.x + pipe.width - 15, pipe.y + pipe.height / 2 - 7, 15, 15);
    }

    function createPipe() {
        const randomPos = Math.floor(Math.random() * (canvas.height - pipeGap));
        const topPipe = {
            x: canvas.width,
            y: 0,
            width: pipeWidth,
            height: randomPos
        };
        const bottomPipe = {
            x: canvas.width,
            y: randomPos + pipeGap,
            width: pipeWidth,
            height: canvas.height - randomPos - pipeGap
        };
        pipes.push(topPipe);
        pipes.push(bottomPipe);
    }

    function movePipes() {
        pipes.forEach(pipe => {
            pipe.x -= pipeSpeed;
        });
        pipes = pipes.filter(pipe => pipe.x > -pipeWidth);

        // Check if bird passes a pipe
        pipes.forEach(pipe => {
            if (pipe.x + pipe.width < bird.x && !pipe.passed) {
                pipe.passed = true;
                passedPipe = true; // Mark that bird has passed a pipe
            }
        });

        // Increment score only if bird has passed a pipe and not already counted in this frame
        if (passedPipe) {
            score++;
            passedPipe = false; // Reset passedPipe for next frame
        }
    }

    function draw() {
        // Draw sky background
        ctx.fillStyle = '#70c5ce'; // Sky color
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        // Draw ground
        ctx.fillStyle = '#8B4513'; // Brown color for ground
        ctx.fillRect(0, canvas.height - 50, canvas.width, 50);

        drawBird();
        pipes.forEach(drawPipe);

        // Draw score
        ctx.fillStyle = '#000';
        ctx.imageSmoothingEnabled = false; // Disable smoothing to create a pixelated effect
        ctx.font = '24px "Press Start 2P"'; // Use a pixel-style font
        ctx.fillText('Score: ' + score, 10, 30);
    }

    function update() {
        bird.velocity += bird.gravity;
        bird.y += bird.velocity;
        if (bird.y + bird.size >= canvas.height - 50 || bird.y <= 0) {
            gameOver = true;
        }
        pipes.forEach(pipe => {
            if (
                bird.x < pipe.x + pipe.width &&
                bird.x + bird.size > pipe.x &&
                bird.y < pipe.y + pipe.height &&
                bird.y + bird.size > pipe.y
            ) {
                gameOver = true;
            }
        });
        if (!gameOver) {
            if (pipes.length === 0 || pipes[pipes.length - 1].x < canvas.width - 300) {
                createPipe();
            }
            movePipes();
        }
    }

    function gameLoop() {
        update();
        draw();
        if (!gameOver) {
            requestAnimationFrame(gameLoop);
        } else {
            ctx.fillStyle = '#000';
            ctx.font = '40px Arial';
            ctx.fillText('Game Over', canvas.width / 2 - 100, canvas.height / 2);
        }
    }

    document.addEventListener('keydown', function(event) {
        if (event.code === 'Space') {
            bird.velocity = -3.5; // Adjust jump height
        }
    });

    gameLoop();
</script>
</body>
</html> 

HTML Online Editor & Compiler

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.

About HTML

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.

Syntax help

Fundamentals

  • Any HTML document must start with document declaration <!DOCTYPE html>
  • HTML documents begin with <html> and ends with </html>
  • Headings are defined with <h1> to <h6> where <h1> is the highest important heading and <h6> is the least important sub-heading.
  • Paragrahs are defined in <p>..</p> tag.
  • Links are defined in <a> tag.

    Example:

    <a href="https://onecompiler.com/html">HTML online compiler</a>
    
  • Images are defined in <img> tag, where src attribute consists of image name.
  • Buttons are defined in <button>..</button> tag
  • Lists are defined in <ul> for unordered/bullet list and <ol> for ordered/number list, and the list items are defined in <li>.

HTML Elements and Attributes

  • HTML element is everything present from start tag to end tag.
  • The text present between start and end tag is called HTML element content.
  • Anything can be a tagname but it's preferred to put the meaningful title to the content present as tag name.
  • Do not forget the end tag.
  • Elements with no content are called empty elements.
  • Elements can have attributes which provides additional information about the element.
  • In the below example, href is an attribute and a is the tag name.

    Example:

    <a href="https://onecompiler.com/html">HTML online compiler</a>
    

CSS

CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.

Example:

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;
}

HTML Tables

  • HTML Tables are defined in <table> tag.
  • Table row should be defined in <tr> tag
  • Table header should be defined in <th> tag
  • Table data should be defined in <td> tag
  • Table caption should be defined in <caption> tag

HTML-Javascript

  • Javascript is used in HTML pages to make them more interactive.
  • <script> is the tag used to write scripts in HTML
  • You can either reference a external script or write script code in this tag.

Example

<script src="script.js"></script>