贪吃蛇游戏程序-制作蛇的身体

发布于 2024-12-06 08:15:08 字数 4446 浏览 1 评论 0原文

编辑:(问题已回答)


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

据我了解,它先绘制身体,然后绘制头部,但它使用相同的 x 和 y 空间...真正需要发生的是它需要绘制头部,穿过线程,然后绘制身体部位,这样它在头部后面的一个空间(因为头部会向前移动)......等等......我只是不知道该怎么做。

这是线程运行器,我非常怀疑您是否真的需要,但只是为了确定

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 更改为普通数组,只是将其头部位置插入到数组的开头,然后在下次打印(在头部移动之后)

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

  }

非常感谢您的所有帮助

EDIT: (QUESTION ANSWERED)


So I have a Thread controlling x and y and making it change. My problem is that when the snakes body gets put up in the GUI it puts it directly on top of the head instead of 12 spaces behind it. I know why it's doing it but I am not sure how to fix this problem.

From what I understand, it's drawing the body and then the head but it's using the same x and y spaces...what really needs to happen is that it needs to draw the head, go through the Thread, then draw the body parts so that it does it a space behind the head (because the head will have moved forward)...so on and so forth... I just can't figure out how to do it.

Here is the Thread runner which i highly doubt you will really need but just to be sure

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

Here is the "Drawing" part of the code

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


  }
}

EDIT: QUESTION ANSWERED BELOW


All that was required was to move the

setBorder(BorderFactory.createLineBorder(Color.WHITE));

and the

this.setBackground(Color.black);

to an earlier part of the code and not in the drawing method there...

I also changed my ArrayList into a normal Array and just made it insert the head location into the beginning of the Array and then made it print the next time around (after the head had moved)

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

  }

Thank you so much for all the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2024-12-13 08:15:08
  1. 将头向前移一处。
  2. 将身体部分放在头部所在的位置。
  3. 擦除最后一个身体部分。

身体的其他部分都不需要移动。

  1. Move the head forward one.
  2. Put a body segment where the head was.
  3. Erase the last body segment.

None of the other body segments need move.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文