79796665

Date: 2025-10-22 10:37:31
Score: 1.5
Natty:
Report link

ভালো — বুঝেছি জীবন। আমি তোমার জন্য একটি সম্পূর্ণ, এক-পেইজ JavaScript + HTML5 Canvas 2D Space Shooter গেমের কোড লিখে দিলাম। এটা পিসি টার্গেট করা, কী-বোর্ডে বাম/ডান/স্পেস ব্যবহার করবে। সবকিছু এক ফাইলে পেস্ট করে ব্রাউজারে খুললেই চলে যাবে।

কিভাবে ব্যবহার করবে

  1. নিম্নের সব কোড কপি করে index.html নাম দিয়ে ফাইল বানাও।

  2. ব্রাউজারে খুলো (ডাবল ক্লিক বা Ctrl+O)।

  3. তত্ক্ষণাত খেলতে পারবে — বাম/ডান অ্যারো দিয়ে চলবে, স্পেস দিয়ে শ্যুট।

কোড (একক HTML ফাইল)

<!doctype html>
<html lang="bn">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <title>Simple Space Shooter — জীবন</title>
  <style>
    html,body { height:100%; margin:0; background:#000; color:#fff; font-family:system-ui,Segoe UI,Roboto;}
    #game {
      display:block;
      margin:20px auto;
      background:#000;
      border:4px solid #222;
      box-shadow:0 10px 30px rgba(0,0,0,.6);
    }
    .ui {
      text-align:center;
      margin-top:6px;
      font-size:14px;
      color:#cfcfcf;
    }
    button { padding:8px 12px; font-size:14px; margin:0 6px; cursor:pointer;}
    @media (max-width:600px){
      #game { width:320px; height:480px; }
    }
  </style>
</head>
<body>
  <canvas id="game" width="480" height="640"></canvas>
  <div class="ui">
    <button id="startBtn">Start / Restart</button>
    <span style="margin-left:12px">Score: <span id="score">0</span></span>
    <span style="margin-left:12px">Lives: <span id="lives">3</span></span>
    <div style="margin-top:6px; font-size:13px; color:#aaa">
      Controls: ← → = move, Space = shoot
    </div>
  </div>

<script>
/*
  Simple Space Shooter
  - Player: small triangle ship
  - Enemies: descending squares
  - Bullets: small rectangles
  - Collision detection, scoring, lives
*/

const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const scoreEl = document.getElementById('score');
const livesEl = document.getElementById('lives');
const startBtn = document.getElementById('startBtn');

let W = canvas.width, H = canvas.height;
window.addEventListener('resize', ()=>{ /* keep fixed size for simplicity */ });

/* Game state */
let keys = {};
let bullets = [];
let enemies = [];
let particles = [];
let score = 0;
let lives = 3;
let gameOver = false;
let spawnTimer = 0;
let spawnInterval = 90; // frames
let fireCooldown = 0;

/* Player */
const player = {
  x: W/2,
  y: H - 60,
  w: 24,
  h: 28,
  speed: 5,
  alive: true
};

/* Input */
window.addEventListener('keydown', (e)=>{ keys[e.code] = true; if(e.code==='Space') e.preventDefault(); });
window.addEventListener('keyup', (e)=>{ keys[e.code] = false; });

startBtn.addEventListener('click', startGame);

/* Utility */
function rand(min,max){ return Math.random()*(max-min)+min; }
function clamp(v,a,b){ return Math.max(a, Math.min(b, v)); }

/* Entities */
function spawnEnemy(){
  const size = Math.round(rand(22,44));
  enemies.push({
    x: rand(size, W-size),
    y: -size,
    w: size,
    h: size,
    speed: rand(1.2, 2.2) + score*0.002, // slight difficulty scaling
    hp: Math.random()<0.15 ? 2 : 1, // some tougher enemies
  });
}

function shoot(){
  if(fireCooldown>0) return;
  bullets.push({
    x: player.x,
    y: player.y - player.h/2 - 6,
    w: 4,
    h: 10,
    speed: 8
  });
  fireCooldown = 12; // frames between shots
}

/* Collision simple AABB */
function coll(a,b){
  return !(a.x + (a.w||0)/2 < b.x - (b.w||0)/2 ||
           a.x - (a.w||0)/2 > b.x + (b.w||0)/2 ||
           a.y + (a.h||0)/2 < b.y - (b.h||0)/2 ||
           a.y - (a.h||0)/2 > b.y + (b.h||0)/2);
}

/* Particle for explosion */
function makeExplosion(x,y,count=12){
  for(let i=0;i<count;i++){
    particles.push({
      x, y,
      vx: Math.cos(Math.random()*Math.PI*2)*rand(1,4),
      vy: Math.sin(Math.random()*Math.PI*2)*rand(1,4),
      life: rand(24,48),
      size: rand(1.5,3.5)
    });
  }
}

/* Update loop */
function update(){
  if(gameOver) {
    drawGameOver();
    return;
  }

  // Input: move left/right
  if(keys['ArrowLeft'] || keys['KeyA']) player.x -= player.speed;
  if(keys['ArrowRight'] || keys['KeyD']) player.x += player.speed;
  player.x = clamp(player.x, 20, W-20);

  if((keys['Space'] || keys['KeyK']) ) shoot();
  if(fireCooldown>0) fireCooldown--;

  // Update bullets
  for(let i=bullets.length-1;i>=0;i--){
    const b = bullets[i];
    b.y -= b.speed;
    if(b.y + b.h < 0) bullets.splice(i,1);
  }

  // Spawn enemies
  spawnTimer++;
  const adaptive = Math.max(20, spawnInterval - Math.floor(score/5));
  if(spawnTimer > adaptive){
    spawnTimer = 0;
    spawnEnemy();
  }

  // Update enemies
  for(let i=enemies.length-1;i>=0;i--){
    const e = enemies[i];
    e.y += e.speed;
    // Enemy collides with player?
    if(coll({x:player.x,y:player.y,w:player.w,h:player.h}, e)){
      // lose a life and remove enemy
      enemies.splice(i,1);
      lives--;
      makeExplosion(player.x, player.y, 18);
      if(lives<=0){ gameOver = true; }
      continue;
    }
    // Offscreen -> remove and lose life
    if(e.y - e.h/2 > H){
      enemies.splice(i,1);
      lives--;
      if(lives<=0) gameOver = true;
      continue;
    }
    // Bullets hit enemy
    for(let j=bullets.length-1;j>=0;j--){
      const b = bullets[j];
      if(coll(b, e)){
        bullets.splice(j,1);
        e.hp--;
        if(e.hp<=0){
          // destroy
          score += 10;
          makeExplosion(e.x, e.y, 14);
          enemies.splice(i,1);
        } else {
          score += 5;
        }
        break;
      }
    }
  }

  // Update particles
  for(let i=particles.length-1;i>=0;i--){
    const p = particles[i];
    p.x += p.vx;
    p.y += p.vy;
    p.vy += 0.08; // gravity-ish
    p.life--;
    if(p.life<=0) particles.splice(i,1);
  }

  // update UI
  scoreEl.textContent = score;
  livesEl.textContent = lives;

  // draw
  draw();


  // next frame
  requestAnimationFrame(update);
}

/* Drawing functions */
function clear
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • No latin characters (2):
  • Low reputation (1):
Posted by: roksana parvin