2014-01-31 37 views
2

我想寫一個生活的互動遊戲,我可以在遊戲領域手動插入滑翔機。 我想要它的工作原理是我有一個滑翔機按鈕,當我按下它後,我可以將光標移動到我想要在網格上設置滑翔機的位置,並且在點擊網格滑翔機在生活遊戲中進行整合之後。 我正在使用處理,我將這個草圖用作啓動代碼。 http://www.openprocessing.org/sketch/95216Java中的生命互動遊戲

這是代碼來創建對小鼠按新細胞(一個在時間)

// 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]; 
     } 
    } 
    } 

滑翔機形狀是:

OXO
OOX
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像素的正方形,所以我知道如何映射它,如果我想構建它,但不知道如何將它粘貼到陣列中,然後將其集成到網格中。

+0

創建一個將單元存儲爲2D座標(x,y)的數組。 x,y是要繪製的滑翔機實例左上角的位置。我建議這樣做,而不是將滑翔機的中間部分設計爲10,10中間沒有一個好的中心([5,5],[5,6],[6,6]和[6,5]是四個centrepoints)。 – Gorbles

回答

2

這裏有兩個不同的東西,一個是如何將網格中的細胞從死亡變爲活着,以及如何在變化之前顯示變化。數組「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; 

另一個檢查無效keyPressed()

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