梦里花落0921
在代码中,您混合了行和列。x 坐标从左到右,这是列。y 轴从顶部到底部,对应于行。不要将 、 和 存储在数组中。但是使用二维数组来存储飞船的位置和鼠标点击的位置:columnrowhitmissboolean [][] ship;boolean [][] click;将飞船单元格保留在画布内如果方向是水平的,则船的x起始位置必须小于:NUM_COLS - shipLengthrandomX = (int)random(NUM_COLS - shipLength);randomY = (int)random(NUM_ROWS);如果方向是水平的,则船的 y 起始位置必须小于 :NUM_ROWS - shipLengthrandomX = (int)random(NUM_COLS);randomY = (int)random(NUM_ROWS - shipLength);呼入而不是 :randomShipsetupdrawvoid setup() { size(600, 500); randomShip(); println(store);}void draw() { // randomShip(); <---- delete drawCells (row, column, shipLength, (255) );}生成船舶的随机位置和大小randomShip;void randomShip () { ship = new boolean[NUM_COLS][NUM_ROWS]; click = new boolean[NUM_COLS][NUM_ROWS]; shipLength = (int)random (3, 8); int store = (int)random(vert, horz); if (store >= 0) { int randomX = (int)random(NUM_COLS - shipLength); int randomY = (int)random(NUM_ROWS); for (int i = 0; i < shipLength; i++ ) { ship[randomX + i][randomY] = true; } } else { int randomX = (int)random(NUM_COLS); int randomY = (int)random(NUM_ROWS - shipLength); for (int i = 0; i < shipLength; i++ ) { ship[randomX][randomY+1] = true; } } println(shipLength);}将鼠标位置(以像素为单位)转换为网格上的行和列如果猜测正确,请将猜测添加到命中数组,如果错过,则将其添加到未命中数组中。单击的单元格可以通过除以鼠标坐标和mouseXmouseYCELLSIZEint cell_x = mouseX / CELLSIZE; int cell_y = mouseY / CELLSIZE; 存储标记点击的单元格,并计算命中和未命中:mouseClickedvoid mouseClicked () { int cell_x = mouseX / CELLSIZE; int cell_y = mouseY / CELLSIZE; if (!click[cell_x][cell_y]) { click[cell_x][cell_y] = true; if ( ship[cell_x][cell_y] ) { hitCount ++; } else { missCount ++; } }}当做出猜测时,除了给单元格着色外,还要在单元格上打印“命中!”或“错过!”评估绘图池中的船舶位置 () 和单击的位置 ()。绘制依赖于 2 个嵌套循环中的状态的单元格和文本:ship[][]click[][]void drawCells(int colour) { for (int i = 0; i < NUM_COLS; i++) { for (int j = 0; j < NUM_ROWS; j++) { float x = i * CELLSIZE; float y = j * CELLSIZE; if (ship[i][j]) { fill (colour); rect(x, y, CELLSIZE, CELLSIZE); } if (click[i][j]) { fill(255, 0, 0); textSize(15); text(ship[i][j] ? "hit" : "miss", x+10, y+30); } } }}当所有电池都被击中时沉没船在 中处理游戏的结束:draw例如void draw() { drawCells(255); if (hitCount == shipLength ) { // [...] }}完整代码列表:final int CELLSIZE = 50;final int NUM_ROWS = 10;final int NUM_COLS = 12;int horz = (int)random(50);int vert = (int)random(-50);int store;int shipLength;boolean [][] ship;boolean [][] click;int hitCount = 0;int missCount = 0;void setup() { size(600, 500); randomShip(); println(store);}void draw() { drawCells(255); if (hitCount == shipLength ) { // [...] }}void drawCells(int colour) { for (int i = 0; i < NUM_COLS; i++) { for (int j = 0; j < NUM_ROWS; j++) { float x = i * CELLSIZE; float y = j * CELLSIZE; if (ship[i][j]) { fill (colour); rect(x, y, CELLSIZE, CELLSIZE); } if (click[i][j]) { fill(255, 0, 0); textSize(15); text(ship[i][j] ? "hit" : "miss", x+10, y+30); } } }}void randomShip () { ship = new boolean[NUM_COLS][NUM_ROWS]; click = new boolean[NUM_COLS][NUM_ROWS]; hitCount = 0; missCount = 0; shipLength = (int)random (3, 8); int store = (int)random(vert, horz); if (store >= 0) { int randomX = (int)random(NUM_COLS - shipLength); int randomY = (int)random(NUM_ROWS); for (int i = 0; i < shipLength; i++ ) { ship[randomX + i][randomY] = true; } } else { int randomX = (int)random(NUM_COLS); int randomY = (int)random(NUM_ROWS - shipLength); for (int i = 0; i < shipLength; i++ ) { ship[randomX][randomY+1] = true; } } println(shipLength);}void mouseClicked () { int cell_x = mouseX / CELLSIZE; int cell_y = mouseY / CELLSIZE; if (!click[cell_x][cell_y]) { click[cell_x][cell_y] = true; if ( ship[cell_x][cell_y] ) { hitCount ++; } else { missCount ++; } }}