T
T
TyLo2019-07-21 19:15:35
linux
TyLo, 2019-07-21 19:15:35

Why does the rendering of the game territory in c++ constantly mow down in the linux terminal?

Good afternoon. For some time I have been struggling with the "render", which, when drawn, should show the game territory in the linux terminal, bounded by walls from "#". But the problem is that it is drawn somehow awry, shifts and other obscurantism appear, which I can’t solve in any way.

The code

#include <curses.h>
#include <iostream>

// Init game loop limiter
bool loop = false;

// Init game area
int area_w = 100;
int area_h = 20;

// Init player
int player_x = 2;
int player_y = 2;
enum player_c {
    UP = 119,
    DOWN = 115,
    LEFT = 97,
    RIGHT = 100
};

// Input
void g_input() {
    char c = getch();
    switch(c) {
        case UP:
            player_y -= 1;
            break;
        case DOWN:
            player_y += 1;
            break;
        case LEFT:
            player_x -= 1;
            break;
        case RIGHT:
            player_x += 1;
            break;
    }
}

// Render 
void g_render() {
    std::system("clear");
    
    for(int py = 0; py < area_h; py++) {
        for(int px = 0; px < area_w; px++) {
            if(px == 0 || px == area_w || py == 0 || py == area_h) {
                printf("#");
            } else {
                printf(" ");
            }
            if(px == player_x && py == player_y) {
                printf("@");
            }
        }
        printf("\n");
    }
}

int main() {
    initscr();
    
    loop = true;
    while(loop) {
        g_render();
        g_input();
    }
    
    endwin();
    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2019-07-21
@TyLo

Probably because the width of the terminal is 80 characters, not 100.

W
whiteblackness, 2019-07-22
@whiteblackness

Another option is that your font is not monospaced.
That is, characters can be of different widths

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question