super car

you play a car game
mail@pastecode.io avatar
unknown
apex
a month ago
1.3 kB
1
Indexable
Never
<!DOCTYPE html>
<html>
<head>
    <title>Simple Racing Game</title>
    <style>
        body { margin: 0; overflow: hidden; }
        canvas { background: #ccc; display: block; margin: 0 auto; }
    </style>
</head>
<body>
    <canvas id="gameCanvas" width="800" height="600"></canvas>
    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        const carWidth = 50;
        const carHeight = 100;
        let carX = (canvas.width - carWidth) / 2;
        let carY = canvas.height - carHeight - 10;
        let carSpeed = 5;

        function drawCar() {
            ctx.fillStyle = 'blue';
            ctx.fillRect(carX, carY, carWidth, carHeight);
        }

        function update() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            drawCar();
        }

        function moveCar(e) {
            if (e.key === 'ArrowLeft' && carX > 0) {
                carX -= carSpeed;
            }
            if (e.key === 'ArrowRight' && carX < canvas.width - carWidth) {
                carX += carSpeed;
            }
        }

        document.addEventListener('keydown', moveCar);

        setInterval(update, 1000 / 60); // Update the game 60 times per second
    </script>
</body>
</html>
Leave a Comment