我正在研究康威的生命克隆游戏,因为这是一种很好的做法,但我遇到了问题。我看到有像素删除和重生,但所有像素只是散布在屏幕的最末端,然后其他像素正在重生,但那时它只是空闲。
以下是一些截图:
我将向您展示我的一些代码以了解此逻辑。这一切都在change
方法中处理:
package main;
import java.awt.Color;
import java.awt.Graphics;
public class Functions {
public static int pixelsize=6,gridwidth=800/6,gridheight=600/6;
static int[][] pixels = new int[gridwidth][gridheight];
static boolean first = true;
public static void change(){
for(int i = 0; i < gridwidth; i++){
for(int j = 0; j < gridheight; j++){
int neighbors = 0;
//check each cell
try{
if(pixels[i+1][j] == 1){neighbors++;}
if(pixels[i-1][j] == 1){neighbors++;}
if(pixels[i+1][j-1] == 1){neighbors++;}
if(pixels[i][j+1] == 1){neighbors++;}
if(pixels[i][j-1] == 1){neighbors++;}
if(pixels[i+1][j+1] == 1){neighbors++;}
if(pixels[i-1][j-1] == 1){neighbors++;}
if(pixels[i-1][j+1] == 1){neighbors++;}
}catch(ArrayIndexOutOfBoundsException e){
}
if(neighbors == 3 || neighbors == 2 ){
pixels[i][j] = 1;
}else if(neighbors < 2 || neighbors >= 4){
pixels[i][j] = 0;
}
}
}
}
public static void render(Graphics g){
for(int i = 0; i < gridwidth;i++){
for(int j = 0; j < gridheight; j++){
if(pixels[i][j] == 1){
g.setColor(Color.red);
g.fillRect(i*6, j*6, 6, 6);
}
}
}
}
}
感谢所有的帮助。可悲的是,它仍然无法正常工作。
现在它正在做同样的事情,但是像这样形成菱形: