我需要在白色方块的交互窗口上创建一个10 * 10网格。然后我需要在中间画一个正方形,并让正方形跟随鼠标。一旦方块通过网格上的方框,更改该方框的颜色,然后根据该颜色移动方块:
○白色:方块向鼠标移动
○蓝色:方形远离鼠标
○绿色:方块随机移动
○红色:方格根本不移动
我的问题是,如何分配网格以便它知道方块何时通过其中一个索引?
public static void main(String[] args)
{
StdDraw.setScale(0, 100);
int[][] grid = new int[10][10];
int gridState = 0;
int antCoord = 0;
int mouseCoord = 0;
moveAnt(gridState, antCoord, mouseCoord);
while (true)
{
for (int i = 0; i <= 100; i++)
{
StdDraw.filledSquare(StdDraw.mouseX(), StdDraw.mouseY(), 1);
}
}
} // end of main
public static int moveAnt(int gridState, int antCoord, int mouseCoord)
{
double mouseMoveX = StdDraw.mouseX();
double mouseMoveY = StdDraw.mouseY();
if (StdDraw.mousePressed())
{
StdDraw.setPenColor(StdDraw.RED);
StdDraw.filledSquare(StdDraw.mouseX(), StdDraw.mouseY(), 1);
}
if (mouseMoveX == mouseMoveX+1)
{
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.filledSquare(StdDraw.mouseX(), StdDraw.mouseY(), 1);
}
return antCoord;
} // end of moveAnt