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;
}