简体   繁体   English

蛇游戏 - 无法让蛇身跟随蛇头

[英]Snake Game - Can't get snake body to follow snake head

I have been working on a snake game using sfml and c++ but I am having trouble getting the tail of my snake to follow the snake head which is defined as Snake[0] in my code below.我一直在使用 sfml 和 C++ 开发蛇游戏,但是我无法让蛇的尾巴跟随下面的代码中定义为 Snake[0] 的蛇头。 I have implemented code that I think should work but doesn't which is the following我已经实现了我认为应该工作但不是以下的代码

    for (int i = 1; i < snakeSize; i++)
    {
        snakePartX[i] = snakePartX[i-1];
        snakePartY[i] = snakePartY[i-1];
    }

The way I understand it (and I am probably incredibly wrong and would appreciate if anyone could point out what is actually happening here) is that, that piece of code should set the value of a snake body part to the position of where the previous body part WAS located so that when the program loops they are set to follow the snake as it travels.我理解它的方式(我可能是非常错误的,如果有人能指出这里实际发生的事情,我会很感激)是,那段代码应该将蛇身体部位的值设置为前一个身体部位的位置部分被定位,以便当程序循环时,它们被设置为在蛇行进时跟随它。

What actually does happen though is that when the snake eats an apple the snake will gain 1 block to its tail, but will not grow any farther.但实际发生的是,当蛇吃苹果时,蛇的尾巴会增加 1 格,但不会长得更远。

In the classic snake game, the snake is made up of segments.在经典的蛇游戏中,蛇是由段组成的。 Each segment contains the coordinate of the segment (among other attributes).每个线段都包含线段的坐标(以及其他属性)。

Specifically, the snake is a container of segments.具体来说,snake 是一个段的容器 The container is your choice, but I recommend a queue.容器是您的选择,但我建议排队。

With the queue, a new head segment is added into the queue and a tail segment removed.有了队列,一个新的头段被添加到队列中,并删除了一个尾段。

Here's a code fragment to help:这是一个可以提供帮助的代码片段:

class Snake_Segment
{
  public:
    int column;
    int row;
};

typedef std::deque<Snake_Segment> Segment_Container;

int main(void)
{
  Segment_Container snake_body;
  Snake_Segment head;
  head.row(25);
  head.column(30);
  snake_body.push_back(head);
  Snake_Segment tail = head;
  ++tail.column;
  snake_body.push_back(tail);
  return EXIT_SUCCESS;
}

Why do index zero here?为什么这里索引为零? Doesn't it get done as part of the for loop too?它不是也作为 for 循环的一部分完成的吗?

        Snake[0].setPosition(snakePartX[0], snakePartY[0]);

        for(int i = 0; i < snakeSize; i++)
        {
            Snake[i].setPosition(snakePartX[i], snakePartY[i]);
        }

I'm concerned about this section :我很关心这个部分:

        snakeSize += 1;
    }

    Apple.setPosition(applePos.x, applePos.y);
    window.draw(Apple);

    for(int i = 0; i < snakeSize; i++)
    {
        window.draw(Snake[i]);
    }

Make sure you don't exceed 20 as your parts are a simple array and you will run into problems once you get 20 parts.确保不要超过 20 个,因为您的零件是一个简单的数组,一旦获得 20 个零件,您就会遇到问题。 More importantly, you never set the value for Snake[i] for the new snakeSize.更重要的是,您永远不会为新的蛇大小设置 Snake[i] 的值。 So when you get an apple, you'll use uninitialized memory to draw one snake part.所以当你得到一个苹果时,你将使用未初始化的内存来绘制一个蛇的部分。

Another potential problem :另一个潜在问题:

    //Clock to set snake speed
    ElapsedTime = clock.getElapsedTime();
    if(ElapsedTime.asSeconds() >= snakeSpeed)
    {

        for(int i = 1; i < snakeSize; i++)
        {
            snakePartX[i] = snakePartX[i - 1];
            snakePartY[i] = snakePartY[i - 1];
        }

You allow keyboard input to move the snake head along, but you only move the body when the timer expires.您允许键盘输入移动蛇头,但您只能在计时器到期时移动身体。 So you head can slide way away from the body and the body doesn't move unless the timer goes off.所以你的头可以从身体滑开,除非计时器响起,否则身体不会移动。

I printed the field of snake game as a Field[x][y] .我将蛇游戏的领域打印为Field[x][y] So by below code the body of snake following the head.所以通过下面的代码,蛇的身体跟随头部。 Also, optionMoveNumber==0 in other function checked if Field[x][y]==food or not which means did the snake eat the food of not.此外,其他函数中的optionMoveNumber==0检查是否Field[x][y]==food或 not,这意味着蛇是否吃了 not 的食物。

int m=1;
void Snake::Movement(int x, int y){
        movey[0]=firstTaily; //first place
        movex[0]=firstTailx; //first place
        movex[m]=x;
        movey[m]=y; 
        for(int n=m; n>=m-ntail; n--){
        Field[movey[n]][movex[n]]=Field[movey[n-1]][movex[n-1]];
    }
    if(optionMoveNumber==0){
        Field[movey[m-1]][movex[m-1]]=tail;
    }
    else{
        Field[movey[m-ntail-1]][movex[m-ntail-1]]=space;
    }
    m++;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM