cutechicken
commited on
Update index.html
Browse files- index.html +140 -120
index.html
CHANGED
@@ -7,6 +7,7 @@
|
|
7 |
margin: 0;
|
8 |
overflow: hidden;
|
9 |
background: #333;
|
|
|
10 |
}
|
11 |
#gameCanvas {
|
12 |
background-repeat: repeat;
|
@@ -19,7 +20,16 @@
|
|
19 |
background: rgba(0,0,0,0.7);
|
20 |
padding: 10px;
|
21 |
border-radius: 5px;
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
z-index: 1000;
|
24 |
}
|
25 |
.button {
|
@@ -43,9 +53,11 @@
|
|
43 |
<div id="instructions">
|
44 |
Controls:<br>
|
45 |
WASD - Move tank<br>
|
46 |
-
Mouse - Aim
|
47 |
-
Space - Fire
|
|
|
48 |
</div>
|
|
|
49 |
<button id="nextRound" class="button">Next Round</button>
|
50 |
<button id="restart" class="button">Restart Game</button>
|
51 |
<canvas id="gameCanvas"></canvas>
|
@@ -55,48 +67,64 @@
|
|
55 |
const ctx = canvas.getContext('2d');
|
56 |
const nextRoundBtn = document.getElementById('nextRound');
|
57 |
const restartBtn = document.getElementById('restart');
|
|
|
58 |
|
59 |
canvas.width = window.innerWidth;
|
60 |
canvas.height = window.innerHeight;
|
61 |
|
62 |
// Load images
|
63 |
-
const
|
64 |
-
|
65 |
|
66 |
-
const
|
67 |
-
|
68 |
|
69 |
-
const turretImg = new Image();
|
70 |
-
turretImg.src = 'turret.png';
|
71 |
-
|
72 |
const enemyImg = new Image();
|
73 |
enemyImg.src = 'enemy.png';
|
74 |
|
75 |
// Audio
|
76 |
-
const
|
77 |
-
const enemyFireSound = new Audio('fire2.mp3');
|
78 |
|
79 |
// Game state
|
80 |
let currentRound = 1;
|
81 |
let gameOver = false;
|
|
|
82 |
let enemies = [];
|
83 |
let bullets = [];
|
84 |
-
let playerBullets = [];
|
85 |
let items = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
-
//
|
88 |
const player = {
|
89 |
x: canvas.width/2,
|
90 |
y: canvas.height/2,
|
91 |
speed: 5,
|
92 |
angle: 0,
|
93 |
-
|
94 |
-
|
95 |
-
height: 80,
|
96 |
health: 1000,
|
97 |
maxHealth: 1000
|
98 |
};
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
class Enemy {
|
101 |
constructor() {
|
102 |
this.x = Math.random() * canvas.width;
|
@@ -107,16 +135,27 @@
|
|
107 |
this.lastShot = 0;
|
108 |
this.shootInterval = 2000;
|
109 |
this.angle = 0;
|
|
|
110 |
}
|
111 |
|
112 |
update() {
|
113 |
-
|
114 |
-
this.x += Math.cos(this.
|
115 |
-
this.y += Math.sin(this.
|
|
|
|
|
|
|
|
|
116 |
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
119 |
|
|
|
120 |
const now = Date.now();
|
121 |
if (now - this.lastShot > this.shootInterval) {
|
122 |
this.shoot();
|
@@ -125,134 +164,114 @@
|
|
125 |
}
|
126 |
|
127 |
shoot() {
|
128 |
-
|
129 |
-
enemyFireSound.play();
|
130 |
bullets.push({
|
131 |
x: this.x,
|
132 |
y: this.y,
|
133 |
angle: this.angle,
|
134 |
speed: 5,
|
135 |
-
isEnemy: true
|
|
|
136 |
});
|
137 |
}
|
138 |
}
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
145 |
}
|
146 |
|
147 |
-
// Game controls
|
148 |
-
const keys = {};
|
149 |
-
let mouseX = 0;
|
150 |
-
let mouseY = 0;
|
151 |
-
let lastShot = 0;
|
152 |
-
const shootInterval = 1000;
|
153 |
-
|
154 |
document.addEventListener('keydown', (e) => {
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
keys[e.key] = false;
|
160 |
});
|
161 |
|
162 |
canvas.addEventListener('mousemove', (e) => {
|
163 |
-
|
164 |
-
mouseY = e.clientY;
|
165 |
});
|
166 |
|
167 |
function fireBullet() {
|
|
|
168 |
const now = Date.now();
|
169 |
-
if (now - lastShot >
|
170 |
-
|
171 |
-
|
172 |
-
playerBullets.push({
|
173 |
x: player.x,
|
174 |
y: player.y,
|
175 |
-
angle: player.
|
176 |
-
speed: 10
|
|
|
|
|
|
|
177 |
});
|
178 |
lastShot = now;
|
179 |
}
|
180 |
}
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
enemies.push(new Enemy());
|
186 |
-
}
|
187 |
-
player.health = player.maxHealth;
|
188 |
-
bullets = [];
|
189 |
-
playerBullets = [];
|
190 |
-
items = [];
|
191 |
-
}
|
192 |
-
|
193 |
-
function spawnHealthItem(x, y) {
|
194 |
-
items.push({
|
195 |
-
x: x,
|
196 |
-
y: y,
|
197 |
-
width: 20,
|
198 |
-
height: 20
|
199 |
-
});
|
200 |
-
}
|
201 |
|
202 |
function updateGame() {
|
203 |
if(gameOver) return;
|
204 |
|
205 |
-
|
206 |
-
if(keys['
|
207 |
-
if(keys['
|
208 |
-
if(keys['
|
|
|
209 |
|
210 |
-
|
|
|
|
|
211 |
|
212 |
-
|
213 |
|
|
|
214 |
enemies.forEach(enemy => enemy.update());
|
215 |
|
216 |
-
// Update bullets
|
217 |
-
|
218 |
bullet.x += Math.cos(bullet.angle) * bullet.speed;
|
219 |
bullet.y += Math.sin(bullet.angle) * bullet.speed;
|
220 |
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
|
|
228 |
}
|
229 |
return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
}
|
231 |
-
return true;
|
232 |
-
});
|
233 |
-
|
234 |
-
return bullet.x >= 0 && bullet.x <= canvas.width &&
|
235 |
-
bullet.y >= 0 && bullet.y <= canvas.height;
|
236 |
-
});
|
237 |
-
|
238 |
-
bullets = bullets.filter(bullet => {
|
239 |
-
bullet.x += Math.cos(bullet.angle) * bullet.speed;
|
240 |
-
bullet.y += Math.sin(bullet.angle) * bullet.speed;
|
241 |
-
|
242 |
-
const dist = Math.hypot(bullet.x - player.x, bullet.y - player.y);
|
243 |
-
if(dist < 30) {
|
244 |
-
player.health -= 100;
|
245 |
-
if(player.health <= 0) {
|
246 |
-
gameOver = true;
|
247 |
-
restartBtn.style.display = 'block';
|
248 |
-
}
|
249 |
-
return false;
|
250 |
}
|
251 |
|
252 |
return bullet.x >= 0 && bullet.x <= canvas.width &&
|
253 |
bullet.y >= 0 && bullet.y <= canvas.height;
|
254 |
});
|
255 |
|
|
|
256 |
items = items.filter(item => {
|
257 |
const dist = Math.hypot(item.x - player.x, item.y - player.y);
|
258 |
if(dist < 30) {
|
@@ -262,6 +281,7 @@
|
|
262 |
return true;
|
263 |
});
|
264 |
|
|
|
265 |
if(enemies.length === 0) {
|
266 |
if(currentRound < 10) {
|
267 |
nextRoundBtn.style.display = 'block';
|
@@ -272,24 +292,25 @@
|
|
272 |
}
|
273 |
}
|
274 |
|
|
|
|
|
|
|
|
|
275 |
function drawHealthBar(x, y, health, maxHealth, width, height, color) {
|
276 |
-
const barX = x - width/2;
|
277 |
-
const barY = y - 50;
|
278 |
ctx.fillStyle = '#333';
|
279 |
-
ctx.fillRect(
|
280 |
ctx.fillStyle = color;
|
281 |
-
ctx.fillRect(
|
282 |
}
|
283 |
|
284 |
function drawGame() {
|
285 |
drawBackground();
|
286 |
-
|
287 |
// Draw player
|
288 |
ctx.save();
|
289 |
ctx.translate(player.x, player.y);
|
290 |
-
ctx.
|
291 |
-
ctx.
|
292 |
-
ctx.drawImage(turretImg, -player.width/2, -player.height/2, player.width, player.height);
|
293 |
ctx.restore();
|
294 |
|
295 |
// Draw player health bar
|
@@ -300,20 +321,20 @@
|
|
300 |
ctx.save();
|
301 |
ctx.translate(enemy.x, enemy.y);
|
302 |
ctx.rotate(enemy.angle);
|
303 |
-
ctx.drawImage(enemyImg, -
|
304 |
ctx.restore();
|
305 |
-
drawHealthBar(enemy.x, enemy.y, enemy.health, enemy.maxHealth,
|
306 |
});
|
307 |
|
308 |
// Draw bullets
|
309 |
-
|
310 |
-
[...playerBullets, ...bullets].forEach(bullet => {
|
311 |
ctx.beginPath();
|
312 |
-
ctx.
|
|
|
313 |
ctx.fill();
|
314 |
});
|
315 |
|
316 |
-
// Draw items
|
317 |
ctx.fillStyle = 'green';
|
318 |
items.forEach(item => {
|
319 |
ctx.beginPath();
|
@@ -347,9 +368,8 @@
|
|
347 |
});
|
348 |
|
349 |
Promise.all([
|
350 |
-
new Promise(resolve =>
|
351 |
-
new Promise(resolve =>
|
352 |
-
new Promise(resolve => turretImg.onload = resolve),
|
353 |
new Promise(resolve => enemyImg.onload = resolve)
|
354 |
]).then(() => {
|
355 |
initRound();
|
|
|
7 |
margin: 0;
|
8 |
overflow: hidden;
|
9 |
background: #333;
|
10 |
+
font-family: Arial;
|
11 |
}
|
12 |
#gameCanvas {
|
13 |
background-repeat: repeat;
|
|
|
20 |
background: rgba(0,0,0,0.7);
|
21 |
padding: 10px;
|
22 |
border-radius: 5px;
|
23 |
+
z-index: 1000;
|
24 |
+
}
|
25 |
+
#weaponInfo {
|
26 |
+
position: fixed;
|
27 |
+
top: 100px;
|
28 |
+
right: 10px;
|
29 |
+
color: white;
|
30 |
+
background: rgba(0,0,0,0.7);
|
31 |
+
padding: 10px;
|
32 |
+
border-radius: 5px;
|
33 |
z-index: 1000;
|
34 |
}
|
35 |
.button {
|
|
|
53 |
<div id="instructions">
|
54 |
Controls:<br>
|
55 |
WASD - Move tank<br>
|
56 |
+
Mouse - Aim<br>
|
57 |
+
Space - Fire<br>
|
58 |
+
R - Switch Weapon
|
59 |
</div>
|
60 |
+
<div id="weaponInfo">Current Weapon: Cannon</div>
|
61 |
<button id="nextRound" class="button">Next Round</button>
|
62 |
<button id="restart" class="button">Restart Game</button>
|
63 |
<canvas id="gameCanvas"></canvas>
|
|
|
67 |
const ctx = canvas.getContext('2d');
|
68 |
const nextRoundBtn = document.getElementById('nextRound');
|
69 |
const restartBtn = document.getElementById('restart');
|
70 |
+
const weaponInfo = document.getElementById('weaponInfo');
|
71 |
|
72 |
canvas.width = window.innerWidth;
|
73 |
canvas.height = window.innerHeight;
|
74 |
|
75 |
// Load images
|
76 |
+
const backgroundImg = new Image();
|
77 |
+
backgroundImg.src = 'city.png';
|
78 |
|
79 |
+
const playerImg = new Image();
|
80 |
+
playerImg.src = 'player.png';
|
81 |
|
|
|
|
|
|
|
82 |
const enemyImg = new Image();
|
83 |
enemyImg.src = 'enemy.png';
|
84 |
|
85 |
// Audio
|
86 |
+
const fireSound = new Audio('firemn.ogg');
|
|
|
87 |
|
88 |
// Game state
|
89 |
let currentRound = 1;
|
90 |
let gameOver = false;
|
91 |
+
let currentWeapon = 'cannon';
|
92 |
let enemies = [];
|
93 |
let bullets = [];
|
|
|
94 |
let items = [];
|
95 |
+
let lastShot = 0;
|
96 |
+
|
97 |
+
const weapons = {
|
98 |
+
cannon: {
|
99 |
+
fireRate: 1000,
|
100 |
+
damage: 0.5,
|
101 |
+
bulletSize: 5
|
102 |
+
},
|
103 |
+
machinegun: {
|
104 |
+
fireRate: 200,
|
105 |
+
damage: 0.05,
|
106 |
+
bulletSize: 2
|
107 |
+
}
|
108 |
+
};
|
109 |
|
110 |
+
// Player
|
111 |
const player = {
|
112 |
x: canvas.width/2,
|
113 |
y: canvas.height/2,
|
114 |
speed: 5,
|
115 |
angle: 0,
|
116 |
+
width: 50,
|
117 |
+
height: 50,
|
|
|
118 |
health: 1000,
|
119 |
maxHealth: 1000
|
120 |
};
|
121 |
|
122 |
+
function drawBackground() {
|
123 |
+
const pattern = ctx.createPattern(backgroundImg, 'repeat');
|
124 |
+
ctx.fillStyle = pattern;
|
125 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
126 |
+
}
|
127 |
+
|
128 |
class Enemy {
|
129 |
constructor() {
|
130 |
this.x = Math.random() * canvas.width;
|
|
|
135 |
this.lastShot = 0;
|
136 |
this.shootInterval = 2000;
|
137 |
this.angle = 0;
|
138 |
+
this.moveAngle = Math.random() * Math.PI * 2;
|
139 |
}
|
140 |
|
141 |
update() {
|
142 |
+
// Movement
|
143 |
+
this.x += Math.cos(this.moveAngle) * this.speed;
|
144 |
+
this.y += Math.sin(this.moveAngle) * this.speed;
|
145 |
+
|
146 |
+
// Bounds check
|
147 |
+
if(this.x < 0 || this.x > canvas.width) this.moveAngle = Math.PI - this.moveAngle;
|
148 |
+
if(this.y < 0 || this.y > canvas.height) this.moveAngle = -this.moveAngle;
|
149 |
|
150 |
+
// Random direction change
|
151 |
+
if(Math.random() < 0.02) {
|
152 |
+
this.moveAngle = Math.random() * Math.PI * 2;
|
153 |
+
}
|
154 |
+
|
155 |
+
// Aim at player
|
156 |
+
this.angle = Math.atan2(player.y - this.y, player.x - this.x);
|
157 |
|
158 |
+
// Shooting
|
159 |
const now = Date.now();
|
160 |
if (now - this.lastShot > this.shootInterval) {
|
161 |
this.shoot();
|
|
|
164 |
}
|
165 |
|
166 |
shoot() {
|
167 |
+
fireSound.cloneNode().play();
|
|
|
168 |
bullets.push({
|
169 |
x: this.x,
|
170 |
y: this.y,
|
171 |
angle: this.angle,
|
172 |
speed: 5,
|
173 |
+
isEnemy: true,
|
174 |
+
size: 3
|
175 |
});
|
176 |
}
|
177 |
}
|
178 |
|
179 |
+
function initRound() {
|
180 |
+
enemies = [];
|
181 |
+
for(let i = 0; i < 5 * currentRound; i++) {
|
182 |
+
enemies.push(new Enemy());
|
183 |
+
}
|
184 |
+
player.health = player.maxHealth;
|
185 |
+
bullets = [];
|
186 |
+
items = [];
|
187 |
}
|
188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
document.addEventListener('keydown', (e) => {
|
190 |
+
if(e.key.toLowerCase() === 'r') {
|
191 |
+
currentWeapon = currentWeapon === 'cannon' ? 'machinegun' : 'cannon';
|
192 |
+
weaponInfo.textContent = `Current Weapon: ${currentWeapon.charAt(0).toUpperCase() + currentWeapon.slice(1)}`;
|
193 |
+
}
|
|
|
194 |
});
|
195 |
|
196 |
canvas.addEventListener('mousemove', (e) => {
|
197 |
+
player.angle = Math.atan2(e.clientY - player.y, e.clientX - player.x);
|
|
|
198 |
});
|
199 |
|
200 |
function fireBullet() {
|
201 |
+
const weapon = weapons[currentWeapon];
|
202 |
const now = Date.now();
|
203 |
+
if (keys[' '] && now - lastShot > weapon.fireRate) {
|
204 |
+
fireSound.cloneNode().play();
|
205 |
+
bullets.push({
|
|
|
206 |
x: player.x,
|
207 |
y: player.y,
|
208 |
+
angle: player.angle,
|
209 |
+
speed: 10,
|
210 |
+
isEnemy: false,
|
211 |
+
damage: weapon.damage,
|
212 |
+
size: weapon.bulletSize
|
213 |
});
|
214 |
lastShot = now;
|
215 |
}
|
216 |
}
|
217 |
|
218 |
+
const keys = {};
|
219 |
+
document.addEventListener('keydown', e => keys[e.key] = true);
|
220 |
+
document.addEventListener('keyup', e => keys[e.key] = false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
function updateGame() {
|
223 |
if(gameOver) return;
|
224 |
|
225 |
+
// Player movement
|
226 |
+
if(keys['w']) player.y -= player.speed;
|
227 |
+
if(keys['s']) player.y += player.speed;
|
228 |
+
if(keys['a']) player.x -= player.speed;
|
229 |
+
if(keys['d']) player.x += player.speed;
|
230 |
|
231 |
+
// Keep player in bounds
|
232 |
+
player.x = Math.max(player.width/2, Math.min(canvas.width - player.width/2, player.x));
|
233 |
+
player.y = Math.max(player.height/2, Math.min(canvas.height - player.height/2, player.y));
|
234 |
|
235 |
+
fireBullet();
|
236 |
|
237 |
+
// Update enemies
|
238 |
enemies.forEach(enemy => enemy.update());
|
239 |
|
240 |
+
// Update bullets
|
241 |
+
bullets = bullets.filter(bullet => {
|
242 |
bullet.x += Math.cos(bullet.angle) * bullet.speed;
|
243 |
bullet.y += Math.sin(bullet.angle) * bullet.speed;
|
244 |
|
245 |
+
if(!bullet.isEnemy) {
|
246 |
+
enemies = enemies.filter(enemy => {
|
247 |
+
const dist = Math.hypot(bullet.x - enemy.x, bullet.y - enemy.y);
|
248 |
+
if(dist < 30) {
|
249 |
+
enemy.health -= enemy.maxHealth * bullet.damage;
|
250 |
+
if(enemy.health <= 0) {
|
251 |
+
spawnHealthItem(enemy.x, enemy.y);
|
252 |
+
return false;
|
253 |
+
}
|
254 |
+
return true;
|
255 |
}
|
256 |
return true;
|
257 |
+
});
|
258 |
+
} else {
|
259 |
+
const dist = Math.hypot(bullet.x - player.x, bullet.y - player.y);
|
260 |
+
if(dist < 30) {
|
261 |
+
player.health -= 100;
|
262 |
+
if(player.health <= 0) {
|
263 |
+
gameOver = true;
|
264 |
+
restartBtn.style.display = 'block';
|
265 |
+
}
|
266 |
+
return false;
|
267 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
}
|
269 |
|
270 |
return bullet.x >= 0 && bullet.x <= canvas.width &&
|
271 |
bullet.y >= 0 && bullet.y <= canvas.height;
|
272 |
});
|
273 |
|
274 |
+
// Update items
|
275 |
items = items.filter(item => {
|
276 |
const dist = Math.hypot(item.x - player.x, item.y - player.y);
|
277 |
if(dist < 30) {
|
|
|
281 |
return true;
|
282 |
});
|
283 |
|
284 |
+
// Check win condition
|
285 |
if(enemies.length === 0) {
|
286 |
if(currentRound < 10) {
|
287 |
nextRoundBtn.style.display = 'block';
|
|
|
292 |
}
|
293 |
}
|
294 |
|
295 |
+
function spawnHealthItem(x, y) {
|
296 |
+
items.push({x, y});
|
297 |
+
}
|
298 |
+
|
299 |
function drawHealthBar(x, y, health, maxHealth, width, height, color) {
|
|
|
|
|
300 |
ctx.fillStyle = '#333';
|
301 |
+
ctx.fillRect(x - width/2, y - height/2, width, height);
|
302 |
ctx.fillStyle = color;
|
303 |
+
ctx.fillRect(x - width/2, y - height/2, width * (health/maxHealth), height);
|
304 |
}
|
305 |
|
306 |
function drawGame() {
|
307 |
drawBackground();
|
308 |
+
|
309 |
// Draw player
|
310 |
ctx.save();
|
311 |
ctx.translate(player.x, player.y);
|
312 |
+
ctx.rotate(player.angle);
|
313 |
+
ctx.drawImage(playerImg, -player.width/2, -player.height/2, player.width, player.height);
|
|
|
314 |
ctx.restore();
|
315 |
|
316 |
// Draw player health bar
|
|
|
321 |
ctx.save();
|
322 |
ctx.translate(enemy.x, enemy.y);
|
323 |
ctx.rotate(enemy.angle);
|
324 |
+
ctx.drawImage(enemyImg, -25, -25, 50, 50);
|
325 |
ctx.restore();
|
326 |
+
drawHealthBar(enemy.x, enemy.y - 40, enemy.health, enemy.maxHealth, 50, 5, 'red');
|
327 |
});
|
328 |
|
329 |
// Draw bullets
|
330 |
+
bullets.forEach(bullet => {
|
|
|
331 |
ctx.beginPath();
|
332 |
+
ctx.fillStyle = bullet.isEnemy ? 'red' : 'yellow';
|
333 |
+
ctx.arc(bullet.x, bullet.y, bullet.size, 0, Math.PI * 2);
|
334 |
ctx.fill();
|
335 |
});
|
336 |
|
337 |
+
// Draw health items
|
338 |
ctx.fillStyle = 'green';
|
339 |
items.forEach(item => {
|
340 |
ctx.beginPath();
|
|
|
368 |
});
|
369 |
|
370 |
Promise.all([
|
371 |
+
new Promise(resolve => backgroundImg.onload = resolve),
|
372 |
+
new Promise(resolve => playerImg.onload = resolve),
|
|
|
373 |
new Promise(resolve => enemyImg.onload = resolve)
|
374 |
]).then(() => {
|
375 |
initRound();
|