File size: 3,308 Bytes
ab20260
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const gameArea = document.getElementById('game-area');
const player = document.getElementById('player');
let playerX = window.innerWidth / 2;
let bullets = [];
let invaders = [];
let invaderDirection = 1;
let invaderSpeed = 1;

// Create player
player.style.left = `${playerX}px`;

// Create invaders
function createInvaders() {
    for (let row = 0; row < 3; row++) {
        for (let col = 0; col < 8; col++) {
            const invader = document.createElement('div');
            invader.classList.add('invader');
            invader.style.left = `${col * 60 + 50}px`;
            invader.style.top = `${row * 60 + 50}px`;
            gameArea.appendChild(invader);
            invaders.push(invader);
        }
    }
}

// Move player
document.addEventListener('keydown', (e) => {
    if (e.key === 'ArrowLeft' && playerX > 0) {
        playerX -= 10;
    } else if (e.key === 'ArrowRight' && playerX < window.innerWidth - 50) {
        playerX += 10;
    } else if (e.key === ' ') {
        shoot();
    }
    player.style.left = `${playerX}px`;
});

// Shoot bullet
function shoot() {
    const bullet = document.createElement('div');
    bullet.classList.add('bullet');
    bullet.style.left = `${playerX + 22.5}px`;
    bullet.style.bottom = '50px';
    gameArea.appendChild(bullet);
    bullets.push(bullet);
}

// Move bullets
function moveBullets() {
    bullets.forEach((bullet, index) => {
        const bottom = parseInt(bullet.style.bottom, 10);
        if (bottom > window.innerHeight) {
            bullet.remove();
            bullets.splice(index, 1);
        } else {
            bullet.style.bottom = `${bottom + 10}px`;
        }
    });
}

// Move invaders
function moveInvaders() {
    let edgeReached = false;
    invaders.forEach(invader => {
        const left = parseInt(invader.style.left, 10);
        if (left + invaderDirection * invaderSpeed < 0 || left + invaderDirection * invaderSpeed > window.innerWidth - 40) {
            edgeReached = true;
        }
    });

    if (edgeReached) {
        invaderDirection *= -1;
        invaders.forEach(invader => {
            const top = parseInt(invader.style.top, 10);
            invader.style.top = `${top + 20}px`;
        });
    } else {
        invaders.forEach(invader => {
            const left = parseInt(invader.style.left, 10);
            invader.style.left = `${left + invaderDirection * invaderSpeed}px`;
        });
    }
}

// Check for collisions
function checkCollisions() {
    bullets.forEach((bullet, bulletIndex) => {
        const bulletRect = bullet.getBoundingClientRect();
        invaders.forEach((invader, invaderIndex) => {
            const invaderRect = invader.getBoundingClientRect();
            if (bulletRect.bottom > invaderRect.top &&
                bulletRect.top < invaderRect.bottom &&
                bulletRect.right > invaderRect.left &&
                bulletRect.left < invaderRect.right) {
                bullet.remove();
                invader.remove();
                bullets.splice(bulletIndex, 1);
                invaders.splice(invaderIndex, 1);
            }
        });
    });
}

// Game loop
function gameLoop() {
    moveBullets();
    moveInvaders();
    checkCollisions();
    requestAnimationFrame(gameLoop);
}

// Initialize game
createInvaders();
gameLoop();