碰撞问题
我在检查屏幕上绘制的对象的碰撞时遇到问题。例如:我目前正在制作一个蛇游戏,我必须让它检查蛇是否击中某个块对象(这使得蛇增加并在蛇击中/“吃”块时增加分数)。所以我做了一个函数来检查蛇和块对象之间的碰撞,但它不能正常工作(当从正上方用蛇移动到对象上时,它确实有效,但否则不起作用):
public boolean checkColision() {
if(SnakeObjs.get(0).x >= obj.x && SnakeObjs.get(0).x
<= obj.x+10 && SnakeObjs.get(0).y
>= obj.y && SnakeObjs.get(0).y <= obj.y+10) {
return true;
}else{
return false;
}
}
此函数检查碰撞。其中 SnakeObjs.get(0)
包含需要撞击块对象的蛇对象的 x 和 y 坐标,而 obj
包含块的 x 和 y 坐标用于检查碰撞的对象。
Paint 方法同时绘制块对象和蛇对象,如下所示:
public void paint(Graphics g) {
for(int i=0;i<SnakeObjs.size();i++) {
g.setColor(new Color(
(int)((double)Math.random()*200.0),
(int)((double)Math.random()*200.0),
(int)((double)Math.random()*200.0)));
g.fillRect(SnakeObjs.get(i).x, SnakeObjs.get(i).y, 10, 10);
g.setColor(Color.WHITE);
g.drawRect(SnakeObjs.get(i).x-1, SnakeObjs.get(i).y-1, 12, 12);
}
g.setColor(new Color(
(int)((double)Math.random()*200.0),
(int)((double)Math.random()*200.0),
(int)((double)Math.random()*200.0)));
g.fillRect(obj.x, obj.y, 10, 10);
g.setColor(Color.BLACK);
g.drawString("Score: "+score, 10, 10);
}
如果有人可以帮助我解决这个碰撞问题,那就太好了。
预先致谢,
最诚挚的问候, 斯凯夫。
I'm having a problem with checking the colision of objects painted on the screen. For example: I'm currently making a snake game and I have to make it check whether the snake is hitting a certain block object ( which makes the snake increase and increases the score when the snake hits/'eats' the block ). So I made a function to check the collision between the snake and the block object but it doesn't work correctly ( when moving over the object with the snake from right above, it does work, but otherwise it does not ):
public boolean checkColision() {
if(SnakeObjs.get(0).x >= obj.x && SnakeObjs.get(0).x
<= obj.x+10 && SnakeObjs.get(0).y
>= obj.y && SnakeObjs.get(0).y <= obj.y+10) {
return true;
}else{
return false;
}
}
This function checks the collision. Where SnakeObjs.get(0)
contains the x and y choords of the snake object that needs to hit the block object and the obj
contains the x and y coordinates of the block object to check the collision with.
The paint method, painting both the block object and the snake object, looks like this:
public void paint(Graphics g) {
for(int i=0;i<SnakeObjs.size();i++) {
g.setColor(new Color(
(int)((double)Math.random()*200.0),
(int)((double)Math.random()*200.0),
(int)((double)Math.random()*200.0)));
g.fillRect(SnakeObjs.get(i).x, SnakeObjs.get(i).y, 10, 10);
g.setColor(Color.WHITE);
g.drawRect(SnakeObjs.get(i).x-1, SnakeObjs.get(i).y-1, 12, 12);
}
g.setColor(new Color(
(int)((double)Math.random()*200.0),
(int)((double)Math.random()*200.0),
(int)((double)Math.random()*200.0)));
g.fillRect(obj.x, obj.y, 10, 10);
g.setColor(Color.BLACK);
g.drawString("Score: "+score, 10, 10);
}
If anyone can help me out on this collision problem it would be great.
Thanks in advance,
Best Regards,
Skyfe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误在于您将一个对象解释为矩形,而将另一个对象解释为点。您检查该点是否在矩形内,而不检查两个矩形是否相交。它从右侧和顶部工作。
我建议为蛇和物体保存一个矩形。添加一个方法 getBounds()。那么你可以使用 Rectangle 中的 intersect() 方法。您还可以使用此边界进行绘制操作。
The mistake is that you interpret one object as rectangle and the other as point. You check if the point is in the rectangle and not if the both rectangle intersect. That it work from right and from top.
I recommended to save an rectangle for the snake and the object. Ad a method getBounds(). then you can use the method intersect() from Rectangle. You can also use this bounds for your draw operation.