【发布时间】:2014-01-31 04:18:33
【问题描述】:
我正在尝试编写一个互动的生活游戏,我可以在游戏字段中手动插入滑翔机。 我希望它如何工作是我有一个滑翔机按钮,按下它后,我可以将光标移动到我希望在网格上设置滑翔机的位置,然后单击网格滑翔机集成到生活游戏中。 我正在使用处理,我正在使用这个草图作为启动代码。 http://www.openprocessing.org/sketch/95216
这是在鼠标按下时创建新单元格的代码(一次一个)
// Create new cells manually on pause
if (pause && !gliderSelected && mousePressed) {
// Map and avoid out of bound errors
int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize));
xCellOver = constrain(xCellOver, 0, width/cellSize-1);
int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize));
yCellOver = constrain(yCellOver, 0, height/cellSize-1);
// Check against cells in buffer
if (cellsBuffer[xCellOver][yCellOver]==1) { // Cell is alive
cells[xCellOver][yCellOver]=0; // Kill
fill(dead); // Fill with kill color
}
else { // Cell is dead
cells[xCellOver][yCellOver]=1; // Make alive
fill(alive); // Fill alive color
}
}
else if (pause && !mousePressed) { // And then save to buffer once mouse goes up
// Save cells to buffer (so we operate with one array keeping the other intact)
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
cellsBuffer[x][y] = cells[x][y];
}
}
}
滑翔机形状是:
氧合
奥克斯
XXX
其中 O 是死细胞,X 是活细胞。
//create gliders on press
if (pause && gliderSelected && mousePressed) {
// Map and avoid out of bound errors
int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize));
xCellOver = constrain(xCellOver, 0, width/cellSize-1);
int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize));
yCellOver = constrain(yCellOver, 0, height/cellSize-1);
//here i thought of maybe creating an array of cells that map the glider and then running a loop to change the grid cell status according to the glider array
}
我不确定如何制作一个存储滑翔机单元位置的数组。每个单元格都是一个 10 像素的正方形,所以如果我只想构建它,我知道如何映射它,但不知道如何将它粘贴到数组中,然后将其整合到网格中。
【问题讨论】:
-
创建一个数组,将单元格存储为二维坐标 (x, y)。 x, y 是要绘制的滑翔机实例左上角的位置。我建议这样做而不是计算滑翔机的中间,因为 10, 10 没有很好的中心([5, 5], [5, 6], [6, 6] 和 [6, 5] 是四个中心点)。
标签: java arrays processing interactive conways-game-of-life