我在蛇游戏中收到错误代码-1073741571

时间:2020-02-12 17:32:54

标签: c++

我正在做蛇游戏,基本上已经完成了;但是有时,当我运行它时,它首先会工作,然后清除(可能在我的代码中,它有很多清除)并返回错误代码-1073741571。
这是代码:

from bacher.models.User import User

编辑:大多数时候它都可以正常工作,只有有时候我会收到错误消息 编辑:堆栈溢出在#include <iostream> #include <string> #include <stdio.h> #include <conio.h> #include "windows.h" #include <vector> #include <random> #include <ctime> #include <cmath> #include <windows.h> using namespace std; struct point { int x; int y; }; bool goTo(point p,vector<point>& snek) { bool flag=false; for (size_t i = 0; i < snek.size(); i++) { if (p.x == snek[i].x && p.y == snek[i].y) { return true; } } for (int i=snek.size()-1; i >0; i--) { snek[i] = snek[i - 1]; } snek[0] = p; return false; } void makeFood(point& apple, vector<point> snek) { bool appleValid = true; apple.x = (rand() % 19) + 1; apple.y = (rand() % 19) + 1; for (int i = 0; i < snek.size() - 1; i++) { if (apple.x == snek[i].x && apple.y == snek[i].y) { appleValid = false; break; } } if (!appleValid) { srand(time(NULL)); makeFood(apple,snek); } } int main() { vector<point> snek; snek.push_back({ 10,10 }); snek.push_back({ 10,9 }); snek.push_back({ 10,8 }); snek.push_back({ 10,7 }); int highscore= 0, score; bool tutorial = false; bool p = false; char move = 'w'; char sure = 'f'; string board[21][21]; int direction=2; point apple; apple.x = rand() % 20; apple.y = rand() % 20; for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { board[i][j] = " "; } } bool loss = false; while (true) { score = snek.size() - 4; bool appleEaten = false; srand(time(NULL)); if (snek[0].x == 0 ) loss = true; if (snek[0].x == 20 ) loss = true; if (snek[0].y == 0 ) loss = true; if (snek[0].y == 20 ) loss = true; if (loss) { system("CLS"); if (score > highscore) { highscore = score; } cout << "You lost with a score of " << snek.size() - 4 << endl; cout << "Your highscore for this session is " << highscore<<endl; cout << "Press any key to play again" << endl; cout << "Press RMB to quit" << endl; while (true) { if (GetAsyncKeyState(VK_RBUTTON)) { system("CLS"); cout << "Are you sure you want to quit? Your highscore for this session will be reset" << endl; cout << "Press Q to quit and P to play again" << endl; sure = _getch(); if (sure == 'q' || sure == 'Q') { _Exit(0); } if (sure == 'p' || sure == 'P') { p = true; } } if (_kbhit() || p) { for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { board[i][j] = " "; } } snek.clear(); snek.push_back({ 10,10 }); snek.push_back({ 10,9 }); snek.push_back({ 10,8 }); snek.push_back({ 10,7 }); loss = false; p = false; break; } } } Sleep(100); if (_kbhit()) { move = _getch(); } system("CLS"); switch (move) { case 'w': loss = goTo({ (snek[0].x - 1),snek[0].y }, snek); Sleep(10); break; case 'a': loss = goTo({ snek[0].x ,(snek[0].y - 1) }, snek); Sleep(10); break; case 's': loss = goTo({ (snek[0].x + 1),snek[0].y }, snek); Sleep(10); break; case'd': loss = goTo({ snek[0].x ,(snek[0].y + 1) }, snek); Sleep(10); break; } board[apple.x][apple.y] = " 0"; for (int k = 0; k < snek.size() - 1; k++) { board[snek[k].x][snek[k].y] = " *"; board[snek[snek.size() - 1].x] [snek[snek.size() - 1].y] = " "; } if (apple.x == snek[0].x && apple.y == snek[0].y) { snek.push_back({snek[snek.size()-1].x+1,snek[snek.size() - 1].y}); appleEaten = true; } if (appleEaten) { makeFood(apple,snek); } for (int i = 0; i < 20; i++) { board[0][i] = "--"; board[20][i] = "--"; } for (int i = 0; i < 20; i++) { board[i][0] = '|'; board[i][20] = '|'; } if (!tutorial) { cout << "You are a snake." << endl; cout << "Your body looks like this" << endl; cout << "*****" << endl; cout << "Move with WASD" << endl; cout << "If you eat the apples, which look like this " << endl << "0" << endl; cout << "You get bigger. If you try to eat yourself or run into walls, you lose" << endl; cout << "Click RMB to begin"; while (true) { if (GetAsyncKeyState(VK_RBUTTON)) { system("CLS"); tutorial = true; break; } } } for (int i = 0; i < 21; i++) { for (int j = 0; j < 21; j++) { cout << board[i][j]; } cout << endl; } cout << "Score: " << score; } }

1 个答案:

答案 0 :(得分:1)

让我们仔细看看您的makeFood函数:

void makeFood(point& apple, vector<point> snek)
{
    bool appleValid = true;
    apple.x = (rand() % 19) + 1;
    apple.y = (rand() % 19) + 1;
    for (int i = 0; i < snek.size() - 1; i++)
    {
        if (apple.x == snek[i].x && apple.y == snek[i].y)
        {
            appleValid = false;
            break;
        }
    }
    if (!appleValid)
    {
        srand(time(NULL));
        makeFood(apple,snek);
    }

}

您的if语句可以使苹果处于蛇形位置时再次调用makeFood(),但是使用srand(time(NULL))可以将苹果放回原处,并再次调用该函数,然后再次无限次。删除srand(time(NULL))中的if(!appleValid),您的程序就不会出现问题,因为您只需要随机种子即可。

如果可以的话,尝试将随机函数转换为<random>标头,但它也可以与rand一起使用:)