好的,所以我正在创建一个Breakout游戏,我需要创建一个为每个积木创建矩形对象的方法,以便可以实现命中检测,我已经有了一个方法,可以像这样绘制积木:
public void drawBricks(Graphics g)
{
g.setColor(brickColor);
for(int i = 0; i<10; i++)
{
for(int a = 0; a<121; a+=30)
{
g.fillRect(x+(width*i)+(spacer*i), y +a, width, height);
// spacer = 10, x and y = 5, width = 50, height = 20, if you need this...
}
}
}
现在到我不能弄清楚的部分。我想用上面绘制的确切对应坐标创建矩形对象,并将它们添加到多维数组中,但是我需要y从5开始并每次为每行增加30。这是我到目前为止所拥有的:*另外,我不确定这是否确实可行,所以请告诉我您是否有其他想法可以做到。
public void setBricks()
{
for(int i= 0; i<10;i++)
{
for(int a=0; a<5; a++)
{
bricks[i][a] = new Rectangle(x+(width*i)+(spacer*i), y +a, width, height);
} // any ideas how to get each y ^ coordinate equal to the one above
} // i need the int variables to stay at 10 and 5 because of the size of the array.
}
好吧,如果您想y
从5
30开始并增加30,请使用5+a*30
:
public void setBricks()
{
for(int i= 0; i<10;i++)
{
for(int a=0; a<5; a++)
{
bricks[i][a] = new Rectangle(x+(width*i)+(spacer*i), 5 + a*30, width, height);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句