我基本上是在做一个战舰猜谜游戏,您必须通过单击鼠标才能确定船的位置。当正确猜到了船的位置时,它将从数组中删除该船只单元;当正确猜到了每个单元时,游戏结束了。

我现在正在努力的是


将船舱放置在画布中
将鼠标位置(以像素为单位)转换为网格中的行和列
如果猜测正确,则将猜测添加到匹配数组,如果未找到,则将其添加到未命中数组。
进行猜测时,除了为单元格着色之外,还可以在单​​元格上打印“ Hit!”或“ Miss!”
当所有细胞都被击中时下沉船

最佳答案

在您的代码中,您混合了行和列。 x坐标从左到右,这是列。 y轴从顶部到底部,并且与各行相对应。

不要将columnrowhitmiss存储在数组中。但是,请使用二维数组来存储飞船的位置和鼠标单击的位置:

boolean [][] ship;
boolean [][] click;



  
  将船舱放置在画布中
  


如果方向是水平的,则船的x起始位置必须小于NUM_COLS - shipLength

randomX = (int)random(NUM_COLS - shipLength);
randomY = (int)random(NUM_ROWS);


如果方向是水平的,则船的y起始位置必须小于NUM_ROWS - shipLength

randomX = (int)random(NUM_COLS);
randomY = (int)random(NUM_ROWS - shipLength);


randomShip中而不是setup中调用draw

void 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);
}



  
  将鼠标位置(以像素为单位)转换为网格中的行和列
  如果猜测正确,则将猜测添加到匹配数组,如果未找到,则将其添加到未命中数组。
  


单击的单元格可以通过将鼠标坐标mouseXmouseY除以CELLSIZE来获得

int cell_x = mouseX / CELLSIZE;
int cell_y = mouseY / CELLSIZE;


存储标记单击的单元格并计算mouseClicked中的命中和未命中:

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 ++;
        }
    }
}



  
  进行猜测时,除了为单元格着色之外,还可以在单​​元格上打印“ Hit!”或“ Miss!”
  


在drawCells中评估船的位置(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 ++;
        }
    }
}

关于java - 如何将以像素为单位的鼠标位置转换为网格中的行和列?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55527159/

10-11 22:32
查看更多