gpt4 book ai didi

c - C 中的 PDCurses (一直显示 "Access violation read location")

转载 作者:行者123 更新时间:2023-11-30 16:50:16 25 4
gpt4 key购买 nike

我昨天刚刚开始了一个新项目(Roguelike)。这是我的第一次,我一直遇到同样的错误。在尝试实现滚动 map 后,我运行了该程序,但每当我按下某个键时,Visual Studio 就会显示一个消息框,指出读取位置存在访问冲突。

代码如下:

#include "curses.h"
#include <stdlib.h>

#define MAX_HEIGHT 16
#define MAX_WIDTH 21

typedef struct tile
{
char *tile;
bool passable;
}tile;

typedef struct player
{
int xpos;
int ypos;
int dx;
int dy;
}player;

tile tileArray[] = {
{".",TRUE},
{"#",FALSE},
{"+",FALSE},
{"/",TRUE}
};

int mapArray[15][21] = {
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,1,1,1,2,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }

};

typedef struct camPos
{
int y;
int x;
}camPos;

void initSetup();
player *actorSetup();
void refreshScreenMap();
void handleInput();
void drawScreenMap();
void interactionOpen();
bool isPassable(int y, int x);
void startupScreen();

player *user;
camPos cam;

int main()
{
initSetup();

user = actorSetup();
cam.y = 2;
cam.x = 2;

startupScreen();

while (TRUE)
{
drawScreenMap();
refreshScreenMap();
handleInput();
clear();
}
endwin();
return 0;
}

void initSetup()
{
initscr();
noecho();
raw();
keypad(stdscr,TRUE);
}

player *actorSetup()
{
player *newactor;
newactor = malloc(sizeof(player));
newactor->xpos = 10;
newactor->ypos = 10;
return newactor;
}

void handleInput()
{
char button = getch();
switch (button)
{
case 's':
user->dy++;
break;
case 'w':
user->dy--;
break;
case 'a':
user->dx--;
break;
case 'd':
user->dx++;
break;
case 'o':
interactionOpen(user);
break;
}
if (isPassable(user->ypos + user->dy, user->xpos + user->dx))
{
user->ypos += user->dy;
user->xpos += user->dx;
}
else
{
user->dx = 0;
user->dy = 0;
}
}

void refreshScreenMap()
{
mvprintw(6, 8, "@");
cam.x += user->dx;
cam.y += user->dy;
}

void drawScreenMap()
{
for (int y = 1; y < 12; y++)
{
move(y,1);
for (int x = 1; x < 16; x++)
{
mvprintw(y, x, tileArray[mapArray[cam.y + y][cam.x + x]].tile);
}
}
}

void interactionOpen()
{
if (mapArray[user->ypos + 1][user->xpos] == 2)
{
mapArray[user->ypos + 1][user->xpos] = 3;
}
else if (mapArray[user->ypos - 1][user->xpos] == 2)
{
mapArray[user->ypos - 1][user->xpos] = 3;
}
else if (mapArray[user->ypos][user->xpos + 1] == 2)
{
mapArray[user->ypos][user->xpos + 1] = 3;
}
else if (mapArray[user->ypos][user->xpos - 1] == 2)
{
mapArray[user->ypos][user->xpos - 1] = 3;
}
else if (mapArray[user->ypos + 1][user->xpos] == 3)
{
mapArray[user->ypos + 1][user->xpos] = 2;
}
else if (mapArray[user->ypos - 1][user->xpos] == 3)
{
mapArray[user->ypos - 1][user->xpos] = 2;
}
else if (mapArray[user->ypos][user->xpos + 1] == 3)
{
mapArray[user->ypos][user->xpos + 1] = 2;
}
else if (mapArray[user->ypos][user->xpos - 1] == 3)
{
mapArray[user->ypos][user->xpos - 1] = 2;
}
}

bool isPassable(int y, int x)
{
bool pass = tileArray[mapArray[y][x]].passable;
if (pass == FALSE)
{
return FALSE;
}
else if (y < cam.y || y >= (cam.y + 11))
{
return FALSE;
}
else if (x < cam.x || x >= (cam.x + 15))
{
return FALSE;
}
else
{
return TRUE;
}
}

void startupScreen()
{
mvprintw(10, 20, "________");
mvprintw(11, 20, "\\______ \\ __ __ ____ ____ ____ ____ ____ ");
mvprintw(12, 20, " | | \\| | \\/ \\ / ___\\_/ __ \\/ _ \\ / \\ ");
mvprintw(13, 20, " | ` \\ | / | \\/ /_/ > ___( <_> ) | \\ ");
mvprintw(14, 20, "/_______ /____/|___| /\\___ / \\___ >____/|___| / ");
mvprintw(15, 20, " \\/ \\//_____/ \\/ \\/");
mvprintw(16, 20, "Made by Will Reid");
mvprintw(17, 20, "Press any key to start...");
getch();
clear();
}

最佳答案

问题在于 isPassable 中使用的变量 cam 未初始化。

我使用 valgrind(在 Linux 上)看到了这个:

==11010== 1 errors in context 1 of 7:
==11010== Invalid read of size 8
==11010== at 0x400B72: drawScreenMap (foo.c:151)
==11010== by 0x40091E: main (foo.c:77)
==11010== Address 0x58e8c2a0 is not stack'd, malloc'd or (recently) free'd
==11010==
==11010==
==11010== 7 errors in context 2 of 7:
==11010== Conditional jump or move depends on uninitialised value(s)
==11010== at 0x40100E: isPassable (foo.c:203)
==11010== by 0x400A46: handleInput (foo.c:124)
==11010== by 0x400928: main (foo.c:79)
==11010== Uninitialised value was created by a heap allocation
==11010== at 0x4C28BED: malloc (vg_replace_malloc.c:263)
==11010== by 0x400974: actorSetup (foo.c:97)
==11010== by 0x4008F9: main (foo.c:69)

此外,打开编译器警告会出现一些问题(Visual Studio 有编译器警告...)。

关于c - C 中的 PDCurses (一直显示 "Access violation read location"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42251576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com