诅咒 C++

问题描述

我最近开始在 Windows 上使用 PDcurses。 我编写了一个简单的程序,它创建一个窗口并根据用户的输入移动一个字符,向上移动 'w',向下移动 's' 等等。

发生了两件奇怪的事情:

  1. 每当我按住“s”向下移动角色时,它就会消失,不允许用户看到移动。
  2. 每当我将角色横向移动时,它会在向右移动时留下“轨迹”(有点)蓝色,如果向左移动则留下红色。

我附上一个简短的 GIF,您可以在其中看到第一个问题和第二个问题的屏幕截图(因为录音质量不足以看到踪迹)。

Animation showing the character disappearing

Static image showing character

这是 C++ 代码。它在循环开始时绘制,在中间扫描输入,并在结束时刷新屏幕。我将 getch() 设置为不使用 nodelay() 中断执行。

initialize();
WINDOW* stage= createstage();
int ywindow,xwindow,heightwindow,widthwindow;

getbegyx(stage,ywindow,xwindow);
getmaxyx(stage,widthwindow);
heightwindow--;
widthwindow--;
int x = xwindow+1,y = ywindow+1;
char c = '@';


while(TRUE){

    Box(stage,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);
}

解决方法

更新: 首先附上程序的完整源码:

#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,widthwindow);
  heightwindow--;
  widthwindow--;
  int x = xwindow+1,y = ywindow+1;
  char c = '@';
  while(TRUE){
    box(stage,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);
}

如您所见,我修改了循环。 结果是现在没有“轨迹”,当角色向下移动时,它不会消失。 不过,另一个问题又出现了。

每当我移动时,盒子(恰好是顶部)就会闪烁。即使我将 box(stage,0); 调用放在循环的开头,也会发生这种情况。

我尝试使用 stdscr 绘制框,并创建了一个子窗口以用作主舞台并在父窗口中绘制框。 我尝试的另一件事是删除认为它们冲突的 Windows API 函数。 这些想法都没有奏效。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...