这串贪吃蛇的代码不知道为什么一输入w,a,s,d就会自动结束程序,如何解决?

这串贪吃蛇的代码不知道为什么一输入w,a,s,d就会自动结束程序,如何解决?

img

 C:\Users\Administrator Desktop\snakegame.exe                                                                                                                      X
                                                                        *
             after 6.098 seconds with return value 3221225477
#include<bits/stdc++.h>
#include<windows.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<<":";
    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(cin>>ch)
    {    
        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<<":";
        body.pop_back();
        body[0][0] = head[0] , body[0][1] = head[1];
        for(auto i = body.size() - 2 ; 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<<"+";
        }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
        gotoxy(5,24);
        if(life == 0) 
        {
            Sleep(800);
            Beep(698,700);
            Sleep(1000);
            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;
}

因为body里没有值,导致body.pop_back()出错。

你打断点看看在哪里代码结束,可能是没有移动逻辑

return value 3221225477,访问越界了
可以debug一下,看哪里越界,定位错误代码

用上下左右键

参考gpt:
结合自己分析给你如下建议:
有可能是你的代码中没有正确地处理空格键的事件,导致每次按下空格键就会触发游戏结束的条件。你可以检查你的代码中是否有类似这样的语句:

if (event.key == 32) //如果按下空格键 (数字32)
{
    game_over = True; //游戏结束
}

如果有,你可以将这段代码删除或注释掉,或者改成其他的逻辑,比如暂停或继续游戏。
有可能是你的代码中没有正确地处理方向键的事件,导致每次按下方向键就会改变蛇的移动方向,而不是在当前方向上前进。你可以检查你的代码中是否有类似这样的语句:

if (event.key in (K_w, K_UP)) //如果按下w或上箭头
{
    pos_x = 0; //x轴方向不变
    pos_y = -1; //y轴方向向上
}

如果有,你可以将这段代码修改为只在蛇不是向下移动时才改变方向,比如:

if (event.key in (K_w, K_UP)) //如果按下w或上箭头
{
    if (pos_y != 1) //如果蛇不是向下移动
    {
        pos_x = 0; //x轴方向不变
        pos_y = -1; //y轴方向向上
    }
}

同理,你可以对其他方向键做类似的修改,以防止蛇反向移动导致游戏结束。

你可以定义一下按键,命令是什么。
如果不行的话,可以看看别人的作品。上次我在csdn上才看到一个好代码

【以下回答由 GPT 生成】

问题产生的原因是在代码中缺少了贪吃蛇移动的逻辑,导致当输入w、a、s、d时,程序并没有对蛇的位置进行更新,因此程序会立即结束运行。

为了解决这个问题,需要添加一个蛇的移动函数,并在移动函数中更新贪吃蛇的位置信息。下面是对代码进行修改的建议:

  1. 首先,在代码中添加一个蛇移动的函数move_snake,并在main函数中调用该函数。

```c++ void move_snake(char direction) { // 根据方向进行不同的移动操作 // 更新蛇的位置信息 // 更新画面 }

int main() { // 其他代码 while (true) { // 获取用户输入 // 调用move_snake函数 } return 0; } ```

  1. 在move_snake函数中,根据用户输入的方向更新蛇的位置信息。可以使用if语句来判断用户输入的方向,并更新蛇头的位置和身体的位置。

```c++ void move_snake(char direction) { int new_headx = head[0]; int new_heady = head[1];

   // 根据direction更新蛇头的位置
   if (direction == 'w')
   {
       new_heady--;
   }
   else if (direction == 's')
   {
       new_heady++;
   }
   else if (direction == 'a')
   {
       new_headx--;
   }
   else if (direction == 'd')
   {
       new_headx++;
   }

   // 更新蛇头的位置
   head[0] = new_headx;
   head[1] = new_heady;

   // 更新身体的位置
   for (int i = body.size() - 1; i > 0; i--)
   {
       body[i] = body[i-1];
   }
   body[0] = head;

   // 更新画面
   // ...

} ```

  1. 在while循环中,获取用户的输入,并调用move_snake函数来更新蛇的位置信息。

```c++ int main() { // 其他代码 while (true) { char direction; cin >> direction;

       move_snake(direction);
   }
   return 0;

} ```

通过以上的修改,程序将会根据用户的输入来更新贪吃蛇的位置信息,从而解决贪吃蛇代码输入w、a、s、d自动结束程序的问题。请根据自己的实际需求,进一步完善蛇的移动和画面更新的逻辑。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^