用C++easyx写了一个贪吃蛇游戏 蛇只能是直线的 弯不了 可以改变方向 但蛇一整条都是直的 不弯曲

问题遇到的现象和发生背景

贪吃蛇游戏 蛇的移动分为蛇头和蛇尾两个函数 蛇的每一节都是一个结构体包含坐标xy及贴图 所有节构成一个数组 s[0]为蛇头 其他元素为蛇尾 用WASD控制蛇头方向 蛇的其他部分跟着蛇头走 以上就是大致思路 但写下来发现蛇尾不会跟着蛇头走 而是每一节蛇组成一条直的蛇 不会弯的蛇。什么原因

问题相关代码,请勿粘贴截图
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>



typedef struct snakecode
{
    int x;
    int y;
    IMAGE snakepic;
    int way;
    
}snakecode;

typedef struct food
{
    int x;
    int y;
    IMAGE foodpic;
}food;


typedef struct snake
{
    int size;
    snakecode ss[500];

}snake;


void playermove(char* input)
{
    char input1;
    if (_kbhit())
    {
        input1 = _getch();
        if (input1 == 'w')
        {
            *input = 'w';

        }
        if (input1 == 'a')
        {
            *input = 'a';



        }
        if (input1 == 's')
        {
            *input = 's';


        }
        if (input1 == 'd')
        {
            *input = 'd';


        }

    }
}


void snakeheadmove(char* input,snake* s)
{
    if (*input == 'w')
    {
        s->ss[0].y -= 1;
        s->ss[0].way = 8;
    }
    if (*input == 'a')
    {
        s->ss[0].x -= 1;
        s->ss[0].way = 4;

    }
    if (*input == 's')
    {
        s->ss[0].y += 1;
        s->ss[0].way = 2;

    }
    if (*input == 'd')
    {
        s->ss[0].x += 1;
        s->ss[0].way = 6;

    }
}
void snakepushmove(snake* s)
{
    for (int i = 1; i < s->size; i++)
    {
        s->ss[i].way = s->ss[i - 1].way;
        if (s->ss[i].way == 8)
        {
            s->ss[i].x = s->ss[i-1].x;
            s->ss[i].snakepic = s->ss[i-1].snakepic;
            s->ss[i].y = s->ss[i-1].y + 30;

            s->ss[i].y -= 1;
        }
    }
    for (int i = 1; i < s->size; i++)
    {
        s->ss[i].way = s->ss[i - 1].way;

        if (s->ss[i].way == 4)
        {
            s->ss[i].x = s->ss[i - 1].x+30;
            s->ss[i].snakepic = s->ss[i - 1].snakepic;
            s->ss[i].y = s->ss[i - 1].y;
            s->ss[i].x -= 1;
        }
    }
    for (int i = 1; i < s->size; i++)
    {
        s->ss[i].way = s->ss[i - 1].way;

        if (s->ss[i].way == 2)
        {
            s->ss[i].x = s->ss[i - 1].x;
            s->ss[i].snakepic = s->ss[i - 1].snakepic;
            s->ss[i].y = s->ss[i - 1].y - 30;
            s->ss[i].y += 1;
        }
    }
    for (int i = 1; i < s->size; i++)
    {
        s->ss[i].way = s->ss[i - 1].way;

        if (s->ss[i].way == 6)
        {
            s->ss[i].x = s->ss[i - 1].x-30;
            s->ss[i].snakepic = s->ss[i - 1].snakepic;
            s->ss[i].y = s->ss[i - 1].y;
            s->ss[i].x += 1;
        }
    }
    
}



void initsnake(snake* s)
{


    for (int i = s->size; i >0;i--)
    {
        if (s->ss[i].way == 8)
        {
            s->ss[i].y = s->ss[i-1].y + 30;
        }
        if (s->ss[i].way == 4)
        {
            s->ss[i].x = s->ss[i-1].x + 30;
        }
        if (s->ss[i].way == 2)
        {
            s->ss[i].y = s->ss[i-1].y - 30;
        }
        if (s->ss[i].way == 6)
        {
            s->ss[i].x = s->ss[i-1].x - 30;
        }


    }
}



void play(snake* s, food* f)
{
    
    BeginBatchDraw();
    for (int i = 0; i < s->size; i++)
    {
        putimage(s->ss[i].x, s->ss[i].y, &(s->ss[0].snakepic), SRCINVERT);

    }

    TCHAR grades[40];
    _stprintf_s(grades, _T("%d"), s->size);
    outtextxy(1100, 100, L"分数:");
    outtextxy(1150, 100, grades);


    putimage(f->x, f->y, &(f->foodpic), SRCINVERT);
    FlushBatchDraw();
    for (int i = 0; i < s->size; i++)
    {
        putimage(s->ss[i].x, s->ss[i].y, &(s->ss[0].snakepic), SRCINVERT);

    }
    outtextxy(1100, 100, L"分数:");
    outtextxy(1150, 100, grades);
    putimage(f->x, f->y, &(f->foodpic), SRCINVERT);


}

void ismergefood(snake *s, food* f)
{
    
    if (s->ss[0].x > f->x - 10 && s->ss[0].x<f->x + 10 && s->ss[0].y>f->y - 10 && s->ss[0].y < f->y + 10)
    {
        s->size++;
        f->x = rand() % 1200;
        f->y = rand() % 650;
        
    }
}

int main()
{
    char input='w';
    snake s;
    s.size = 5;
    s.ss[0].x = 400;
    s.ss[0].y = 600;
    s.ss[0].way = 8;
    loadimage(&(s.ss[0].snakepic), L"./res/snakejpg.jpg", 0, 0);


    food f;
    f.x = 200;
    f.y = 200;
    loadimage(&(f.foodpic), L"./res/coinjpg.jpg", 0, 0);


    


    initgraph(1280, 720);

    while (1)
    {
        
        playermove(&input);

        snakeheadmove(&input, &s);

        snakepushmove(&s);
        
        ismergefood(&s, &f);

        play(&s, &f);
        Sleep(5);
        
    }


    return 0;
}

运行结果及报错内容

不报错

img

img

我的解答思路和

尝试过的方法

我想要达到的结果

想让贪吃蛇游戏正常走

蛇是直的,弯曲不了,本来画的就是小矩形连接在一起。

我用vector写的


#include<iostream>
#include<vector>
#include<windows.h>
#include<conio.h>
using namespace std;
char ch; 
int score = 0;
vector<vector<int>> candy;  
vector<vector<int>> body; 
vector<int> clone_body;
vector<int> head;
void gotoxy(short x, short y) 
{
    COORD coord = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void print_candy(int headx , int heady , char ch)
{
    int ret = 0;
    for(auto x : candy)
    {
        if(headx == x[0] && heady == x[1])  
        {
            candy.erase(candy.begin() + ret);
            clone_body.resize(0);
            if(body.size() > 1)
            {
                clone_body.push_back(body[body.size() - 1][0] + 1);
                clone_body.push_back(body[body.size() - 1][1]);
                body.push_back(clone_body);
            }
            else
            {
                clone_body.push_back(headx+1);
                clone_body.push_back(heady);
                body.push_back(clone_body);
            } 
            score++; 
            print_candy(headx,heady,ch);    
            break;
        }
        else
        {
            gotoxy(x[0],x[1]);
            cout<<"*";
        }
        ret++;
    }    
}
void print_edge()
{
    for(int i = 5; i <= 109 ; i++) { gotoxy(i,4); cout << "__"; }
    for(int i = 5; i <= 109 ; i++) { gotoxy(i,22); cout << "__"; }
    for(int i = 5; i <= 22 ; i++) { gotoxy(5,i); cout << "|"; }
    for(int i = 5; i <= 22 ; i++) { gotoxy(110,i); cout << "|"; }
}
int main()
{
    int headx = 58 , heady = 13; 
    print_edge();
    for(int i = 0 ; i <= 120 ; i++) 
    {
        int candyx = rand()%(100) + 6 , candyy = rand()%(15)+6;
        gotoxy(candyx,candyy); 
        candy.resize(121);
        candy[i].resize(3);
        candy[i][0] = candyx;
        candy[i][1] = candyy;
        cout<<"*";
    }
    head.push_back(headx);
    head.push_back(heady);
    gotoxy(headx,heady);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
    cout<<"o";
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
    int life = 3;
    gotoxy(5,24);
    cout << "life : ";
    for(int i = 1 ; i <= 3 ; i++)  cout << "# ";
    gotoxy(5,25);
    cout<< "score : " << score;
    gotoxy(5,29);
    cout << "注意 :不要去边缘!";
    gotoxy(5,27); 
    cout << "请在这里输入指示符(wasd) : ";
    while(true)
    {    
        if(_kbhit())
        {
            ch = _getch();
            Sleep(500);
            int judgex = 0;
            if(headx < 109 && headx > 5 && heady > 5 && heady < 21)
            {
                if(ch == 'w') heady-=1; 
                if(ch == 's') heady+=1;
                if(ch == 'a') headx-=1;
                if(ch == 'd') headx+=1;
            }    
            if(headx == 109) { headx = 108;  life--; } 
            if(heady == 21)  { heady = 20;   life--; } 
            if(heady == 5)   { heady = 6;    life--; }
            if(headx == 5)   { headx = 6;    life--; }
            system("cls");
            print_edge();
            print_candy(headx , heady , ch);
            gotoxy(headx,heady);
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout<<"o";
            if(body.size() > 1) 
            {
                for(auto i = body.size() - 1 ; i >= 1 ; i--)  
                {
                    body[i][0] = body[i - 1][0];
                    body[i][1] = body[i - 1][1];
                    gotoxy(body[i][0],body[i][1]);
                    cout<<"+";
                }
            }
            if(body.size() != 0) 
            {
                body[0][0] = head[0] , body[0][1] = head[1];
                gotoxy(body[0][0],body[0][1]);
                if(ch != 'w' && ch != 's') cout<<"_"; 
                else cout<<"+";
            }
            else if(body.size() == 1)
            {
                gotoxy(body[0][0],body[0][1]);
                if(ch != 'w' && ch != 's') cout<<"_"; 
                else cout<<"|";
            } 
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
            gotoxy(5,24);
            if(life == 0) 
            {
                Sleep(800);
                Beep(698,700);
                Sleep(100);
                cout << "You lose!" << endl << endl << "     本次得分:" << score << "分!" << endl  << endl << endl << endl << endl << endl;
                return 0;
            }
            cout << "life : ";
            for(int i = 1 ; i <= life ; i++)  cout << "# ";
            gotoxy(5,25);
            cout<< "score : " << score;
            gotoxy(5,29);
            cout << "注意 :不要去边缘!";
             gotoxy(5,27); 
            head.resize(0); 
            head.push_back(headx);
            head.push_back(heady);
            cout << "请在这里输入指示符(wasd) : "; 
        }
    }
    return 0;
}