tekulveriley-ui

WW2 plane

Kodari AI
KodariJun 25 at 11:44 pm 11

I’ll create an arcade-style WW2 plane game extension.

WW2PlaneGame
An arcade-style WW2 fighter plane game. Control a Spitfire through enemy territory, dodge obstacles, and shoot down enemy aircraft. Features canvas-based gameplay with keyboard controls, score tracking, difficulty progression, and high score persistence. Includes multiple enemy types and historical plane facts between rounds.

Your WW2 plane game is ready! Control your Spitfire with arrow keys, shoot with spacebar, and rack up high scores by taking down enemy aircraft. The game features progressive difficulty and displays historical plane facts after each mission.

Kodari can make mistakes. Review important code before using.
Files
background.js
manifest.json
popup.css
popup.html
popup.js
Code
5 453
popup.js
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');

let gameState = 'start';
let score = 0;
let highScore = 0;
let health = 100;
let gameLoop;

const keys = {};
const player = {
  x: 50,
  y: canvas.height / 2,
  width: 40,
  height: 25,
  speed: 4,
  bullets: []
};

const enemies = [];
const clouds = [];
let enemySpawnTimer = 0;
let difficultyMultiplier = 1;

const planeFacts = [
  "The Supermarine Spitfire had a top speed of 378 mph and was powered by a Rolls-Royce Merlin engine.",
  "The P-51 Mustang could fly 1,650 miles on a single tank of fuel, making it perfect for bomber escorts.",
  "German Bf 109 fighters were produced in greater numbers than any other fighter aircraft in history.",
  "The Japanese Zero was so maneuverable it could out-turn almost any Allied fighter early in the war.",
  "The B-17 Flying Fortress could take incredible punishment and still bring its crew home safely.",
  "Soviet Yak-3 fighters were so effective, German pilots were ordered to avoid combat with them below 5,000 feet."
];

document.getElementById('startBtn').addEventListener('click', startGame);
document.getElementById('restartBtn').addEventListener('click', startGame);

document.addEventListener('keydown', (e) => {
  keys[e.key] = true;
  if (e.key === ' ') {
    e.preventDefault();
    if (gameState === 'playing') shoot();
  }
});

document.addEventListener('keyup', (e) => {
  keys[e.key] = false;
});

chrome.storage.local.get(['highScore'], (result) => {
  highScore = result.highScore || 0;
  document.getElementById('highScore').textContent = highScore;
});

function startGame() {
  gameState = 'playing';
  score = 0;
  health = 100;
  difficultyMultiplier = 1;
  player.y = canvas.height / 2;
  player.bullets = [];
  enemies.length = 0;
  clouds.length = 0;
  enemySpawnTimer = 0;
  
  for (let i = 0; i < 5; i++) {
    clouds.push({
      x: Math.random() * canvas.width,
      y: Math.random() * canvas.height,
      size: 30 + Math.random() * 40,
      speed: 0.5 + Math.random() * 1
    });
  }
  
  document.getElementById('startScreen').classList.add('hidden');
  document.getElementById('gameOverScreen').classList.add('hidden');
  document.getElementById('gameScreen').classList.remove('hidden');
  
  if (gameLoop) cancelAnimationFrame(gameLoop);
  update();
}

function shoot() {
  player.bullets.push({
    x: player.x + player.width,
    y: player.y + player.height / 2,
    width: 10,
    height: 3,
    speed: 8
  });
}

function spawnEnemy() {
  const type = Math.random() < 0.7 ? 'plane' : 'bomber';
  enemies.push({
    x: canvas.width,
    y: Math.random() * (canvas.height - 40) + 20,
    width: type === 'bomber' ? 50 : 35,
    height: type === 'bomber' ? 30 : 20,
    speed: (1.5 + Math.random() * 1.5) * difficultyMultiplier,
    type: type,
    health: type === 'bomber' ? 3 : 1
  });
}

function update() {
  if (gameState !== 'playing') return;
  
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  
  clouds.forEach(cloud => {
    cloud.x -= cloud.speed;
    if (cloud.x + cloud.size < 0) {
      cloud.x = canvas.width + cloud.size;
      cloud.y = Math.random() * canvas.height;
    }
    
    ctx.fillStyle = 'rgba(255, 255, 255, 0.6)';
    ctx.beginPath();
    ctx.arc(cloud.x, cloud.y, cloud.size, 0, Math.PI * 2);
    ctx.fill();
  });
  
  if (keys['ArrowUp'] && player.y > 0) player.y -= player.speed;
  if (keys['ArrowDown'] && player.y < canvas.height - player.height) player.y += player.speed;
  
  ctx.fillStyle = '#2d5016';
  ctx.fillRect(player.x, player.y, player.width, player.height);
  ctx.fillStyle = '#4a7c2c';
  ctx.beginPath();
  ctx.moveTo(player.x, player.y + player.height / 2);
  ctx.lineTo(player.x + 15, player.y);
  ctx.lineTo(player.x + 15, player.y + player.height);
  ctx.closePath();
  ctx.fill();
  ctx.fillStyle = '#1a3010';
  ctx.fillRect(player.x + player.width - 15, player.y + 8, 15, 9);
  
  player.bullets.forEach((bullet, index) => {
    bullet.x += bullet.speed;
    
    ctx.fillStyle = '#ffff00';
    ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
    
    if (bullet.x > canvas.width) {
      player.bullets.splice(index, 1);
    }
  });
  
  enemySpawnTimer++;
  if (enemySpawnTimer > Math.max(60 - score / 10, 30)) {
    spawnEnemy();
    enemySpawnTimer = 0;
  }
  
  difficultyMultiplier = 1 + score / 500;
  
  enemies.forEach((enemy, eIndex) => {
    enemy.x -= enemy.speed;
    
    if (enemy.type === 'bomber') {
      ctx.fillStyle = '#8b0000';
      ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
      ctx.fillStyle = '#a52a2a';
      ctx.beginPath();
      ctx.moveTo(enemy.x + enemy.width, enemy.y + enemy.height / 2);
      ctx.lineTo(enemy.x + enemy.width - 20, enemy.y);
      ctx.lineTo(enemy.x + enemy.width - 20, enemy.y + enemy.height);
      ctx.closePath();
      ctx.fill();
    } else {
      ctx.fillStyle = '#8b0000';
      ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
      ctx.fillStyle = '#a52a2a';
      ctx.beginPath();
      ctx.moveTo(enemy.x + enemy.width, enemy.y + enemy.height / 2);
      ctx.lineTo(enemy.x + enemy.width - 12, enemy.y);
      ctx.lineTo(enemy.x + enemy.width - 12, enemy.y + enemy.height);
      ctx.closePath();
      ctx.fill();
    }
    
    player.bullets.forEach((bullet, bIndex) => {
      if (
        bullet.x < enemy.x + enemy.width &&
        bullet.x + bullet.width > enemy.x &&
        bullet.y < enemy.y + enemy.height &&
        bullet.y + bullet.height > enemy.y
      ) {
        player.bullets.splice(bIndex, 1);
        enemy.health--;
        
        if (enemy.health <= 0) {
          enemies.splice(eIndex, 1);
          score += enemy.type === 'bomber' ? 30 : 10;
          document.getElementById('score').textContent = 'Score: ' + score;
        }
      }
    });
    
    if (