Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

@SivsAI بازی دایناسور گوگل رو در قالب HTML و جاوا اسکریپت رو به من بده

  1. User

    @SivsAI بازی دایناسور گوگل رو در قالب HTML و جاوا اسکریپت رو به من بده

  2. Assistant

    مطمئناً! در زیر یک نمونه ساده از بازی دایناسور گوگل (T-Rex Runner) به صورت HTML و جاوااسکریپت آورده شده است. شما می‌توانید این کد را در یک فایل .html ذخیره کنید و آن را در مرورگر خود باز کنید.

    <!DOCTYPE html>
    <html lang="fa">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>بازی دایناسور گوگل</title>
        <style>
            body {
                margin: 0;
                overflow: hidden;
                background-color: #f7f7f7;
            }
            #gameCanvas {
                display: block;
                background-color: #fff;
                position: relative;
                margin: 0 auto;
                border: 2px solid #000;
            }
            .dino {
                width: 50px;
                height: 50px;
                background-color: green;
                position: absolute;
                bottom: 20px;
                left: 50px;
            }
            .cactus {
                width: 20px;
                height: 40px;
                background-color: brown;
                position: absolute;
                bottom: 20px;
            }
        </style>
    </head>
    <body>
        <div id="gameCanvas" style="width: 800px; height: 400px;">
            <div class="dino"></div>
        </div>
    
        <script>
            const gameCanvas = document.getElementById('gameCanvas');
            const dino = document.querySelector('.dino');
    
            let isJumping = false;
            let gravity = 0;
            let score = 0;
    
            document.addEventListener('keydown', function(event) {
                if (event.code === 'Space' && !isJumping) {
                    isJumping = true;
                    jump();
                }
            });
    
            function jump() {
                let position = 0;
                const jumpInterval = setInterval(function() {
                    if (position >= 100) {
                        clearInterval(jumpInterval);
                        const fallInterval = setInterval(function() {
                            if (position <= 0) {
                                clearInterval(fallInterval);
                                isJumping = false;
                            }
                            position -= 5;
                            dino.style.bottom = position + 'px';
                        }, 20);
                    }
                    position += 5;
                    dino.style.bottom = position + 'px';
                }, 20);
            }
    
            function createCactus() {
                const cactus = document.createElement('div');
                cactus.classList.add('cactus');
                cactus.style.left = '800px';
                gameCanvas.appendChild(cactus);
    
                let cactusInterval = setInterval(function() {
                    if (parseInt(cactus.style.left) < -20) {
                        clearInterval(cactusInterval);
                        gameCanvas.removeChild(cactus);
                        score++;
                        console.log('نمره:', score);
                    } else {
                        cactus.style.left = parseInt(cactus.style.left) - 5 + 'px';
                    }
    
                    // Detect collision
                    if (detectCollision(cactus)) {
                        alert('بازی تمام شد! نمره شما: ' + score);
                        clearInterval(cactusInterval);
                        window.location.reload();
                    }
                }, 20);
            }
    
            function detectCollision(cactus) {
                const dinoRect = dino.getBoundingClientRect();
                const cactusRect = cactus.getBoundingClientRect();
                return !(
                    dinoRect.top > cactusRect.bottom ||
                    dinoRect.bottom < cactusRect.top ||
                    dinoRect.right < cactusRect.left ||
                    dinoRect.left > cactusRect.right
                );
            }
    
            setInterval(createCactus, 2000);
        </script>
    </body>
    </html>
    

    با استفاده از این کد، شما می‌توانید بازی دایناسور را در مرورگر خود اجرا کنید. با فشار دادن کلید Space، دایناسور پرش می‌کند و باید از موانع (کاکتوس‌ها) عبور کنید. اگر به یکی از موانع برخورد کنید، بازی تمام می‌شود و نمره شما نمایش داده می‌شود.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.