Untitled
unknown
plain_text
9 months ago
4.7 kB
9
Indexable
<!doctype html>
enemies.push({x:ex + Math.random()*40 - 20, y:ey, w:28, h:18, spd:30 + level*8 + Math.random()*30, hp:1 + Math.floor(level/3), type, dir:1, cd:Math.random()*2});
}
}
function reset(){
score = 0; lives = 3; level = 1; enemies.length=0; bullets.length=0; particles.length=0; powerups.length=0; player.x = W/2; player.power=1; running = true; spawnWave(6 + level*2);
}
startBtn.addEventListener('click', ()=>{ reset(); });
function rectColl(a,b){ return a.x - a.w/2 < b.x + b.w/2 && a.x + a.w/2 > b.x - b.w/2 && a.y - a.h/2 < b.y + b.h/2 && a.y + a.h/2 > b.y - b.h/2; }
function update(dt){
if(!running) return;
if(keys.left) player.x -= player.speed*dt;
if(keys.right) player.x += player.speed*dt;
if(keys.shoot) player.shoot();
player.x = Math.max(20, Math.min(W-20, player.x));
player.cooldown = Math.max(0, player.cooldown - dt);
bullets.forEach((b,i)=>{ b.y -= b.spd*dt; if(b.y<-20) bullets.splice(i,1); });
enemies.forEach((e,i)=>{
if(e.type==='zigzag') e.x += Math.sin(e.y*0.05)*80*dt;
e.y += e.spd*dt;
if(e.type==='shooter'){ e.cd-=dt; if(e.cd<=0){ e.cd=2; enemyBullets.push({x:e.x,y:e.y+10,w:4,h:10,spd:200}); playSound('enemy'); }}
if(rectColl(player,e)){ createExplosion(player.x, player.y); enemies.splice(i,1); lives -= 1; playSound('explosion'); if(lives<=0){ running=false; saveHigh(); } }
bullets.forEach((b,j)=>{ if(rectColl(b,e)){ bullets.splice(j,1); e.hp -= 1; createExplosion(e.x,e.y); playSound('hit'); if(e.hp<=0){ enemies.splice(i,1); score+=10; if(Math.random()<0.15) powerups.push({x:e.x,y:e.y,w:16,h:16,type:'power'}); } }});
if(e.y>H+40){ enemies.splice(i,1); lives -= 1; if(lives<=0){ running=false; saveHigh(); } }
});
enemyBullets.forEach((b,i)=>{ b.y += b.spd*dt; if(rectColl(player,b)){ enemyBullets.splice(i,1); lives--; playSound('explosion'); if(lives<=0){ running=false; saveHigh(); } } if(b.y>H+20) enemyBullets.splice(i,1); });
powerups.forEach((p,i)=>{ p.y += 60*dt; if(rectColl(player,p)){ powerups.splice(i,1); player.power=Math.min(3,player.power+1); playSound('powerup'); } if(p.y>H+20) powerups.splice(i,1); });
if(enemies.length===0){ level++; spawnWave(6+level*2); }
particles.forEach((p,i)=>{ p.x+=p.vx*dt; p.y+=p.vy*dt; p.life-=dt; p.vy+=60*dt; if(p.life<=0) particles.splice(i,1); });
scoreEl.textContent = 'Score: '+score;
livesEl.textContent = 'Lives: '+lives;
levelEl.textContent = 'Level: '+level;
highscoreEl.textContent = 'High Score: '+highscore;
}
const enemyBullets = [];
function createExplosion(x,y){ for(let i=0;i<12;i++) particles.push({x,y,vx:(Math.random()-0.5)*220,vy:(Math.random()-0.9)*200,life:0.4+Math.random()*0.4}); }
function saveHigh(){ if(score>highscore){ highscore=score; localStorage.setItem('cosmoCourierHigh',score); } }
function playSound(type){
const ctxAudio=new(AudioContext||webkitAudioContext)();
const o=ctxAudio.createOscillator();
const g=ctxAudio.createGain();
o.connect(g); g.connect(ctxAudio.destination);
o.type='square'; let f=440;
if(type==='shoot') f=880; if(type==='enemy') f=220; if(type==='explosion') f=110; if(type==='powerup') f=660; if(type==='hit') f=550;
o.frequency.setValueAtTime(f,ctxAudio.currentTime);
o.start(); o.stop(ctxAudio.currentTime+0.15);
}
const stars=Array.from({length:120},()=>({x:Math.random()*W,y:Math.random()*H,r:Math.random()*1.5+0.2}));
function drawStars(){ ctx.fillStyle='#061226'; ctx.fillRect(0,0,W,H); ctx.fillStyle='#bfe6ff'; stars.forEach(s=>{ ctx.globalAlpha=0.7; ctx.fillRect(s.x,s.y,s.r,s.r); }); ctx.globalAlpha=1; }
function draw(){
ctx.clearRect(0,0,W,H);
drawStars();
player.draw();
ctx.fillStyle='#bff0ff'; bullets.forEach(b=>ctx.fillRect(b.x-b.w/2,b.y-b.h/2,b.w,b.h));
ctx.fillStyle='#ffb3b3'; enemies.forEach(e=>{ctx.save();ctx.translate(e.x,e.y);ctx.fillRect(-e.w/2,-e.h/2,e.w,e.h);ctx.restore();});
ctx.fillStyle='#ff6666'; enemyBullets.forEach(b=>ctx.fillRect(b.x-b.w/2,b.y-b.h/2,b.w,b.h));
ctx.fillStyle='#00ff99'; powerups.forEach(p=>ctx.fillRect(p.x-p.w/2,p.y-p.h/2,p.w,p.h));
particles.forEach(p=>{ctx.globalAlpha=Math.max(0,p.life/0.8);ctx.fillStyle='#ffd3a3';ctx.fillRect(p.x,p.y,2,2);ctx.globalAlpha=1;});
if(!running){ ctx.fillStyle='rgba(5,8,16,0.6)';ctx.fillRect(0,0,W,H);ctx.fillStyle='#fff';ctx.textAlign='center';ctx.font='20px system-ui';ctx.fillText(lives>0?'Paused — press Start':'Game Over — press Start',W/2,H/2-10);ctx.font='14px system-ui';ctx.fillText('Score: '+score+' — Level: '+level+' — High Score: '+highscore,W/2,H/2+16); }
}
function loop(ts){ if(!lastTs) lastTs=ts; const dt=Math.min(0.033,(ts-lastTs)/1000); update(dt); draw(); lastTs=ts; requestAnimationFrame(loop); }
requestAnimationFrame(loop);
})();
</script>
</body>
</html>Editor is loading...
Leave a Comment