C ++ source code

Source: Internet
Author: User

From: http://blog.csdn.net/hp_justdoit/article/details/8456000

# Include <iostream> # include <windows. h> # include <conio. h> # include <time. h> # include <stdlib. h> using namespace STD; const int n = 21; // n indicates the side length of the square that the snake can move. Void get_xy (int x, int y) // locate the cursor position {handle Hout; coord Pos; POS. X = x * 2; POS. y = y; Hout = getstdhandle (std_output_handle); setconsolecursorposition (Hout, POS);} void color (INT num) // set the color {handle Hout; Hout = getstdhandle (handler ); setconsoletextattribute (Hout, num);} void initial () // initialize {int I, j; int wall [n + 2] [n + 2] ={ {0 }}; for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) wall [I] [J] = 1; color (11); for (I = 0; I <n + 2; I ++) {for (j = 0; j <n + 2; j ++) {If (Wall [I] [J]) cout <"■"; else cout <"□" ;}cout <Endl ;}get_xy (N + 3, 1 ); color (20); cout <"perform operations by 'w', 's', 'A', and 'D'" <Endl; get_xy (n + ); color (20); cout <"press any key to pause" <Endl; get_xy (N + 3); color (20); cout <"score: "<Endl;} void game () {int ** snake = NULL; // snake is an array of int Len = 1; int I; int score = 0; int Apple [2]; int tail [2]; char CH = 'P'; initial (); // obtain a random food location srand (unsigned) Time (null); Apple [0] = rand () % N + 1; Apple [1] = rand () % N + 1; get_xy (Apple [0], Apple [1]); color (12); cout <"●" <Endl; // allocate memory space to the snake array. Snake = (INT **) realloc (snake, sizeof (int *) * Len); for (I = 0; I <Len; I ++) Snake [I] = (int *) malloc (sizeof (INT) * 2 ); // start placing the snake in the center of the page. Snake [0] [0] = n/2; snake [0] [1] = n/2 + 1; get_xy (snake [0] [0], snake [0] [1]); color (14); cout <"⊙" <Endl; int flag = 1; // while (1) {// each move, the last tail is restored to the background color if (FLAG) {tail [0] = snake [len-1] [0]; tail [1] = snake [len-1] [1]; get_xy (tail [0], tail [1]); color (11 ); cout <"■" <Endl;} flag = 1; for (I = len-1; I> 0; I --) {snake [I] [0] = snake [I-1] [0]; snake [I] [1] = snake [I-1] [1]; get_xy (snake [I] [0], snake [I] [1]); color (14); cout <"★"<Endl ;} /* ===================================================== ================================ Function Name: in kbhit () (VC ++ 6.0, It is the _ kbhit () function and return value: Check whether there is a keyboard input. If yes, a non-0 value is returned. Otherwise, the return 0 method is used: int kbhit (void); contains the header file: Include <conio. h ========================================================== =================================== */If (kbhit ()) {get_xy (0, N + 3); CH = getche () ;}switch (CH) {Case 'W ': snake [0] [1] --; break; Case's ': Snake [0] [1] ++; break; Case 'A': c ASE 'A': Snake [0] [0] --; break; Case 'D': Snake [0] [0] ++; break; default: break;} for (I = 1; I <Len; I ++) {// The snake bites the snake and the game ends! If (snake [0] [0] = snake [I] [0] & snake [0] [1] = snake [I] [1]) {get_xy (n/2, n/2); color (30); cout <"game over! "<Endl; exit (0) ;}} get_xy (snake [0] [0], snake [0] [1]); color (14 ); cout <"⊙" <Endl; /* ===================================================== ============================== Function Name: sleep function: run the unsigned sleep (unsigned milliseconds) function for a period of time; Use the header file # include <windows. h> in the GCC compiler, the header files used vary with GCC versions # include <unistd. h ========================================================== ===================================== * // the higher the score, the faster the snake moves, sleep (ABS (200-0.5 * score); // snake If (snake [0] [0] = Apple [0] & snake [0] [1] = Apple [1]) {flag = 0; score ++; Len ++; srand (unsigned) Time (null); snake = (INT **) realloc (snake, sizeof (int *) * Len ); snake [len-1] = (int *) malloc (sizeof (INT) * 2); get_xy (n + 6, 3); color (20); cout <score <Endl; apple [0] = rand () % N + 1; Apple [1] = rand () % N + 1; get_xy (Apple [0], Apple [1]); color (12); cout <"●" <Endl ;}// the snake head hits the wall and the game is over! If (snake [0] [0] = 0 | snake [0] [0] = n | snake [0] [1] = 0 | snake [0] [1] = N) {get_xy (n/2, n/2); color (30); cout <"game over! "<Endl; for (I = 0; I <Len; I ++) Free (snake [I]); sleep (infinite); exit (0 );} // game clearance! If (LEN> = N * n/20) {get_xy (n/2, n/2); color (30); cout <"win! "<Endl; for (I = 0; I <Len; I ++) Free (snake [I]); sleep (infinite); exit (0 );}}} int main () {game (); Return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.