-6
的遊戲
以下更改色彩是康威生命遊戲的代碼,雖然該方案完全在JAVA GUI(Eclipse的)生命
運行在黑白打印GUI塊。如何更改它的代碼打印出紅(死)和綠色(活的),而不是
public class GameofLife {
private static final long serialVersionUID = 1L;
public static int frameSize = 360;
public static String title = "Game Of Life";
public Random r = new Random();
public int gridSize = 100;
public double generationSpeed = 20.0;
public BufferedImage image;
public int[] pixels;
public boolean[] cGrid;
public boolean[] pGrid;
public GameofLife() {
Dimension d = new Dimension(frameSize, frameSize);
setMinimumSize(d);
setMaximumSize(d);
setPreferredSize(d);
image = new BufferedImage(gridSize, gridSize, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
}
public void start() {
cGrid = new boolean[pixels.length];
pGrid = new boolean[pixels.length];
for (int i = 0; i < cGrid.length; i++)
cGrid[i] = r.nextInt(100)/100.0 > 0.8 ? true: false;
new Thread(this).start();
}
public void run() {
double frameCut = 1000000000.0/generationSpeed;
long currentTime = System.nanoTime();;
long previouseTime = currentTime;
long passedTime = 0;
double unprocessedTime = 0.0;
long frameCounter = System.currentTimeMillis();
int generations = 1;
while (true) {
previouseTime = currentTime;
currentTime = System.nanoTime();
passedTime = currentTime - previouseTime;
unprocessedTime += passedTime;
if (unprocessedTime > frameCut) {
unprocessedTime = 0;
update();
generations++;
}
if (System.currentTimeMillis() - frameCounter >= 1000) {
frameCounter = System.currentTimeMillis();
}
render();
}
}
public void update() {
for (int i = 0; i < pixels.length; i++)
pGrid[i] = cGrid[i];
for (int y = 0; y < gridSize; y++) {
for (int x = 0; x < gridSize; x++) {
int res = 0;
int xx0 = x - 1;
int yy0 = y - 1;
int xx1 = x + 1;
int yy1 = y + 1;
if (x != 0) res += pGrid[xx0 + gridSize * y] ? 1 : 0;
if (y != 0) res += pGrid[x + gridSize * yy0] ? 1 : 0;
if (x != gridSize - 1) res += pGrid[xx1 + gridSize * y] ? 1 : 0;
if (y != gridSize - 1) res += pGrid[x + gridSize * yy1] ? 1 : 0;
if (x != 0 && y != 0) res += pGrid[xx0 + gridSize * yy0] ? 1 : 0;
if (x != 0 && y != gridSize - 1) res += pGrid[xx0 + gridSize * yy1] ? 1 : 0;
if (x != gridSize - 1 && y != 0) res += pGrid[xx1 + gridSize * yy0] ? 1 : 0;
if (x != gridSize - 1 && y != gridSize - 1) res += pGrid[xx1 + gridSize * yy1] ? 1 : 0;
if (! (pGrid[x + gridSize * y] && (res == 3 || res == 2))) cGrid[x + gridSize * y] = false;
if (!pGrid[x + gridSize * y] && res == 3) cGrid[x + gridSize * y] = true;
}
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
for (int i = 0; i < pixels.length; i++)
pixels[i] = 0;
for (int i = 0; i < pixels.length; i++)
pixels[i] = cGrid[i] ? 0xffffff: 0;
g.drawImage(image, 0, 0, frameSize, frameSize, null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setAlwaysOnTop(true);
GameofLife gol = new GameofLife();
frame.add(gol);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
gol.start();
}
}
你可以,或者說,你試過,發現顏色? – Justas
這是誰的代碼?你在哪裏找到它? –
不知道,但我完全只是吸引了Abe Lincoln的腦袋弄亂了這些變數。 –