java - 在处理(java)编码中使用数组或类的数字网格

标签 java arrays class coding-style processing

使用处理我正在尝试重新创建蛇和梯子游戏。网格完成了,“骰子”也完成了。

我想像实际的蛇梯子游戏一样对网格进行编号。并且还添加一种方式,当我移动棋子时,“骰子”(显示在屏幕底部的数字)和棋子将移动的盒子数量之间会有一个链接。

   // Class that displays a single cell of a checker board
class GameBoard {
  /*-----------------------properties------------------*/
  float x;
  float y;
  int rVal;
  int gVal;
  int bVal;
  color squareColor; //color of cell fill
  int ccSize; //size of cell


  /*-----------------------methods---------------------*/
  // constructor method
  public GameBoard(int tempX, int tempY, color tempColor, int tempSize) {
    x = tempX;
    y = tempY;
    squareColor = tempColor; 
    ccSize = tempSize;
  }

  // draws    ccColor = tempColor; the cell. This is public because other objects will need to 
  // be able to tell the object to draw itself.
  public void display() {
    fill(squareColor);
    noStroke();
    rectMode(CORNER);
    rect(x, y, ccSize, ccSize);
  }
}

class Dice {  
  float xDice;
  float yDice;
  float diceSize;
  String randomNumber = "";

  public Dice (float xNewDice, float yNewDice, float newDiceSize) {
    xDice = xNewDice;
    yDice = yNewDice;
    diceSize = newDiceSize;
    randomNumber = "4";
  }
  public void displayDice() {
    stroke(0);
    fill(255);
    rectMode(CENTER);
    rect(xDice, yDice, diceSize, diceSize);
  }

  public void displayNumber() {

    textSize(50); 
    fill(0);
    textAlign(CENTER, CENTER);
    text(randomNumber, xDice, yDice);
  }
  public void getRandomNumber() {
    float referenceNumber = int(random(1, 7));

    if (referenceNumber == 1) {
      randomNumber = "1";
    }

    if (referenceNumber == 2) {
      randomNumber = "2";
    }

    if (referenceNumber == 3) {
      randomNumber = "3";
    }

    if (referenceNumber == 4) {
      randomNumber = "4";
    }

    if (referenceNumber == 5) {
      randomNumber = "5";
    }

    if (referenceNumber == 6) {
      randomNumber = "6";
    }
  }

  public void generateRandom() {

    textSize(50); 
    fill(0);
    textAlign(CENTER, CENTER);
    text(randomNumber, xDice, yDice);

    float referenceNumber = int(random(1, 7));

    if (referenceNumber == 1) {
      randomNumber = "1";
    }

    if (referenceNumber == 2) {
      randomNumber = "2";
    }

    if (referenceNumber == 3) {
      randomNumber = "3";
    }

    if (referenceNumber == 4) {
      randomNumber = "4";
    }

    if (referenceNumber == 5) {
      randomNumber = "5";
    }

    if (referenceNumber == 6) {
      randomNumber = "6";
    }
  }
}


boolean rollingDice = false;

//create an array of GameBoard objects
GameBoard[] [] squareBoard;
Dice randomDice;
//number of GameBoards
int cellSize = 100;



// set things up
void setup() {
   noStroke();
  size(700, 800);
  background(0);
  //initialize the array of GameBoard objects
  //base it on the size of the canvas and the size of the individual cells
  squareBoard = new GameBoard[width/cellSize][height/cellSize];
  randomDice = new Dice (width/2, 750, 80);
  //initialize checker board
  initCheckerboard();
}

// main drawing loop
void draw() {
  background(44, 44, 44);
  drawCheckerboard();
  randomDice.displayDice();
  randomDice.displayNumber();
  if(rollingDice) {
    randomDice.generateRandom();
  }
}

void mouseClicked() {
  rollingDice = !rollingDice;
}

// draws a checkerboard pattern 
void initCheckerboard() {
  // flag to indicate whether to fill with white or black
  boolean white = true; 
  GameBoard cCell;

  // flag to indicate whether we are starting an odd or even column
  // this is used to ensure that column n + 1 always starts with a different fill
  // than column n.
  boolean oddColumn = true;

  // walk across the x-axis
  for (int x=0; x < width; x = x+cellSize) {
    // walk down the y-axis
    for (int y=0; y < height-100; y = y+cellSize) {
      // create the GameBoard
      if (white) {
        cCell = new GameBoard(x, y, color(64, 64, 64), cellSize);
      } 
      else {
        cCell = new GameBoard(x, y, color(44, 44, 44), cellSize);
      }
      squareBoard[x/cellSize][y/cellSize] = cCell;
      //invert the fill color for next cell
      white = !white;
    }

    // make sure that each successive column starts with a different fill
    white = !oddColumn;

    // flip the flag that tells us whether we're starting an even or odd column
    oddColumn = !oddColumn;
  }
}

// draws a checkerboard pattern 
void drawCheckerboard() {
   noStroke();
  // walk across the x-axis
  for (int x=0; x < width; x = x+cellSize) {
    // walk down the y-axis
    for (int y=0; y < height-100; y = y+cellSize) {
      // draw the GameBoard
      squareBoard[x/cellSize][y/cellSize].display();
    }
  }
}

最佳答案

我尝试尽可能少地更改您的代码。至于运动和骰子,那是一个不同的主题,你没有代码可以做到这一点...我建议先尝试一些事情,如果你不能得到它问另一个问题!

对于贪吃蛇游戏中的数字,您需要先填充行,从下面开始,每行交换检查矩形的x:

// Class that displays a single cell of a checker board
class GameBoard {
  /*-----------------------properties------------------*/
  float x;
  float y;
  int rVal;
  int gVal;
  int bVal;
  color squareColor; //color of cell fill
  int ccSize; //size of cell
  int index;


  /*-----------------------methods---------------------*/
  // constructor method
  public GameBoard(int tempX, int tempY, color tempColor, int tempSize, int i) {
    x = tempX;
    y = tempY;
    squareColor = tempColor; 
    ccSize = tempSize;
    index = i;
  }

  // draws    ccColor = tempColor; the cell. This is public because other objects will need to 
  // be able to tell the object to draw itself.
  public void display() {
    fill(squareColor);
    noStroke();
    rectMode(CORNER);
    rect(x, y, ccSize, ccSize);
    fill(255);
    text(String.valueOf(index), x-ccSize*0.5, y-ccSize*0.5, 200, 200);
  }
}

class Dice {  
  float xDice;
  float yDice;
  float diceSize;
  String randomNumber = "";

  public Dice (float xNewDice, float yNewDice, float newDiceSize) {
    xDice = xNewDice;
    yDice = yNewDice;
    diceSize = newDiceSize;
    randomNumber = "4";
  }
  public void displayDice() {
    stroke(0);
    fill(255);
    rectMode(CENTER);
    rect(xDice, yDice, diceSize, diceSize);
  }

  public void displayNumber() {

    textSize(50); 
    fill(0);
    textAlign(CENTER, CENTER);
    text(randomNumber, xDice, yDice);
  }
  public void getRandomNumber() {
    float referenceNumber = int(random(1, 7));

    if (referenceNumber == 1) {
      randomNumber = "1";
    }

    if (referenceNumber == 2) {
      randomNumber = "2";
    }

    if (referenceNumber == 3) {
      randomNumber = "3";
    }

    if (referenceNumber == 4) {
      randomNumber = "4";
    }

    if (referenceNumber == 5) {
      randomNumber = "5";
    }

    if (referenceNumber == 6) {
      randomNumber = "6";
    }
  }

  public void generateRandom() {

    textSize(50); 
    fill(0);
    textAlign(CENTER, CENTER);
    text(randomNumber, xDice, yDice);

    float referenceNumber = int(random(1, 7));

    if (referenceNumber == 1) {
      randomNumber = "1";
    }

    if (referenceNumber == 2) {
      randomNumber = "2";
    }

    if (referenceNumber == 3) {
      randomNumber = "3";
    }

    if (referenceNumber == 4) {
      randomNumber = "4";
    }

    if (referenceNumber == 5) {
      randomNumber = "5";
    }

    if (referenceNumber == 6) {
      randomNumber = "6";
    }
  }
}


boolean rollingDice = false;

//create an array of GameBoard objects
GameBoard[] [] squareBoard;
Dice randomDice;
//number of GameBoards
int cellSize = 100;



// set things up
void setup() {
  noStroke();
  size(700, 800);
  background(0);
  //initialize the array of GameBoard objects
  //base it on the size of the canvas and the size of the individual cells
  squareBoard = new GameBoard[width/cellSize][height/cellSize];
  randomDice = new Dice (width/2, 750, 80);
  //initialize checker board
  initCheckerboard();
}

// main drawing loop
void draw() {
  background(44, 44, 44);
  drawCheckerboard();
  randomDice.displayDice();
  randomDice.displayNumber();
  if (rollingDice) {
    randomDice.generateRandom();
  }
}

void mouseClicked() {
  rollingDice = !rollingDice;
}

// draws a checkerboard pattern 
void initCheckerboard() {
  // flag to indicate whether to fill with white or black
  boolean white = true; 
  GameBoard cCell;

  // flag to indicate whether we are starting an odd or even column
  // this is used to ensure that column n + 1 always starts with a different fill
  // than column n.
  boolean oddColumn = true;
  int index = 0;
  boolean switcher = false;
  // walk down the y-axis
  for (int y=height-100-cellSize; y >= 0 ; y = y-cellSize) {
    // walk across the x-axis
    for (int x=0; x < width; x = x+cellSize) {
      // create the GameBoard
      int nx = x;
      if(switcher) nx = width - cellSize - x;
      if (white) {
        cCell = new GameBoard(nx, y, color(64, 64, 64), cellSize, index);
      } 
      else {
        cCell = new GameBoard(nx, y, color(44, 44, 44), cellSize, index);
      }
      println(y);
      squareBoard[x/cellSize][y/cellSize] = cCell;
      //invert the fill color for next cell
      white = !white;
      index++;
    }
    switcher = !switcher;

    // make sure that each successive column starts with a different fill
    white = !oddColumn;

    // flip the flag that tells us whether we're starting an even or odd column
    oddColumn = !oddColumn;
  }
}

// draws a checkerboard pattern 
void drawCheckerboard() {
  noStroke();
  // walk across the x-axis
  for (int x=0; x < width; x = x+cellSize) {
    // walk down the y-axis
    for (int y=0; y < height-100; y = y+cellSize) {
      // draw the GameBoard
      squareBoard[x/cellSize][y/cellSize].display();
    }
  }
}

关于java - 在处理(java)编码中使用数组或类的数字网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20136953/

相关文章:

c++ - C++ 包装类的含义是什么?

python - 创建可变数量的类实例

java.lang.UnsupportedOperationException 在 javax.faces.context.FacesContext.isReleased(FacesContext.java :609)

java - 我了解 Unix 和 C++,但不懂 RPM 或 Java,如何运行 Java Hello World?

java - 为什么我不能使用这个泛型作为看似相同边界的参数?

c - 在 C 中操作数组

Ruby:从模块中重新打开类

java - 无法开始 Activity 。由 : android. view.InflateException 引起:

arrays - Kotlin 数组中的 indexOf

c++ - 在二维数组中查找最小值和最大值?