Snake, c, c ++, Snake

Source: Internet
Author: User

Snake, c, c ++, Snake

I learned c ++ for half a semester and made a game by myself. (What's wrong, please note )
# Include <windows. h> # include <conio. h> # include <ctime> # include <iostream> # include <cmath> # include <mmsystem. h> # include <stdio. h> # include <stdlib. h> # pragma comment (lib, "Winmm. lib ") // import Winmm. lib library using namespace std; int node; // int life of the snake node; // life, 0 live, 1 death int Q; // control the food color int fa, fb; // The Food coordinate int q; // the upper and lower sides of the control int ti = 0, ti1 = 0; // timeint nd = 0; // The difficulty char k; int a [4] [2] = {-}, {}, {0,-1}, {}; // struct food {I Nt f1; int f2; int need;} f; struct she {int x; int y;} s [1000], t; HANDLE hout = GetStdHandle (STD_OUTPUT_HANDLE ); // COORD coord; void hide () // hide the cursor {lele_cursor_info cursor_info = {}; SetConsoleCursorInfo (hout, & cursor_info);} void gotoxy (int x, int y) {lele_screen_buffer_info cs; HANDLE hConsoleOut = GetStdHandle (STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo (hConsoleOut, & cs); cs. dwCursorPositio N. X = y; cs. dwCursorPosition. Y = x; SetConsoleCursorPosition (hConsoleOut, cs. dwCursorPosition);} void color (int a) // color function {SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), a);} void weiqiang (void) // circle {color (14); int a = 25, B = 20, c = 25, d = 20; while (--) {cout <'-';} cout <endl; while (B --) {cout <'|'; cout <endl;} while (d --) {gotoxy (20-d, 24); cout <'|'; cout <endl;} while (c --) {cout <'-';} cout <endl; Gotoxy (1, 1);} void food1 (void) // food {f. f1 = 5; f. f2 = 5; int I; srand (unsigned) time (NULL); while (1) {loop: f. f1 = rand () % 19; // random number f. f2 = rand () % 24; for (I = 0; I <node; I ++) {if (f. f1 = s [I]. x | f. f2 = s [I]. y) {goto loop;} if (f. f1 = 0 | f. f2 = 0) // prevent exceeding the box {continue;} else {break;} if (f. need = 0) {fa = f. f1; fb = f. f2; // f. f1, f. f2 is assigned to the global variable gotoxy (f. f1, f. f2); while (1) {Q = rand () % 16; if (Q> 9) {break;} color (Q ); cout <"*" ;}} int died (void) // Judge Death {Int I, flag = 0; for (I = 1; I <node; I ++) {if (s [0]. x = s [I]. x & s [0]. y = s [I]. y) {flag = 1 ;}} if (s [0]. x = 0 | s [0]. x = 21 | s [0]. y = 0 | s [0]. y = 24) | flag = 1) {return 0;} else {return 1 ;}} void tyd (char c) {if (abs (c-k )! = 3 & abs (c-k )! = 4 & abs (c-k )! = 0) // control the Snake Head following the body {if (c = 'A') {q = 2;} else if (c ='s ') {q = 1;} else if (c = 'W') {q = 0;} else if (c = 'D') {q = 3 ;}} s [0]. x + = a [q] [0]; s [0]. y + = a [q] [1];} void yd (void) // direction of movement {char c = 'D'; int I, a = 200; s [0]. x = 1; s [0]. y = 1; node = 2; do {ti + = 1; // it must be returned to 0 again, otherwise ti1 + = ti; if (ti> 1000) {ti = 0;} if (ti % 150 = 0) {if (a> 25) {nd ++; a-= 25 ;}} food1 (); t. x = s [node-1]. x; // save the last * t. y = s [node-1]. y; for (I = node-1; I> 0; I --) // body movement {s [I]. x = s [I-1]. x; s [I]. y = s [I-1]. y;} For (I = 1; I <node; I ++) {color (13); gotoxy (s [I]. x, s [I]. y); cout <"*";} gotoxy (t. x, t. y); // Delete the last * cout <""; if (_ kbhit () // accept the upper, lower, and left of the keyboard input, and change the direction to {k = c; c = _ getch ();} tyd (c); // move gotoxy (s [0]. x, s [0]. y); // move the cout header <"#"; if (s [0]. x = fa & s [0]. y = fb) // only one {f. need = 0; node ++;} else {f. need = 1 ;}sleep (a); if (died () = 0) // determines whether {system ("cls"); // clear the screen break ;} color (12); gotoxy (); cout <"food statistics:" <node-2; gotoxy (); col Or (11); cout <"game difficulty:" <nd;} while (1);} void jieshu (void) {color (12 ); cout <"time spent:" <ti1/60 <"s" <endl; cout <"o (too many) o (too many seconds) o (always writable) o "<endl; cout <" ----------------------------------------------- "<endl; cout <" | You lose! | "<Endl; cout <" --------------------------------------------- "<endl;} int main () {PlaySound (TEXT (" D :\\ create a folder (3) \ Zhang Xueyou-use Yu Sheng to love .wav "), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); // loop playback // SND_ASYNC | SND_NODEFAULT (single playback) while (1) {weiqiang (); yd (); jieshu (); system ("pause"); break;} 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.