gpt4 book ai didi

java - Java 中的生命互动游戏

转载 作者:行者123 更新时间:2023-11-30 09:12:39 25 4
gpt4 key购买 nike

我正在尝试编写一个交互式生活游戏,我可以在其中手动将滑翔机插入游戏区域。我希望它如何工作是我有一个滑翔机按钮,按下它后我可以将光标移动到我希望在网格上设置滑翔机的位置,然后单击网格滑翔机集成到生活游戏中。我正在使用处理,我正在使用这个草图作为启动代码。 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 像素的正方形,所以如果我只想构建它,我知道如何映射它,但不确定如何将它粘贴到阵列中,然后将它集成到网格中。

最佳答案

这里有两个不同的东西在起作用,如何将网格中的细胞从死的变成活的,以及如何在你做之前显示变化。数组“gliderArray”存储你的滑翔机形状,并通过遍历数组并将底层网格替换为数组中的任何内容来应用于网格...至于显示,您要么必须为显示它们将要更改的单元格设置不同的状态,要么重新绘制它们的矩形...此解决方案是第二种方式...

void draw() {

//Draw grid
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
if (cells[x][y]==1) {
fill(alive); // If alive
}
else {
fill(dead); // If dead
}
rect (x*cellSize, y*cellSize, cellSize, cellSize);
}
}
// Iterate if timer ticks
if (millis()-lastRecordedTime>interval) {
if (!pause) {
iteration();
lastRecordedTime = millis();
}
}
//Glider shape is :
//OXO
//OOX
//XXX
//where O is a dead cell and X is an alive cell.
int [][] gliderArray = new int [][] {
{ 0, 1, 0 }
,
{ 0, 0, 1 }
,
{ 1, 1, 1 }
};
// Create new cells manually on pause
if (pause) {
// 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);
if (glider) {
// Map again because glider takes +- 1 cells on each direction
xCellOver = constrain(xCellOver, 1, width/cellSize-2);
yCellOver = constrain(yCellOver, 1, height/cellSize-2);
}
if (mousePressed) {
// Check against cells in buffer
if (!glider) {
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 {
for (int i=-1; i<=1; i++) {
for (int j=-1; j<=1; j++) {
cells[xCellOver+j][yCellOver+i] = gliderArray[i+1][j+1];
}
}
}
}
else {
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
cellsBuffer[x][y] = cells[x][y];
if (glider && x >= xCellOver-1 && x <= xCellOver+1 && y >= yCellOver-1 && y <= yCellOver+1) {
for (int i=-1; i<=1; i++) {
for (int j=-1; j<=1; j++) {
if (x == xCellOver+j && y == yCellOver+i) fill(gliderArray[i+1][j+1] == 1? color(255, 125, 0) : dead);
}
}
rect (x*cellSize, y*cellSize, cellSize, cellSize);
}
else if (x == xCellOver && y == yCellOver) {
fill(cellsBuffer[x][y] == 1? color(0,0,255) : color(255, 125, 0));
rect (x*cellSize, y*cellSize, cellSize, cellSize);
}
}
}
}
}
}

您还需要一个全局 boolean 值:

boolean glider = false;

和另一个检查 void keyPressed()

if (key == 'g') glider = !glider;

关于java - Java 中的生命互动游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21472316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com