package tictactoe;
import processing.core.PApplet;
public class TicTacToe extends PApplet {
int cols = 3;
int rows = 3;
int h;
int w;
public static void main(String[] args) {
PApplet.main("tictactoe.TicTacToe");
}
public void setup() {
//Organization for size of columns and rows
w = width / cols;
h = height / rows;
}
public void settings() {
size(300, 300);
}
public void draw() {
//Draw a tic-tac-toe grid
background(255);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
stroke (0);
noFill();
rect(i*w, j*h, w, h);
}
}
}
public class GridSquare{
//Game state (x's and o's), variables, etc.
public float x;
public float y;
public float w;
public float h;
public int state;
public void drawTurn() {
if (state == 0) {
ellipse(x+w/2,y+h/2,w,h);
}
if (state == 1) {
line(x,y,x+w,y+h);
line(x+w,y,x,y+h);
}
}
//Trouble understanding Grid Detection
void onClick(int clickedX, int clickedY, int turn) {
if (clickedX > x && clickedX < x + w && clickedY > y && clickedY < y + h) {
}
}
}
}
我无法理解网格检测if语句。由于在GridSquare类中定义参数x,y,w和h时都有分号,这是否意味着这些变量都等于0?在那种情况下,if语句是否不检查click x整数是否大于0,而click x小于0(x)+ 0(w)?还是我错了,代表代码的变量是先前在代码中定义的吗?
答案 0 :(得分:0)
如果您考虑同时进行多个游戏(GridSquares),每个游戏都有一个边框,并且每个游戏都绘制在同一面板上,则可能更容易理解。检查只是确定面板中的单击是否对该游戏有效(GridSquare)。如果同时进行多个游戏,则每个游戏的x和y值都不同;想象两个游戏并排放置,第一个可能会拥有(x = 0,y = 0),但是第二个游戏会在右边至少有300个单位(可能是像素),并给出值(x = 300,y = 0)。