简体   繁体   English

c++ 蛇游戏,生成的第一个蛇段不会跟随蛇

[英]c++ snake game, first snake segment generated wont follow snake

I'm a beginner in c++ so I started making a snake game in c++.我是 c++ 的初学者,所以我开始在 c++ 中制作蛇游戏。 But just as I complete the program i run into a problem and i just cant find the fix.但就在我完成程序时,我遇到了一个问题,我只是找不到修复程序。 When the snake picks up the fruit, it generates a segment, but the first segment doesn't follow the snake.当蛇捡起水果时,它会生成一个段,但第一个段没有跟随蛇。 The next segments do follow the snake.接下来的部分确实跟随蛇。 Any help to the solution is really appreciated.非常感谢对解决方案的任何帮助。

#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

bool gameOver;

const int width = 40;
const int height = 40;

int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100]; // snail coordinates
int nTail;

enum eDirection {STOP = 0, LEFT,RIGHT,UP,DOWN}; //controls for snake
eDirection dir;


//set fruit position and snake position
void setup(){
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = width / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
}

//draw the map
void draw(){
    system("cls");

    for(int i = 0; i < width+2; i++)
        cout << "#";
    std::cout << endl;

    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
            if (j == 0){
                std::cout << "|";
            }
            if(i == y && j == x){
                std::cout << "O";
            
            } else if (i == fruitY && j == fruitX) {
                std::cout << "F";
            } else {
                bool segment = false;
                for(int l = 0; l < nTail; l++){
                    if (tailX[l] == j && tailY[l] == i){
                        std::cout << "o";
                        segment = true;
                    };
                }
                
                if(!segment){
                    std::cout << " ";
                }

            }
            if(j == width-1) {
                std::cout << "|";
            }

        }
        std::cout << endl;
    }

    for(int i = 0; i < width+2; i++)
        cout << "#";
    std::cout << endl;

    //display score
    std::cout << " Score: " << score << endl;

}

//set snake direction
void movement(){
    if(_kbhit()){
        switch(_getch()){
            case 'a':
                dir = LEFT;
                break;

            case 'w':
                dir = UP;
                break;

            case 's':
                dir = DOWN;
                break;

            case 'd':
                dir = RIGHT;
                break;

            case 'x':
                gameOver = true;
                break;

        }
    }
}

//update snake direction
void movementUpdate(){

    // make tail and coordinate tail
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;

    for(int i = 0; i < nTail; i++){
        prev2X = tailX[i];
        prev2Y = tailY[i];

        tailX[i] = prevX;
        tailY[i] = prevY;

        prevX = prev2X;
        prevY = prev2Y;
    }

    switch(dir){
        case STOP:
            break;
        
        case LEFT:
            x--;
            break;
        
        case RIGHT:
            x++;
            break;

        case UP:
            y--;
            break;
        
        case DOWN:
            y++;
            break;

        default:
            break;
    }
    //allow player to move through left and right walls
    if(x >= width){
        x = 0;
    } else if ( x < 0) {
        x = width -1;
    }

    if(y >= height){
        y = 0;
    } else if ( y < 0) {
        y = height -1;
    }


    // check for game over
    // if(x > width || x < 0 || y > height || y < 0){
    //     gameOver = true;
    // }

    //check if snake hit itself
    for(int i = 0; i < nTail; i++){
        if(tailX[i] == x && tailY[i] == y){
            gameOver = true;
        }
    }

    //check if snake takes fruit
    if(x == fruitX && y == fruitY){
        nTail++;
        score += 10;
        fruitX = rand() % width;
        fruitY = rand() % height;

    }
}

// initialize game
int main(){
    setup();

    while(!gameOver){
        draw();
        movement();
        movementUpdate();
        //sleep(10);
    }

    return 0;
}

In the first for loop in movement update where you update the tail.在移动更新的第一个 for 循环中更新尾部。 If you think about in the case of nTail = 1 you are basically only setting tailX[0] = tailX[0] and tailY[0] = tailY[0].如果您考虑 nTail = 1 的情况,您基本上只是设置 tailX[0] = tailX[0] 和 tailY[0] = tailY[0]。

Simplified for loop when nTail = 1: nTail = 1 时的简化 for 循环:

// make tail and coordinate tail
int prevX = tailX[0];
int prev2X;
tailX[0] = x;
{
    prev2X = tailX[0];

    tailX[0] = prevX; // prevX = tailX[0]

    prevX = prev2X;
}

This is a working for loop:这是一个工作循环:

// make tail and coordinate tail
int nextX = x;
int nextY = y;
int tX, tY;
for(int i = 0; i < nTail; i++){
    tX = tailX[i];
    tY = tailY[i];
    tailX[i] = nextX;
    tailY[i] = nextY;
    nextX = tX;
    nextY = tY;
}

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

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