C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2009, 03:22 PM   #1
The Registered User
 
Aparavoid's Avatar
 
Join Date: May 2009
Posts: 72
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 03:23 PM. Reason: old code
Aparavoid is offline   Reply With Quote
Old 08-02-2009, 03:34 PM   #2
The Registered User
 
Aparavoid's Avatar
 
Join Date: May 2009
Posts: 72
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 03:56 PM.
Aparavoid is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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