java - 蛇游戏程序-制作蛇的 body

标签 java multithreading arraylist

编辑:(已回答问题)


所以我有一个控制 x 和 y 并使其发生变化的线程。我的问题是,当蛇的 body 被放置在 GUI 中时,它会将它直接放在头顶而不是它后面的 12 个空间。我知道它为什么会这样,但我不确定如何解决这个问题。

据我了解,它先绘制 body ,然后绘制头部,但它使用相同的 x 和 y 空间......真正需要做的是它需要绘制头部,通过线程,然后绘制 body 部位,以便它在头部后面留出空间(因为头部会向前移动)......等等......我只是不知道该怎么做。

这是 Thread runner,我非常怀疑你是否真的需要它,但只是为了确定

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

  }

非常感谢大家的帮助

最佳答案

  1. 将头向前移动一个。
  2. 将 body 部分放在头部所在的位置。
  3. 删除最后一个 body 片段。

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

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

相关文章:

java - 按长度对字符串的 ArrayList 进行排序

Java Selenium 多表结构,遍历所有

java按静音区分割声音

Java代码从客户端机器打印PDF文件

C++ 控制台输入 block 所以我不能杀死线程

c++ - Visual Studio C++ 6.0 是否有线程安全队列类?

java - 无法知道此多线程程序的输出

java - Hibernate 可审计的多对多关系

java - 将更改(永久)保存在数组列表中?

java - java 对二维数组列表进行排序并获取索引