Gregniuki commited on
Commit
ab20260
·
verified ·
1 Parent(s): f06ff6a

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +114 -0
script.js ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const gameArea = document.getElementById('game-area');
2
+ const player = document.getElementById('player');
3
+ let playerX = window.innerWidth / 2;
4
+ let bullets = [];
5
+ let invaders = [];
6
+ let invaderDirection = 1;
7
+ let invaderSpeed = 1;
8
+
9
+ // Create player
10
+ player.style.left = `${playerX}px`;
11
+
12
+ // Create invaders
13
+ function createInvaders() {
14
+ for (let row = 0; row < 3; row++) {
15
+ for (let col = 0; col < 8; col++) {
16
+ const invader = document.createElement('div');
17
+ invader.classList.add('invader');
18
+ invader.style.left = `${col * 60 + 50}px`;
19
+ invader.style.top = `${row * 60 + 50}px`;
20
+ gameArea.appendChild(invader);
21
+ invaders.push(invader);
22
+ }
23
+ }
24
+ }
25
+
26
+ // Move player
27
+ document.addEventListener('keydown', (e) => {
28
+ if (e.key === 'ArrowLeft' && playerX > 0) {
29
+ playerX -= 10;
30
+ } else if (e.key === 'ArrowRight' && playerX < window.innerWidth - 50) {
31
+ playerX += 10;
32
+ } else if (e.key === ' ') {
33
+ shoot();
34
+ }
35
+ player.style.left = `${playerX}px`;
36
+ });
37
+
38
+ // Shoot bullet
39
+ function shoot() {
40
+ const bullet = document.createElement('div');
41
+ bullet.classList.add('bullet');
42
+ bullet.style.left = `${playerX + 22.5}px`;
43
+ bullet.style.bottom = '50px';
44
+ gameArea.appendChild(bullet);
45
+ bullets.push(bullet);
46
+ }
47
+
48
+ // Move bullets
49
+ function moveBullets() {
50
+ bullets.forEach((bullet, index) => {
51
+ const bottom = parseInt(bullet.style.bottom, 10);
52
+ if (bottom > window.innerHeight) {
53
+ bullet.remove();
54
+ bullets.splice(index, 1);
55
+ } else {
56
+ bullet.style.bottom = `${bottom + 10}px`;
57
+ }
58
+ });
59
+ }
60
+
61
+ // Move invaders
62
+ function moveInvaders() {
63
+ let edgeReached = false;
64
+ invaders.forEach(invader => {
65
+ const left = parseInt(invader.style.left, 10);
66
+ if (left + invaderDirection * invaderSpeed < 0 || left + invaderDirection * invaderSpeed > window.innerWidth - 40) {
67
+ edgeReached = true;
68
+ }
69
+ });
70
+
71
+ if (edgeReached) {
72
+ invaderDirection *= -1;
73
+ invaders.forEach(invader => {
74
+ const top = parseInt(invader.style.top, 10);
75
+ invader.style.top = `${top + 20}px`;
76
+ });
77
+ } else {
78
+ invaders.forEach(invader => {
79
+ const left = parseInt(invader.style.left, 10);
80
+ invader.style.left = `${left + invaderDirection * invaderSpeed}px`;
81
+ });
82
+ }
83
+ }
84
+
85
+ // Check for collisions
86
+ function checkCollisions() {
87
+ bullets.forEach((bullet, bulletIndex) => {
88
+ const bulletRect = bullet.getBoundingClientRect();
89
+ invaders.forEach((invader, invaderIndex) => {
90
+ const invaderRect = invader.getBoundingClientRect();
91
+ if (bulletRect.bottom > invaderRect.top &&
92
+ bulletRect.top < invaderRect.bottom &&
93
+ bulletRect.right > invaderRect.left &&
94
+ bulletRect.left < invaderRect.right) {
95
+ bullet.remove();
96
+ invader.remove();
97
+ bullets.splice(bulletIndex, 1);
98
+ invaders.splice(invaderIndex, 1);
99
+ }
100
+ });
101
+ });
102
+ }
103
+
104
+ // Game loop
105
+ function gameLoop() {
106
+ moveBullets();
107
+ moveInvaders();
108
+ checkCollisions();
109
+ requestAnimationFrame(gameLoop);
110
+ }
111
+
112
+ // Initialize game
113
+ createInvaders();
114
+ gameLoop();