贪吃蛇游戏程序 - 让蛇的身体 [英] Snake Game Program - Making the Body of the Snake

查看:227
本文介绍了贪吃蛇游戏程序 - 让蛇的身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:(回答过的问题)


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

据我了解,它的绘图身体,然后头,但它使用相同的X和Y空间......真正需要做的是,它需要借鉴的头部,经过线程,然后绘制身体部位,以便它在它后面的头部空间(因为头会是向前发展)......等等等等......我只是无法弄清楚如何做到这一点。

下面是线程亚军,我很怀疑你真的需要,但只是要确定

 类ThreadRunnable继承Thread实现Runnable {  私人布尔allDone = FALSE;
  私人布尔权;
  私人布尔LEFT;
  私人布尔UP;
  私人布尔DOWN;
  公共ThreadRunnable(布尔起来,布尔权,布尔下来,布尔左){    UP =起来;
    RIGHT =权利;
    DOWN =下;
    LEFT =左;  }
  公共无效的run(){    如果(UP ==真){
      尝试{
        而(UP){
          如果(γ&下; = yBase&放大器;&放大器y与其所连接; = 0){
            Y = Y - 12;
          }
          其他{
            Y = yBase;
          }
          视频下载(40);
          重绘();
          如果(allDone){
            返回;
          }
        }
      }
      赶上(InterruptedException的除外){
      }
      最后{
      }
    }    如果(RIGHT ==真){
      尝试{
        而(右){
          如果(X LT =的xBase&放大器;&放大器; X> = 0){
            X = X + 12;
          }
          其他{
            X = 0;
          }
          视频下载(40);
          重绘();
          如果(allDone){
            返回;
          }
        }
      }
      赶上(InterruptedException的除外){
      }
      最后{
      }
    }    如果(DOWN ==真){
      尝试{
        而(DOWN){
          如果(γ&下; = yBase&放大器;&放大器y与其所连接; = 0){
            Y = Y + 12;
          }
          其他{
            Y = 0;
          }
          视频下载(40);
          重绘();
          如果(allDone){
            返回;
          }
        }
      }
      赶上(InterruptedException的除外){
      }
      最后{
      }
    }    如果(LEFT ==真){
      尝试{
        而(左){
          如果(X LT =的xBase&放大器;&放大器; X> = 0){
            X = X-12;
          }
          其他{
            X =的xBase;
          }
          视频下载(40);
          重绘();
          如果(allDone){
            返回;
          }
        }
      }
      赶上(InterruptedException的除外){
      }
      最后{
      }
    }
  }
}

下面是code的绘图部分

 公共无效的paintComponent(图形G){
  super.paintComponent方法(G);
  setBorder(BorderFactory.createLineBorder(Color.WHITE));  this.setBackground(Color.black);  如果(numOfFood == 1){
    g.setColor(Color.BLUE);
    g.fillRect(foodX,FOODY,12,12); //绘制的食物
  }
  其他{
    foodX = random.nextInt(105)* 12; //随机x对于蛇吃的食物后位置
    FOODY = random.nextInt(59)* 12; //随机y代表蛇吃的食物后位置    numOfFood = 1;
  }
  矩形headRect =新的Rectangle(X,Y,12,12); //头(不涂)
  矩形foodRect =新的Rectangle(foodX,FOODY,12,12); //食品(不涂)
  如果(body.size()> = 0){
    body.add(新BodyPart的(X,Y,12,12));
    身体=新的ArrayList<&BodyPart的GT;(body.subList(body.size() - N,body.size()));
    g.setColor(Color.RED);
    对(INT I = body.size() - 1; I&GT = body.size() - (N-1);我 - ){
      g.fillRect(body.get(ⅰ).getX(),body.get(ⅰ).getY(),body.get(ⅰ).getWidth(),body.get(ⅰ).getHeight()); //这是调用类,将得到的数组列表的这些部件(X,Y,宽,高)
    }
  }  g.setColor(Color.RED);
  g.fillRect(X,Y​​,12,12); //绘制头
  g.setColor(Color.WHITE);
  g.fillRect(X + 2,Y + 2,8,8); //这是在头部的中间白色正方​​形,以表明它是头
  如果(headRect.intersects(foodRect)){    numOfFood = 0;
    Ñ​​++;
  }
}


编辑:回答过的问题在


所有这一切都移到需要的是在

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

this.setBackground(Color.black);

到code的较早部分,而不是在拉伸方法有...

我也改变了我的ArrayList到一个正常的阵列,只是使它插入头部位置到数组的开头,然后使其周围打印下一次(后头部已转移)

  g.setColor(Color.RED);  //打印的身体部位
  如果(N 0){
    的for(int i = 0; I< N ++我){
      g.fillRect(体[Ⅰ] [0],主体[I] [1],12,12);
    }
  }  //插入头部位置
  的for(int i = N-1; I> = 0; --i){    体第[i + 1] [0] =身体[I] [0];
    体第[i + 1] [1] =身体[I] [1];    如果(我== 0){
      体[Ⅰ] [0] = X;
      身体[I] [1] = Y;
    }  }

感谢你这么多所有帮助


解决方案

  1. 将头向前。

  2. 将一个主体段在头部了。

  3. 删除最后一个主体段。

其他体节无需要移动。

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

解决方案

  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.

这篇关于贪吃蛇游戏程序 - 让蛇的身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆