How to run a loop until a keypress in C?

Abhinand

New Member
Disciple
Jun 3, 2009
10
0
0
28
How do i keep running a loop until user presses a key.

I'm coding a snake game with ncurses (linux) and its all done except that the snake does not move automatically :D

i heard it can be done be creating a seperate thread using pthreads?
 

deaddevil

Well-Known Member
Adept
Jul 17, 2006
422
9
87
gfxpixels.co.nr
I think it would be helpfull it you would post the code, so that a person who needs to help can study the code and then provide a solution.

I have never coded in ncurses, so unfortunately unable to help, but tried googling and came across this post.

check the link below and see if it helps.

Need help with a snake game (ncurses)

oh and btw once the prog is done, post the program, would like to run it :)
 

rapt0r

New Member
Disciple
Jun 15, 2006
699
11
0
in turboc we have a kbhit() function which returns true if a key is pressed

while(1)

{

code

if( kbhit() )

break;

}

i am not sure if it returns true or false, use ! appropriately.
 

Abhinand

New Member
Disciple
Jun 3, 2009
10
0
0
28
thanks guys.... managed to get it working by creating a new thread using pthread.h (have to get a better understanding of threading though)

heres the program incase anyone's interested

Code:
#include <unistd.h>

#include <curses.h>

#include <stdlib.h>

#include <pthread.h>

int wx,wy,x,y;

int ytar=0,xtar=0,nmove=0;

int posx[50],posy[50],snsize=5;

void placetar(void);

void moveup(void);

void moveright(void);

void moveleft(void);

void movedown(void);

void bingo (void);

void rempos (void);

void draw (void);

void input (char *);

int main(int argc, char** argv)

{
	char move;

	cbreak();
	pthread_t t2;
	WINDOW *win;

	win=initscr();

	getmaxyx(win,wx,wy);

	clear();

	refresh();
	x=0;y=0;
	while(1) {

		

		clear();

	    //move = getchar();

		pthread_create(&t2,NULL,input,&move);

		

		usleep(100000);

		if( move == 'd') {

			moveright();

			}

		else if(move == 's') {

		     movedown();

			 }

	    else if(move == 'a') {

		     moveleft(); 

			 }

		 

		 else if(move == 'w') {

		     moveup();

			 }

		

		else

		continue;

		

		draw();

		bingo();

		refresh();

	}

//pthread_join(t2, NULL);

	endwin();

	return 0;

}

void placetar(void) {

	if(!nmove) {

		xtar = rand() % wx;

		ytar = rand() % wy;

		nmove=1; 

	}

		move(xtar,ytar);

		delch();

		insch('*');

		
	}

        

void moveleft(void) {

 if(y==0)

 y=wy-1;

 else

 y--;

 rempos();

}

void moveright(void) {

 if(y==wy-1)

 y=0;

 else

 y++;

 rempos();

}

void moveup(void) {

 if(x==0)

 x=wx-1;

 else

 x--;

 rempos();

}

void movedown(void) {

 if(x==wx-1)

 x=0;

 else

 x++;

 rempos();

}

void bingo (void) {

	if (xtar==x && ytar==y) {

		move(xtar,ytar);

		delch();

		insch('#');

	    nmove=0;

		if(snsize <= 50)                   //max values array can hold is 50 :(

		snsize++;

	}

}

void rempos (void) {

	int i;

	static int prevsize;

	if( prevsize == snsize) {                      //if snake size increases

	for (i=0;i<snsize;i++) {                       //dont shift elements

		posx[i]=posx[i+1];                         //just add a # to array end

		posy[i]=posy[i+1];

  }}
	posx[snsize-1]=x;

	posy[snsize-1]=y;

	prevsize=snsize;

}

void draw (void) {

	int i;
	for (i=0;i<snsize;i++) {

		move(posx[i],posy[i]);

		delch();

		insch('#');

	}
	placetar();

}

void input (char *mv) {

	*mv=getchar();

}
U'r pretty much invincible in the game.... it has no "GAME OVER" :hap2:

and controls are w,a,s,d
 

nitant

Active Member
Adept
Mar 9, 2009
589
34
42
33
I think getch() will work. This will keep on waiting until any key is pressed.

Whoops just saw that u got it working.