gpt4 book ai didi

c++ - 在 C++ 中检测与数组的冲突

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

所以我打印了一个二维 map 数组。我将 0 设置为空格,将 1 设置为墙。我有陷阱和健康以及玩家等变量。但是,当两者发生冲突时,我无法实现得分。我想知道我如何检测玩家和生命何时发生碰撞,这会使健康变量增加 1。现在每次我移动它都会增加。谢谢你!这些是我的变量函数

void putplayer()
{
setcolor(9);
gotoxy( player.x +mazex, player.y +mazey);
cout<<playersymbol;
setcolor(7);

}

void puttreasure()
{
setcolor(14);
gotoxy(treasure.x +mazex, treasure.y +mazey);
cout<<treasuresymbol;
setcolor(7);

}

void puttraps()
{
setcolor(14);
gotoxy( traps.x +mazex, traps.y +mazey);
cout<<trapssymbol;
setcolor(7);

}

void putlives()
{
setcolor(14);
gotoxy( lives.x +mazex, lives.y +mazey);
cout<<livessymbol;
setcolor(7);

}

void moveleft()
{
gotoxy(31,7);
cout<<"left key move player left \n\n";
if (maze[player.y][player.x-1]==0) player.x = player.x -1;
}

然后是我的 Main,但是我删除了一些代码,因为它可能不相关

int main()
{
DWORD mode; /* Preserved console mode */
INPUT_RECORD event; /* Input event */
BOOL EXITGAME = FALSE; /* Program termination flag */
// unsigned int counter = 0; /* The number of times 'Esc' is pressed */


/* Get the console input handle */
HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );

/* Preserve the original console mode */
GetConsoleMode( hstdin, &mode );

/* Set to no line-buffering, no echo, no special-key-processing */
SetConsoleMode( hstdin, 0 );

srand ( time(NULL) ); //initialize the random seed

// Variables
int health = 0;

// Declare variable positions
player.x=1;
player.y=1;
treasure.x = 1;
treasure.y = 3;
traps.x = 1;
traps.y = 7;
lives.x = 1;
lives.y = 9;

clrscr();
setcolor(15);


while (!EXITGAME)
{
if (WaitForSingleObject( hstdin, 0 ) == WAIT_OBJECT_0) /* if kbhit */
{
DWORD count; /* ignored */

/* Get the input event */
ReadConsoleInput( hstdin, &event, 1, &count );

/* Only respond to key release events */
if ((event.EventType == KEY_EVENT)
&& !event.Event.KeyEvent.bKeyDown)

clrscr();
putmenu();
gotoxy(6,20);
cout<<"Lives: " << health;
Sleep(100);


switch (event.Event.KeyEvent.wVirtualKeyCode)
{
case VK_ESCAPE:
EXITGAME = TRUE;
break;

case VK_LEFT:
// left key move player left
moveleft();
break;

case VK_RIGHT:
// right key move player right
moveright();

break;

case VK_UP:
// up key move player up
moveup();

break;

case VK_DOWN:
// down key move player down
movedown();

break;

case VK_A:
// left key move player left
moveleft();

break;

case VK_D:
// right key move player right

moveright();
break;

case VK_W:
// up key move player up

moveup();
break;

case VK_S:
// down key move player down
movedown();
break;


}//switch

putplayer();
puttreasure();
puttraps();
putlives();




}
if(maze[player.x][player.y] = maze[lives.x][lives.y]){
health++;
}

else if(maze[player.x][player.y] = maze[traps.x][traps.y]){
health--;
}
}

gotoxy(1,23);cout<<" ";
SetConsoleMode( hstdin, mode );
return 0;
}

最佳答案

你有密码

if(maze[player.x][player.y] = maze[lives.x][lives.y])

将 maze[player.x][player.y] 设置为 maze[lives.x][lives.y] 的值,然后对其求值(如果为 1(墙),则求值为 true,为 false如果为 0(空格))。我想你是想拥有类似的东西

if((player.x == lives.x) && (player.y == lives.y))

这将(据我所知)评估玩家和生命是否在同一位置。

关于c++ - 在 C++ 中检测与数组的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29957689/

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