Spaces:
Running
Running
File size: 12,390 Bytes
62cabf1 ec0a41c 62cabf1 959eace 62cabf1 d4db2d1 62cabf1 959eace 62cabf1 959eace 62cabf1 959eace 62cabf1 959eace 62cabf1 d4db2d1 959eace 62cabf1 959eace 62cabf1 959eace 62cabf1 959eace 62cabf1 959eace d4db2d1 62cabf1 d4db2d1 959eace 62cabf1 959eace 62cabf1 959eace d4db2d1 62cabf1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace 62cabf1 d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 62cabf1 d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace 62cabf1 959eace 62cabf1 d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace 62cabf1 d4db2d1 959eace d4db2d1 62cabf1 959eace d4db2d1 62cabf1 959eace 62cabf1 d4db2d1 959eace 62cabf1 959eace 62cabf1 d4db2d1 959eace 62cabf1 d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace 62cabf1 959eace d4db2d1 b622b52 959eace d4db2d1 959eace d4db2d1 959eace d4db2d1 959eace 62cabf1 959eace d4db2d1 b622b52 d4db2d1 b622b52 62cabf1 d4db2d1 b622b52 959eace 62cabf1 959eace 62cabf1 959eace 62cabf1 959eace 342d5b8 959eace 342d5b8 959eace d4db2d1 959eace 342d5b8 959eace 342d5b8 959eace |
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
<!DOCTYPE html>
<html>
<head>
<title>Tank Battle</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #333;
font-family: Arial;
}
#gameCanvas {
background-repeat: repeat;
}
#instructions {
position: fixed;
top: 10px;
right: 10px;
color: white;
background: rgba(0,0,0,0.7);
padding: 10px;
border-radius: 5px;
z-index: 1000;
}
#weaponInfo {
position: fixed;
top: 100px;
right: 10px;
color: white;
background: rgba(0,0,0,0.7);
padding: 10px;
border-radius: 5px;
z-index: 1000;
}
.button {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px 40px;
font-size: 24px;
background: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
display: none;
z-index: 1000;
}
</style>
</head>
<body>
<div id="instructions">
Controls:<br>
WASD - Move tank<br>
Mouse - Aim<br>
Space - Fire<br>
R - Switch Weapon
</div>
<div id="weaponInfo">Current Weapon: Cannon</div>
<button id="nextRound" class="button">Next Round</button>
<button id="restart" class="button">Restart Game</button>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const nextRoundBtn = document.getElementById('nextRound');
const restartBtn = document.getElementById('restart');
const weaponInfo = document.getElementById('weaponInfo');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Load images
const backgroundImg = new Image();
backgroundImg.src = 'city.png';
const playerImg = new Image();
playerImg.src = 'player.png';
const enemyImg = new Image();
enemyImg.src = 'enemy.png';
// Audio
const fireSound = new Audio('firemn.ogg');
// Game state
let currentRound = 1;
let gameOver = false;
let currentWeapon = 'cannon';
let enemies = [];
let bullets = [];
let items = [];
let lastShot = 0;
const weapons = {
cannon: {
fireRate: 1000,
damage: 0.5,
bulletSize: 5
},
machinegun: {
fireRate: 200,
damage: 0.05,
bulletSize: 2
}
};
// Player
const player = {
x: canvas.width/2,
y: canvas.height/2,
speed: 5,
angle: 0,
width: 50,
height: 50,
health: 1000,
maxHealth: 1000
};
function drawBackground() {
const pattern = ctx.createPattern(backgroundImg, 'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
class Enemy {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.health = 1000;
this.maxHealth = 1000;
this.speed = 2;
this.lastShot = 0;
this.shootInterval = 2000;
this.angle = 0;
this.moveAngle = Math.random() * Math.PI * 2;
}
update() {
// Movement
this.x += Math.cos(this.moveAngle) * this.speed;
this.y += Math.sin(this.moveAngle) * this.speed;
// Bounds check
if(this.x < 0 || this.x > canvas.width) this.moveAngle = Math.PI - this.moveAngle;
if(this.y < 0 || this.y > canvas.height) this.moveAngle = -this.moveAngle;
// Random direction change
if(Math.random() < 0.02) {
this.moveAngle = Math.random() * Math.PI * 2;
}
// Aim at player
this.angle = Math.atan2(player.y - this.y, player.x - this.x);
// Shooting
const now = Date.now();
if (now - this.lastShot > this.shootInterval) {
this.shoot();
this.lastShot = now;
}
}
shoot() {
fireSound.cloneNode().play();
bullets.push({
x: this.x,
y: this.y,
angle: this.angle,
speed: 5,
isEnemy: true,
size: 3
});
}
}
function initRound() {
enemies = [];
for(let i = 0; i < 5 * currentRound; i++) {
enemies.push(new Enemy());
}
player.health = player.maxHealth;
bullets = [];
items = [];
}
document.addEventListener('keydown', (e) => {
if(e.key.toLowerCase() === 'r') {
currentWeapon = currentWeapon === 'cannon' ? 'machinegun' : 'cannon';
weaponInfo.textContent = `Current Weapon: ${currentWeapon.charAt(0).toUpperCase() + currentWeapon.slice(1)}`;
}
});
canvas.addEventListener('mousemove', (e) => {
player.angle = Math.atan2(e.clientY - player.y, e.clientX - player.x);
});
function fireBullet() {
const weapon = weapons[currentWeapon];
const now = Date.now();
if (keys[' '] && now - lastShot > weapon.fireRate) {
fireSound.cloneNode().play();
bullets.push({
x: player.x,
y: player.y,
angle: player.angle,
speed: 10,
isEnemy: false,
damage: weapon.damage,
size: weapon.bulletSize
});
lastShot = now;
}
}
const keys = {};
document.addEventListener('keydown', e => keys[e.key] = true);
document.addEventListener('keyup', e => keys[e.key] = false);
function updateGame() {
if(gameOver) return;
// Player movement
if(keys['w']) player.y -= player.speed;
if(keys['s']) player.y += player.speed;
if(keys['a']) player.x -= player.speed;
if(keys['d']) player.x += player.speed;
// Keep player in bounds
player.x = Math.max(player.width/2, Math.min(canvas.width - player.width/2, player.x));
player.y = Math.max(player.height/2, Math.min(canvas.height - player.height/2, player.y));
fireBullet();
// Update enemies
enemies.forEach(enemy => enemy.update());
// Update bullets
bullets = bullets.filter(bullet => {
bullet.x += Math.cos(bullet.angle) * bullet.speed;
bullet.y += Math.sin(bullet.angle) * bullet.speed;
if(!bullet.isEnemy) {
enemies = enemies.filter(enemy => {
const dist = Math.hypot(bullet.x - enemy.x, bullet.y - enemy.y);
if(dist < 30) {
enemy.health -= enemy.maxHealth * bullet.damage;
if(enemy.health <= 0) {
spawnHealthItem(enemy.x, enemy.y);
return false;
}
return true;
}
return true;
});
} else {
const dist = Math.hypot(bullet.x - player.x, bullet.y - player.y);
if(dist < 30) {
player.health -= 100;
if(player.health <= 0) {
gameOver = true;
restartBtn.style.display = 'block';
}
return false;
}
}
return bullet.x >= 0 && bullet.x <= canvas.width &&
bullet.y >= 0 && bullet.y <= canvas.height;
});
// Update items
items = items.filter(item => {
const dist = Math.hypot(item.x - player.x, item.y - player.y);
if(dist < 30) {
player.health = Math.min(player.health + 200, player.maxHealth);
return false;
}
return true;
});
// Check win condition
if(enemies.length === 0) {
if(currentRound < 10) {
nextRoundBtn.style.display = 'block';
} else {
gameOver = true;
restartBtn.style.display = 'block';
}
}
}
function spawnHealthItem(x, y) {
items.push({x, y});
}
function drawHealthBar(x, y, health, maxHealth, width, height, color) {
ctx.fillStyle = '#333';
ctx.fillRect(x - width/2, y - height/2, width, height);
ctx.fillStyle = color;
ctx.fillRect(x - width/2, y - height/2, width * (health/maxHealth), height);
}
function drawGame() {
drawBackground();
// Draw player
ctx.save();
ctx.translate(player.x, player.y);
ctx.rotate(player.angle);
ctx.drawImage(playerImg, -player.width/2, -player.height/2, player.width, player.height);
ctx.restore();
// Draw player health bar
drawHealthBar(canvas.width/2, 30, player.health, player.maxHealth, 200, 20, 'green');
// Draw enemies
enemies.forEach(enemy => {
ctx.save();
ctx.translate(enemy.x, enemy.y);
ctx.rotate(enemy.angle);
ctx.drawImage(enemyImg, -25, -25, 50, 50);
ctx.restore();
drawHealthBar(enemy.x, enemy.y - 40, enemy.health, enemy.maxHealth, 50, 5, 'red');
});
// Draw bullets
bullets.forEach(bullet => {
ctx.beginPath();
ctx.fillStyle = bullet.isEnemy ? 'red' : 'yellow';
ctx.arc(bullet.x, bullet.y, bullet.size, 0, Math.PI * 2);
ctx.fill();
});
// Draw health items
ctx.fillStyle = 'green';
items.forEach(item => {
ctx.beginPath();
ctx.arc(item.x, item.y, 10, 0, Math.PI * 2);
ctx.fill();
});
// Draw round number
ctx.fillStyle = 'white';
ctx.font = '24px Arial';
ctx.fillText(`Round ${currentRound}/10`, 10, 30);
}
function gameLoop() {
updateGame();
drawGame();
requestAnimationFrame(gameLoop);
}
nextRoundBtn.addEventListener('click', () => {
currentRound++;
nextRoundBtn.style.display = 'none';
initRound();
});
restartBtn.addEventListener('click', () => {
currentRound = 1;
gameOver = false;
restartBtn.style.display = 'none';
initRound();
});
Promise.all([
new Promise(resolve => backgroundImg.onload = resolve),
new Promise(resolve => playerImg.onload = resolve),
new Promise(resolve => enemyImg.onload = resolve)
]).then(() => {
initRound();
gameLoop();
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
</script>
</body>
</html> |