在处理(java)编码中使用数组或类对网格进行编号
使用处理,我试图重新创建蛇和梯子的游戏。网格完成了,“骰子”也完成了 我想数字网格像实际的蛇和梯子游戏。另外,当我移动棋子时,在“骰子”(屏幕底部显示的数字)和棋子移动的盒子数量之间会有一个链接在处理(java)编码中使用数组或类对网格进行编号,java,arrays,class,coding-style,processing,Java,Arrays,Class,Coding Style,Processing,使用处理,我试图重新创建蛇和梯子的游戏。网格完成了,“骰子”也完成了 我想数字网格像实际的蛇和梯子游戏。另外,当我移动棋子时,在“骰子”(屏幕底部显示的数字)和棋子移动的盒子数量之间会有一个链接 // Class that displays a single cell of a checker board class GameBoard { /*-----------------------properties------------------*/ float x; floa
// 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;
浮动y;
int-rVal;
国际gVal;
int bVal;
color squareColor;//单元格填充的颜色
int ccSize;//单元格的大小
/*-----------------------方法---------------------*/
//构造函数方法
公共游戏板(int-tempX、int-tempY、color-tempColor、int-tempSize){
x=tempX;
y=tempY;
squareColor=tempColor;
ccSize=tempSize;
}
//绘制ccColor=tempColor;单元格。这是公共的,因为其他对象需要
//能够告诉对象绘制自身。
公共空间显示(){
填充(方形颜色);
仰泳();
矩形模式(拐角);
rect(x,y,ccSize,ccSize);
}
}
类骰子{
浮冰;
浮冰;
浮动骰子大小;
字符串随机数=”;
公共骰子(float xNewDice、float yNewDice、float newdice size){
xDice=xNewDice;
yDice=yNewDice;
diceSize=newDiceSize;
randomNumber=“4”;
}
公共void displayDice(){
冲程(0);
填充(255);
矩形模式(中心);
rect(xDice、yDice、diceSize、diceSize);
}
公共编号(){
文本大小(50);
填充(0);
textAlign(居中,居中);
文本(随机数、xDice、yDice);
}
public void getRandomNumber(){
float referenceNumber=int(随机(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”;
}
}
公共void generateradom(){
文本大小(50);
填充(0);
textAlign(居中,居中);
文本(随机数、xDice、yDice);
float referenceNumber=int(随机(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”;
}
}
}
布尔rollingDice=false;
//创建游戏板对象数组
游戏板[][]方板;
随机骰子;
//游戏板的数量
int cellSize=100;
//安排
无效设置(){
仰泳();
大小(700800);
背景(0);
//初始化GameBoard对象的数组
//基于画布的大小和单个单元格的大小
squareBoard=新游戏板[宽度/单元大小][高度/单元大小];
随机骰子=新骰子(宽度/2750,80);
//初始化棋盘格
初始化棋盘();
}
//主牵引回路
作废提款(){
背景(44,44,44);
抽棋盘();
displayDice();
randomDice.displayNumber();
如果(滚动骰子){
randomDice.generateradom();
}
}
void mouseClicked(){
rollingDice=!rollingDice;
}
//绘制棋盘格图案
void initcheckboard(){
//指示填充白色还是黑色的标志
布尔白=真;
游戏板cCell;
//标志,指示是开始奇数列还是偶数列
//这用于确保列n+1始终以不同的填充开始
//比列n。
布尔值oddColumn=true;
//穿过x轴行走
用于(int x=0;x
我试图尽可能少地更改您的代码。至于移动和骰子,这是一个不同的主题,你没有代码在那里做它。。。我建议你先尝试一些东西,如果你做不到,再问另一个问题
至于蛇游戏中的数字,您需要先填充行,从下面开始,每行切换检查矩形的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();
}
}
}
//显示棋盘格的单个单元格的类
类游戏板{
/*-----------------------性质------------------*/
浮动x;
浮动y;
int-rVal;
国际gVal;
int bVal;
color squareColor;//单元格填充的颜色
int ccSize;//单元格的大小
整数指数;
/*-----------------------方法---------------------*/
//构造函数方法
公共游戏板(int-tempX、int-tempY、color-tempColor、int-tempSize、int-i){
x=tempX;
y=tempY;
squareColor=tempColor;
ccSize=tempSize;
指数=i;
}
//绘制ccColor=tempColor;单元格。这是公共的,因为其他对象需要
//能够