荒野求生 · 手游版
by HyperFalcon761245 lines36.8 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="theme-color" content="#0a0c14">
<title>荒野求生 · 手游版</title>
<style>
*{margin:0;padding:0;box-sizing:border-box;-webkit-tap-highlight-color:transparent;}
html,body{
width:100%;height:100%;overflow:hidden;background:#0a0c14;
touch-action:none;overscroll-behavior:none;
user-select:none;-webkit-user-select:none;
font-family:system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;
}
canvas{display:block;touch-action:none;}
</style>
</head>
<body>
<canvas id="game"></canvas>
<script>
(() => {
'use strict';
/* ================= 基础 ================= */
const cvs = document.getElementById('game');
const ctx = cvs.getContext('2d');
const lightCvs = document.createElement('canvas');
const lctx = lightCvs.getContext('2d');
let W = 0, H = 0;
let isTouch = ('ontouchstart' in window) || navigator.maxTouchPoints > 0;
function resize(){
W = cvs.width = Math.floor(window.innerWidth);
H = cvs.height = Math.floor(window.innerHeight);
lightCvs.width = W; lightCvs.height = H;
}
window.addEventListener('resize', resize);
window.addEventListener('orientationchange', () => setTimeout(resize, 120));
if(window.visualViewport) window.visualViewport.addEventListener('resize', resize);
resize();
const TAU = Math.PI * 2;
const rand = (a,b) => a + Math.random()*(b-a);
const randInt = (a,b) => Math.floor(rand(a,b+1));
const clamp = (v,a,b) => v<a?a:(v>b?b:v);
const dist = (x1,y1,x2,y2) => Math.hypot(x1-x2, y1-y2);
const FONT = 'system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif';
/* ================= 世界 ================= */
const WORLD = { w: 3000, h: 3000 };
const state = {
time: 0, dayLen: 62, nightAt: 0.6,
day: 1, over: false,
spawnTimer: 4, shake: 0,
};
const player = {
x: WORLD.w/2, y: WORLD.h/2, r: 13,
hp: 100, maxHp: 100,
hunger: 100, thirst: 100,
speed: 172, facing: 0,
atkCd: 0, atkAnim: 0, hurtCd: 0, actCd: 0,
walk: 0,
};
const inv = { wood: 0, stone: 0, berry: 2 };
let trees = [], rocks = [], bushes = [], pools = [], fires = [];
let monsters = [], parts = [], texts = [];
let target = null;
let toastMsg = '', toastTimer = 0;
let groundPattern = null;
/* ================= 触摸控件状态 ================= */
const stick = { active:false, id:null, cx:0, cy:0, R:52, dx:0, dy:0, mag:0 };
const btnPress = {}; // id -> 剩余高亮时间
const btnDefs = ['attack','interact','fire','eat'];
/* ================= 工具 ================= */
function toast(msg){ toastMsg = msg; toastTimer = 1.7; }
function findSpot(minD, list){
for(let t=0;t<40;t++){
const x = rand(100, WORLD.w-100), y = rand(100, WORLD.h-100);
let ok = true;
for(const o of list){ if(dist(x,y,o.x,o.y) < minD){ ok = false; break; } }
if(ok) return {x,y};
}
return { x: rand(100,WORLD.w-100), y: rand(100,WORLD.h-100) };
}
function makeGround(){
const t = document.createElement('canvas');
t.width = t.height = 256;
const c = t.getContext('2d');
c.fillStyle = '#38472e';
c.fillRect(0,0,256,256);
for(let i=0;i<520;i++){
c.fillStyle = Math.random()<0.5 ? 'rgba(72,94,58,0.55)' : 'rgba(42,56,34,0.6)';
const x = Math.random()*256, y = Math.random()*256, s = Math.random()*4+1;
c.fillRect(x,y,s,s);
}
for(let i=0;i<26;i++){
c.strokeStyle = 'rgba(60,80,48,0.5)';
c.lineWidth = 1;
const x = Math.random()*256, y = Math.random()*256;
c.beginPath(); c.moveTo(x,y); c.lineTo(x+rand(-4,4), y-rand(4,9)); c.stroke();
}
groundPattern = ctx.createPattern(t, 'repeat');
}
/* ================= 初始化 ================= */
function reset(){
state.time = 0; state.day = 1; state.over = false;
state.spawnTimer = 4; state.shake = 0;
player.x = WORLD.w/2; player.y = WORLD.h/2;
player.hp = player.maxHp; player.hunger = 100; player.thirst = 100;
player.atkCd = 0; player.atkAnim = 0; player.hurtCd = 0; player.actCd = 0;
player.facing = 0; player.walk = 0;
inv.wood = 0; inv.stone = 0; inv.berry = 2;
trees = []; rocks = []; bushes = []; pools = []; fires = [];
monsters = []; parts = []; texts = []; target = null;
toastMsg = ''; toastTimer = 0;
stick.active = false; stick.id = null; stick.dx = 0; stick.dy = 0; stick.mag = 0;
for(const k in btnPress) delete btnPress[k];
const all = [];
for(let i=0;i<7;i++){
const p = findSpot(420, all);
const pool = { x:p.x, y:p.y, r: rand(72,110) };
pools.push(pool); all.push(pool);
}
for(let i=0;i<150;i++){
const p = findSpot(52, all);
const t = { x:p.x, y:p.y, r: rand(15,21), alive:true, respawn:0, seed: Math.random()*TAU };
trees.push(t); all.push(t);
}
for(let i=0;i<62;i++){
const p = findSpot(58, all);
const r = { x:p.x, y:p.y, r: rand(13,18), alive:true, respawn:0, seed: Math.random()*TAU };
rocks.push(r); all.push(r);
}
for(let i=0;i<55;i++){
const p = findSpot(52, all);
const b = { x:p.x, y:p.y, r: rand(14,18), berries: 3, maxBerries: 3, rt: rand(6,14), seed: Math.random()*TAU };
bushes.push(b); all.push(b);
}
makeGround();
}
/* ================= 键盘 ================= */
const keys = {};
window.addEventListener('keydown', e => {
const k = e.key.toLowerCase();
if([' ','arrowup','arrowdown','arrowleft','arrowright'].includes(k)) e.preventDefault();
if(state.over){ if(k === 'r') reset(); return; }
if(e.repeat) return;
keys[k] = true;
if(k === ' ') doAttack();
if(k === 'q') buildFire();
if(k === 'f' || k === '2') eatBerry();
});
window.addEventListener('keyup', e => { keys[e.key.toLowerCase()] = false; });
window.addEventListener('blur', () => { for(const k in keys) keys[k] = false; });
/* ================= 玩家行为 ================= */
function damagePlayer(amount, overTime){
if(!overTime){
if(player.hurtCd > 0) return;
player.hurtCd = 0.35;
state.shake = Math.min(10, state.shake + 4);
}
player.hp -= amount;
if(player.hp <= 0){ player.hp = 0; state.over = true; }
}
function eatBerry(){
if(inv.berry <= 0){ toast('没有浆果了'); return; }
if(player.hunger > 96){ toast('还不饿'); return; }
inv.berry--;
player.hunger = Math.min(100, player.hunger + 25);
toast('吃下浆果 +25 饱食');
spawnParticles(player.x, player.y, '#c8384a', 8);
}
function buildFire(){
if(inv.wood < 5 || inv.stone < 3){ toast('需要 5 木头 + 3 石头'); return; }
inv.wood -= 5; inv.stone -= 3;
const bx = clamp(player.x + Math.cos(player.facing)*44, 40, WORLD.w-40);
const by = clamp(player.y + Math.sin(player.facing)*44, 40, WORLD.h-40);
fires.push({ x:bx, y:by, light: 205, life: 46, maxLife: 46, flick: Math.random()*TAU });
toast('篝火点燃了');
spawnParticles(bx, by, '#ff9b3d', 16);
}
function doAttack(){
if(player.atkCd > 0) return;
player.atkCd = 0.5;
player.atkAnim = 0.2;
let hit = false;
for(const m of monsters){
const d = dist(player.x, player.y, m.x, m.y);
if(d > 80) continue;
const a = Math.atan2(m.y - player.y, m.x - player.x);
if(Math.abs(angDiff(a, player.facing)) < 1.15){
m.hp -= 30;
hit = true;
spawnParticles(m.x, m.y, '#c77ce0', 8);
m.x += Math.cos(a)*20;
m.y += Math.sin(a)*20;
}
}
if(hit) state.shake = Math.min(7, state.shake + 2.5);
}
function angDiff(a,b){
let d = (a - b) % TAU;
if(d > Math.PI) d -= TAU;
if(d < -Math.PI) d += TAU;
return d;
}
function spawnParticles(x, y, color, n){
for(let i=0;i<n;i++){
const a = Math.random()*TAU, s = rand(30,150);
parts.push({
x, y, vx: Math.cos(a)*s, vy: Math.sin(a)*s - 40,
life: rand(0.35,0.75), max: 0.75, color, r: rand(1.8,3.6)
});
}
}
function floatText(x, y, txt, color){
texts.push({ x, y, txt, color, life: 1 });
}
/* ================= 交互 ================= */
function findTarget(){
let best = null, bestD = Infinity;
const reach = 48;
for(const t of trees){
if(!t.alive) continue;
const d = dist(player.x, player.y, t.x, t.y);
if(d < t.r + reach && d < bestD){ bestD = d; best = { kind:'tree', obj:t, label:'砍树', color:'#9ed36a' }; }
}
for(const r of rocks){
if(!r.alive) continue;
const d = dist(player.x, player.y, r.x, r.y);
if(d < r.r + reach && d < bestD){ bestD = d; best = { kind:'rock', obj:r, label:'采石', color:'#c9c9d2' }; }
}
for(const b of bushes){
const d = dist(player.x, player.y, b.x, b.y);
if(d < b.r + reach && d < bestD){
bestD = d;
best = b.berries > 0
? { kind:'bush', obj:b, label:'摘浆果', color:'#ff8f9c' }
: { kind:'bush', obj:b, label:'浆果未熟', color:'#8a8a8a' };
}
}
for(const p of pools){
const d = dist(player.x, player.y, p.x, p.y);
if(d < p.r + 26 && d < bestD){ bestD = d; best = { kind:'pool', obj:p, label:'喝水', color:'#7ec8f0' }; }
}
return best;
}
function doInteract(tg){
const o = tg.obj;
if(tg.kind === 'tree'){
o.alive = false; o.respawn = 42;
const n = randInt(2,3);
inv.wood += n;
toast(`+${n} 木头`);
floatText(o.x, o.y - 20, `+${n} 木`, '#e0b93a');
spawnParticles(o.x, o.y - 10, '#8a6b3f', 12);
}
else if(tg.kind === 'rock'){
o.alive = false; o.respawn = 55;
const n = randInt(1,3);
inv.stone += n;
toast(`+${n} 石头`);
floatText(o.x, o.y - 20, `+${n} 石`, '#c4c4cf');
spawnParticles(o.x, o.y, '#8f8f99', 12);
}
else if(tg.kind === 'bush'){
if(o.berries <= 0){ toast('浆果还没长好'); return; }
o.berries--;
o.rt = 12;
inv.berry++;
toast('+1 浆果');
floatText(o.x, o.y - 20, '+1 果', '#ff6b7d');
spawnParticles(o.x, o.y, '#c8384a', 7);
}
else if(tg.kind === 'pool'){
if(player.thirst > 96){ toast('还不渴'); return; }
player.thirst = Math.min(100, player.thirst + 35);
toast('喝饱了水 +35');
floatText(player.x, player.y - 26, '+35 水', '#7ec8f0');
spawnParticles(o.x, o.y, '#4a9fd4', 6);
}
}
/* ================= 昼夜 ================= */
function darkness(){
const t = (state.time % state.dayLen) / state.dayLen;
if(t < state.nightAt) return 0;
const nt = (t - state.nightAt) / (1 - state.nightAt);
let d = 1;
if(nt < 0.12) d = nt / 0.12;
else if(nt > 0.88) d = (1 - nt) / 0.12;
return clamp(d, 0, 1) * 0.85;
}
const isNight = () => darkness() > 0.3;
function timeToNight(){
const t = (state.time % state.dayLen) / state.dayLen;
if(t < state.nightAt) return (state.nightAt - t) * state.dayLen;
return (1 - t) * state.dayLen;
}
/* ================= 怪物 ================= */
function spawnMonster(){
const ang = Math.random() * TAU;
const d = rand(520, 700);
const x = clamp(player.x + Math.cos(ang)*d, 60, WORLD.w-60);
const y = clamp(player.y + Math.sin(ang)*d, 60, WORLD.h-60);
const hp = 30 + state.day * 8;
monsters.push({
x, y, r: 16,
hp, maxHp: hp,
speed: 74 + state.day * 7,
dmg: 8 + state.day * 1.5,
atkCd: 0,
wobble: Math.random() * TAU,
});
}
/* ================= 更新 ================= */
function update(dt){
state.time += dt;
const nd = Math.floor(state.time / state.dayLen) + 1;
if(nd !== state.day){
state.day = nd;
toast(`第 ${state.day} 天`);
}
// 移动输入:摇杆优先
let moveX = 0, moveY = 0, mag = 1;
if(keys['w'] || keys['arrowup']) moveY--;
if(keys['s'] || keys['arrowdown']) moveY++;
if(keys['a'] || keys['arrowleft']) moveX--;
if(keys['d'] || keys['arrowright']) moveX++;
if(moveX || moveY){
const l = Math.hypot(moveX, moveY);
moveX /= l; moveY /= l;
}
if(stick.active && stick.mag > 0.02){
const l = Math.hypot(stick.dx, stick.dy) || 1;
moveX = stick.dx / l;
moveY = stick.dy / l;
mag = clamp(stick.mag, 0, 1);
}
if(moveX || moveY){
player.x += moveX * player.speed * mag * dt;
player.y += moveY * player.speed * mag * dt;
player.facing = Math.atan2(moveY, moveX);
player.walk += dt * 11;
} else {
player.walk *= 0.88;
}
player.x = clamp(player.x, 28, WORLD.w - 28);
player.y = clamp(player.y, 28, WORLD.h - 28);
// 需求
player.hunger = Math.max(0, player.hunger - 0.9 * dt);
player.thirst = Math.max(0, player.thirst - 1.2 * dt);
if(player.hunger <= 0) damagePlayer(2.0 * dt, true);
if(player.thirst <= 0) damagePlayer(2.5 * dt, true);
// 冷却
player.atkCd = Math.max(0, player.atkCd - dt);
player.atkAnim = Math.max(0, player.atkAnim - dt);
player.hurtCd = Math.max(0, player.hurtCd - dt);
player.actCd = Math.max(0, player.actCd - dt);
state.shake *= 0.86;
if(state.shake < 0.2) state.shake = 0;
if(toastTimer > 0) toastTimer -= dt;
// 按钮高亮衰减
for(const k in btnPress){
btnPress[k] -= dt;
if(btnPress[k] <= 0) delete btnPress[k];
}
// 交互
target = findTarget();
if(keys['e'] && player.actCd <= 0 && target){
player.actCd = 0.3;
doInteract(target);
}
// 资源重生
for(const t of trees){
if(!t.alive){ t.respawn -= dt; if(t.respawn <= 0) t.alive = true; }
}
for(const r of rocks){
if(!r.alive){ r.respawn -= dt; if(r.respawn <= 0) r.alive = true; }
}
for(const b of bushes){
if(b.berries < b.maxBerries){
b.rt -= dt;
if(b.rt <= 0){ b.berries++; b.rt = 12; }
}
}
// 篝火
for(let i=fires.length-1;i>=0;i--){
const f = fires[i];
f.life -= dt;
f.flick += dt * 9;
if(f.life <= 0) fires.splice(i,1);
}
// 怪物生成
if(isNight()){
state.spawnTimer -= dt;
if(state.spawnTimer <= 0){
state.spawnTimer = Math.max(1.0, 3.4 - state.day * 0.3);
const cap = 4 + state.day * 2;
if(monsters.length < cap) spawnMonster();
}
} else {
state.spawnTimer = Math.max(state.spawnTimer, 1.2);
}
// 怪物更新
for(let i=monsters.length-1;i>=0;i--){
const m = monsters[i];
if(m.hp <= 0){
monsters.splice(i,1);
spawnParticles(m.x, m.y, '#7a3b8f', 16);
continue;
}
let repelled = false;
for(const f of fires){
const d = dist(m.x, m.y, f.x, f.y);
if(d < f.light){
m.hp -= 24 * dt;
const a = Math.atan2(m.y - f.y, m.x - f.x);
m.x += Math.cos(a) * 135 * dt;
m.y += Math.sin(a) * 135 * dt;
repelled = true;
}
}
if(!repelled){
const a = Math.atan2(player.y - m.y, player.x - m.x);
m.x += Math.cos(a) * m.speed * dt;
m.y += Math.sin(a) * m.speed * dt;
m.wobble += dt * 6;
}
m.atkCd -= dt;
if(m.atkCd <= 0 && dist(m.x, m.y, player.x, player.y) < m.r + player.r + 4){
m.atkCd = 0.9;
damagePlayer(m.dmg, false);
spawnParticles(player.x, player.y, '#e04a4a', 7);
}
}
// 粒子
for(let i=parts.length-1;i>=0;i--){
const p = parts[i];
p.life -= dt;
if(p.life <= 0){ parts.splice(i,1); continue; }
p.x += p.vx * dt;
p.y += p.vy * dt;
p.vy += 150 * dt;
p.vx *= 0.96;
}
// 浮动文字
for(let i=texts.length-1;i>=0;i--){
const t = texts[i];
t.life -= dt * 1.3;
t.y -= 34 * dt;
if(t.life <= 0) texts.splice(i,1);
}
}
/* ================= 布局 ================= */
function stickCenter(){
const m = Math.min(W, H);
const pad = Math.max(66, Math.min(104, m * 0.20));
return { x: pad + 8, y: H - pad - 8, R: Math.max(44, Math.min(60, m * 0.115)) };
}
function buttonsLayout(){
const m = Math.min(W, H);
const big = Math.max(38, Math.min(52, m * 0.13));
const mid = big * 0.72;
const sml = big * 0.62;
const ax = W - big - 18, ay = H - big - 20;
return [
{ id:'attack', x: ax, y: ay, r: big, label:'攻', color:'#e04a4a', sub:'攻击' },
{ id:'interact', x: ax - big - mid - 2, y: ay - mid*0.42, r: mid, label:'采', color:'#42a05e', sub:'采集' },
{ id:'eat', x: ax - mid*0.25, y: ay - big - sml - 2, r: sml, label:'食', color:'#d4557f', sub:'进食' },
{ id:'fire', x: ax - big - sml*0.7, y: ay - sml*0.55, r: sml, label:'火', color:'#e08a2e', sub:'篝火' },
];
}
function touchUI(){ return isTouch; }
/* ================= 触摸事件 ================= */
function toLocal(t){
const r = cvs.getBoundingClientRect();
return { x: t.clientX - r.left, y: t.clientY - r.top };
}
function handleBtn(id){
btnPress[id] = 0.18;
if(state.over){ if(id === 'attack') reset(); return; }
if(id === 'attack') doAttack();
else if(id === 'interact'){
if(player.actCd > 0) return;
if(!target){ toast('附近没有可交互目标'); return; }
player.actCd = 0.3;
doInteract(target);
}
else if(id === 'fire') buildFire();
else if(id === 'eat') eatBerry();
}
cvs.addEventListener('touchstart', e => {
e.preventDefault();
isTouch = true;
for(const t of e.changedTouches){
const p = toLocal(t);
// 死亡界面:点击重开
if(state.over){
reset();
return;
}
// 1) 按钮
let hitBtn = null;
for(const b of buttonsLayout()){
if(dist(p.x, p.y, b.x, b.y) < b.r + 10){ hitBtn = b; break; }
}
if(hitBtn){ handleBtn(hitBtn.id); continue; }
// 2) 摇杆
if(stick.id === null){
const sc = stickCenter();
if(dist(p.x, p.y, sc.x, sc.y) < sc.R * 1.75){
stick.active = true;
stick.id = t.identifier;
stick.cx = sc.x; stick.cy = sc.y; stick.R = sc.R;
stick.dx = 0; stick.dy = 0; stick.mag = 0;
}
}
}
}, { passive:false });
cvs.addEventListener('touchmove', e => {
e.preventDefault();
for(const t of e.changedTouches){
if(t.identifier !== stick.id) continue;
const p = toLocal(t);
let dx = p.x - stick.cx, dy = p.y - stick.cy;
const len = Math.hypot(dx, dy);
if(len > stick.R){
dx = dx / len * stick.R;
dy = dy / len * stick.R;
}
stick.dx = dx; stick.dy = dy;
stick.mag = clamp(Math.hypot(dx, dy) / stick.R, 0, 1);
}
}, { passive:false });
function endTouch(e){
e.preventDefault();
for(const t of e.changedTouches){
if(t.identifier === stick.id){
stick.active = false;
stick.id = null;
stick.dx = 0; stick.dy = 0; stick.mag = 0;
}
}
}
cvs.addEventListener('touchend', endTouch, { passive:false });
cvs.addEventListener('touchcancel', endTouch, { passive:false });
/* 桌面鼠标也能玩(拖拽移动 + 点击按钮) */
let mouseDown = false;
cvs.addEventListener('mousedown', e => {
const p = { x: e.clientX, y: e.clientY };
if(state.over){ reset(); return; }
for(const b of buttonsLayout()){
if(dist(p.x, p.y, b.x, b.y) < b.r + 10){ handleBtn(b.id); return; }
}
const sc = stickCenter();
if(dist(p.x, p.y, sc.x, sc.y) < sc.R * 1.75){
mouseDown = true;
stick.active = true;
stick.cx = sc.x; stick.cy = sc.y; stick.R = sc.R;
}
});
window.addEventListener('mousemove', e => {
if(!mouseDown) return;
let dx = e.clientX - stick.cx, dy = e.clientY - stick.cy;
const len = Math.hypot(dx, dy);
if(len > stick.R){ dx = dx/len*stick.R; dy = dy/len*stick.R; }
stick.dx = dx; stick.dy = dy;
stick.mag = clamp(Math.hypot(dx,dy)/stick.R, 0, 1);
});
window.addEventListener('mouseup', () => {
mouseDown = false;
stick.active = false; stick.dx = 0; stick.dy = 0; stick.mag = 0;
});
/* ================= 渲染辅助 ================= */
let camX = 0, camY = 0, shX = 0, shY = 0;
function inView(x, y, pad){
pad = pad || 90;
return x > camX - pad && x < camX + W + pad && y > camY - pad && y < camY + H + pad;
}
function drawLight(c, x, y, r, strength){
const g = c.createRadialGradient(x, y, 0, x, y, r);
g.addColorStop(0, `rgba(0,0,0,${strength})`);
g.addColorStop(0.55, `rgba(0,0,0,${strength*0.62})`);
g.addColorStop(1, 'rgba(0,0,0,0)');
c.fillStyle = g;
c.beginPath();
c.arc(x, y, r, 0, TAU);
c.fill();
}
function roundRect(c, x, y, w, h, r){
if(w <= 0 || h <= 0) return;
r = Math.min(r, w/2, h/2);
c.beginPath();
c.moveTo(x+r, y);
c.arcTo(x+w, y, x+w, y+h, r);
c.arcTo(x+w, y+h, x, y+h, r);
c.arcTo(x, y+h, x, y, r);
c.arcTo(x, y, x+w, y, r);
c.closePath();
}
/* ================= 渲染 ================= */
function render(){
ctx.setTransform(1,0,0,1,0,0);
ctx.clearRect(0,0,W,H);
camX = clamp(player.x - W/2, 0, Math.max(0, WORLD.w - W));
camY = clamp(player.y - H/2, 0, Math.max(0, WORLD.h - H));
shX = state.shake > 0 ? rand(-state.shake, state.shake) : 0;
shY = state.shake > 0 ? rand(-state.shake, state.shake) : 0;
ctx.save();
ctx.translate(-camX + shX, -camY + shY);
/* 地面 */
if(groundPattern){
ctx.fillStyle = groundPattern;
ctx.fillRect(camX - 20, camY - 20, W + 40, H + 40);
} else {
ctx.fillStyle = '#38472e';
ctx.fillRect(camX - 20, camY - 20, W + 40, H + 40);
}
/* 水池 */
for(const p of pools){
if(!inView(p.x, p.y, p.r + 60)) continue;
ctx.save();
ctx.beginPath();
ctx.ellipse(p.x, p.y, p.r, p.r * 0.72, 0, 0, TAU);
ctx.fillStyle = '#2a4a63';
ctx.fill();
ctx.strokeStyle = '#3d6585';
ctx.lineWidth = 4;
ctx.stroke();
ctx.clip();
const tt = state.time * 0.8;
for(let i=0;i<4;i++){
const ry = p.y - p.r*0.5 + i * p.r * 0.32 + Math.sin(tt + i) * 3;
ctx.strokeStyle = 'rgba(120,190,235,0.22)';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(p.x - p.r, ry);
ctx.quadraticCurveTo(p.x, ry + 6, p.x + p.r, ry);
ctx.stroke();
}
ctx.restore();
}
/* 篝火地面阴影 */
for(const f of fires){
if(!inView(f.x, f.y, f.light)) continue;
ctx.fillStyle = 'rgba(0,0,0,0.22)';
ctx.beginPath();
ctx.ellipse(f.x, f.y + 6, 30, 13, 0, 0, TAU);
ctx.fill();
}
/* 石头 */
for(const r of rocks){
if(!r.alive || !inView(r.x, r.y)) continue;
ctx.fillStyle = 'rgba(0,0,0,0.22)';
ctx.beginPath();
ctx.ellipse(r.x + 4, r.y + r.r*0.55, r.r*0.95, r.r*0.45, 0, 0, TAU);
ctx.fill();
ctx.fillStyle = '#6e6e78';
ctx.beginPath();
ctx.moveTo(r.x - r.r, r.y + r.r*0.5);
ctx.lineTo(r.x - r.r*0.75, r.y - r.r*0.6);
ctx.lineTo(r.x - r.r*0.05, r.y - r.r);
ctx.lineTo(r.x + r.r*0.8, r.y - r.r*0.35);
ctx.lineTo(r.x + r.r, r.y + r.r*0.55);
ctx.closePath();
ctx.fill();
ctx.fillStyle = '#8b8b96';
ctx.beginPath();
ctx.moveTo(r.x - r.r*0.55, r.y - r.r*0.45);
ctx.lineTo(r.x - r.r*0.05, r.y - r.r*0.9);
ctx.lineTo(r.x + r.r*0.35, r.y - r.r*0.3);
ctx.closePath();
ctx.fill();
}
/* 灌木 */
for(const b of bushes){
if(!inView(b.x, b.y)) continue;
ctx.fillStyle = 'rgba(0,0,0,0.2)';
ctx.beginPath();
ctx.ellipse(b.x + 3, b.y + b.r*0.7, b.r*0.9, b.r*0.4, 0, 0, TAU);
ctx.fill();
ctx.fillStyle = '#2c5a34';
ctx.beginPath();
ctx.arc(b.x, b.y, b.r, 0, TAU);
ctx.fill();
ctx.fillStyle = '#3a7042';
ctx.beginPath();
ctx.arc(b.x - b.r*0.3, b.y - b.r*0.3, b.r*0.62, 0, TAU);
ctx.fill();
for(let i=0;i<b.berries;i++){
const a = (i / b.maxBerries) * TAU + b.seed;
const bx = b.x + Math.cos(a) * b.r * 0.55;
const by = b.y + Math.sin(a) * b.r * 0.55;
ctx.fillStyle = '#d43a4e';
ctx.beginPath();
ctx.arc(bx, by, 3.4, 0, TAU);
ctx.fill();
}
}
/* 树 */
for(const t of trees){
if(!t.alive || !inView(t.x, t.y, t.r + 40)) continue;
ctx.fillStyle = 'rgba(0,0,0,0.24)';
ctx.beginPath();
ctx.ellipse(t.x + 8, t.y + t.r*0.85, t.r*1.0, t.r*0.42, 0, 0, TAU);
ctx.fill();
ctx.fillStyle = '#5a4028';
ctx.fillRect(t.x - 4, t.y - 6, 8, t.r * 1.0);
const sway = Math.sin(state.time * 1.1 + t.seed) * 1.6;
ctx.fillStyle = '#2b6236';
ctx.beginPath();
ctx.arc(t.x + sway, t.y - 8, t.r, 0, TAU);
ctx.fill();
ctx.fillStyle = '#3d8a4a';
ctx.beginPath();
ctx.arc(t.x - t.r*0.3 + sway, t.y - t.r*0.75, t.r*0.7, 0, TAU);
ctx.fill();
ctx.fillStyle = '#4fa25c';
ctx.beginPath();
ctx.arc(t.x + t.r*0.35 + sway, t.y - t.r*0.55, t.r*0.5, 0, TAU);
ctx.fill();
}
/* 篝火 */
for(const f of fires){
if(!inView(f.x, f.y, 120)) continue;
const fl = 1 + Math.sin(f.flick) * 0.22;
const fl2 = 1 + Math.sin(f.flick * 1.7 + 1) * 0.3;
ctx.fillStyle = '#4e4e57';
for(let i=0;i<8;i++){
const a = (i/8) * TAU;
ctx.beginPath();
ctx.arc(f.x + Math.cos(a)*23, f.y + Math.sin(a)*23, 6, 0, TAU);
ctx.fill();
}
ctx.fillStyle = '#6b4a2a';
ctx.fillRect(f.x - 12, f.y - 3, 24, 5);
ctx.fillRect(f.x - 3, f.y - 12, 5, 24);
ctx.fillStyle = '#ff7a1f';
ctx.beginPath();
ctx.ellipse(f.x, f.y - 8, 11*fl, 20*fl, 0, 0, TAU);
ctx.fill();
ctx.fillStyle = '#ffb545';
ctx.beginPath();
ctx.ellipse(f.x, f.y - 6, 7*fl2, 14*fl2, 0, 0, TAU);
ctx.fill();
ctx.fillStyle = '#fff0a8';
ctx.beginPath();
ctx.ellipse(f.x, f.y - 4, 3.5*fl, 7*fl, 0, 0, TAU);
ctx.fill();
}
/* 怪物 */
for(const m of monsters){
if(!inView(m.x, m.y)) continue;
const wob = Math.sin(m.wobble) * 2;
ctx.fillStyle = 'rgba(0,0,0,0.3)';
ctx.beginPath();
ctx.ellipse(m.x, m.y + 13, 15, 6, 0, 0, TAU);
ctx.fill();
ctx.fillStyle = '#54246b';
ctx.beginPath();
ctx.arc(m.x, m.y + wob, m.r, 0, TAU);
ctx.fill();
ctx.strokeStyle = '#8b4aa3';
ctx.lineWidth = 2;
ctx.stroke();
ctx.fillStyle = '#ff4d4d';
ctx.beginPath(); ctx.arc(m.x - 5.5, m.y + wob - 3, 3.2, 0, TAU); ctx.fill();
ctx.beginPath(); ctx.arc(m.x + 5.5, m.y + wob - 3, 3.2, 0, TAU); ctx.fill();
if(m.hp < m.maxHp){
const bw = 38;
ctx.fillStyle = 'rgba(0,0,0,0.55)';
roundRect(ctx, m.x - bw/2, m.y - 32, bw, 5, 2.5); ctx.fill();
ctx.fillStyle = '#e04040';
roundRect(ctx, m.x - bw/2, m.y - 32, bw * (m.hp/m.maxHp), 5, 2.5); ctx.fill();
}
}
/* 玩家 */
ctx.fillStyle = 'rgba(0,0,0,0.28)';
ctx.beginPath();
ctx.ellipse(player.x, player.y + 11, 14, 6, 0, 0, TAU);
ctx.fill();
const bob = Math.sin(player.walk) * 2;
ctx.save();
ctx.translate(player.x, player.y + bob);
ctx.fillStyle = '#3f86d0';
ctx.beginPath();
ctx.arc(0, 0, player.r, 0, TAU);
ctx.fill();
ctx.strokeStyle = '#255a8f';
ctx.lineWidth = 3;
ctx.stroke();
ctx.rotate(player.facing);
ctx.fillStyle = '#ffd166';
ctx.beginPath();
ctx.arc(8, 0, 5.2, 0, TAU);
ctx.fill();
ctx.restore();
if(player.atkAnim > 0){
const p = player.atkAnim / 0.2;
ctx.save();
ctx.translate(player.x, player.y);
ctx.rotate(player.facing);
ctx.strokeStyle = `rgba(255,255,255,${p * 0.85})`;
ctx.lineWidth = 7;
ctx.lineCap = 'round';
ctx.beginPath();
ctx.arc(0, 0, 46, -1.0, 1.0);
ctx.stroke();
ctx.restore();
}
/* 粒子 */
for(const p of parts){
ctx.globalAlpha = clamp(p.life / p.max, 0, 1);
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(p.x, p.y, p.r, 0, TAU);
ctx.fill();
}
ctx.globalAlpha = 1;
/* 浮动文字 */
ctx.textAlign = 'center';
ctx.font = `bold 15px ${FONT}`;
for(const t of texts){
ctx.globalAlpha = clamp(t.life, 0, 1);
ctx.fillStyle = 'rgba(0,0,0,0.6)';
ctx.fillText(t.txt, t.x + 1, t.y + 1);
ctx.fillStyle = t.color;
ctx.fillText(t.txt, t.x, t.y);
}
ctx.globalAlpha = 1;
ctx.restore();
/* 夜色 */
const dark = darkness();
if(dark > 0.01){
lctx.setTransform(1,0,0,1,0,0);
lctx.clearRect(0,0,W,H);
lctx.fillStyle = `rgba(9,11,30,${dark})`;
lctx.fillRect(0,0,W,H);
lctx.globalCompositeOperation = 'destination-out';
drawLight(lctx, player.x - camX + shX, player.y - camY + shY, 165, 0.6);
for(const f of fires){
const fx = f.x - camX + shX, fy = f.y - camY + shY;
if(fx < -340 || fx > W + 340 || fy < -340 || fy > H + 340) continue;
const flick = 0.93 + Math.sin(f.flick * 3) * 0.07;
drawLight(lctx, fx, fy, f.light * flick, 1);
}
lctx.globalCompositeOperation = 'source-over';
ctx.drawImage(lightCvs, 0, 0);
ctx.globalCompositeOperation = 'lighter';
for(const f of fires){
const fx = f.x - camX + shX, fy = f.y - camY + shY;
if(fx < -340 || fx > W + 340 || fy < -340 || fy > H + 340) continue;
const R = f.light * 1.15;
const g = ctx.createRadialGradient(fx, fy, 0, fx, fy, R);
g.addColorStop(0, 'rgba(255,145,45,0.20)');
g.addColorStop(0.55, 'rgba(255,110,20,0.07)');
g.addColorStop(1, 'rgba(255,100,0,0)');
ctx.fillStyle = g;
ctx.fillRect(fx - R, fy - R, R*2, R*2);
}
ctx.globalCompositeOperation = 'source-over';
}
drawHUD();
if(touchUI() && !state.over) drawControls();
if(state.over) drawGameOver();
}
/* ================= HUD ================= */
function drawBar(x, y, w, h, ratio, color, label, fs){
ratio = clamp(ratio, 0, 1);
ctx.fillStyle = 'rgba(0,0,0,0.5)';
roundRect(ctx, x - 2, y - 2, w + 4, h + 4, h/2 + 2); ctx.fill();
ctx.fillStyle = 'rgba(255,255,255,0.10)';
roundRect(ctx, x, y, w, h, h/2); ctx.fill();
if(ratio > 0){
ctx.fillStyle = color;
roundRect(ctx, x, y, w * ratio, h, h/2); ctx.fill();
}
ctx.fillStyle = 'rgba(255,255,255,0.94)';
ctx.font = `bold ${fs}px ${FONT}`;
ctx.textAlign = 'left';
ctx.fillText(label, x + 8, y + h - (h - fs)/2 - 1.5);
ctx.fillStyle = 'rgba(255,255,255,0.7)';
ctx.textAlign = 'right';
ctx.fillText(Math.round(ratio * 100) + '%', x + w - 8, y + h - (h - fs)/2 - 1.5);
}
function drawHUD(){
const touch = touchUI();
// 状态条
const barW = touch ? Math.min(178, W * 0.46) : 220;
const barH = touch ? 15 : 19;
const bx = touch ? 12 : 20, by = touch ? 14 : 22;
const gapY = barH + 4;
const fs = touch ? 10 : 12;
drawBar(bx, by, barW, barH, player.hp / player.maxHp, '#e04747', '生命', fs);
drawBar(bx, by + gapY, barW, barH, player.hunger / 100, '#e8a33d', '饱食', fs);
drawBar(bx, by + gapY*2, barW, barH, player.thirst / 100, '#3da9e8', '水分', fs);
// 资源
const items = [
{ k:'木', v: inv.wood, c:'#e0b93a' },
{ k:'石', v: inv.stone, c:'#c4c4cf' },
{ k:'果', v: inv.berry, c:'#ff6b7d' },
];
const boxW = touch ? 56 : 84;
const boxH = touch ? 34 : 42;
const gap = touch ? 7 : 10;
const totalW = items.length * boxW + (items.length - 1) * gap;
let rbx = touch ? bx : W/2 - totalW/2;
const rby = touch ? by + gapY*3 - 2 : H - 66;
ctx.textAlign = 'center';
for(const it of items){
ctx.fillStyle = 'rgba(0,0,0,0.45)';
roundRect(ctx, rbx, rby, boxW, boxH, 9); ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,0.12)';
ctx.lineWidth = 1;
roundRect(ctx, rbx + 0.5, rby + 0.5, boxW - 1, boxH - 1, 9); ctx.stroke();
ctx.fillStyle = it.c;
ctx.font = `bold ${touch ? 16 : 20}px ${FONT}`;
ctx.fillText(String(it.v), rbx + boxW/2, rby + (touch ? 18 : 22));
ctx.fillStyle = 'rgba(255,255,255,0.55)';
ctx.font = `${touch ? 9 : 11}px ${FONT}`;
ctx.fillText(it.k, rbx + boxW/2, rby + (touch ? 30 : 36));
rbx += boxW + gap;
}
// 天数与时间
ctx.textAlign = 'right';
const dayFont = touch ? 20 : 26;
ctx.font = `bold ${dayFont}px ${FONT}`;
ctx.fillStyle = 'rgba(0,0,0,0.55)';
ctx.fillText(`第 ${state.day} 天`, W - (touch ? 11 : 19), touch ? 34 : 45);
ctx.fillStyle = '#f0e6cf';
ctx.fillText(`第 ${state.day} 天`, W - (touch ? 12 : 20), touch ? 33 : 44);
const night = isNight();
ctx.font = `bold ${touch ? 11 : 15}px ${FONT}`;
ctx.fillStyle = night ? '#8fa7ff' : '#ffd479';
const txt = night
? `夜晚 · ${Math.ceil(timeToNight())}s 后黎明`
: `白天 · ${Math.ceil(timeToNight())}s 后入夜`;
ctx.fillText(txt, W - 12, touch ? 50 : 70);
const pbW = touch ? 96 : 150, pbX = W - 12 - pbW, pbY = touch ? 57 : 80;
ctx.fillStyle = 'rgba(0,0,0,0.45)';
roundRect(ctx, pbX, pbY, pbW, 4, 2); ctx.fill();
const prog = (state.time % state.dayLen) / state.dayLen;
ctx.fillStyle = night ? '#5568b8' : '#e2b23c';
roundRect(ctx, pbX, pbY, pbW * prog, 4, 2); ctx.fill();
// 交互提示
if(target && !state.over){
ctx.textAlign = 'center';
ctx.font = `bold ${touch ? 14 : 17}px ${FONT}`;
const msg = `[E] ${target.label}`;
const tw = ctx.measureText(msg).width;
const py = touch ? H * 0.30 : H - 128;
ctx.fillStyle = 'rgba(0,0,0,0.6)';
roundRect(ctx, W/2 - tw/2 - 14, py, tw + 28, 30, 15); ctx.fill();
ctx.fillStyle = target.color;
ctx.fillText(msg, W/2, py + 21);
}
// 桌面提示
if(!touch){
ctx.textAlign = 'center';
ctx.font = `12px ${FONT}`;
const canBuild = inv.wood >= 5 && inv.stone >= 3;
ctx.fillStyle = canBuild ? 'rgba(255,210,120,0.85)' : 'rgba(255,255,255,0.3)';
ctx.fillText('[Q] 建造篝火 (5木 3石)', W/2, H - 84);
ctx.textAlign = 'left';
ctx.fillStyle = 'rgba(255,255,255,0.32)';
ctx.fillText('WASD 移动 · E 采集 · 空格 攻击 · Q 篝火 · F 吃浆果', 20, H - 22);
}
// Toast
if(toastTimer > 0){
ctx.textAlign = 'center';
ctx.font = `bold ${touch ? 17 : 20}px ${FONT}`;
ctx.globalAlpha = clamp(toastTimer / 0.5, 0, 1);
const tw = ctx.measureText(toastMsg).width;
const ty = touch ? H * 0.16 : H/2 - 178;
ctx.fillStyle = 'rgba(0,0,0,0.66)';
roundRect(ctx, W/2 - tw/2 - 16, ty, tw + 32, 38, 19); ctx.fill();
ctx.fillStyle = '#ffe6a8';
ctx.fillText(toastMsg, W/2, ty + 26);
ctx.globalAlpha = 1;
}
// 低血量红屏
if(player.hp < 35 && !state.over){
const a = (1 - player.hp/35) * 0.28 * (0.75 + Math.sin(state.time * 6) * 0.25);
const g = ctx.createRadialGradient(W/2, H/2, Math.min(W,H)*0.28, W/2, H/2, Math.max(W,H)*0.72);
g.addColorStop(0, 'rgba(180,0,0,0)');
g.addColorStop(1, `rgba(180,0,0,${a})`);
ctx.fillStyle = g;
ctx.fillRect(0, 0, W, H);
}
}
/* ================= 触摸控件绘制 ================= */
function drawControls(){
const sc = stickCenter();
// 摇杆底盘
ctx.beginPath();
ctx.arc(sc.x, sc.y, sc.R, 0, TAU);
ctx.fillStyle = 'rgba(0,0,0,0.20)';
ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,0.20)';
ctx.lineWidth = 2;
ctx.stroke();
// 摇杆手柄
const hx = sc.x + (stick.active ? stick.dx : 0);
const hy = sc.y + (stick.active ? stick.dy : 0);
const hr = sc.R * 0.44;
ctx.beginPath();
ctx.arc(hx, hy, hr, 0, TAU);
const ag = ctx.createRadialGradient(hx - hr*0.3, hy - hr*0.3, 0, hx, hy, hr);
ag.addColorStop(0, 'rgba(255,255,255,0.62)');
ag.addColorStop(1, 'rgba(255,255,255,0.20)');
ctx.fillStyle = ag;
ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,0.5)';
ctx.lineWidth = 2;
ctx.stroke();
// 动作按钮
for(const b of buttonsLayout()){
const pressed = btnPress[b.id] || 0;
const sc2 = 1 + (pressed > 0 ? 0.10 : 0);
const R = b.r * sc2;
const disabled =
(b.id === 'fire' && (inv.wood < 5 || inv.stone < 3)) ||
(b.id === 'eat' && inv.berry <= 0) ||
(b.id === 'attack' && player.atkCd > 0) ||
(b.id === 'interact' && !target);
ctx.save();
ctx.beginPath();
ctx.arc(b.x, b.y, R, 0, TAU);
ctx.fillStyle = disabled ? 'rgba(20,22,30,0.42)' : 'rgba(12,14,22,0.52)';
ctx.fill();
ctx.lineWidth = 3;
ctx.strokeStyle = disabled ? 'rgba(255,255,255,0.12)' : b.color;
ctx.stroke();
// 内发光
if(!disabled){
const g = ctx.createRadialGradient(b.x, b.y - R*0.35, 0, b.x, b.y, R);
g.addColorStop(0, hexA(b.color, 0.30));
g.addColorStop(1, hexA(b.color, 0));
ctx.fillStyle = g;
ctx.fill();
}
// 文字
ctx.textAlign = 'center';
ctx.font = `bold ${Math.round(R * 0.82)}px ${FONT}`;
ctx.fillStyle = disabled ? 'rgba(255,255,255,0.28)' : 'rgba(255,255,255,0.94)';
ctx.fillText(b.label, b.x, b.y + R * 0.30);
// 副标签
ctx.font = `${Math.round(R * 0.36)}px ${FONT}`;
ctx.fillStyle = disabled ? 'rgba(255,255,255,0.18)' : 'rgba(255,255,255,0.55)';
ctx.fillText(b.sub, b.x, b.y + R + 13);
ctx.restore();
}
// 建篝火资源提示
if(inv.wood < 5 || inv.stone < 3){
const fb = buttonsLayout().find(b => b.id === 'fire');
if(fb){
ctx.textAlign = 'center';
ctx.font = `bold 9px ${FONT}`;
ctx.fillStyle = 'rgba(255,255,255,0.38)';
ctx.fillText('5木3石', fb.x, fb.y + fb.r + 25);
}
}
}
function hexA(hex, a){
const n = parseInt(hex.slice(1), 16);
const r = (n >> 16) & 255, g = (n >> 8) & 255, b = n & 255;
return `rgba(${r},${g},${b},${a})`;
}
/* ================= 结算 ================= */
function drawGameOver(){
ctx.fillStyle = 'rgba(4,5,12,0.80)';
ctx.fillRect(0, 0, W, H);
ctx.textAlign = 'center';
const big = Math.min(46, W * 0.10);
ctx.fillStyle = '#e8e8e8';
ctx.font = `bold ${big}px ${FONT}`;
ctx.fillText('你没能撑过去', W/2, H/2 - 70);
ctx.fillStyle = '#e8c06a';
ctx.font = `bold ${Math.min(30, W*0.075)}px ${FONT}`;
ctx.fillText(`存活了 ${state.day} 天`, W/2, H/2 + 4);
ctx.fillStyle = 'rgba(255,255,255,0.5)';
ctx.font = `${Math.min(14, W*0.038)}px ${FONT}`;
ctx.fillText('饥饿、干渴与黑夜中的怪物,都是荒野的规则', W/2, H/2 + 50);
const blink = 0.55 + Math.sin(performance.now() / 320) * 0.45;
ctx.fillStyle = `rgba(255,220,140,${blink})`;
ctx.font = `bold ${Math.min(20, W*0.05)}px ${FONT}`;
ctx.fillText('点击屏幕重新开始', W/2, H/2 + 112);
}
/* ================= 主循环 ================= */
let last = performance.now();
function loop(now){
const dt = Math.min(0.05, (now - last) / 1000);
last = now;
if(!state.over) update(dt);
render();
requestAnimationFrame(loop);
}
reset();
requestAnimationFrame(loop);
})();
</script>
</body>
</html>Game Source: 荒野求生 · 手游版
Creator: HyperFalcon76
Libraries: none
Complexity: complex (1245 lines, 36.8 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-hyperfalcon76" to link back to the original. Then publish at arcadelab.ai/publish.