java - I am having trouble understanding constructor parameter values relating to coordinate detection
问题描述
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) {
}
}
}
}
I am having trouble understanding the grid detection if statement. Since the parameters x, y, w, and h all had a semi colon when being defined in the GridSquare class, wouldn't that mean that those variables all equal 0? In that case, wouldn't the if statement be checking if the click x integer is greater than 0, and clicked x is less than 0 (x) + 0 (w)? Or am I wrong, and are the variables representing integers previously defined in the code?
解决方案
如果您考虑同时进行多个游戏(GridSquares),每个游戏周围都有一个边框,并且每个游戏都绘制在同一个面板上,这可能更容易理解。检查只是确定面板中的点击是否对该游戏有效(GridSquare)。一次玩多个游戏,每个游戏的 x 和 y 值都会不同;想象两个游戏,并排,第一个可能有 (x=0, y=0),但第二个游戏将在右侧至少 300 个单位(可能是像素),给出值 (x=300, y =0)。
推荐阅读
- postgresql - 使用带有时区的 generate_series
- python - 实现一个基本的神经网络来预测 y = 2x
- python-3.x - 如何有效地绘制 matplotlib 的这段代码
- excel - 根据月份和客户将文件保存到文件夹的宏
- cassandra - 卡桑德拉:无法打开 /var/lib/cassandra/hints
- c# - CS0173 在具有不同返回类型的三元组上编译错误
- json - 遍历json并将结果解析为scala中的变量
- flutter - Flutter Native SplashScreens 后台进程
- .htaccess - 如何使用 .htaccess 重定向我的页面
- django - 如何在模型的两个字段中保存一个表单的值?