【问题标题】:Increment, and Decrement not acting as expected增量和减量未按预期运行
【发布时间】:2021-06-22 07:10:31
【问题描述】:

我正在用 C++ 编写一个基于控制台的游戏,我已经到了通过箭头键移动玩家的地步,但是它们 x-- 和 y-- 不会改变 x 或 y 的值,我试过了其他配置,例如 x -= 1 和 x = x-1,但都不起作用

(我也尝试了 _Game 结构作为一个类,没有更好的结果)

我的代码是

#include <iostream>
#include <unistd.h>
#include <termios.h>
// #include "colorlib.hpp"
// #include "dict.hpp"

using namespace std;
// using namespace UnixColor;
// using namespace Dict;



// dict colors = initColors();
// string red = getC(colors, "red");
// string gray = getC(colors, "gray");

int x, y;

struct _Game {
    int board[5][5] {
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0}
    };

    void checkBoard() {
        for(int i = 0; i < 5; i++) {
            for(int o = 0; o < 5; o++) {
                if(board[i][o] == 1) {
                    board[i][o] = 0;
                }
            }
        }
        board[y][x] = 1;
    }

    void bprint() {
        checkBoard();
        for(auto var : board) {
            for(int i = 0; i < 5; i++) {
                // cout << var[i];
                if(var[i] == 0) {
                    // cout << gray << "{}" << RESET;
                    cout << "{}";
                }else if(var[i] == 1) {
                    // cout << red << "[]" << RESET;
                    cout << "[]";
                }
            }
            cout << endl;
        }
    }

};

// taken from https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed
char getch() {
    char buf = 0;
    struct termios old = {0};
    if (tcgetattr(0, &old) < 0)
        perror("tcsetattr()");
    old.c_lflag &= ~ICANON;
    old.c_lflag &= ~ECHO;
    old.c_cc[VMIN] = 1;
    old.c_cc[VTIME] = 0;
    if (tcsetattr(0, TCSANOW, &old) < 0)
        perror("tcsetattr ICANON");
    if (read(0, &buf, 1) < 0)
        perror ("read()");
    old.c_lflag |= ICANON;
    old.c_lflag |= ECHO;
    if (tcsetattr(0, TCSADRAIN, &old) < 0)
        perror ("tcsetattr ~ICANON");
    return (buf);
}

int main() {
    _Game game;
    x = 0;
    y = 0;
    char card;
    // for(int i = 0; i < 4; i++) {
    //  system("clear");
    //  game.bprint();
    //  x--;
    //  sleep(1);
    // }
    // for(int i = 0; i < 4; i++) {
    //  system("clear");
    //  game.bprint();
    //  y--;
    //  sleep(1);
    // }
    while(true) {
        system("clear");
        game.bprint();
        card = getch();
        if(card == '[') {
            card = getch();
            switch(card) {
                case 'A':
                    y--;
                case 'B':
                    y++;
                case 'D':
                    x = x-1;
                case 'C':
                    x++;
                default:
                    cout << card;
            }
            cout << x << ", " << y << endl;
        }
        else if(card == 'e') {
            break;
        }
    }
    system("clear");
    game.bprint();
}

ubuntu 20.04 LTS 上的 g++ 和 C++ 11

【问题讨论】:

    标签: c++11 g++ console-application


    【解决方案1】:

    由于您在按下“A”时忘记调用break,因此执行是:y--; y++, x--; x++;。 “D”的效果几乎相同。如果你想打破你需要使用break这样的情况的顺序:

    switch(card) {
       case 'A':
          y--;
          break; // jumps to the end of the switch
       case 'B':
          y++;
          break;
       case 'D':
          x = x-1;
          break;
       case 'C':
          x++;
          break;
       default:
          cout << card;
          break;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 2020-08-02
      相关资源
      最近更新 更多