I wrote this article when I was a beginner in Linux. I used the curse library. Now I want to share it with you.
Blow it up: it seems that I have learned the C language well now, haha.
After talking nonsense, go to the Code:
// Mysnke1.0.c // compile the command: CC mysnke1.0.c-lcurses-O mysnke1.0 // use the direction key to control the direction of the snake # include <stdio. h> # include <stdlib. h> # include <curses. h> # include <signal. h> # include <sys/time. h> # define num 60 struct direct // indicates the {int CX; int Cy ;}; typedef struct node // The node of the linked list {int CX; int Cy; struct node * back; struct node * Next;} node; void initgame (); // initialize the game int setticker (INT); // set the timer void show (); // display the void showinformati of the entire Screen On (); // display Game Information (first two rows) void showsnake (); // display the body void getorder () of the snake (); // obtain the command void over (int I) from the keyboard; // void creatlink () after the game is completed; // (lead tail node) bidirectional linked list and its operation void insertnode (int x, int y); void deletenode (); void deletelink (); int ch; // input command int hour, minute, second; // int length, tTime, level; // (snake) length, timer, (game) grade struct direct Dir, food; // Forward Direction of the snake, node * head, * tail; // int main () {initscr (); initgame (); Signal (sigalrm, show); getorder (); endwin (); Return 0;} void initgame () {cbreak (); // enable noecho () in the cbreak mode of the terminal (); // close the echo curs_set (0); // set the cursor to invisible keypad (stdscr, true ); // use the keypad srand (time (0) on the keyboard of the user terminal; // set the Random Seed // initialize the data hour = minute = Second = tTime = 0; length = 1; Dir. cx = 1; Dir. cy = 0; CH = 'a'; food. cx = rand () % Cols; food. cy = rand () % (lines-2) + 2; creatlink (); setticker (20);} // sets the timer (this function is a book) In the preceding example, there is a change) int setticker (INT n_msecs) {struct itimerval new_timeset; long n_sec, n_usecs; n_sec = n_msecs/1000; n_usecs = (n_msecs % 1000) * 1000l; new_timeset.it_interval. TV _sec = n_sec;/* Set reload */records = n_usecs;/* new ticker value */n_msecs = 1; n_sec = n_msecs/1000; n_usecs = (n_msecs % 1000) * 1000l; new_timeset.it_value. TV _sec = n_sec;/* store t His */new_timeset.it_value. TV _usec = n_usecs;/* And this */return setitimer (itimer_real, & new_timeset, null);} void showinformation () {tTime ++; if (tTime> = 1000000) // tTime = 0; if (1! = TTime % 50) return; move (0, 3); // display the time printw ("Time: % d % C", hour, minute, second); Second ++; If (second> num) {second = 0; minute ++;} If (minute> num) {minute = 0; hour ++ ;} // display length, grade move (1, 0); int I; for (I = 0; I <Cols; I ++) addstr ("-"); move (0, cols/2-5); printw ("Length: % d", length); move (0, cols-10); Level = length/3 + 1; printw ("level: % d", level);} // The expression of the snake is represented by a two-way linked list of the leading tail node. // each forward of the snake Add a node to the head of the linked list and delete a node at the end. // If the snake eats a food, you do not need to delete the node. Void showsnake () {if (1! = TTime % (30-level) return; // determine whether the length of the snake has changed bool lenchange = false; // display the food move (food. cy, food. CX); printw ("@"); // If the snake hits the wall, if (Cols-1 = head-> next-> CX & 1 = dir. CX) | (0 = head-> next-> CX &-1 = dir. CX) | (lines-1 = head-> next-> Cy & 1 = dir. cy) | (2 = head-> next-> Cy &-1 = dir. cy) {over (1); return;} // If the snakeskin heads reach its own body, the game ends if ('*' = mvinch (Head-> next-> Cy + dir. cy, head-> next-> cx + dir. CX) {over (2 ); Return;} insertnode (Head-> next-> cx + dir. CX, head-> next-> Cy + dir. cy); // The snake eats a "food" If (Head-> next-> Cx = food. CX & head-> next-> Cy = food. cy) {lenchange = true; length ++; // congratulations, if (length >=50) {over (3); return ;} // reset the food location. cx = rand () % Cols; food. cy = rand () % (lines-2) + 2;} If (! Lenchange) {move (tail-> back-> cy, tail-> back-> CX); printw (""); deletenode ();} move (head-> next-> cy, head-> next-> CX); printw ("*");} void show () {signal (sigalrm, show ); // set the interrupt signal showinformation (); showsnake (); refresh (); // refresh the real screen} void getorder () {// create an endless loop, to read the command from the keyboard while (1) {CH = getch (); If (key_left = CH) {dir. cx =-1; Dir. cy = 0;} else if (key_up = CH) {dir. cx = 0; Dir. cy =-1;} else if (Key _ Right = CH) {dir. cx = 1; Dir. cy = 0;} else if (key_down = CH) {dir. cx = 0; Dir. cy = 1;} // Add these else if ('q' = CH) {endwin (); exit (1) ;}setticker (20 );}} void over (int I) {// display the reason for ending move (0, 0); Int J; For (j = 0; j <Cols; j ++) addstr (""); move (0, 2); if (1 = I) addstr ("crash the wall. game over. "); else if (2 = I) addstr (" crash itself. game Over "); else if (3 = I) addstr (" Mission complete "); sett Icker (0); // disable the timer deletelink (); // release the space of the linked list // Add these moves (1, 0); For (j = 0; j <Cols; j ++) addstr (""); move (1, 0); addstr ("Please enter any key to quit. "); getch (); endwin (); exit (1) ;}// create a two-way linked list void creatlink () {node * temp = (node *) malloc (sizeof (node); Head = (node *) malloc (sizeof (node); tail = (node *) malloc (sizeof (node )); temp-> Cx = 5; temp-> Cy = 10; head-> back = tail-> next = NULL; head-> next = Temp; temp-> next = tail; tail-> back = temp; temp-> back = head;} // The Head of the linked list (non-header node) insert a node void insertnode (int x, int y) {node * temp = (node *) malloc (sizeof (node); temp-> Cx = X; temp-> Cy = y; temp-> next = head-> next; head-> next = temp; temp-> back = head; temp-> next-> back = temp;} // Delete the last node void deletenode () {node * temp = tail-> back of the linked list (non-tail node; node * btemp = temp-> back; btemp-> next = tail; tail-> back = B Temp; temp-> next = temp-> back = NULL; free (temp); temp = NULL;} // Delete the whole linked table void deletelink () {While (Head-> next! = Tail) deletenode (); head-> next = tail-> back = NULL; free (head); free (tail );}
Run: