#include #include #include #include #include #include #include "config.h" #include "misc.h" #include "screen.h" int quit (int sig) { endgraphics(); exit(0); } int main (int argc, char *argv[]) { // The tree windows (Main,Grid,Status in that order) used by ncurses WINDOW *w [3]; // The grid, to make it easy to tell where the worm is bool grid [GRIDX] [GRIDY]; // The worm history short int hx [HISTORYSIZE]; short int hy [HISTORYSIZE]; // Worm variables int length = STARTLENGTH; // the length of the worm int extralength = STARTELENGTH; // how much the worm is going to grow int curx,cury; // current position of the worm int dir = RIGHT; // direction the worm is facing int lastdir = RIGHT; // last direction int score = 0; bool paused = false; int dotx,doty; // position of the dot (worm food) int tick; // current tick // User input int ch; // Initialize ncurses and the windows startgraphics(w); // Seed the random number generator randomize(); // clear the grid memset(grid,0,sizeof(grid)); // Initialize and draw the worm curx = (GRIDX+length) >> 1; cury = GRIDY >> 1; for (tick=0;tick=GRIDX) || (cury<0) || (cury>=GRIDY)) { endgraphics(); printf("You crashed in a wall. Your score was %d.\n",score); exit(0); } // check whether or not the worm has crashed with itself if (grid[curx][cury]) { endgraphics(); printf("You crashed with yourself. Your score was %d.\n",score); exit(0); } // the worm is still on the grid, and it seems healty :) drawblock(w,curx,cury); // let's draw the new block grid[curx][cury] = true; // and update the grid // update the history array hx[tick] = curx; hy[tick] = cury; // has the worm eaten food? if ((dotx == curx) && (doty == cury)) { score++; extralength += WORMGROWTH; newdot(w,grid,&dotx,&doty); } // if the worm still has some growing to do.. if (extralength > 0) { extralength--; length++; } else { // otherwise, clear the last block clearblock(w,hx[tick-length],hy[tick-length]); grid[hx[tick-length]][hy[tick-length]] = 0; } wrefresh(w[Grid]); // finally, update the status line mvwprintw(w[Status],0,0,"Score: %d",score); mvwprintw(w[Status],0,GRIDX - 2 /* because of the border lines */ - 7 /* strlen("PAUSED ") */," "); wrefresh(w[Status]); usleep(TICKSPEED); tick++; // if the history array is full, copy the length of the worm // to the beginning of the array and decrease the tick counter if (tick == HISTORYSIZE) { memcpy(hx, hx+HISTORYSIZE-length, length * sizeof(short int)); memcpy(hy, hy+HISTORYSIZE-length, length * sizeof(short int)); tick = length; } // ready for next tick! } endgraphics(); }