Ncurses game error

This is a discussion on Ncurses game error within the Linux Programming forums, part of the Platform Specific Boards category; I made a quick game with ncurses but 2 parts of the game I cant get to work. The quit ...

  1. #1
    The Registered User Aparavoid's Avatar
    Join Date
    May 2009
    Posts
    74

    Ncurses game error

    I made a quick game with ncurses but 2 parts of the game I cant get to work. The quit key(q) doesn't respond and the player in the game is able to go all directions except up. They are probably small things that I am missing but I still can't seem to find them. It might be a pain to try because the game gets stuck without a wall to stop the player from moving. Here is the source:

    Code:
    #include <ncurses.h>
    #include <stdlib.h>
    #include <time.h>
    
    typedef struct
    {
        int x;
        int y;
    }
    point;
    
    int up(point pink, point walls[50]);
    int left(point pink, point walls[50]);
    int down(point pink, point walls[50]);
    int right(point pink, point walls[50]);
    void move_up(point *pink, point walls[50]);
    void move_left(point *pink, point walls[50]);
    void move_down(point *pink, point walls[50]);
    void move_right(point *pink, point walls[50]);
    
    int main(void)
    {
        point pink = {0, 0};
        point scr;
        point walls[50];
        char c;
        int  i;
    
        srand(time(NULL));
    
        initscr();
        cbreak();
        noecho();
        curs_set(0);
        getmaxyx(stdscr, scr.y, scr.x);
    
        for(i = 0; i < 50; i++)
        {
            walls[i].x = rand() % scr.x;
            walls[i].y = rand() % scr.y;
        }
    
        do
        {
    
            for(i = 0; i < 50; i++)
            {
                mvaddch(walls[i].y, walls[i].x, ACS_BOARD);
            }
    
            mvaddch(pink.y, pink.x, 'P');
            refresh();
    
            c = getch();
    
            switch(c)
            {
                case 'w':
                    move_up(&pink, walls);
                    break;
                case 'a':
                    move_left(&pink, walls);
                    break;
                case 's':
                    move_down(&pink, walls);
                    break;
                case 'd':
                    move_right(&pink, walls);
                    break;
            }
    
            clear();
        }
        while(c != 'q' || c != 'Q');
    
        endwin();
        return 0;
    }
    
    int up(point pink, point walls[50])
    {
        int i;
    
        for(i = 0; i < 50; i++)
        {
    
            if(pink.y - 1 == walls[i].y && pink.x == walls[i].x)
            {
                return 1;
            }
    
        }
    
        return 0;
    }
    
    int left(point pink, point walls[50])
    {
        int i;
    
        for(i = 0; i < 50; i++)
        {
    
            if(pink.x - 1 == walls[i].x && pink.y == walls[i].y)
            {
                return 1;
            }
    
        }
    
        return 0;
    }
    
    int down(point pink, point walls[50])
    {
        int i;
    
        for(i = 0; i < 50; i++)
        {
    
            if(pink.y + 1 == walls[i].y && pink.x == walls[i].x)
            {
                return 1;
            }
    
        }
    
        return 0;
    }
    
    int right(point pink, point walls[50])
    {
        int i;
    
        for(i = 0; i < 50; i++)
        {
    
            if(pink.x + 1 == walls[i].x && pink.y == walls[i].y)
            {
                return 1;
            }
    
        }
    
        return 0;
    }
    
    void move_up(point *pink, point walls[50])
    {
    
        while(!up(*pink, walls))
        {
            pink->y--;
        }
    
        return;
    }
    
    void move_left(point *pink, point walls[50])
    {
    
        while(!left(*pink, walls))
        {
            pink->x--;
        }
    
        return;
    }
    
    void move_down(point *pink, point walls[50])
    {
    
        while(!down(*pink, walls))
        {
            pink->y++;
        }
    
        return;
    }
    
    void move_right(point *pink, point walls[50])
    {
    
        while(!right(*pink, walls))
        {
            pink->x++;
        }
    
        return;
    }
    Last edited by Aparavoid; 08-02-2009 at 04:23 PM. Reason: old code

  2. #2
    The Registered User Aparavoid's Avatar
    Join Date
    May 2009
    Posts
    74
    Nevermind. I got the features to work. Sorry for wasting a post. Ill post the finished game later for anyone thats want some ncurses example source code. It will have a level editor and some bugs fixed.
    Last edited by Aparavoid; 08-02-2009 at 04:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21