#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();
}