我正在开发一款类似于积木的游戏,我希望某些东西在被击中时从屏幕上消失。我已经完成了碰撞检测,代码如下:
Rectangle r1 = new Rectangle(x,y, 100,25);
Rectangle r2 = new Rectangle(120,15, 90,40);
Rectangle r3 = new Rectangle(10,15, 90,40);
Rectangle r4 = new Rectangle(230, 15, 90, 40);
Rectangle r5 = new Rectangle(xb, yb, 30,29);
g.setColor(Color.RED);
g.fillRect(r1.x, r1.y, r1.width, r1.height);
g.setColor(Color.YELLOW);
g.fillRect(r2.x, r2.y, r2.width, r2.height);
g.setColor(Color.YELLOW);
g.fillRect(r3.x, r3.y, r3.width, r3.height);
g.setColor(Color.BLUE);
g.fillRect(r5.x, r5.y, r5.width, r5.height);
g.setColor(Color.YELLOW);
g.fillRect(r4.x, r4.y, r4.width, r4.height);
if (r5.intersects(r1))
{
velxb = -velxb;
velyb = -velyb;
}
if (r5.intersects(r2))
{
g.drawString("Hello", 10, 10);
}
if (r5.intersects(r3))
{
g.drawString("Hello", 10, 10);
}
if (r5.intersects(r4))
{
g.drawString("Hello", 10, 10);
}
如您所见,我制作了几个矩形并完成了碰撞检测。但是,当r5
与r2
orr3
或相交时r4
,我希望它消失。