我来为您创建一个生物演化游戏。这是一个相当复杂的项目,我将使用HTML5 Canvas和JavaScript来实现。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>生物演化游戏</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
#gameContainer {
display: flex;
gap: 20px;
align-items: flex-start;
}
#gameCanvas {
border: 2px solid #333;
background-color: #fff;
cursor: crosshair;
}
#controls {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
width: 300px;
}
.control-group {
margin-bottom: 15px;
}
.control-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.control-group input, .control-group button {
width: 100%;
padding: 8px;
margin-bottom: 5px;
}
button {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
#stats {
background-color: #f9f9f9;
padding: 10px;
border-radius: 4px;
margin-top: 20px;
}
.stat-item {
margin: 5px 0;
font-size: 14px;
}
#instructions {
background-color: #e8f4f8;
padding: 15px;
border-radius: 4px;
margin-bottom: 20px;
font-size: 14px;
}
#instructions h3 {
margin-top: 0;
}
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="controls">
<div id="instructions">
<h3>操作说明</h3>
<ul>
<li>点击画布:在点击位置添加食物</li>
<li>拖动生物:按住并拖动生物改变位置</li>
<li>使用下方控制面板调整游戏参数</li>
</ul>
</div>
<div class="control-group">
<label>添加生物</label>
<button onclick="game.addCreature()">添加新生物</button>
</div>
<div class="control-group">
<label>寿命流逝速度</label>
<input type="range" id="lifeSpeed" min="0.1" max="3" step="0.1" value="1">
<span id="lifeSpeedValue">1.0</span>
</div>
<div class="control-group">
<label>移动消耗倍率</label>
<input type="range" id="moveCost" min="0.5" max="2" step="0.1" value="1">
<span id="moveCostValue">1.0</span>
</div>
<div id="stats">
<h3>游戏统计</h3>
<div class="stat-item">生物数量: <span id="creatureCount">0</span></div>
<div class="stat-item">食物数量: <span id="foodCount">0</span></div>
<div class="stat-item">总分数: <span id="totalScore">0</span></div>
<div class="stat-item">平均寿命: <span id="avgLife">0</span></div>
</div>
</div>
</div>
<script>
// 工具函数
function getRandomColor() {
const colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8', '#F7DC6F', '#BB8FCE'];
return colors[Math.floor(Math.random() * colors.length)];
}
function distance(x1, y1, x2, y2) {
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
}
// 分数类
class Score {
constructor(value = 0) {
this.value = value;
}
add(amount) {
this.value += amount;
}
subtract(amount) {
this.value = Math.max(0, this.value - amount);
}
getValue() {
return this.value;
}
}
// 食物类
class Food {
constructor(x, y) {
this.x = x;
this.y = y;
this.value = 1;
this.radius = 5;
}
draw(ctx) {
ctx.fillStyle = '#4CAF50';
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
}
}
// 生物类
class Creature {
constructor(x, y, id) {
this.id = id;
this.x = x;
this.y = y;
this.score = new Score(2);
this.life = 100;
this.speed = 1;
this.visionAngle = Math.PI * 2; // 初始为完整圆形
this.visionRadius = 50;
this.maxScore = 2;
this.color = getRandomColor();
this.targetX = x;
this.targetY = y;
this.lastMoveTime = Date.now();
this.moveFrequency = 0;
this.isDragging = false;
this.visionDirection = 0;
}
get size() {
return Math.max(10, this.maxScore * 2);
}
update(creatures, foods, deltaTime, lifeSpeed, moveCostMultiplier) {
if (this.life <= 0) return;
// 寿命衰减
this.life -= deltaTime * lifeSpeed * 0.05;
this.life = Math.max(0, this.life);
// 寻找目标
if (!this.isDragging) {
this.findTarget(creatures, foods);
this.moveToTarget(moveCostMultiplier);
}
// 更新最高分
if (this.score.getValue() > this.maxScore) {
this.maxScore = this.score.getValue();
}
// 检查碰撞
this.checkCollisions(creatures, foods);
}
findTarget(creatures, foods) {
let closestTarget = null;
let closestDistance = Infinity;
// 检查视野内的食物
foods.forEach(food => {
if (this.isInVision(food.x, food.y, creatures)) {
const dist = distance(this.x, this.y, food.x, food.y);
if (dist < closestDistance) {
closestDistance = dist;
closestTarget = { x: food.x, y: food.y };
}
}
});
// 检查视野内的生物
creatures.forEach(other => {
if (other.id !== this.id && other.life > 0 &&
other.score.getValue() < this.score.getValue() &&
this.isInVision(other.x, other.y, creatures)) {
const dist = distance(this.x, this.y, other.x, other.y);
if (dist < closestDistance) {
closestDistance = dist;
closestTarget = { x: other.x, y: other.y };
}
}
});
if (closestTarget) {
this.targetX = closestTarget.x;
this.targetY = closestTarget.y;
} else {
// 随机游走
if (Math.random() < 0.02) {
this.targetX = this.x + (Math.random() - 0.5) * 100;
this.targetY = this.y + (Math.random() - 0.5) * 100;
}
}
}
isInVision(targetX, targetY, creatures) {
const dist = distance(this.x, this.y, targetX, targetY);
if (dist > this.visionRadius) return false;
// 检查是否被其他生物遮挡
for (let creature of creatures) {
if (creature.id === this.id || creature.life <= 0) continue;
// 简化的遮挡检测
const blockDist = distance(this.x, this.y, creature.x, creature.y);
const targetDist = distance(this.x, this.y, targetX, targetY);
if (blockDist < targetDist) {
const angle = Math.atan2(targetY - this.y, targetX - this.x);
const blockAngle = Math.atan2(creature.y - this.y, creature.x - this.x);
const angleDiff = Math.abs(angle - blockAngle);
if (angleDiff < 0.2) { // 约11度的遮挡角度
return false;
}
}
}
return true;
}
moveToTarget(moveCostMultiplier) {
const dx = this.targetX - this.x;
const dy = this.targetY - this.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist > 2) {
const moveDistance = Math.min(this.speed, dist);
this.x += (dx / dist) * moveDistance;
this.y += (dy / dist) * moveDistance;
// 计算移动消耗
const now = Date.now();
const timeSinceLastMove = now - this.lastMoveTime;
this.moveFrequency = 1000 / timeSinceLastMove;
this.lastMoveTime = now;
// 分数消耗
const distanceCost = moveDistance * 0.01 * this.size * 0.01;
const frequencyCost = this.moveFrequency * 0.001;
const totalCost = (distanceCost + frequencyCost * 2) * moveCostMultiplier;
this.score.subtract(totalCost);
// 更新速度(对数增长)
this.speed = 1 + Math.log(1 + this.moveFrequency * 0.1) * 0.5;
}
}
checkCollisions(creatures, foods) {
if (this.life <= 0) return;
// 检查食物碰撞
for (let i = foods.length - 1; i >= 0; i--) {
const food = foods[i];
if (distance(this.x, this.y, food.x, food.y) < this.size + food.radius) {
this.score.add(food.value);
foods.splice(i, 1);
}
}
// 检查生物碰撞
creatures.forEach(other => {
if (other.id === this.id) return;
const dist = distance(this.x, this.y, other.x, other.y);
if (dist < this.size + other.size) {
if (other.life <= 0) {
// 从死亡生物获取分数
const gainScore = Math.min(this.score.getValue(), other.score.getValue());
this.score.add(gainScore);
other.score.subtract(gainScore);
} else if (other.score.getValue() < this.score.getValue()) {
// 吞噬较小生物
this.score.add(other.score.getValue());
other.score = new Score(0);
other.life = 0;
}
}
});
}
draw(ctx, creatures) {
// 绘制视觉范围
ctx.save();
ctx.globalAlpha = 0.2;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.visionRadius, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
// 绘制生物本体
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
// 绘制生命条
if (this.life > 0) {
const barWidth = this.size * 2;
const barHeight = 4;
const barY = this.y - this.size - 10;
ctx.fillStyle = '#ddd';
ctx.fillRect(this.x - barWidth / 2, barY, barWidth, barHeight);
ctx.fillStyle = '#4CAF50';
ctx.fillRect(this.x - barWidth / 2, barY, barWidth * (this.life / 100), barHeight);
}
// 绘制分数
ctx.fillStyle = '#000';
ctx.font = '12px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(Math.floor(this.score.getValue()), this.x, this.y);
}
}
// 游戏主类
class Game {
constructor() {
this.canvas = document.getElementById('gameCanvas');
this.ctx = this.canvas.getContext('2d');
this.creatures = [];
this.foods = [];
this.creatureIdCounter = 0;
this.lastTime = Date.now();
this.lifeSpeed = 1;
this.moveCostMultiplier = 1;
this.draggedCreature = null;
this.mouseX = 0;
this.mouseY = 0;
this.initControls();
this.initEventListeners();
this.initFood();
this.gameLoop();
}
initControls() {
const lifeSpeedSlider = document.getElementById('lifeSpeed');
const moveCostSl