猿问

如何将鼠标位置(以像素为单位)转换为网格上的行和列?

我基本上是在制作一个战舰猜谜游戏,你必须通过点击鼠标到达一艘船的位置。当正确猜出飞船的位置时,它会从数组中删除该飞船单元格,当每个单元格都猜对时,游戏就结束了。

我现在挣扎的是

  1. 将飞船单元格保留在画布内

  2. 将鼠标位置(以像素为单位)转换为网格上的行和列

  3. 如果猜测正确,请将猜测添加到命中数组,如果错过,则将其添加到未命中数组中。

  4. 当做出猜测时,除了给单元格着色外,还要在单元格上打印“命中!”或“错过!”

  5. 当所有电池都被击中时沉没船


慕无忌1623718
浏览 127回答 1
1回答

梦里花落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() {&nbsp; &nbsp; size(600, 500);&nbsp; &nbsp; randomShip();&nbsp; &nbsp; println(store);}void draw() {&nbsp; &nbsp; // randomShip(); <---- delete&nbsp; &nbsp; drawCells (row, column, shipLength, (255) );}生成船舶的随机位置和大小randomShip;void randomShip () {&nbsp; &nbsp; ship = new boolean[NUM_COLS][NUM_ROWS];&nbsp; &nbsp; click = new boolean[NUM_COLS][NUM_ROWS];&nbsp; &nbsp; shipLength = (int)random (3, 8);&nbsp; &nbsp; int store = (int)random(vert, horz);&nbsp;&nbsp;&nbsp; &nbsp; if (store >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; int randomX = (int)random(NUM_COLS - shipLength);&nbsp; &nbsp; &nbsp; &nbsp; int randomY = (int)random(NUM_ROWS);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < shipLength; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship[randomX + i][randomY] = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int randomX = (int)random(NUM_COLS);&nbsp; &nbsp; &nbsp; &nbsp; int randomY = (int)random(NUM_ROWS - shipLength);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < shipLength; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship[randomX][randomY+1] = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; println(shipLength);}将鼠标位置(以像素为单位)转换为网格上的行和列如果猜测正确,请将猜测添加到命中数组,如果错过,则将其添加到未命中数组中。单击的单元格可以通过除以鼠标坐标和mouseXmouseYCELLSIZEint cell_x = mouseX / CELLSIZE;&nbsp;int cell_y = mouseY / CELLSIZE;&nbsp;存储标记点击的单元格,并计算命中和未命中:mouseClickedvoid mouseClicked () {&nbsp; &nbsp; int cell_x = mouseX / CELLSIZE;&nbsp;&nbsp; &nbsp; int cell_y = mouseY / CELLSIZE;&nbsp; &nbsp; if (!click[cell_x][cell_y]) {&nbsp; &nbsp; &nbsp; &nbsp; click[cell_x][cell_y] = true;&nbsp; &nbsp; &nbsp; &nbsp; if ( ship[cell_x][cell_y] ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hitCount ++;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; missCount ++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}当做出猜测时,除了给单元格着色外,还要在单元格上打印“命中!”或“错过!”评估绘图池中的船舶位置 () 和单击的位置 ()。绘制依赖于 2 个嵌套循环中的状态的单元格和文本:ship[][]click[][]void drawCells(int colour) {&nbsp; &nbsp; for (int i = 0; i < NUM_COLS; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < NUM_ROWS; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float x = i * CELLSIZE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float y = j * CELLSIZE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ship[i][j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fill (colour);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rect(x, y, CELLSIZE, CELLSIZE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (click[i][j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fill(255, 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textSize(15);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text(ship[i][j] ? "hit" : "miss", x+10, y+30);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}当所有电池都被击中时沉没船在 中处理游戏的结束:draw例如void draw() {&nbsp; &nbsp; drawCells(255);&nbsp; &nbsp; if (hitCount == shipLength ) {&nbsp; &nbsp; &nbsp; &nbsp; // [...]&nbsp; &nbsp; }}完整代码列表: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() {&nbsp; &nbsp; size(600, 500);&nbsp; &nbsp; randomShip();&nbsp; &nbsp; println(store);}void draw() {&nbsp; &nbsp; drawCells(255);&nbsp; &nbsp; if (hitCount == shipLength ) {&nbsp; &nbsp; &nbsp; &nbsp; // [...]&nbsp; &nbsp; }}void drawCells(int colour) {&nbsp; &nbsp; for (int i = 0; i < NUM_COLS; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < NUM_ROWS; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float x = i * CELLSIZE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float y = j * CELLSIZE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ship[i][j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fill (colour);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rect(x, y, CELLSIZE, CELLSIZE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (click[i][j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fill(255, 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textSize(15);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text(ship[i][j] ? "hit" : "miss", x+10, y+30);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}void randomShip () {&nbsp; &nbsp; ship = new boolean[NUM_COLS][NUM_ROWS];&nbsp; &nbsp; click = new boolean[NUM_COLS][NUM_ROWS];&nbsp; &nbsp; hitCount = 0;&nbsp; &nbsp; missCount = 0;&nbsp; &nbsp; shipLength = (int)random (3, 8);&nbsp; &nbsp; int store = (int)random(vert, horz);&nbsp;&nbsp;&nbsp; &nbsp; if (store >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; int randomX = (int)random(NUM_COLS - shipLength);&nbsp; &nbsp; &nbsp; &nbsp; int randomY = (int)random(NUM_ROWS);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < shipLength; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship[randomX + i][randomY] = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int randomX = (int)random(NUM_COLS);&nbsp; &nbsp; &nbsp; &nbsp; int randomY = (int)random(NUM_ROWS - shipLength);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < shipLength; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship[randomX][randomY+1] = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; println(shipLength);}void mouseClicked () {&nbsp; &nbsp; int cell_x = mouseX / CELLSIZE;&nbsp;&nbsp; &nbsp; int cell_y = mouseY / CELLSIZE;&nbsp; &nbsp; if (!click[cell_x][cell_y]) {&nbsp; &nbsp; &nbsp; &nbsp; click[cell_x][cell_y] = true;&nbsp; &nbsp; &nbsp; &nbsp; if ( ship[cell_x][cell_y] ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hitCount ++;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; missCount ++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答