gpt4 book ai didi

C - Ncurses 编译错误

转载 作者:行者123 更新时间:2023-11-30 15:31:37 24 4
gpt4 key购买 nike

我正在制作一个迷宫游戏,人们在我创建的 printf 迷宫中移动光标。迷宫由许多充当墙壁的“F”字符组成。有什么方法可以告诉我程序,如果光标即将移动的地方有一个“F”字符,则执行以下操作,例如打印消息。我编写了下面的程序,当光标移动到字符“F”上时,该程序应该显示一条消息。当我编译它时,它给出错误“ncurses.c:24:14: warning: left-hand operand of comma expression has noeffect [-Wunused-value]”。

这是整个程序的关注部分,可以在下面找到

                if(m == 's')
{
if((oldy+1,x)=='F')
{
mvwprintw(win, 0,0,"Sorry, you cant move there.");
wrefresh(win);
}
else
{
move((y= oldy+1),x);
refresh();
oldy = y;
}
}

如果您想查看,这是完整的程序

#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
int y = 0;
int x = 0;
int oldy = 0;
int oldx = 0;
char m = 'l';
initscr();
WINDOW* win;
win = newwin(1, 40, 40 ,0);
refresh();
mvwprintw(win, 0,0,"First line");
wrefresh(win);

while(m != 'q')
{
m=getch();
if(m == 's')
{
if((oldy+1,x)=='F')
{
mvwprintw(win, 0,0,"Sorry, you cant move there.");
wrefresh(win);
}
else
{
move((y= oldy+1),x);
refresh();
oldy = y;
}
}
else if(m == 'd')
{
move(y,(x=oldx+1));
refresh();
oldx = x;
}
else if(m == 'a')
{
move(y,(x=oldx-1));
refresh();
oldx = x;
}
else if(m == 'w')
{
move((y= oldy-1),x);
refresh();
oldy = y;
}
else
m = 'q';
}

delwin(win);
endwin();

return 0;
}

最佳答案

你认为这条线在做什么?

if((oldy+1,x)=='F')

表达式(oldy+1,x)使用逗号运算符,它计算其第一个参数(oldy+1)的副作用,并在计算之前丢弃结果它的第二个参数 (x)。然后将结果 (x) 与 'F' 进行比较。

大概你想在那里调用一个函数。返回窗口中某个位置的字符的curses 函数是mvwinch。这可能会达到您的意思:

if(mvwinch(win,oldy+1,x)=='F')

关于C - Ncurses 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24643583/

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