Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Hey guys I need some help with my c++ snek program

+3 votes
asked Aug 25, 2020 by ZX228 (400 points)
closed Aug 25, 2020 by ZX228
The getch isn't working the way I want it to.

namespace sense {
    using namespace sense;
    
    void change_direction() {
        if ((c == 'd') && (direction != "left")) {direction = "right";}
        if ((c == 'a') && (direction != "right")) {direction = "left";}
        if ((c == 'w') && (direction != "down")) {direction = "up";}
        if ((c == 's') && (direction != "up")) {direction = "down";}
    }
    
    void sense_keypress() {
        if ((c == 'a') || ((c == 'd') || ((c == 's') || ((c == 'w')) {d = c;}
        initscr();
        timeout(100);
        c = getch();
        endwin();
        
        if ((c != 'd') && (c != 'a') && (c != 'w') && (c != 's')) {c = d;}
        cout << c;
        if ((c == 'd') && (direction != "left")) {direction = "right";}
        if ((c == 'a') && (direction != "right")) {direction = "left";}
        if ((c == 'w') && (direction != "down")) {direction = "up";}
        if ((c == 's') && (direction != "up")) {direction = "down";}
        
        cout << direction;
    }
    
    
}

int doit = (100000 - (level_num^10));

int dist_2 = 0;
namespace run {
    using namespace run;
    using namespace setup;
    using namespace this_thread; // sleep_for, sleep_until
    using namespace chrono; //nanoseconds, system_clock, seconds
    using namespace sense;
    using namespace setup;
    
    
    void move_right() {
        if ((head_pos[1] < 28) && (touching_self == false) && (dist_moved == dist_2)) {
            if (length == 1) {base[head_pos[0]][head_pos[1]] = 0;}
            dist_2++;
            head_pos[1]++;
            
            base[head_pos[0]][head_pos[1]] = 2;
            
        }
    }
    
    void move_left() {
        if ((head_pos[1] > 1) && (touching_self == false) && (dist_moved == dist_2)) {
            if (length == 1) {base[head_pos[0]][head_pos[1]] = 0;}
            dist_2++;
            head_pos[1]--;
            
            base[head_pos[0]][head_pos[1]] = 2;
            
        }
    }
    
    void move_up() {
        if ((head_pos[0] > 1) && (touching_self == false) && (dist_moved == dist_2)) {
            if (length == 1) {base[head_pos[0]][head_pos[1]] = 0;}
            dist_2++;
            head_pos[0]--;
            
            base[head_pos[0]][head_pos[1]] = 2;
            
        }
    }
    
    void move_down() {
        if ((head_pos[0] < 18) && (touching_self == false) && (dist_moved == dist_2)) {
            if (length == 1) {base[head_pos[0]][head_pos[1]] = 0;}
            dist_2++;
            head_pos[0]++;
            
            base[head_pos[0]][head_pos[1]] = 2;
            
        }
    }
    
    
    void move() {
        usleep(doit);
        
        
        sense_keypress();
        
        if (direction == "right") {
            move_right();
            usleep(10000);
            dist_moved++;
            
        }
        
        if (direction == "left") {
            move_left();
            usleep(10000);
            dist_moved++;
            
            
        }
        
        if (direction == "up") {
            move_up();
            usleep(10000);
            dist_moved++;
            
        }
        
        if (direction == "down") {
            move_down();
            usleep(10000);
            dist_moved++;
            
        }
        
        for (int z = 25; z > 0; z--) {
            cout << "\033[F                                                               ";
        }
        cout << "\r";
        
        for (int r = 0; r < 20; r++) {
            for (int c = 0; c < 30; c++) {
                if (base[ r ][ c ] == 5) {cout << " |";}
                if (base[ r ][ c ] == 4) {cout << "| ";}
                if (base[ r ][ c ] == 3) {cout << "--";}
                if (base[ r ][ c ] == 2) {cout << "[]";}
                if (base[ r ][ c ] == 1) {cout << "{}";}
                if (base[ r ][ c ] == 0) {cout << "  ";}
            }
        cout << endl;
        }
        
        
        
        
    }
    
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...