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

I got the error code -1073741571 in my snake game

本文关键字:错误代码 -1073741571 游戏      更新时间:2023-10-16

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

#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;
}
}

编辑:大多数时候它工作正常,只有有时我得到错误 编辑:堆栈溢出处于int main()

让我们仔细看看你的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))会将苹果放回同一个位置,这会一次又一次地调用该函数无限长的时间。删除if(!appleValid)中的srand(time(NULL)),您的程序应该不会出现问题,因为您只需要随机播种。

如果可以,请尝试将随机函数转换为<random>标头,但它也应该适用于rand:)