诅咒 C++ 的问题 [英] Problems with curses C++
问题描述
我最近开始在 Windows 上使用 PDcurses.我编写了一个简单的程序,它创建一个窗口并根据用户的输入移动一个字符,向上移动 'w',向下移动 's' 等等.
I've recently started to use PDcurses for Windows. I wrote a simple program that creates a window and moves a character according to the user's inputs, going up for 'w', down for 's' and so on.
发生了两件奇怪的事情:
There are 2 weird things that happen:
- 每当我按住s"向下移动角色时它消失了,不允许用户看到运动.
- 每当我将角色横向移动时,它都会留下轨迹";(有点)如果向右移动则为蓝色,如果向左移动则为红色.
我附上一个简短的 GIF,您可以在其中看到第一个问题和第二个问题的屏幕截图(因为录音质量不足以看到踪迹).
I Attach a short GIF in which you can see the first problem and a screenshot for the second (since the quality of the recording was not enough to see the trail).
这是 C++ 代码.它在循环开始时绘制,在中间扫描输入,并在结束时刷新屏幕.我将 getch()
设置为不使用 nodelay()
中断执行.
Here's the C++ code. It draws at the at the beginning of the loop, scans for inputs in the middle, and refreshes the screen at the end. I set the getch()
not to interrupt the execution with nodelay()
.
initialize();
WINDOW* stage= createstage();
int ywindow,xwindow,heightwindow,widthwindow;
getbegyx(stage,ywindow,xwindow);
getmaxyx(stage,heightwindow,widthwindow);
heightwindow--;
widthwindow--;
int x = xwindow+1, y = ywindow+1;
char c = '@';
while(TRUE){
box(stage,0,0);
wmove(stage,y,x);
wprintw(stage,"%c",'@');
c=getch();
if(c=='w' && y-1>ywindow){
y--;
}
if (c=='s' && y+1<heightwindow){
y++;
}
if(c=='d' && x+1<widthwindow){
x++;
}
if(c=='a' && x-1>xwindow){
x--;
}
wrefresh(stage);
Sleep(16);
wclear(stage);
}
推荐答案
更新:首先附上程序的完整源码:
Update:
First of all I attach the complete source of the program:
#include "curses.h"
#include <windows.h>
void disableselection(){
HANDLE hInput;
DWORD prev_mode;
hInput = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hInput, &prev_mode);
SetConsoleMode(hInput, prev_mode & ENABLE_EXTENDED_FLAGS);
}
void disableresizing(){
HWND consoleWindow = GetConsoleWindow();
SetWindowLong(consoleWindow, GWL_STYLE, GetWindowLong(consoleWindow, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX);
}
void initialize(){
initscr();
curs_set(0);
disableselection();
disableresizing();
noecho();
nodelay(stdscr,TRUE);
}
WINDOW* createstage(int starty=0, int startx=0, int height=LINES, int width=COLS-COLS*1/5){
WINDOW* win=newwin(height-starty,width-startx,starty,startx);
refresh();
return win;
}
void end(WINDOW* stage){
clear();
refresh();
nodelay(stdscr,FALSE);
printw("press something to exit");
getch();
endwin();
}
int main(){
initialize();
WINDOW* stage= createstage();
int ywindow,xwindow,heightwindow,widthwindow;
getbegyx(stage,ywindow,xwindow);
getmaxyx(stage,heightwindow,widthwindow);
heightwindow--;
widthwindow--;
int x = xwindow+1, y = ywindow+1;
char c = '@';
while(TRUE){
box(stage,0,0);
wmove(stage,y,x);
waddch(stage,'@');
wrefresh(stage);
c=wgetch(stage);
if(c=='w' && y-1>ywindow){
y--;
}
if (c=='s' && y+1<heightwindow){
y++;
}
if(c=='d' && x+1<widthwindow){
x++;
}
if(c=='a' && x-1>xwindow){
x--;
}
napms(16);
werase(stage);
wrefresh(stage);
}
end(stage);
}
如您所见,我修改了循环.结果是现在没有踪迹"了.当角色向下移动时,它不会消失.不过,另一个问题出现了.
As you can see I modified the loop. The result is that now there are no "trails" and when the character moves down it doesn't disappear. Another issue has come up though.
每当我移动时,盒子(恰好是顶部)就会闪烁.即使我将 box(stage,0,0);
调用放在循环的开头,也会发生这种情况.
The box (precisely the top side) flickers whenever I move. This happens even If I put the box(stage,0,0);
call at the start of the loop.
我尝试使用 stdscr
绘制框,并创建了一个子窗口用作主舞台并在父窗口中绘制框.我尝试的另一件事是删除认为它们冲突的 Windows API 函数.这些想法都没有奏效.
I've tried drawing the box using stdscr
and also creating a subwindow to use as main stage and drawing the box in the parent window.
Another thing I tried is removing the Windows API functions thinking they were conflicting.
None of these ideas worked.
这篇关于诅咒 C++ 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!