Simple Invaders

Code breakdown


Program Details (HTML)

<!DOCTYPE html>

<html>

<head>

    <title>Space Invaders</title>

    <link rel="stylesheet" type="text/css" href="styles.css">

</head>

<body>

    <div id="game">

        <div id="player"></div>

        <div id="bullet"></div>

        <div class="invader" style="top: 40px; left: 40px;"></div>

        <div class="invader" style="top: 40px; left: 80px;"></div>

        <div class="invader" style="top: 40px; left: 120px;"></div>

        <!-- Add more invader elements as desired -->

    </div>


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

</body>

</html>



Program Details (CSS)

<style>

    #game {

        width: 480px;

        height: 320px;

        margin: 0 auto;

        position: relative;

        border: 2px solid #000;

    }


    .invader {

        width: 20px;

        height: 20px;

        background: #f00;

        position: absolute;

    }


    #player {

        width: 40px;

        height: 20px;

        background: #00f;

        position: absolute;

        bottom: 0;

    }


    #bullet {

        width: 2px;

        height: 10px;

        background: #000;

        position: absolute;

    }

</style>


Program Details (JAVASCIPT)

<script>

    document.addEventListener('DOMContentLoaded', () => {

        const player = document.getElementById('player');

        const bullet = document.getElementById('bullet');

        const invaders = document.getElementsByClassName('invader');

        const game = document.getElementById('game');


        const playerSpeed = 5;

        const bulletSpeed = 10;

        let invaderSpeed = 2;


        let bulletInterval = null;

        let invaderInterval = null;


        document.addEventListener('keydown', (event) => {

            const key = event.key;

            const playerLeft = parseInt(getComputedStyle(player).left);


            if (key === 'ArrowLeft' && playerLeft > 0) {

                player.style.left = `${playerLeft - playerSpeed}px`;

            } else if (key === 'ArrowRight' && playerLeft < game.clientWidth - player.clientWidth) {

                player.style.left = `${playerLeft + playerSpeed}px`;

            } else if (key === ' ') {

                if (!bulletInterval) {

                    bullet.style.left = `${playerLeft + player.clientWidth / 2 - bullet.clientWidth / 2}px`;

                    bullet.style.top = `${game.clientHeight - player.clientHeight - bullet.clientHeight}px`;

                    bulletInterval = setInterval(moveBullet, 20);

                }

            }

        });


        function moveBullet() {

            const bulletTop = parseInt(getComputedStyle(bullet).top);

            bullet.style.top = `${bulletTop - bulletSpeed}px`;


            if (bulletTop <= 0) {

                clearInterval(bulletInterval);

                bulletInterval = null;

            } else {

                checkCollision();

            }

        }


        function checkCollision() {

            const bulletRect = bullet.getBoundingClientRect();


            for (let i = 0; i < invaders.length; i++) {

                const invader = invaders[i];

                const invaderRect = invader.getBoundingClientRect();


                if (isColliding(bulletRect, invaderRect)) {

                    invader.remove();

                    clearInterval(bulletInterval);

                    bulletInterval = null;

                    break;

                }

            }

        }


        function isColliding(rect1, rect2) {

            return rect1.left < rect2.right &&

                rect1.right > rect2.left &&

                rect1.top < rect2.bottom &&

                rect1.bottom > rect2.top;

        }


        function moveInvaders() {

            invaderInterval = setInterval(() => {

                for (let i = 0; i < invaders.length; i++) {

                    const invader = invaders[i];

                    const invaderLeft = parseInt(getComputedStyle(invader).left);

                    const invaderTop = parseInt(getComputedStyle(invader).top);


                    invader.style.left = `${invaderLeft + invaderSpeed}px`;


                    if (invaderLeft >= game.clientWidth - invader.clientWidth || invaderLeft <= 0) {

                        invader.style.top = `${invaderTop + 20}px`;

                        invaderSpeed *= -1;

                    }


                    if (invaderTop >= game.clientHeight - invader.clientHeight) {

                        gameOver();

                    }

                }

            }, 200);

        }


        function gameOver() {

            clearInterval(invaderInterval);

            clearInterval(bulletInterval);

            alert('Game Over');

        }


        moveInvaders();

    });

</script>