Flappybox

Flappy Bird in hitbox!

目前為 2024-10-06 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Flappybox
// @namespace    http://tampermonkey.net/
// @version      1.0
// @author       CarManiac
// @description  Flappy Bird in hitbox!
// @match        https://heav.io/game.html
// @match        https://hitbox.io/game.html
// @match        https://heav.io/game2.html
// @match        https://hitbox.io/game2.html
// @match        https://hitbox.io/game-beta.html
// @icon         https://www.google.com/s2/favicons?sz=64&domain=heav.io
// @icon         https://www.google.com/s2/favicons?sz=64&domain=hitbox.io
// @grant        none
// @license      MIT
// ==/UserScript==
(function() {
    'use strict';

    let isGameRunning = false
    let birdY = 150, birdX = 50, velocity = 0, gravity = 0.35, lift = -6;
    let pipes = [];
    let score = 0;
    let frameCount = 0;
    let highScore = localStorage.getItem('flappyHighScore') || 0;
    const pipeSpeed = 2;
    const pipeGap = 120;
    const groundY = 380;

    const birdColorDecimal = parseInt(localStorage.getItem('basic_col_1'), 10);
    const birdColorHex = birdColorDecimal ? `#${birdColorDecimal.toString(16).padStart(6, '0')}` : '#FF0000';
    let rotation = 0;

    function createGameUI() {
        const gameContainer = document.createElement('div');
        gameContainer.id = 'flappyGameContainer';
        gameContainer.style.position = 'fixed';
        gameContainer.style.top = '50px';
        gameContainer.style.left = '50px';
        gameContainer.style.width = '300px';
        gameContainer.style.height = '400px';
        gameContainer.style.borderRadius = '10px';
        gameContainer.style.border = '1px solid black';
        gameContainer.style.overflow = 'hidden';
        gameContainer.style.position = 'relative';

        const canvas = document.createElement('canvas');
        canvas.id = 'flappyCanvas';
        canvas.width = 300;
        canvas.height = 400;

        const gameOverText = document.createElement('div');
        gameOverText.id = 'gameOverText';
        gameOverText.style.display = 'none';
        gameOverText.style.position = 'absolute';
        gameOverText.style.top = '50%';
        gameOverText.style.left = '50%';
        gameOverText.style.transform = 'translate(-50%, -50%)';
        gameOverText.style.color = '';
        gameOverText.style.fontFamily = 'Bai Jamjuree, sans-serif';
        gameOverText.style.fontWeight = 'bold';
        gameOverText.style.fontSize = '20px';
        gameOverText.style.textAlign = 'center';

        gameContainer.appendChild(canvas);
        gameContainer.appendChild(gameOverText);
        document.body.appendChild(gameContainer);

        return gameContainer;
    }

    function startGame() {
        const canvas = document.getElementById('flappyCanvas');
        const ctx = canvas.getContext('2d');
        birdY = 150; velocity = 0; score = 0; pipes = [];
        pipes.push(createPipe());
        frameCount = 0;
        rotation = 0;

        document.getElementById('gameOverText').style.display = 'none';
        isGameRunning = true;

        const backgroundImage = new Image();
        backgroundImage.src = 'https://user-images.githubusercontent.com/18351809/46888871-624a3900-ce7f-11e8-808e-99fd90c8a3f4.png';

        backgroundImage.onload = function() {
            function gameLoop() {
                if (!isGameRunning) return;

                frameCount++;
                ctx.clearRect(0, 0, canvas.width, canvas.height);

                // Draw the static background without stretching
                ctx.drawImage(backgroundImage, 0, -100);

                velocity += gravity;
                birdY += velocity;

                if (velocity < 0) {
                    rotation = Math.max(-30, rotation - 2);
                } else {
                    rotation = Math.min(30, rotation + 1);
                }

                // Check for collision with ground
                if (birdY + 20 > groundY) {
                    endGame();
                }

                ctx.save();
                ctx.translate(birdX + 10, birdY + 10);
                ctx.rotate(rotation * Math.PI / 180);
                ctx.fillRect(-11, -11, 22, 22);
                ctx.fillStyle = birdColorHex;
                ctx.fillRect(-10, -10, 20, 20);
                ctx.restore();

                if (frameCount % 90 === 0) pipes.push(createPipe());
                pipes.forEach((pipe, index) => {
                    pipe.x -= pipeSpeed;
                    if (pipe.x + pipe.width < 0) pipes.splice(index, 1);
                    drawPipe(ctx, pipe);
                    if (checkCollision(pipe)) endGame();
                    if (pipe.x === birdX) score++;
                });

                // Draw ground
                ctx.fillStyle = '#4F7942'; // Brown color for the ground
                ctx.fillRect(0, groundY, canvas.width, canvas.height - groundY);

                ctx.fillStyle = '#fff';
                ctx.textAlign = 'center';
                ctx.textBaseline = 'middle';
                ctx.font = 'bold 40px Bai Jamjuree, sans-serif';

                const yPosition = canvas.height * 0.2; // 30% down from the top
                ctx.fillText(score, canvas.width / 2, yPosition);

                requestAnimationFrame(gameLoop);
            }
            gameLoop();
        };
    }

    function flap() {
        velocity = lift;
        rotation = -20;
    }

    function createPipe() {
        const pipeHeight = Math.random() * 200 + 50;
        const pipeWidth = Math.random() * 10 + 40; // Vary pipe width
        const capWidth = pipeWidth + 10; // Cap width is wider than the pipe
        return {
            x: 300,
            width: pipeWidth,
            capWidth: capWidth, // Add cap width
            height: pipeHeight,
            gapY: pipeHeight + pipeGap,
            capHeight: 20
        };
    }

    function drawPipe(ctx, pipe) {
        const gradient = ctx.createLinearGradient(pipe.x, 0, pipe.x + pipe.width, 0);
        gradient.addColorStop(0, '#37b827');
        gradient.addColorStop(0.5, '#4CAF50');
        gradient.addColorStop(1, '#37b827');

        // Draw main pipe body
        ctx.fillStyle = gradient;
        ctx.fillRect(pipe.x, 0, pipe.width, pipe.height);
        ctx.fillRect(pipe.x, pipe.gapY, pipe.width, 400 - pipe.gapY);

        // Draw pipe caps with wider width
        ctx.fillStyle = '#2e7d32'; // Darker green for caps
        ctx.fillRect(pipe.x - 5, pipe.height - pipe.capHeight, pipe.capWidth, pipe.capHeight); // Top cap
        ctx.fillRect(pipe.x - 5, pipe.gapY, pipe.capWidth, pipe.capHeight); // Bottom cap
    }

    function checkCollision(pipe) {
        if (birdX + 20 > pipe.x && birdX < pipe.x + pipe.width) {
            if (birdY < pipe.height || birdY + 20 > pipe.gapY || birdY + 20 > groundY) {
                return true;
            }
        }
        return false;
    }

    function endGame() {
        isGameRunning = false;
        if (score > highScore) {
            highScore = score;
            localStorage.setItem('flappyHighScore', highScore);
        }
        showGameOverMessage();
    }

    function showGameOverMessage() {
        const gameOverText = document.getElementById('gameOverText');
        gameOverText.innerHTML = `Game Over!<br>Your Score: ${score}<br>Best: ${highScore}`;
        gameOverText.style.display = 'block';
    }

    function restartGame() {
        birdY = 150;
        velocity = 0;
        pipes = [];
        score = 0;
        frameCount = 0;
        rotation = 0;
        startGame();
    }

    function toggleGame() {
        const gameContainer = document.getElementById('flappyGameContainer');
        if (gameContainer.style.display === 'none') {
            gameContainer.style.display = 'block';
            startGame();
        } else {
            gameContainer.style.display = 'none';
            isGameRunning = false;
        }
        document.activeElement.blur();
    }

    function createToggleButton() {
        const button = document.createElement('button');
        button.innerText = 'Toggle Flappy Bird Game';
        button.style.position = 'fixed';
        button.style.top = '10px';
        button.style.left = '50%';
        button.style.fontFamily = 'Bai Jamjuree, sans-serif';
        button.style.borderRadius = '5px';
        button.style.padding = '10px';
        button.style.backgroundColor = '#darkgray';
        button.style.cursor = 'pointer';
        button.onclick = toggleGame;
        document.body.appendChild(button);
    }

    document.addEventListener('click', function() {
        if (isGameRunning) {
            flap();
        }
    });

    document.addEventListener('keydown', (e) => {
        if (e.key === ' ') {
            if (isGameRunning) {
                flap();
            }
        }
        if (e.key === 'r'){
            if (!isGameRunning){
                restartGame()
            }
        }
    });

    createGameUI();
    createToggleButton();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址