卡车追击小汽车游戏,卡车追击小汽车,公路狂飙大作战

这是一个经典的“卡车追击小汽车”游戏,你可以直接复制下面的代码,保存为一个 .html 文件(truck-chase.html),然后在浏览器中打开即可游玩。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">卡车追击小汽车</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
user-select: none;
}
body {
background: #1a2a32;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: 'Segoe UI', Roboto, system-ui, sans-serif;
}
.game-wrapper {
background: #2e4c3e;
padding: 20px 20px 25px;
border-radius: 40px;
box-shadow: 0 20px 30px rgba(0,0,0,0.6), inset 0 0 0 2px #5f7a6b;
}
canvas {
display: block;
width: 800px;
height: 500px;
border-radius: 24px;
background: #3e5f4b;
box-shadow: inset 0 0 30px #1e2f25, 0 10px 15px rgba(0,0,0,0.4);
cursor: none;
}
.status-bar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 15px 0;
color: #e9f2e6;
font-weight: 600;
text-shadow: 2px 2px 0 #1f2e24;
letter-spacing: 1px;
}
.restart-btn {
background: #f4c542;
border: none;
border-radius: 40px;
padding: 8px 24px;
font-weight: 700;
font-size: 1.1rem;
color: #1e2f25;
box-shadow: 0 5px 0 #9e7d2b, 0 8px 12px rgba(0,0,0,0.3);
cursor: pointer;
transition: all 0.08s ease;
letter-spacing: 0.5px;
}
.restart-btn:active {
transform: translateY(4px);
box-shadow: 0 1px 0 #9e7d2b, 0 5px 10px rgba(0,0,0,0.3);
}
.hint {
font-size: 0.9rem;
color: #b6d2b0;
text-shadow: 1px 1px 0 #1a2a1f;
}
</style>
</head>
<body>
<div class="game-wrapper">
<canvas id="gameCanvas" width="800" height="500"></canvas>
<div class="status-bar">
<span>⏱️ <span id="timerDisplay">0.0</span> s</span>
<span class="hint">← → 移动卡车 | 抓住小汽车!</span>
<button class="restart-btn" id="restartButton">🔄 重来</button>
</div>
</div>
<script>
(function() {
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const timerDisplay = document.getElementById('timerDisplay');
// 游戏尺寸
const CANVAS_W = 800;
const CANVAS_H = 500;
// ---------- 卡车 ----------
const truck = {
x: 400,
y: 380, // 卡车底盘Y坐标 (轮胎中心)
width: 70,
height: 40,
speed: 7,
color: '#d94f4f',
// 碰撞箱 (相对卡车中心)
collider: {
offsetX: 0, // 相对于中心
offsetY: -10,
width: 70,
height: 40
},
// 视觉细节
wheelRadius: 12
};
// ---------- 小汽车 (被追击) ----------
const car = {
x: 200,
y: 200,
width: 56,
height: 30,
vx: 2.8,
vy: 2.0,
color: '#4d8ec9',
// 碰撞箱 (实际比视觉稍小一点,但感觉公平)
collider: {
width: 56,
height: 30
}
};
// 游戏状态
let gameActive = true;
let startTime = performance.now();
let elapsedSeconds = 0;
let caught = false;
let catchTime = 0; // 抓住所花时间
// 键盘控制
const keys = {
ArrowLeft: false,
ArrowRight: false
};
// 移动端触摸支持 (简单按钮)
let leftPressed = false;
let rightPressed = false;
let touchTimer = null;
// ---------- 重置游戏 ----------
function resetGame() {
gameActive = true;
caught = false;
startTime = performance.now();
elapsedSeconds = 0;
timerDisplay.textContent = '0.0';
// 重置卡车位置
truck.x = 400;
// 重置小汽车位置 (上方随机)
car.x = 120 + Math.random() * (CANVAS_W - 240);
car.y = 80 + Math.random() * 120;
// 给随机速度方向,但保证不会太快跑出边界
const speedBase = 2.6;
const angle = Math.random() * Math.PI * 2;
car.vx = Math.cos(angle) * speedBase;
car.vy = Math.sin(angle) * speedBase * 0.9; // 垂直稍慢
// 保证没有零向量
if (Math.abs(car.vx) < 1.2) car.vx = car.vx > 0 ? 1.8 : -1.8;
if (Math.abs(car.vy) < 0.8) car.vy = car.vy > 0 ? 1.4 : -1.4;
// 重置键盘状态
keys.ArrowLeft = false;
keys.ArrowRight = false;
leftPressed = false;
rightPressed = false;
}
// ---------- 边界限制 & 物理更新 ----------
function updateCarPosition() {
// 更新位置
car.x += car.vx;
car.y += car.vy;
// 边界反弹 (保留小汽车完整显示)
const carHalfW = car.width / 2;
const carHalfH = car.height / 2;
// 左右边界
if (car.x - carHalfW < 0) {
car.x = carHalfW;
car.vx *= -1;
} else if (car.x + carHalfW > CANVAS_W) {
car.x = CANVAS_W - carHalfW;
car.vx *= -1;
}
// 上下边界 (顶部至少留一点,底部避免和卡车同一水平线太接近,但允许)
if (car.y - carHalfH < 0) {
car.y = carHalfH;
car.vy *= -1;
} else if (car.y + carHalfH > CANVAS_H - 40) { // 底部留出卡车区域,但不强制
car.y = CANVAS_H - carHalfH - 20;
car.vy *= -1;
}
// 防止速度归零
if (Math.abs(car.vx) < 0.8) car.vx = car.vx > 0 ? 1.2 : -1.2;
if (Math.abs(car.vy) < 0.6) car.vy = car.vy > 0 ? 1.0 : -1.0;
}
// ---------- 卡车控制 ----------
function updateTruckPosition() {
let move = 0;
if (keys.ArrowLeft || leftPressed) move -= 1;
if (keys.ArrowRight || rightPressed) move += 1;
truck.x += move * truck.speed;
// 限制卡车不超出左右边界 (考虑车身宽度)
const halfW = truck.width / 2;
if (truck.x - halfW < 0) truck.x = halfW;
if (truck.x + halfW > CANVAS_W) truck.x = CANVAS_W - halfW;
}
// ---------- 碰撞检测 (矩形碰撞) ----------
function checkCollision() {
// 卡车碰撞箱 (基于中心点)
const truckLeft = truck.x - truck.width / 2;
const truckRight = truck.x + truck.width / 2;
const truckTop = truck.y - truck.height / 2 - 8; // 车身向上偏移一点
const truckBottom = truck.y + truck.height / 2 + 5;
// 小汽车碰撞箱
const carLeft = car.x - car.width / 2;
const carRight = car.x + car.width / 2;
const carTop = car.y - car.height / 2;
const carBottom = car.y + car.height / 2;
return !(truckRight < carLeft || truckLeft > carRight ||
truckBottom < carTop || truckTop > carBottom);
}
// ---------- 绘制场景 ----------
function drawRoad() {
// 草地背景
ctx.fillStyle = '#3e5f4b';
ctx.fillRect(0, 0, CANVAS_W, CANVAS_H);
// 路面 (灰色带纹理)
ctx.fillStyle = '#4a4f55';
ctx.fillRect(0, 120, CANVAS_W, 260);
// 路面颗粒感
for (let i = 0; i < 300; i++) {
ctx.fillStyle = i % 3 === 0 ? '#5d636b' : '#3e4248';
ctx.beginPath();
ctx.arc(Math.random() * CANVAS_W, 130 + Math.random() * 240, 1 + Math.random() * 2, 0, Math.PI * 2);
ctx.fill();
}
// 车道分割线 (虚線)
ctx.strokeStyle = '#f0e68c';
ctx.lineWidth = 6;
ctx.setLineDash([30, 30]);
ctx.beginPath();
ctx.moveTo(0, 250);
ctx.lineTo(CANVAS_W, 250);
ctx.stroke();
ctx.setLineDash([]); // 重置
// 路肩
ctx.fillStyle = '#6b8c6b';
ctx.fillRect(0, 120, CANVAS_W, 10);
ctx.fillRect(0, 370, CANVAS_W, 10);
// 装饰小石子
for (let i = 0; i < 70; i++) {
ctx.fillStyle = '#a9b8a1';
ctx.beginPath();
ctx.arc(Math.random() * CANVAS_W, 130 + Math.random() * 230, 1 + Math.random() * 3, 0, Math.PI * 2);
ctx.fill();
}
}
// 绘制小汽车 (带一点动感)
function drawCar() {
const left = car.x - car.width / 2;
const top = car.y - car.height / 2;
// 车身阴影
ctx.shadowColor = '#1d2b1d';
ctx.shadowBlur = 12;
ctx.shadowOffsetY = 4;
// 车身
ctx.fillStyle = car.color;
ctx.beginPath();
ctx.roundRect(left, top, car.width, car.height, 12);
ctx.fill();
// 车窗
ctx.shadowBlur = 6;
ctx.fillStyle = '#2a486b';
ctx.beginPath();
ctx.roundRect(left + 8, top + 5, 16, 10, 6);
ctx.fill();
ctx.beginPath();
ctx.roundRect(left + 30, top + 5, 16, 10, 6);
ctx.fill();
// 车灯
ctx.fillStyle = '#ffd966';
ctx.beginPath();
ctx.arc(left + 5, top + car.height - 6, 5, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(left + car.width - 5, top + car.height - 6, 5, 0, Math.PI * 2);
ctx.fill();
// 轮子
ctx.shadowBlur = 8;
ctx.fillStyle = '#1f1f1f';
ctx.beginPath();
ctx.ellipse(left + 12, top + car.height, 10, 7, 0, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.ellipse(left + car.width - 12, top + car.height, 10, 7, 0, 0, Math.PI * 2);
ctx.fill();
// 重置阴影
ctx.shadowBlur = 0;
ctx.shadowOffsetY = 0;
}
// 绘制卡车
function drawTruck() {
const width = truck.width;
const height = truck.height;
const left = truck.x - width / 2;
const top = truck.y - height / 2 - 5; // 让卡车视觉上更靠近地面
// 阴影
ctx.shadowColor = '#1e2b1e';
ctx.shadowBlur = 16;
ctx.shadowOffsetY = 6;
// 卡车车厢 (后部)
ctx.fillStyle = '#b53b3b';
ctx.beginPath();
ctx.roundRect(left, top, width, height, 8);
ctx.fill();
// 卡车驾驶室 (前部,颜色稍亮)
ctx.fillStyle = '#e05a5a';
ctx.beginPath();
ctx.roundRect(left + 8, top - 8, 28, 16, 8);
ctx.fill();
// 车窗
ctx.fillStyle = '#2a3d4f';
ctx.beginPath();
ctx.roundRect(left + 12, top - 5, 14, 10, 4);
ctx.fill();
// 车灯
ctx.fillStyle = '#fceea7';
ctx.beginPath();
ctx.arc(left + 10, top + height - 7, 6, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(left + width - 10, top + height - 7, 6, 0, Math.PI * 2);
ctx.fill();
// 轮子
ctx.shadowBlur = 10;
ctx.fillStyle = '#1e1e1e';
// 左轮
ctx.beginPath();
ctx.ellipse(left + 14, top + height + 3, 12, 8, 0, 0, Math.PI * 2);
ctx.fill();
// 右轮
ctx.beginPath();
ctx.ellipse(left + width - 14, top + height + 3, 12, 8, 0, 0, Math.PI * 2);
ctx.fill();
// 轮毂装饰
ctx.fillStyle = '#aaa';
ctx.beginPath();
ctx.arc(left + 14, top + height + 3, 4, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(left + width - 14, top + height + 3, 4, 0, Math.PI * 2);
ctx.fill();
// 重置阴影
ctx.shadowBlur = 0;
ctx.shadowOffsetY = 0;
}
// 绘制游戏结束/胜利信息
function drawGameMessage() {
if (caught && gameActive === false) {
ctx.font = 'bold 42px "Segoe UI", Roboto, sans-serif';
ctx.fillStyle = '#f7e05e';
ctx.shadowColor = '#1e2b1e';
ctx.shadowBlur = 20;
ctx.shadowOffsetY = 6;
ctx.textAlign = 'center';
ctx.fillText('🎉 抓获!', CANVAS_W / 2, 150);
ctx.font = 'bold 28px "Segoe UI", Roboto, sans-serif';
ctx.fillStyle = '#ffffff';
ctx.fillText(`用时 ${catchTime.toFixed(1)} 秒`, CANVAS_W / 2, 210);
ctx.shadowBlur = 0;
ctx.shadowOffsetY = 0;
}
}
// 画一点瞄准/装饰
function drawExtras() {
// 在卡车附近画一点指示
if (gameActive && !caught) {
ctx.font = '18px "Segoe UI", sans-serif';
ctx.fillStyle = '#fff9d7';
ctx.shadowColor = '#2e4c3e';
ctx.shadowBlur = 8;
ctx.textAlign = 'center';
ctx.fillText('⚡ 追上它!', truck.x, truck.y - 55);
ctx.shadowBlur = 0;
}
}
// 主渲染循环
function draw() {
ctx.clearRect(0, 0, CANVAS_W, CANVAS_H);
drawRoad();
drawCar();
drawTruck();
drawExtras();
drawGameMessage();
}
// 更新逻辑
function update() {
if (!gameActive || caught) return;
// 更新卡车
updateTruckPosition();
// 更新小汽车位置 (游戏进行中才移动)
updateCarPosition();
// 碰撞检测
if (checkCollision()) {
// 抓住!
caught = true;
gameActive = false;
catchTime = elapsedSeconds;
timerDisplay.textContent = catchTime.toFixed(1);
return;
}
// 更新计时器
const now = performance.now();
elapsedSeconds = (now - startTime) / 1000;
timerDisplay.textContent = elapsedSeconds.toFixed(1);
}
// 动画循环
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// ---------- 事件监听 ----------
window.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
e.preventDefault();
keys[e.key] = true;
}
});
window.addEventListener('keyup', (e) => {
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
e.preventDefault();
keys[e.key] = false;
}
});
// 触摸支持 (简单: 点击左/右半屏)
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
const rect = canvas.getBoundingClientRect();
const touchX = e.touches[0].clientX - rect.left;
const halfWidth = rect.width / 2;
if (touchX < halfWidth) {
leftPressed = true;
rightPressed = false;
} else {
rightPressed = true;
leftPressed = false;
}
}, { passive: false });
canvas.addEventListener('touchend', (e) => {
e.preventDefault();
leftPressed = false;
rightPressed = false;
}, { passive: false });
canvas.addEventListener('touchcancel', (e) => {
e.preventDefault();
leftPressed = false;
rightPressed = false;
}, { passive: false });
// 防止页面滚动
canvas.addEventListener('touchmove', (e) => e.preventDefault(), { passive: false });
// 重启按钮
document.getElementById('restartButton').addEventListener('click', () => {
resetGame();
});
// 初始化
resetGame();
// 启动循环
gameLoop();
// 扩展 CanvasRenderingContext2D 的 roundRect 方法 (用于圆角矩形)
if (!CanvasRenderingContext2D.prototype.roundRect) {
CanvasRenderingContext2D.prototype.roundRect = function(x, y, w, h, r) {
if (w < 2 * r) r = w / 2;
if (h < 2 * r) r = h / 2;
this.moveTo(x + r, y);
this.lineTo(x + w - r, y);
this.quadraticCurveTo(x + w, y, x + w, y + r);
this.lineTo(x + w, y + h - r);
this.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
this.lineTo(x + r, y + h);
this.quadraticCurveTo(x, y + h, x, y + h - r);
this.lineTo(x, y + r);
this.quadraticCurveTo(x, y, x + r, y);
this.closePath();
return this;
};
}
})();
</script>
</body>
</html>
玩法说明
- 目标:在尽可能短的时间内,用红色卡车追上蓝色小汽车。
- 操作:按键盘 和 方向键移动卡车,如果你在手机上玩,触摸画布左/右半区即可。
- 计时:顶部会实时显示游戏时间,抓住小汽车后时间停止。
- 重置:点击右下角的“重来”按钮可以随时重新开始。
游戏中的小汽车会持续移动并在边缘反弹,卡车需要灵活走位才能拦截它,祝你玩得开心!


发表评论