Java Code To Create Flappy Bird

Feel Free To Use This Code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;

public class BirdFlyingGame implements ActionListener, KeyListener, MouseListener {

    private final int WIDTH = 800, HEIGHT = 600;
    private JFrame frame;
    private Renderer renderer;
    private Rectangle bird;
    private ArrayList<Rectangle> pipes;
    private Random random;

    private int ticks, yMotion, score;
    private boolean gameOver, started;

    public BirdFlyingGame() {
        frame = new JFrame();
        Timer timer = new Timer(20, this);
        renderer = new Renderer();
        random = new Random();

        frame.add(renderer);
        frame.setTitle("Bird Flying Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH, HEIGHT);
        frame.addKeyListener(this);
        frame.addMouseListener(this);
        frame.setResizable(false);
        frame.setVisible(true);

        bird = new Rectangle(WIDTH / 4 - 10, HEIGHT / 2 - 10, 20, 20);
        pipes = new ArrayList<>();
        addPipe(true);
        addPipe(true);

        timer.start();
    }

    public void addPipe(boolean start) {
        int space = 300;
        int pipeWidth = 100;
        int pipeHeight = 50 + random.nextInt(300);

        if (start) {
            pipes.add(new Rectangle(WIDTH + pipeWidth + pipes.size() * 300, HEIGHT - pipeHeight - 120, pipeWidth, pipeHeight));
            pipes.add(new Rectangle(WIDTH + pipeWidth + (pipes.size() - 1) * 300, 0, pipeWidth, HEIGHT - pipeHeight - space));
        } else {
            pipes.add(new Rectangle(pipes.get(pipes.size() - 1).x + 600, HEIGHT - pipeHeight - 120, pipeWidth, pipeHeight));
            pipes.add(new Rectangle(pipes.get(pipes.size() - 1).x, 0, pipeWidth, HEIGHT - pipeHeight - space));
        }
    }

    public void paintPipe(Graphics g, Rectangle pipe) {
        g.setColor(Color.green.darker());
        g.fillRect(pipe.x, pipe.y, pipe.width, pipe.height);
    }

    public void jump() {
        if (gameOver) {
            bird = new Rectangle(WIDTH / 4 - 10, HEIGHT / 2 - 10, 20, 20);
            pipes.clear();
            yMotion = 0;
            score = 0;

            addPipe(true);
            addPipe(true);

            gameOver = false;
        }

        if (!started) {
            started = true;
        }

        if (yMotion > 0) {
            yMotion = 0;
        }

        yMotion -= 10;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int speed = 10;

        if (started) {
            ticks++;

            for (int i = 0; i < pipes.size(); i++) {
                Rectangle pipe = pipes.get(i);
                pipe.x -= speed;
            }

            if (ticks % 2 == 0 && yMotion < 15) {
                yMotion += 2;
            }

            for (int i = 0; i < pipes.size(); i++) {
                Rectangle pipe = pipes.get(i);

                if (pipe.x + pipe.width < 0) {
                    pipes.remove(pipe);

                    if (pipe.y == 0) {
                        addPipe(false);
                    }
                }
            }

            bird.y += yMotion;

            for (Rectangle pipe : pipes) {
                if (pipe.y == 0 && bird.x + bird.width / 2 > pipe.x + pipe.width / 2 - 10 && bird.x + bird.width / 2 < pipe.x + pipe.width / 2 + 10) {
                    score++;
                }

                if (pipe.intersects(bird)) {
                    gameOver = true;

                    if (bird.x <= pipe.x) {
                        bird.x = pipe.x - bird.width;
                    } else {
                        if (pipe.y != 0) {
                            bird.y = pipe.y - bird.height;
                        } else if (bird.y < pipe.height) {
                            bird.y = pipe.height;
                        }
                    }
                }
            }

            if (bird.y > HEIGHT - 120 || bird.y < 0) {
                gameOver = true;
            }

            if (bird.y + yMotion >= HEIGHT - 120) {
                bird.y = HEIGHT - 120 - bird.height;
            }
        }

        renderer.repaint();
    }

    public void repaint(Graphics g) {
        g.setColor(Color.cyan);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.orange);
        g.fillRect(0, HEIGHT - 120, WIDTH, 120);

        g.setColor(Color.green);
        g.fillRect(0, HEIGHT - 120, WIDTH, 20);

        g.setColor(Color.red);
        g.fillRect(bird.x, bird.y, bird.width, bird.height);

        for (Rectangle pipe : pipes) {
            paintPipe(g, pipe);
        }

        g.setColor(Color.white);
        g.setFont(new Font("Arial", Font.PLAIN, 100));

        if (!started) {
            g.drawString("Click to Start", 100, HEIGHT / 2 - 50);
        }

        if (gameOver) {
            g.drawString("Game Over", 100, HEIGHT / 2 - 50);
        }

        if (!gameOver && started) {
            g.setFont(new Font("Arial", Font.PLAIN, 50));
            g.drawString(String.valueOf(score), WIDTH / 2 - 25, 100);
        }
    }

    public static void main(String[] args) {
        new BirdFlyingGame();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        jump();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            jump();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {}

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}
}

class Renderer extends JPanel {
    private static final long serialVersionUID = 1L;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        BirdFlyingGame game = new BirdFlyingGame();
        game.repaint(g);
    }
}
Java

Html and Css Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bird Flying Game</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f8ff;
            text-align: center;
            margin: 0;
            padding: 0;
        }

        header {
            background-color: #00bfff;
            padding: 20px;
            color: white;
        }

        h1 {
            margin: 0;
            font-size: 36px;
        }

        .container {
            margin: 20px auto;
            max-width: 800px;
        }

        .game-info {
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .game-info h2 {
            color: #00bfff;
            font-size: 24px;
        }

        .play-area {
            margin-top: 30px;
            border: 2px solid #00bfff;
            padding: 10px;
            background-color: #f5f5f5;
        }

        button {
            padding: 10px 20px;
            font-size: 18px;
            background-color: #00bfff;
            color: white;
            border: none;
            cursor: pointer;
            margin: 10px;
            border-radius: 5px;
        }

        button:hover {
            background-color: #007acc;
        }
    </style>
</head>
<body>

<header>
    <h1>Bird Flying Game</h1>
</header>

<div class="container">
    <div class="game-info">
        <h2>Instructions:</h2>
        <p>Use the spacebar or click to control the bird. Avoid the pipes and score points by passing through them!</p>
    </div>

    <div class="play-area">
        <h2>Game is loading...</h2>
        <p>Start the game by pressing the button below.</p>
        <button onclick="startGame()">Start Game</button>
    </div>
</div>

<script>
    function startGame() {
        // This would trigger the Java game if we were linking it
        alert("Starting the Bird Flying game...");
        // The Java game starts as a separate window on desktop.
        window.location.href = "path-to-your-java-game.jar";
    }
</script>

</body>
</html>
HTML

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top