jeremyjia / Games

Home Page Link:
https://jeremyjia.github.io/Games/
3 stars 8 forks source link

生命科学游戏-JavaScript版本 #1062

Closed jeremyjia closed 1 month ago

jeremyjia commented 3 months ago

const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const cellSize = 20; // 每个单元格的大小 const gridWidth = canvas.width / cellSize; const gridHeight = canvas.height / cellSize; let grid = []; // 游戏的二维网格 let gameRunning = false; let intervalId;

var oBtnStart = document.getElementById("startGame"); var oBtnPause = document.getElementById("pauseGame"); var oBtnReset = document.getElementById("resetGame"); // 初始化网格 function initGrid() { for (let i = 0; i < gridHeight; i++) { grid[i] = []; for (let j = 0; j < gridWidth; j++) { grid[i][j] = Math.random() < 0.5; // 随机初始化细胞状态 } } }

// 绘制网格 function drawGrid() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (let i = 0; i < gridHeight; i++) { for (let j = 0; j < gridWidth; j++) { if (grid[i][j]) { ctx.fillStyle = 'green'; // 活细胞颜色 } else { ctx.fillStyle = 'blue'; // 死细胞颜色 } ctx.fillRect(j cellSize, i cellSize, cellSize, cellSize); } } }

// 更新网格 function updateGrid() { const newGrid = [...grid]; for (let i = 0; i < gridHeight; i++) { for (let j = 0; j < gridWidth; j++) { const liveNeighbors = countLiveNeighbors(i, j); if (grid[i][j]) { // 规则1:如果一个活细胞周围正好有两个或三个活细胞,则该细胞保持为活状态,否则该细胞死亡。 newGrid[i][j] = liveNeighbors === 2 || liveNeighbors === 3; } else { // 规则2:如果一个死细胞周围正好有三个活细胞,则该细胞变为活状态,否则该细胞保持为死状态。 newGrid[i][j] = liveNeighbors === 3; } } } grid = newGrid; }

// 计算活邻居数量 function countLiveNeighbors(x, y) { let count = 0; for (let dx = -1; dx <= 1; dx++) { for (let dy = -1; dy <= 1; dy++) { const nx = x + dx; const ny = y + dy; if ( nx >= 0 && nx < gridHeight && ny >= 0 && ny < gridWidth && (dx !== 0 || dy !== 0) && // 不计算自己 grid[nx][ny] ) { count++; } } } return count; }

// 游戏循环 function gameLoop() { if (gameRunning) { updateGrid(); drawGrid(); } requestAnimationFrame(gameLoop); }

// 开始/继续游戏 oBtnStart.onclick = function () { startGame(); } oBtnPause.onclick = function () { pauseGame(); } oBtnReset.onclick = function () { resetGame(); } function startGame() { if (!gameRunning) { initGrid(); drawGrid(); gameRunning = true; } intervalId = setInterval(gameLoop, 500); // 每500毫秒更新一次 }

// 暂停游戏 function pauseGame() { clearInterval(intervalId); gameRunning = false; }

// 重置游戏 function resetGame() { pauseGame(); initGrid(); drawGrid(); }

// 初始化 initGrid(); drawGrid();

jeremyjia commented 3 months ago

背景知识

生命游戏,这是一个由英国数学家约翰·何顿·康威在1970年发明的细胞自动机。以下是对这个游戏的详细介绍:

基本概念:

生命游戏是一个零玩家游戏,展示了一个二维方格子世界。 每个方格子居住着一个细胞,这些细胞可以是活的或死的。

游戏规则:

存活:如果一个活细胞周围有2个或3个活邻居(总共8个邻居),则该细胞在下一代中继续存活。 死亡: 如果一个活细胞有少于2个活邻居,它会因为孤独而死亡。 如果一个活细胞有超过3个活邻居,它会因为资源匮乏而死亡。 新生:如果一个死细胞正好有3个活邻居,则这个死细胞会复活。

游戏过程:

游戏开始时,可以随机生成活细胞,或者手动设置初始状态。 随着游戏的进行,细胞会按照上述规则进行演变,生成各种结构和形态。 一些形状可能会保持不变(静止生命形式),而其他形状则可能会因为无序细胞的“入侵”而被破坏。

游戏意义:

生命游戏能够模拟自然界某些生命现象,如生物的发展、演变与消亡。 它引起了全世界许多爱好者的兴趣,并成为了细胞自动机理论的一个简单而优异的实例。

其他信息:

生命游戏最初在马丁·加德纳的“数学游戏”专栏中出现,后来在《科学美国人》杂志中得到了更广泛的传播。 康威是生命游戏的发明者,但他已经于2020年4月11日去世。

希望以上信息能帮助你更清晰地了解生命游戏。如果你还有其他问题或需要进一步的解释,请随时告诉我

jeremyjia commented 3 months ago

借助文心一言完成代码,见主页