-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
236 lines (217 loc) · 6.35 KB
/
index.html
File metadata and controls
236 lines (217 loc) · 6.35 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
margin: 2em;
background-color: #dcebff;
}
.player {
font-family: monospace;
white-space: pre;
position: absolute;
font-weight: 900;
width: 100px;
height: 20px;
background-color: #333;
border-radius: 20%;
}
.player-stats {
position: fixed;
top: 10px;
left: 10px;
width: 150px;
background-color: rgba(255, 255, 255, 0.8);
padding: 10px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
font-size: 18px;
z-index: 1000;
}
.player-stats div {
display: flex;
justify-content: space-between;
padding: 5px;
}
</style>
<script>
let gameTimer = setInterval(gameLoop, 1000 / 60);
let movementSpeed = 10;
let bullets = [];
let enemies = [];
let powerUps = [];
let score = 0;
function updateScore(points) {
score += points;
document.getElementById("scoreLabel").innerText = score;
}
function updateSpeed(amount) {
movementSpeed += amount;
document.getElementById("movementSpeedLabel").innerText = movementSpeed;
}
function createEnemy() {
let enemy = document.createElement("div");
enemy.style.position = "absolute";
enemy.style.width = "50px";
enemy.style.height = "50px";
enemy.style.backgroundColor = "orange";
enemy.style.borderRadius = "20%";
enemy.style.top = "0px";
enemy.style.left = Math.floor(Math.random() * (window.innerWidth - 50)) + "px";
document.body.appendChild(enemy);
enemies.push(enemy);
}
function createPowerUp() {
let powerUp = document.createElement("div");
powerUp.style.position = "absolute";
powerUp.style.width = "15px";
powerUp.style.height = "15px";
powerUp.style.backgroundColor = "green";
powerUp.style.borderRadius = "50%";
powerUp.style.top = "0px";
powerUp.style.left = Math.floor(Math.random() * (window.innerWidth - 30)) + "px";
document.body.appendChild(powerUp);
powerUps.push(powerUp);
}
function gameLoop() {
// Handle bullets
for (let i = 0; i < bullets.length; i++) {
let bullet = bullets[i];
let top = parseInt(bullet.style.top);
if (top <= 0) {
document.body.removeChild(bullet);
bullets.splice(i, 1);
i--;
} else {
bullet.style.top = (top - 15) + "px";
// Check for collisions with enemies
for (let j = 0; j < enemies.length; j++) {
let enemy = enemies[j];
let enemyRect = enemy.getBoundingClientRect();
let bulletRect = bullet.getBoundingClientRect();
if (!(bulletRect.right < enemyRect.left ||
bulletRect.left > enemyRect.right ||
bulletRect.bottom < enemyRect.top ||
bulletRect.top > enemyRect.bottom)) {
// Collision detected
document.body.removeChild(enemy);
enemies.splice(j, 1);
j--;
document.body.removeChild(bullet);
bullets.splice(i, 1);
i--;
updateScore(10);
break;
}
}
}
}
// Randomly create enemies
if (Math.random() < 0.02) {
createEnemy();
}
// Handle enemies
for (let i = 0; i < enemies.length; i++) {
let enemy = enemies[i];
let top = parseInt(enemy.style.top);
if (top >= window.innerHeight) {
document.body.removeChild(enemy);
enemies.splice(i, 1);
i--;
} else {
enemy.style.top = (top + 5) + "px";
// Checl for collisions with player
let player = document.getElementById("player");
let playerRect = player.getBoundingClientRect();
let enemyRect = enemy.getBoundingClientRect();
if (!(enemyRect.right < playerRect.left ||
enemyRect.left > playerRect.right ||
enemyRect.bottom < playerRect.top ||
enemyRect.top > playerRect.bottom)) {
// Collision detected
document.body.removeChild(enemy);
enemies.splice(i, 1);
i--;
updateSpeed(-5);
updateScore(-20);
}
}
}
// Handle power-ups
if (Math.random() < 0.005) {
createPowerUp();
}
for (let i = 0; i < powerUps.length; i++) {
let powerUp = powerUps[i];
let top = parseInt(powerUp.style.top);
if (top >= window.innerHeight) {
document.body.removeChild(powerUp);
powerUps.splice(i, 1);
i--;
} else {
powerUp.style.top = (top + 3) + "px";
// Check for collisions with player and increase movement speed
let player = document.getElementById("player");
let playerRect = player.getBoundingClientRect();
let powerUpRect = powerUp.getBoundingClientRect();
if (!(powerUpRect.right < playerRect.left ||
powerUpRect.left > playerRect.right ||
powerUpRect.bottom < playerRect.top ||
powerUpRect.top > playerRect.bottom)) {
// Collision detected
document.body.removeChild(powerUp);
powerUps.splice(i, 1);
i--;
updateSpeed(5);
}
}
}
}
function fireBullet() {
let bullet = document.createElement("div");
bullet.style.position = "absolute";
bullet.style.width = "10px";
bullet.style.height = "10px";
bullet.style.borderRadius = "50%";
bullet.style.backgroundColor = "#333";
let player = document.getElementById("player");
let playerLeft = parseInt(player.style.left);
let playerTop = parseInt(player.style.top);
bullet.style.left = (playerLeft + 47.5) + "px";
bullet.style.top = (playerTop - 10) + "px";
document.body.appendChild(bullet);
bullets.push(bullet);
}
function gameInput() {
if (event.key === "ArrowLeft") {
let player = document.getElementById("player");
let left = parseInt(player.style.left);
if (left >= 0) {
player.style.left = (left - movementSpeed) + "px";
}
}
if (event.key === "ArrowRight") {
let player = document.getElementById("player");
let left = parseInt(player.style.left);
if (left <= window.innerWidth - 50) {
player.style.left = (left + movementSpeed) + "px";
}
}
if (event.code === "Space") {
fireBullet();
}
}
</script>
</head>
<body onkeydown="gameInput()">
<div class="player-stats">
<div>
Score: <span id="scoreLabel">0</span>
</div>
<div>
Speed: <span id="movementSpeedLabel">10</span>
</div>
</div>
<div id="player" class="player" style="top: 1000; left: 0px;"></div>
</body>
</html>