【问题标题】:Snake Game - Collision Error贪吃蛇游戏 - 碰撞错误
【发布时间】:2012-05-11 04:05:39
【问题描述】:

所以我正在制作一个snake game,但是如果我从一个方向移动到另一个方向非常快,那么它说我与身体发生了碰撞并结束了游戏(例如,如果我要向左,然后我向下击,然后再次非常快地向左或向下向右非常快,等等)

我已经尝试了一些方法。我更改了检查碰撞的方式,将其设为.intersects,而不是检查if (x == body[i][0] && y = body[i][1])。我还做了几个System.out.println() 看看那里是否有问题。我注意到有时其中一个值会重复(x 或 y 取决于方向),但我不知道为什么......我认为重复是它弄乱碰撞的原因,但我找不到它会让它重复的地方。

x 和 y 正在被一个线程更改。

这是我的代码的“绘图”部分。如果您需要任何其他 sn-ps 代码,请告诉我。

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (numOfFood == 1) {//Checks wether there is food on the GUI
        g.setColor(Color.BLUE);
        g.fillRect(foodX,foodY,12,12);
    }
    else {
        foodX = random.nextInt(103)*12; //Both this and the below line get a random x or y value to put on GUI for food placement
        foodY = random.nextInt(57)*12;

        numOfFood = 1;
    }
    Rectangle headRect = new Rectangle( x, y, 12, 12 ); //Actual rectangle of the head

    Rectangle foodRect = new Rectangle(foodX, foodY, 12, 12); //Food rectangle

    g.setColor(Color.RED);
    g.fillRect(x,y,12,12); //Draws head of Snake
    g.setColor(Color.WHITE);
    g.fillRect(x+2,y+2,8,8); //Draws a white square in the head of the snake

    for (int i = 0; i < n; ++i) { //Collision Checker
        Rectangle bodyRect = new Rectangle(body[i][0],body[i][1],12,12);
        if ( headRect.intersects(bodyRect)) {
            for (int j = 0; j < n; ++j) {
                body[j][0] = -1;
                body[j][1] = -1;
            }
            numOfFood = 1;
            n = 0;
            x = 624;
            y = 348;
            endGame = true;
        }
    }

    g.setColor(Color.RED);
    if (n > 0) { //Puts the snakes body behind the head
        for (int i = 0;i < n; ++i) {
            g.fillRect(body[i][0],body[i][1],12,12);
        }
    }

    for (int i = n-1;i >= 0; --i) { //Inserts the head at the first part of the array so that the body moves
        if (body[i][0] != -1 && body[i][1] != -1) {
            body[i+1][0] = body[i][0];
            body[i+1][1] = body[i][1];
        }

        if (i == 0) {
            body[i][0] = x;
            body[i][1] = y;
        }
    }

    if (headRect.intersects(foodRect)) { //If the food rectangle and the head rectangle intersect then the snake got the food.
      numOfFood = 0;
      n++;
    }
}

【问题讨论】:

  • 再一次,您在paintComponent 方法中拥有不属于它的程序逻辑。问题是您无法完全控制何时或什至何时调用paintComponent 方法。通过程序逻辑,我的意思是随机变量的任何使用、任何碰撞检查、此方法之外的任何变量的任何设置,例如 endGame、numOfFood 和 body 数组。你需要把这个逻辑放在你的游戏循环中,用它来设置类字段,调用 repaint,然后让paintComponent 只绘制图像就可以了。
  • 这和我在你上一个帖子里给你的建议是一样的,但我猜你不相信我?
  • @HovercraftFullOfEels 我搬了一些东西,我想我没有完全理解你让我做的一些事情。我将边框和背景颜色的东西移到了较早的区域,这很有帮助。它使它成为可以画蛇的地方。不过,我现在就照你说的做。再次感谢。
  • 顺便说一句,否决票不是我的,我不确定是谁,因为他们没有发表评论。
  • 好的,我让它完美地工作,非常感谢@HovercraftFullOfEels。我想我上次确实误会了你,我以为你只是说需要移动的边框和背景。我会说您是回答我问题的人,但您没有给出实际答案,您只是发表了评论。所以我说你发表了非常棒的评论。非常感谢!

标签: java user-interface collision


【解决方案1】:

你什么时候打电话给paintComponent?我怀疑你有一种方法可以让蛇定期向前移动,但paintComponent 负责让蛇变长。

您应该将碰撞和移动蛇移动到负责将头部朝蛇移动的方向移动的相同方法中。

否则,paintComponent 可能会在一次移动更新中被多次调用,这会导致数组中的 x 和 y 重复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多