편집 : 답변 발견! CAG가 올바른 방향으로 나를 설득 시켰지만 그에 대한 보상을 할 것입니다. 그래도 정답은 나에게 제공된다.JavaFX Snake Thread.Sleep()이 FXML을로드하지 않습니다.
저는 Canvas를 사용하여 JavaFX에서 Snake 게임을 만들고 있습니다.
- 인쇄 올바른 VBox의 세포의 배경 색상을 설정하여 그리드의 시각적 표현 :
나는 while 루프에서 실행되는 게임이있다. 입력
- 기다립니다 (Thread.sleep를 (1000).
- 다음 시각적 효과를 생성 할 수 있습니다.
을 문제는 내가 Thread.sleep를()를 사용하는 경우, 내 캔버스 뒤에. 전혀로드하지 않습니다이며, 그러나 벽을 치고 죽을 때까지 게임은 여전히 실행 중입니다.
내가 여기서 잘못하고있는 것이 있습니까? thread.sleep()이 JavaFX 노드를로드하고 표시하는 기능을 일시 중단하고 있습니까?
Thread gameThread = new Thread() {
@Override
public synchronized void start() {
super.start();
printGridToGUI();
while (KEEP_PLAYING) {
generateNextGrid();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(SnakeGUIController.class.getName()).log(Level.SEVERE, null, ex);
}
Platform.runLater(() -> {
printGridToGUI();
});
}
/*Stop continuing to play. You either won or lost.*/
if (WON_GAME) {
System.out.println("Congratulations!");
} else {
System.out.println("You lose.");
}
}
};
gameThread.start();
printGrid는()입니다 :
/**
* Prints the grid, with chars in place of on and off areas.
*/
public void printGridToGUI() {
resetCanvas();
for (Coordinate c : coordinates) {
drawCell(c.row, c.col, true);
}
drawCell(food.row, food.col, true);
}
및 resetCanvas은 다음과 같습니다
/**
* Clears the boolean array, setting all values to false. A quick way to
* wipe the grid.
*/
public final void resetCanvas() {
/*Lay out the grid on the canvas.*/
GraphicsContext gc = canvas.getGraphicsContext2D();
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
drawCell(row, col, false);
}
}
}
및 drawCell은 다음과 같습니다
/**
* Draws a cell on the canvas at the specified row and col. The row, col
* coordinates are translated into x,y coordinates for the graphics context.
*
* @param row The row of the cell to paint.
* @param col The col of the cell to paint.
* @param cellON The state of the cell, if it is on or off.
*/
private void drawCell(int row, int col, boolean cellON) {
/*Translate the row, col value into an x-y cartesian coordinate.*/
int xCoord = 0 + col * CELL_SIZE;
int yCoord = 0 + row * CELL_SIZE;
/*Draw on the canvas.*/
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
gc.fillRect(xCoord, yCoord, CELL_SIZE, CELL_SIZE);
if (!cellON) {
gc.setFill(Color.WHITE);
int BORDER = 1;
gc.fillRect(xCoord + BORDER, yCoord + BORDER, CELL_SIZE - BORDER, CELL_SIZE - BORDER);
}
}
정확하게'Thread.sleep()'을 사용하고 있습니까? 질문을 뒷받침 할 수있는 코드를 게시 할 수 있습니까? – ItachiUchiha
@ItachiUchiha가 추가되었습니다. –