编辑:(已回答问题)

所以我有一个线程控制x和y并使其更改。我的问题是,当蛇形体被放置在GUI中时,它将蛇形体直接放在头部顶部而不是其后部12个空格。我知道为什么会这样做,但是我不确定如何解决此问题。

据我了解,它先绘制 body ,然后绘制头部,但是它使用相同的x和y空间...真正需要发生的是它需要绘制头部,穿过Thread,然后绘制 body 部位,因此它确实在头部后面留了一个空间(因为头部会向前移动)……依此类推……我只是不知道该怎么做。

这是线程运行器,我非常怀疑您是否真的需要,但是请确保

class ThreadRunnable extends Thread implements Runnable {

  private boolean allDone = false;
  private boolean RIGHT;
  private boolean LEFT;
  private boolean UP;
  private boolean DOWN;
  public ThreadRunnable (boolean up, boolean right, boolean down, boolean left){

    UP = up;
    RIGHT = right;
    DOWN = down;
    LEFT = left;

  }
  public void run() {

    if(UP == true) {
      try {
        while(UP) {
          if (y <= yBase && y >= 0) {
            y = y - 12;
          }
          else {
            y = yBase;
          }
          Thread.sleep(40);
          repaint();


          if (allDone) {
            return;
          }
        }
      }
      catch (InterruptedException exception) {
      }
      finally {
      }
    }

    if(RIGHT == true) {
      try {
        while(RIGHT) {
          if (x <= xBase && x >= 0) {
            x = x+12;
          }
          else {
            x = 0;
          }
          Thread.sleep(40);
          repaint();


          if(allDone) {
            return;
          }
        }
      }
      catch (InterruptedException exception) {
      }
      finally {
      }
    }

    if(DOWN == true) {
      try {
        while(DOWN) {
          if (y <= yBase && y >= 0) {
            y = y+12;
          }
          else {
            y = 0;
          }
          Thread.sleep(40);
          repaint();


          if (allDone) {
            return;
          }
        }
      }
      catch (InterruptedException exception) {
      }
      finally {
      }
    }

    if(LEFT == true) {
      try {
        while(LEFT) {
          if (x <= xBase && x >= 0) {
            x = x-12;
          }
          else {
            x = xBase;
          }
          Thread.sleep(40);
          repaint();


          if (allDone) {
            return;
          }
        }
      }
      catch (InterruptedException exception) {
      }
      finally {
      }
    }
  }
}

这是代码的“绘图”部分
public void paintComponent(Graphics g) {
  super.paintComponent(g);
  setBorder(BorderFactory.createLineBorder(Color.WHITE));

  this.setBackground(Color.black);

  if (numOfFood == 1) {
    g.setColor(Color.BLUE);
    g.fillRect(foodX,foodY,12,12); //Paints the food
  }
  else {
    foodX = random.nextInt(105)*12; //Random x for food position after snake eats
    foodY = random.nextInt(59)*12; //Random y for food position after snake eats

    numOfFood = 1;
  }
  Rectangle headRect = new Rectangle( x, y, 12, 12 ); //The head (not painted)
  Rectangle foodRect = new Rectangle(foodX, foodY, 12, 12); //The food (not painted)


  if ( body.size() >= 0) {
    body.add(new BodyPart( x, y, 12, 12));
    body = new ArrayList<BodyPart>( body.subList( body.size() - n, body.size() ) );
    g.setColor(Color.RED);
    for ( int i = body.size() - 1; i >= body.size() - (n-1); i--  ) {
      g.fillRect( body.get( i ).getX(), body.get( i ).getY(), body.get( i ).getWidth(), body.get( i ).getHeight() ); //This is calling a class that will get these parts of the array list (x,y,width,height)
    }
  }

  g.setColor(Color.RED);
  g.fillRect(x,y,12,12); //Draws head
  g.setColor(Color.WHITE);
  g.fillRect(x+2,y+2,8,8); //This is a white square in the middle of the head to show that it is the head


  if (headRect.intersects(foodRect)) {

    numOfFood = 0;
    n++;


  }
}

编辑:问题在下面回答

所需要做的就是移动
setBorder(BorderFactory.createLineBorder(Color.WHITE));

this.setBackground(Color.black);
到代码的较早部分,而不是那里的绘图方法中...

我也将ArrayList更改为普通Array,只是将其头部位置插入Array的开头,然后在下一次(头部移动之后)进行打印。
  g.setColor(Color.RED);

  //Prints the body parts
  if (n > 0) {
    for (int i = 0;i < n; ++i) {
      g.fillRect(body[i][0],body[i][1],12,12);
    }
  }

  //Inserts the head location
  for (int i = n-1;i >= 0; --i) {

    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;
    }

  }

非常感谢您的所有帮助

最佳答案

  • 将头向前移动一个。
  • 将 body 部位放在头部所在的位置。
  • 删除最后一个正文段。

  • 其他 body 部分都不需要移动。

    关于java - 蛇游戏程序-制作蛇的 body ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7541660/

    10-12 03:42
    查看更多