狗狗大战 - 搜打撤射击
by EpicFlare67433 lines17.3 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>狗狗大战 - 搜打撤射击</title>
<style>
*{margin:0;padding:0;box-sizing:border-box;font-family:system-ui}
body{background:#111;color:#fff;overflow:hidden}
#gameCanvas{background:#1a1a1a;border:2px solid #444}
.ui{position:absolute;top:4px;left:4px;z-index:10}
.btn{padding:6px 10px;margin:2px;border:none;background:#3377dd;color:#fff;border-radius:4px;cursor:pointer}
.btn-danger{background:#bb3333}
.btn-success{background:#229944}
.panel{position:absolute;background:#222;border:2px solid #666;padding:10px;z-index:20;display:none}
.item{display:inline-block;padding:4px;margin:2px;background:#333;width:70px;height:70px;text-align:center;vertical-align:top;font-size:12px}
.item:hover{background:#444}
.minimap{position:absolute;top:4px;right:4px;width:160px;height:120px;background:#000;border:1px solid #888}
</style>
</head>
<body>
<div class="ui">
<button class="btn" onclick="openPanel('backpack')">🎒背包</button>
<button class="btn" onclick="openPanel('storage')">🏠仓库</button>
<button class="btn" onclick="openPanel('market')">🛒市场</button>
<button class="btn" onclick="openPanel('workbench')">🔨工作台</button>
<button class="btn" onclick="openPanel('research')">📚研究</button>
<button class="btn" onclick="openPanel('operator')">🐶干员</button>
<button class="btn btn-success" onclick="startMission()">🗺️出击</button>
<button class="btn btn-danger" onclick="retreat()">🚪撤退</button>
<br>
<div>金币:<span id="gold">0</span></div>
</div>
<canvas id="gameCanvas" width="1000" height="600"></canvas>
<canvas id="minimap" class="minimap" width="160" height="120"></canvas>
<!-- 背包 -->
<div class="panel" id="panel-backpack" style="left:10px;top:60px;width:320px">
<h3>🎒本局背包</h3>
<div id="backpack-grid"></div>
<button class="btn" onclick="closePanel('backpack')">关闭</button>
</div>
<!-- 仓库 -->
<div class="panel" id="panel-storage" style="left:340px;top:60px;width:320px">
<h3>🏠仓库(永久)</h3>
<div id="storage-grid"></div>
<button class="btn" onclick="closePanel('storage')">关闭</button>
</div>
<!-- 市场 -->
<div class="panel" id="panel-market" style="left:10px;top:60px;width:360px">
<h3>🛒市场</h3>
<div id="market-list"></div>
<button class="btn" onclick="closePanel('market')">关闭</button>
</div>
<!-- 工作台 -->
<div class="panel" id="panel-workbench" style="left:10px;top:60px;width:360px">
<h3>🔨合成</h3>
<div id="craft-list"></div>
<button class="btn" onclick="closePanel('workbench')">关闭</button>
</div>
<!-- 研究 -->
<div class="panel" id="panel-research" style="left:10px;top:60px;width:340px">
<h3>📚永久研究</h3>
<div id="research-list"></div>
<button class="btn" onclick="closePanel('research')">关闭</button>
</div>
<!-- 干员 -->
<div class="panel" id="panel-operator" style="left:10px;top:60px;width:340px">
<h3>🐶干员</h3>
<div id="operator-list"></div>
<button class="btn" onclick="closePanel('operator')">关闭</button>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const mmCanvas = document.getElementById('minimap');
const mmCtx = mmCanvas.getContext('2d');
const W = canvas.width, H = canvas.height;
// 物品定义
const ITEMS = {
ammo:{name:"弹药",price:5,desc:"子弹",stack:99},
med:{name:"急救包",price:20,desc:"回30血",stack:20},
boost:{name:"强化针",price:50,desc:"本局伤害+25%",stack:10},
armor:{name:"护甲",price:80,desc:"减伤",stack:5},
mat:{name:"材料",price:10,desc:"合成、研究用",stack:99}
};
// 合成配方
const RECIPES = [
{out:"boost",count:1,need:{mat:5}},
{out:"ammo",count:20,need:{mat:2}},
{out:"med",count:1,need:{mat:3}}
];
// 研究
const RESEARCH = [
{id:"dmg",name:"伤害强化",cost:{mat:15},add:{dmg:0.08},max:10},
{id:"hp",name:"生命强化",cost:{mat:15},add:{maxHp:10},max:10},
{id:"spd",name:"移速强化",cost:{mat:15},add:{spd:0.15},max:10}
];
// 干员
const OPERATORS = [
{id:"dog1",name:"普通修狗",unlock:true,base:{hp:100,dmg:1,spd:4}},
{id:"dog2",name:"突击犬",unlock:false,base:{hp:80,dmg:1.3,spd:5},cost:200},
{id:"dog3",name:"重甲犬",unlock:false,base:{hp:160,dmg:0.8,spd:3},cost:300}
];
// 全局存档
let save = {
gold:100,
storage:{ammo:20,med:2,boost:0,armor:0,mat:10},
researchLv:{dmg:0,hp:0,spd:0},
opIdx:0,
operators:JSON.parse(JSON.stringify(OPERATORS))
};
// 对局内
let mission = {active:false,player:null,bullets:[],enemies:[],lootBoxes:[],wave:1,bossSpawned:false,retreatPoint:{x:850,y:500},boostBuff:false,alive:true};
let backpack = {ammo:0,med:0,boost:0,armor:0,mat:0};
const key = {};
let mouse = {x:0,y:0,down:false};
let openId = null;
// 按键鼠标
window.addEventListener('keydown',e=>{key[e.key.toLowerCase()]=true;});
window.addEventListener('keyup',e=>key[e.key.toLowerCase()]=false);
canvas.addEventListener('mousemove',e=>{const r=canvas.getBoundingClientRect();mouse.x=e.clientX-r.left;mouse.y=e.clientY-r.top;});
canvas.addEventListener('mousedown',()=>mouse.down=true);
canvas.addEventListener('mouseup',()=>mouse.down=false);
function getPlayerStats(){
const op = save.operators[save.opIdx];
const r = save.researchLv;
return {
maxHp:op.base.hp + r.hp*10,
dmgMul:op.base.dmg * (1 + r.dmg*0.08),
spd:op.base.spd + r.spd*0.15
}
}
function resetBackpack(){
backpack = {ammo:0,med:0,boost:0,armor:0,mat:0};
}
function addItem(obj,id,num=1){
if(!obj[id]) obj[id]=0;
obj[id] = Math.min(obj[id]+num, ITEMS[id].stack);
}
function hasItem(obj,id,num=1){return (obj[id]||0)>=num;}
function consumeItem(obj,id,num=1){if(!hasItem(obj,id,num))return false;obj[id]-=num;if(obj[id]<=0)delete obj[id];return true;}
// 面板
function openPanel(id){closePanel(openId);openId=id;document.getElementById('panel-'+id).style.display="block";renderPanel(id);}
function closePanel(id){if(id)document.getElementById('panel-'+id).style.display="none";openId=null;}
function renderPanel(id){
if(id==="backpack") renderGrid("backpack-grid",backpack);
if(id==="storage") renderGrid("storage-grid",save.storage);
if(id==="market") renderMarket();
if(id==="workbench") renderCraft();
if(id==="research") renderResearch();
if(id==="operator") renderOperator();
document.getElementById('gold').innerText = save.gold;
}
function renderGrid(divId,bag){
const d=document.getElementById(divId);d.innerHTML="";
for(let k in ITEMS){
const v=bag[k]||0;
const it=ITEMS[k];
const div=document.createElement("div");
div.className="item";
div.innerHTML=`<div>${it.name}</div><div>x${v}</div>`;
if(openId==="backpack"&&v>0){
div.onclick=()=>useItem(k);
}
d.appendChild(div);
}
}
function useItem(id){
if(!mission.active) return;
if(id==="med"&&consumeItem(backpack,"med",1)){mission.player.hp=Math.min(mission.player.maxHp,mission.player.hp+30);}
if(id==="boost"&&consumeItem(backpack,"boost",1)){mission.boostBuff=true;}
renderPanel("backpack");
}
function renderMarket(){
const d=document.getElementById("market-list");d.innerHTML="";
for(let k in ITEMS){
const it=ITEMS[k];
const btn=document.createElement("button");
btn.className="btn";
btn.innerText=`${it.name} ${it.price}金币`;
btn.onclick=()=>{
if(save.gold>=it.price){
save.gold -= it.price;
addItem(save.storage,k,1);
renderPanel("market");
}
}
d.appendChild(btn);
}
}
function renderCraft(){
const d=document.getElementById("craft-list");d.innerHTML="";
RECIPES.forEach(r=>{
const needText=Object.entries(r.need).map(([k,v])=>ITEMS[k].name+":"+v).join(" ");
const btn=document.createElement("button");
btn.className="btn";
btn.innerText=`合成${ITEMS[r.out].name} 需要:${needText}`;
btn.onclick=()=>{
let ok=true;
for(let k in r.need) if(!hasItem(save.storage,k,r.need[k])) ok=false;
if(ok){
for(let k in r.need) consumeItem(save.storage,k,r.need[k]);
addItem(save.storage,r.out,r.count);
renderPanel("workbench");
}
}
d.appendChild(btn);
})
}
function renderResearch(){
const d=document.getElementById("research-list");d.innerHTML="";
RESEARCH.forEach(r=>{
const lv=save.researchLv[r.id];
const needText=Object.entries(r.cost).map(([k,v])=>ITEMS[k].name+":"+v).join(" ");
const btn=document.createElement("button");
btn.className="btn";
btn.innerText=`${r.name} Lv${lv}/${r.max} | ${needText}`;
btn.disabled=lv>=r.max;
btn.onclick=()=>{
if(lv>=r.max)return;
let ok=true;
for(let k in r.cost) if(!hasItem(save.storage,k,r.cost[k])) ok=false;
if(ok){
for(let k in r.cost) consumeItem(save.storage,k,r.cost[k]);
save.researchLv[r.id]++;
renderPanel("research");
}
}
d.appendChild(btn);
})
}
function renderOperator(){
const d=document.getElementById("operator-list");d.innerHTML="";
save.operators.forEach((op,i)=>{
const btn=document.createElement("button");
btn.className="btn";
btn.innerText=op.name + (op.unlock?"【已解锁】":"解锁"+op.cost+"金币");
if(!op.unlock) btn.onclick=()=>{
if(save.gold>=op.cost){save.gold-=op.cost;op.unlock=true;renderPanel("operator");}
};
else btn.onclick=()=>{save.opIdx=i;renderPanel("operator");}
d.appendChild(btn);
})
}
// 出击
function startMission(){
if(mission.active) return;
resetBackpack();
// 仓库物资转移进背包
for(let k in save.storage){
addItem(backpack,k,save.storage[k]);
save.storage[k]=0;
}
const stat = getPlayerStats();
mission = {
active:true,
player:{x:100,y:300,hp:stat.maxHp,maxHp:stat.maxHp,spd:stat.spd,dmgMul:stat.dmgMul,shootCd:0,shootDelay:12},
bullets:[],enemies:[],lootBoxes:[],wave:1,bossSpawned:false,
retreatPoint:{x:850,y:500},
boostBuff:false,alive:true
};
mission.enemies.push(...spawnWave(mission.wave));
}
function retreat(){
if(!mission.active) return;
const p = mission.player;
const rt = mission.retreatPoint;
const dist = Math.hypot(p.x-rt.x, p.y-rt.y);
if(dist<50){
// 成功撤退,背包全部存入仓库
for(let k in backpack) addItem(save.storage,k,backpack[k]);
mission.active=false;
alert("✅ 成功撤退!物资带回仓库!");
}else{
alert("⚠️ 你不在撤离点!跑到右下角撤离点再撤退!");
}
}
function spawnWave(w){
const arr=[];
const cnt=3+w*2;
for(let i=0;i<cnt;i++) arr.push(createEnemy(false));
return arr;
}
function createEnemy(isBoss=false){
if(isBoss){
return {x:Math.random()*(W-120)+60,y:Math.random()*(H-120)+60,hp:350,maxHp:350,spd:1.8,size:40,dmg:18,shootDelay:45,shootCd:Math.random()*45,isBoss:true,alive:true};
}else{
return {x:Math.random()*(W-120)+60,y:Math.random()*(H-120)+60,hp:50,maxHp:50,spd:2.2,size:20,dmg:10,shootDelay:70,shootCd:Math.random()*70,isBoss:false,alive:true};
}
}
function shootBullet(shooter,tx,ty,isPlayer){
if(shooter.shootCd>0) return;
if(isPlayer && backpack.ammo<=0) return;
if(isPlayer) backpack.ammo--;
shooter.shootCd = shooter.shootDelay;
const dx=tx-shooter.x, dy=ty-shooter.y;
const d=Math.hypot(dx,dy);
const spd = isPlayer?10:6;
let dmg = shooter.dmg||12;
if(isPlayer){
dmg *= mission.player.dmgMul;
if(mission.boostBuff) dmg *=1.25;
}
mission.bullets.push({x:shooter.x,y:shooter.y,vx:dx/d*spd,vy:dy/d*spd,isPlayer,dmg});
}
function rand(a,b){return Math.floor(Math.random()*(b-a+1))+a}
// 主循环
function loop(){
ctx.fillStyle="#1a1a1a";ctx.fillRect(0,0,W,H);
if(mission.active && mission.alive){
const p = mission.player;
// 撤离点
ctx.fillStyle="#22cc22";ctx.beginPath();ctx.arc(mission.retreatPoint.x,mission.retreatPoint.y,35,0,Math.PI*2);ctx.fill();
ctx.fillStyle="#fff";ctx.font="14px system-ui";ctx.fillText("撤离点",mission.retreatPoint.x-25,mission.retreatPoint.y+4);
//移动
if(key['w']&&p.y>30)p.y-=p.spd;
if(key['s']&&p.y<H-30)p.y+=p.spd;
if(key['a']&&p.x>30)p.x-=p.spd;
if(key['d']&&p.x<W-30)p.x+=p.spd;
if(p.shootCd>0)p.shootCd--;
if(mouse.down) shootBullet(p,mouse.x,mouse.y,true);
//子弹
for(let i=mission.bullets.length-1;i>=0;i--){
const b=mission.bullets[i];
b.x+=b.vx;b.y+=b.vy;
if(b.x<0||b.x>W||b.y<0||b.y>H){mission.bullets.splice(i,1);continue;}
if(b.isPlayer){
for(const e of mission.enemies){
if(!e.alive)continue;
const d=Math.hypot(b.x-e.x,b.y-e.y);
if(d<e.size){
e.hp -= b.dmg;
mission.bullets.splice(i,1);
if(e.hp<=0){
e.alive=false;
mission.lootBoxes.push({x:e.x,y:e.y,isBossLoot:e.isBoss,ammo:e.isBoss?rand(60,120):rand(12,35),med:e.isBoss?rand(3,6):rand(1,2),mat:e.isBoss?rand(10,20):rand(2,5)});
}
break;
}
}
}else{
const d=Math.hypot(b.x-p.x,b.y-p.y);
if(d<22){
p.hp -= b.dmg;
mission.bullets.splice(i,1);
if(p.hp<=0){
mission.alive=false;
alert("💀 你阵亡!本局物资全部丢失!");
mission.active=false;
}
}
}
}
//敌人AI
for(const e of mission.enemies){
if(!e.alive)continue;
const dx=p.x-e.x, dy=p.y-e.y;
const d=Math.hypot(dx,dy);
if(d>180){e.x+=dx/d*e.spd;e.y+=dy/d*e.spd;}
e.shootCd--;
if(e.shootCd<=0 && d<550){e.shootCd=e.shootDelay;shootBullet(e,p.x,p.y,false);}
}
//拾取盒子
for(let i=mission.lootBoxes.length-1;i>=0;i--){
const box=mission.lootBoxes[i];
const d=Math.hypot(p.x-box.x,p.y-box.y);
if(d<35){
addItem(backpack,"ammo",box.ammo);
addItem(backpack,"med",box.med);
addItem(backpack,"mat",box.mat);
mission.lootBoxes.splice(i,1);
}
}
//波次检查
const aliveEn = mission.enemies.filter(x=>x.alive);
if(aliveEn.length===0){
if(!mission.bossSpawned && mission.wave>=3){
mission.enemies.push(createEnemy(true));
mission.bossSpawned=true;
}else if(mission.bossSpawned){
alert("🎉 击杀BOSS!可以去撤离点撤退!");
mission.bossSpawned=false;
}else{
mission.wave++;
mission.enemies.push(...spawnWave(mission.wave));
}
}
//绘制玩家狗狗
drawDog(p, "#82552e");
for(const e of mission.enemies) if(e.alive) drawDog(e, e.isBoss?"#b82626":"#603f26");
//子弹
for(const b of mission.bullets){
ctx.beginPath();ctx.arc(b.x,b.y,5,0,Math.PI*2);
ctx.fillStyle=b.isPlayer?"#ffdd33":"#ff4444";ctx.fill();
}
//物资盒
for(const box of mission.lootBoxes){
ctx.strokeStyle="#ffdd33";ctx.lineWidth=2;ctx.strokeRect(box.x-18,box.y-18,36,36);
ctx.fillStyle="#b47b35";ctx.fillRect(box.x-14,box.y-14,28,28);
}
//UI
ctx.fillStyle="#fff";ctx.font="18px system-ui";
ctx.fillText(`血量:${p.hp.toFixed(0)}/${p.maxHp} | 弹药:${backpack.ammo} | 波次:${mission.wave}`,10,H-20);
//小地图
mmCtx.fillStyle="#000";mmCtx.fillRect(0,0,160,120);
mmCtx.fillStyle="#0f0";mmCtx.fillRect(mission.retreatPoint.x/W*160-3,mission.retreatPoint.y/H*120-3,6,6);
mmCtx.fillStyle="#ff0";mmCtx.fillRect(p.x/W*160-3,p.y/H*120-3,6,6);
for(const e of aliveEn){mmCtx.fillStyle="#f00";mmCtx.fillRect(e.x/W*160-2,e.y/H*120-2,4,4);}
}
requestAnimationFrame(loop);
}
function drawDog(u,color){
ctx.beginPath();ctx.arc(u.x,u.y,u.size||22,0,Math.PI*2);ctx.fillStyle=color;ctx.fill();
ctx.fillStyle="#fff";
ctx.beginPath();ctx.arc(u.x - (u.size||22)/3, u.y - (u.size||22)/4,4,0,Math.PI*2);
ctx.arc(u.x + (u.size||22)/3, u.y - (u.size||22)/4,4,0,Math.PI*2);ctx.fill();
const bw=(u.size||22)*2;
ctx.fillStyle="#dd3333";ctx.fillRect(u.x - bw/2, u.y - (u.size||22)-12, bw,6);
ctx.fillStyle="#38d138";ctx.fillRect(u.x - bw/2, u.y - (u.size||22)-12, bw * u.hp/u.maxHp,6);
}
loop();
</script>
</body>
</html>
Game Source: 狗狗大战 - 搜打撤射击
Creator: EpicFlare67
Libraries: none
Complexity: complex (433 lines, 17.3 KB)
The full source code is displayed above on this page.
Remix Instructions
To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: game-epicflare67" to link back to the original. Then publish at arcadelab.ai/publish.