【问题标题】:Interactive Game of Life in JavaJava中的交互式生命游戏
【发布时间】: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


【解决方案1】:

这里有两件不同的事情,如何将网格中的单元格从死变为活,以及如何在执行之前显示更改。数组“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 glider = false;

然后再次检查 void keyPressed()

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

【讨论】:

    猜你喜欢
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多