1

我在 linux 中使用以下代码构建了一段代码:

g++ -o main main.cpp -lncurses

但由于某种原因,它无法正常工作。文件应如下所示:

_+____+____________________________________________________+
-_ ___+_++++++++++++++++++++++++++++++++++++++++++++++++++_+
 ++++_+_+____________________________-_- - - - - - - - - -_+
______+_+_                                                  
-    -+_+_                                                  
       _+-_ _______________________________________________+
_______-+                                              ____+
+++++++_+____________-________--__________________________-+
_______ + ___ _---_           __----- - -- -- -   - - - - -+
_ _ _  _+__- - ----- - - --- ------------ - - -- -- - - -- +
+++++++_+++________________________________________________+
_________ ______        _ _ _ _ - -_ _ --  - -_ _ - - - - _+
-----------------------------------------------------------+
                                                           +
____________+____________+_______________+_________________+
- - - - - ----------- -  -------------------- --------- -- +
  _ _   _ _     _ _ -___ _ -__ - -_ -- -__- - -- ----------+
-----------------------------------------------------------+
____________-----____   ______________ ______ _____________+
_________ ________   _________  _________ ___________    _ +
-------------              --------- -          -----------+
___________________________________________________________+
___________________________________________________________+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++_+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++__

但它最终看起来更像这样: 在此处输入图像描述

然后,此外,在我最终退出程序后,终端的行为很奇怪,所以它似乎endwin()不起作用。

这是完整代码的副本:

// Imports
#include <ncurses.h>
#include <fstream>
#include <cstring>

// Set Namespace
using namespace std;

// Set Contants
const int BOARD_HEIGHT = 25;
const int BOARD_WIDTH = 60;

// Global Varibles
char gameBoard [BOARD_WIDTH][BOARD_HEIGHT];

// Declare Functions
void createBoard(string);
void displayBoard();
void initCurses();
string getBoard(string);

// CHARACTER CLASS - All operations for the 'o'
class chr{
public:
    int coor [2];
    chr();
    ~chr();
    void moveCenter(int, int);
    char characterOn;
    void win();
    void loose();
    void die();
    int lives;
    void displayLives();
private:
//  char characterOn;
    void spawn(int, int);
};
void getKeyMoveChar(chr*);

// Character Constructor
chr::chr(){
    lives = 4;
    chr::spawn(0,0);
}

// Destructor
chr::~chr(){
    printw("\n"); 
    refresh();
}

// Die - Move the character to 0,0 and then loose a life
void chr::die(){
    if(lives > 0){
        gameBoard[coor[0]][coor[1]] = characterOn;
        lives -= 1;
        spawn(0,0);
    }
    else{
        moveCenter(BOARD_WIDTH - 1, BOARD_HEIGHT -1);
    }

}

// Win - Display "You win!" and close the program.
void chr::win(){
    clear();
    int row, col;
    getmaxyx(stdscr,row,col);
    char mesg[9] = "You win!";
    mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
    refresh();
}

// Loose - Display "You loose!" and close the program.
void chr::loose(){
    clear();
    int row, col;
    getmaxyx(stdscr,row,col);
    char mesg[11] = "You lose!";
    mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
    refresh();
}

// Spawn the character on the board
void chr::spawn(int x, int y){
    coor[0] = x;
    coor[1] = y;
    characterOn = gameBoard[x][y];
    gameBoard[x][y] = 'o';
}

// Move the character to another spot
void chr::moveCenter(int x, int y){
    if(x > -1 && y > -1 && y <= BOARD_HEIGHT - 1 && x <= BOARD_WIDTH - 1 && gameBoard[x][y] != '+'){
        if(characterOn == '-'){
            int ox = x;
            int oy = y;
            if(x > coor[0]){x++;}
            else if(x < coor[0]){x--;}
            else if(y > coor[1]){y++;}
            else if(y < coor[1]){y--;}
            if(gameBoard[x][y] == '+'){
                x = ox;
                y = oy;
            }
        }
        gameBoard[coor[0]][coor[1]] = characterOn;
        coor[0] = x;
        coor[1] = y;
        characterOn = gameBoard[x][y];
        gameBoard[coor[0]][coor[1]] = 'o';
    }
}

// Display the live count at the bottom of the game.
void chr::displayLives(){
    int row, col;
    getmaxyx(stdscr,row,col);
    mvprintw(row -1 ,0,"Lives: %d", lives - 1);
    refresh();
}

// Main stuff
int main()
{
    string BOARD_INPUT = getBoard("map3.txt");
    createBoard(BOARD_INPUT);
    chr player;
    initCurses();
    displayBoard();
    while ((player.coor[0] != BOARD_WIDTH - 1 || player.coor[1] != BOARD_HEIGHT - 1) && player.lives != 0){
        getKeyMoveChar(&player);
        displayBoard();
        if(player.characterOn == ' '){
            player.die();
        }
        player.displayLives();
    }
    if(player.lives != 0){
        player.win();
    }
    else{
        player.loose();
    }
    getch();
    endwin();
    return 0;
}

void getKeyMoveChar(chr* player){
    int ch;
    ch = getch();
    if(ch == KEY_LEFT){
        player->moveCenter(player->coor[0] - 1, player->coor[1]);
    }
    else if(ch == KEY_RIGHT){
        player->moveCenter(player->coor[0] + 1, player->coor[1]);
    }
    else if(ch == KEY_DOWN){
        player->moveCenter(player->coor[0], player->coor[1] + 1);
    }
    else if(ch == KEY_UP){
        player->moveCenter(player->coor[0], player->coor[1] - 1);
    }
}
void initCurses(){
    initscr();
    raw();
    noecho();
    keypad(stdscr, TRUE);
    resize_term(BOARD_HEIGHT + 3, BOARD_WIDTH + 3);
}

/*
+ Wall
- Ice
_ Ground
  Hole
*/

// Read the map
string getBoard(string filename){
    string board;
    string line;
    ifstream myfile (filename.c_str());
    if(myfile.is_open()){
        while(myfile.good()){
            getline (myfile,line);
            board += line;
        }
        myfile.close();
    }
    else{
        printw("Error opening map.txt.");
    }
    return board;
}

// Create map from string
void createBoard(string inputString){
    for(int i=0;i<BOARD_HEIGHT;i++){
        for(int j=0;j<BOARD_WIDTH;j++){
            gameBoard[j][i] = inputString[i*BOARD_WIDTH + j];
        }
    }
}

void displayBoard(){
    move(0,0);
    for(int i=0;i<BOARD_HEIGHT;i++){
        for(int j=0;j<BOARD_WIDTH;j++){
            addch(gameBoard[j][i]);
        }
        printw("\n");
    }
    refresh();
}
4

1 回答 1

2

对于第一个问题:

显示可能是错误的,因为您的地图数据文件中有 DOS 行结尾。(如果我在将地图数据map3.txt粘贴getline()到名为. )\n\r

对于第二个问题:

endwin()本身工作正常;这里的问题是它不是最后一个被调用的东西:printw()refresh()~chr()构函数playermain().

于 2012-07-07T13:49:50.497 回答