A rich array of functions, such as color snakes, item items, and level !, Snake item

Source: Internet
Author: User

A rich array of functions, such as color snakes, item items, and level !, Snake item

Color snake implementation, powerful functions, level modules, item functions, effect preview later, first on the Code:

Console. h

# Include <windows. h> // color definition # define FWHITE | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE // white foreground # define FRED foreground | foreground // red foreground # define FBLUE foreground | FOREGROUND_BLUE Foreground/blue foreground # define FGREEN foreground | FOREGROUND_GREEN // green foreground # define FYELLOW foreground | FOREGROUND_RED | FOREGROUND_GREEN // yellow foreground # define FPURPLE foreground | FOREGROUND_BLUE // purple foreground # define fblack null // black foreground # define bblack null // black background # define BWHITE background | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE // white background # define BRED background | BACKGROUND_RED // red background # define BBLUE background | BACKGROUND_BLUE // blue background # define BGREEN background | BACKGROUND_GREEN // green background # define BYELLOW background | BACKGROUND_GREEN // yellow background # define BPURPLE background | BACKGROUND_RED | BACKGROUND_BLUE // purple background void xSetCursor (int x, int y) {COORD c; c. X = x-1; c. Y = y-1; SetConsoleCursorPosition (GetStdHandle (rows), c);} void HideCursor () {lele_cursor_info cursor_info = {1, 0}; SetConsoleCursorInfo (GetStdHandle (rows ), & cursor_info);} void xSetColor (WORD color) {SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), color);}/* int SetConsoleTitle (char * title) */int ShowConsoleInfo () {int ret; HANDLE hWin = GetStdHandle (STD_OUTPUT_HANDLE); lele_screen_buffer_info csbiInfo; ret = GetConsoleScreenBufferInfo (hWin, & csbiInfo); if (ret) {printf ("Buffer Size: [X] % d-[Y] % d \ n ", csbiInfo. dwSize. x, csbiInfo. dwSize. y); printf ("Cursor Pos: [X] % d-[Y] % d \ n", csbiInfo. dwCursorPosition. x, csbiInfo. dwCursorPosition. y); printf ("Attributes: % d \ n", csbiInfo. wAttributes); printf ("Current Win: [L] % d-[R] % d-[T] % d-[B] % d \ n", \ csbiInfo. srWindow. left, csbiInfo. srWindow. right, csbiInfo. srWindow. top, csbiInfo. srWindow. bottom); printf ("Maximum Win: [X] % d-[Y] % d \ n", csbiInfo. dwMaximumWindowSize. x, csbiInfo. dwMaximumWindowSize. y); puts ("Over. ");} return ret ;}


Setting. h

# Include <windows. h> // set the interface pattern to char sharp [] [3] = {"★"," ■ "," □", "◎", "◆ "}; # Define snkehead0 # define region # define Wall1 # define White2 # define snkeend3 # define SFood4 # define startsnkelen8 // game setting # define MAP_WIDTH 24 # define MAP_HIGH 22 # define limit 5 # define limit 10 # define CTRLWNDHIGH MAP_HIGH # define MINSPEED 400 # define MAXSPEED 200 // Snake defines struct Snake {int x, y; struct Snake * next, * front;}; struct Snake * snke_head; struct Snake * snke_end = NULL; struct Snake * snke_oldend = NULL; struct Snake Food; struct Snake * ExtraFood [5]; struct Snake temp; enum direct {LEFT, RIGHT, UP, DOWN}; enum direct Direct direct; // food enum styfood {FOOD_NORMAL, FOOD_GETDEF, FOOD_FIVEFOOD}; enum styfood FoodStyle; enum styfood ExtraFoodStyle [5]; // defines the attribute long intScore = 0; intIsPause = 0; intEatNum = 0; intLEVEL = 0; long intEXP = 0; intSPEED = MINSPEED; intTool_Defense = 0; intTool_FiveFood = 0;/* initialize data */void Init () {snke_head = (struct Snake *) malloc (sizeof (struct Snake )); int I; for (I = 0; I <5; I ++) {ExtraFood [I] = (struct Snake *) malloc (sizeof (struct Snake )); extraFood [I]-> next = NULL;} temp. x = 0; temp. y = 0; Score = 0; IsPause = 0; EatNum = 0; LEVEL = 0; EXP = 0; SPEED = MINSPEED; Direct = RIGHT; Tool_Defense = 10; tool_FiveFood = 10 ;}


Snake. h

# Include <stdio. h> # include <conio. h> # include <stdlib. h> # include <time. h> # include <string. h> # include "setting. h "# include" console. h "/* function declaration * // * opening screen */void BeginImage ();/* initialize, draw snakes, and draw food */void Start ();/* move, including snake profiling */void Move ();/* draw map */void Draw_Map ();/* draw all snakes */void Draw_Snake (); /* output score */void Draw_Score ();/* draw food */void Draw_Food ();/* draw control area */void Draw_CtrlWnd (); /* output the game prompt */void Draw_Play ();/* output the upgrade prompt */void Draw_UpLevel ();/* enter Output tool information */void Draw_Tool ();/* determine whether to die */int IsDeath ();/* determine whether the input is a game control button */int IsGameCtrl (unsigned char ch ); /* generate food */int CreateFood (int count);/* food is eaten, including food painting */int EatFood (); /// * snake control */int snke_ctrl (unsigned char ch);/* snake not dead */int snke_defense ();/* game pause */void Game_Pause (); /* game end */void Game_Over ();/* calculate and output the score */void CalcScore (int num);/* clear the control area */void Cls_CtrlWnd (); /* Control Area flashing prompt */void CtrlWnd_Flash (char * a, char * B, int lo C, int); void BeginImage () {// HideCursor (); xSetColor (FGREEN ); printf ("\ n/^ \ n"); printf ("◇ _|__ | ◇ |\\ n "); printf ("\\/~ 〓 \ _/\ N "); printf (" by \ ____ | ________/\ n "); printf ("Ren \ _______ \ n"); printf ("Yi '\ n "); printf ("Key | greedy \ n"); printf ("Incoming // eat \ n "); printf ("inbound // snake \\\\\ n"); printf ("greedy // \\\\\ n "); printf ("Eat // \\\\\ n"); printf ("Snake // _ ---- \\\\\\ n "); printf ("World //_-~ ~ -_ \ | \ N "); printf (" ((_-~ _--_~ -_ \/| \ N "); printf ("! \\~ -____-~ _-~ ~ -_~ -_-~ /\ N "); printf ("~ -__-~ ~ -__-~ \ N "); printf ("~ --______-~ ~ -___-~ \ N "); printf (" \ n "); int t = getch (); if (t = 0 | t = 224) getch (); // special input will contain two characters, for example, F1 ~ F12 and control key system ("cls");} void Start () {snail ke_head-> x = MAP_WIDTH/3*4-1; // The coordinates of the Snake header are "Snail ke_head"-> y = MAP_HIGH/4*1; "Snail ke_head"-> front = NULL; "struct Snake * ps =" Snail ke_head "," * p; int m; for (m = 2; m <= startsnkelen; m ++) // Snake coordinate {p = (struct Snake *) malloc (sizeof (struct Snake )); p-> x = MAP_WIDTH/3*4-m * 2 + 1; p-> y = MAP_HIGH/4*1; p-> next = NULL; p-> front = ps; ps-> next = p; ps = p;} snail ke_end = snail ke_head; while (S Nake_End-> next! = NULL) snail ke_end = fig-> next; Draw_Map (); Draw_CtrlWnd (); Draw_Snake (); Draw_Score (); Draw_Tool (); Draw_Play (); xSetColor (FPURPLE ); xSetCursor (MAP_WIDTH/2*2-7, MAP_HIGH/2); printf ("Start with any key! "); Int t = getch (); if (t = 0 | t = 224) getch (); // special input will enter two characters, such as F1 ~ F12 and control key xSetColor (FGREEN); xSetCursor (MAP_WIDTH/2*2-7, MAP_HIGH/2); for (m = 1; m <= 8; m ++) printf ("% 2 s", sharp [White]); srand (time (NULL); CreateFood (0); Draw_Food ();} void Draw_Map () {// depict map xSetColor (FGREEN); int m, n; for (m = 1; m <= MAP_HIGH; m ++) {for (n = 1; n <= MAP_WIDTH; n ++) printf ("% s", sharp [White]); xSetCursor (1, m + 1) ;}for (m = 1; m <= MAP_HIGH; m ++) {xSetCursor (1, m); printf ("% s", sharp [Wall]); xSe TCursor (MAP_WIDTH * 2-1, m); printf ("% s", sharp [Wall]);} for (m = 1; m <= MAP_WIDTH; m ++) {xSetCursor (m * 2-1,1); printf ("% s", sharp [Wall]); xSetCursor (m * 2-1, MAP_HIGH ); printf ("% s", sharp [Wall]) ;}void Draw_Snake () {// draw all snakes xSetColor (FRED); struct Snake * ps = snke_head; (; ps! = NULL; ps = ps-> next) {xSetCursor (ps-> x, ps-> y); if (ps = snke_head) printf ("% s ", sharp [snkehead]); else if (ps-> next = NULL) printf ("% s", sharp [snkeend]); else printf ("% s ", sharp [snkebody]) ;}} int IsDeath () {struct Snake * ps = snail ke_head; switch (Direct) // collided with the Snake body {case LEFT: for (; ps! = NULL; ps = ps-> next) if (snail ke_head-> y = ps-> y) & (snail ke_head-> x-2 = ps-> x) return 1; if (snail ke_head-> x-2 <= 2) return 1; break; case RIGHT: for (; ps! = NULL; ps = ps-> next) if (snail ke_head-> y = ps-> y) & (snail ke_head-> x + 2 = ps-> x) return 1; if (snail ke_head-> x + 2> = MAP_WIDTH * 2-1) return 1; break; case UP: for (; ps! = NULL; ps = ps-> next) if (snail ke_head-> y-1 = ps-> y) & (snail ke_head-> x = ps-> x) return 1; if (snail ke_head-> y-1 <= 1) return 1; break; case DOWN: for (; ps! = NULL; ps = ps-> next) if (snail ke_head-> y + 1 = ps-> y) & (snail ke_head-> x = ps-> x) return 1; if (snail ke_head-> y + 1> = MAP_HIGH) return 1; break;} return 0 ;} void Move () {// Move struct Snake * ps = (struct Snake *) malloc (sizeof (struct Snake); // initialize the new Snake header switch (Direct) {case LEFT: ps-> x = snail ke_head-> x-2; ps-> y = snail ke_head-> y; break; case RIGHT: ps-> x = snail ke_head-> x + 2; ps-> y = snail ke_head-> y; break; case UP: ps-> x = Snail ke_head-> x; ps-> y = snail ke_head-> y-1; break; case DOWN: ps-> x = snail ke_head-> x; ps-> y = snail ke_head-> y + 1; break;} ps-> front = NULL; ps-> next = snail ke_head; snail ke_head-> front = ps; snail ke_head = ps; // fill the map xSetColor (FGREEN); // Map Color xSetCursor (snail ke_end-> x, snail ke_end-> y); printf ("% 2 s", sharp [White]); // draw the snake xSetColor (FRED); // The Snake color xSetCursor (snail ke_head-> x, snail ke_head-> y); // draw the Snake Head printf ("% 2 s ", sharp [snkehead]); xSetCursor (Snail ke_head-> next-> x, snail ke_head-> next-> y); // draw the second section of the snake body printf ("% 2 s", sharp [snail kebody]); xSetCursor (snail ke_end-> front-> x, snail ke_end-> front-> y); // draw a snake tail printf ("% 2 s", sharp [snail keend]); snail ke_end = snail ke_end-> front; free (snail ke_end-> next); // Delete the last node, namely, snail ke_end-> next = NULL;} int CreateFood (int count) {// generate the int Success = 0; struct Snake * ps; int num, I; if (count = 0) {while (! Success) {Food. x = rand () % (MAP_WIDTH-3) * 2 + 3; Food. y = rand () % (MAP_HIGH-2) + 2; for (ps = snail ke_head; ps! = NULL; ps = ps-> next) if (ps-> x = Food. x) & (ps-> y = Food. y) {Success = 0; break;} if (ps = NULL) Success = 1;} if (Success) // food style {num = rand () % 16; if (num = 1) FoodStyle = FOOD_GETDEF; else if (num = 2) FoodStyle = FOOD_FIVEFOOD; elseFoodStyle = FOOD_NORMAL; }} else {for (I = 0; I <5; I ++) {Success = 0; while (! Success) {// random food location ExtraFood [I]-> x = rand () % (MAP_WIDTH-3) * 2 + 3; extraFood [I]-> y = rand () % (MAP_HIGH-2) + 2; for (ps = snke_head; ps! = NULL; ps = ps-> next) if (ps-> x = ExtraFood [I]-> x) & (ps-> y = ExtraFood [I]-> y) {Success = 0; break;} if (ps = NULL) Success = 1 ;} if (Success) // food style {ExtraFood [I]-> next = & temp; // avoid NULL value num = rand () % 20; if (num = 1) extraFoodStyle [I] = FOOD_GETDEF; else if (num = 2) ExtraFoodStyle [I] = FOOD_FIVEFOOD; elseExtraFoodStyle [I] = FOOD_NORMAL ;}} return Success;} int EatFood () {int success = 0; struct Snake * ps = (Struct Snake *) malloc (sizeof (struct Snake); if (snail ke_head-> x = Food. x & amp; snke_head-& gt; y = Food. y) {// The Food is eaten ps-> x = Food. x; ps-> y = Food. y; ps-> next = snail ke_head; ps-> front = NULL; snail ke_head-> front = ps; snail ke_head = ps; // generate food CreateFood (0 ); // depict Food xSetColor (FWHITE); xSetCursor (Food. x, Food. y); printf ("% 2 s", sharp [SFood]); success = 1; if (FoodStyle = FOOD_GETDEF) Tool_Defense ++; if (FoodStyle = FOOD_FIVEFOOD) Tool_FiveFood ++;} int I; for (I = 0; I <5; I ++) // additional food {if (ExtraFood [I]-> next! = NULL) {if (snail ke_head-> x = ExtraFood [I]-> x & snail ke_head-> y = ExtraFood [I]-> y) {ps = (struct Snake *) malloc (sizeof (struct Snake); ps-> x = ExtraFood [I]-> x; ps-> y = ExtraFood [I]-> y; ps-> next = snail ke_head; ps-> front = NULL; snail ke_head-> front = ps; snail ke_head = ps; if (ExtraFoodStyle [I] = FOOD_GETDEF) Tool_Defense ++; if (ExtraFoodStyle [I] = FOOD_FIVEFOOD) Tool_FiveFood ++; ExtraFood [I]-> next = NULL; success = 1 ;}}} Draw_Tool (); return success;} int snke_ctrl (unsigned char ch) {switch (ch) // returns 1 for Sanke_Defense () {case 224: snail ke_ctrl (getch (); break; case 72: case 119: if (Direct! = DOWN) Direct = UP; return 1; case 80: case 115: if (Direct! = UP) Direct = DOWN; return 1; case 77: case 100: if (Direct! = LEFT) Direct = RIGHT; return 1; case 75: case 97: if (Direct! = RIGHT) Direct = LEFT; return 1; case 32: IsPause = 1; break; case 49: if (Tool_FiveFood> 0) {CreateFood (5); Draw_Food (); tool_FiveFood --; Draw_Tool ();} default: while (kbhit () {if (IsGameCtrl (ch) snke_ctrl (getch (); else ch = getch ();}} return 0;} int snail ke_defense () {if (Tool_Defense> 0) {CtrlWnd_Flash ("crashed! "," Invincible shield enabled! ", 4, 1); CtrlWnd_Flash (" crashed! "," Invincible shield enabled! ", 4, 1); CtrlWnd_Flash (" crashed! "," Invincible shield enabled! ", 4, 1); CtrlWnd_Flash (" hurry up! "," Continue in another direction! ", 6, 1); CtrlWnd_Flash (" hurry up! "," Continue in another direction! ", 6, 0); while (IsDeath () {if (snail ke_ctrl (getch ())&&! IsDeath () break;} CtrlWnd_Flash ("", "", 6, 0); Tool_Defense --; Draw_Play (); Draw_Tool (); return 1;} return 0 ;} void Draw_Score () {xSetColor (FGREEN); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 4, 3); printf ("grade: % 8d", LEVEL ); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 4, 4); if (Score = 0) printf ("Experience: % 8d", EXP); elseprintf ("experience: % 6d/% d ", EXP, (LEVEL + 1) * 30); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 4, 5); printf (" Swallow: % 8d ", eatN Um); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 4, 6); printf ("Score: % 8d", Score);} void Draw_CtrlWnd () {// painting Control Panel int m, n; // xSetColor (BBLACK | FBLUE); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT, 1); for (n = 1; n <= CTRLWNDWIDTH; n ++) {printf ("% 2 s", sharp [Wall]);} xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT, CTRLWNDHIGH ); for (n = 1; n <= CTRLWNDWIDTH; n ++) {printf ("% 2 s", sharp [Wall]) ;}for (m = 1; m <= CTRLWNDHIGH; m ++) {xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT, m); printf ("% 2 s", sharp [Wall]); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + CTRLWNDWIDTH * 2, m ); printf ("% 2 s", sharp [Wall]) ;}} int IsGameCtrl (unsigned char ch) {switch (ch) {case 49: // 1 case 97: // a case 100: // dcase 115: // scase 119: // wcase 224: // control case 72: // upper case 75: // left case 80: // case 77: // right case 32: // space return 1; default: return 0 ;}} void Game_Pause () {int m; xSetColor (FPURPLE ); xSetCursor (MA P_WIDTH/2*2-6, MAP_HIGH/2); printf ("paused! "); While (IsPause) {CtrlWnd_Flash (" paused! "," Press any key to continue! ", 224); if (kbhit () {if (getch () =) getch (); IsPause = 0 ;}} xSetColor (FGREEN ); xSetCursor (MAP_WIDTH/2*2-7, MAP_HIGH/2); for (m = 1; m <= 8; m ++) printf ("% 2 s ", sharp [White]); Draw_Snake (); Draw_Play ();} void Game_Over () {Cls_CtrlWnd (); Draw_Score (); xSetColor (FPURPLE ); xSetCursor (MAP_WIDTH/2*2-10, MAP_HIGH/2); printf ("game over! SCORE = % d ", Score); CtrlWnd_Flash (" killed! "," Press enter to try again! ", 9, 0); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 3, CTRLWNDHIGH-12); printf (" Final Score: % d ", Score); while (getch ()! = 13); Cls_CtrlWnd ();} void CalcScore (int num) {Score = Score + num * 5 + rand () % 3; EatNum ++; EXP = EXP + (LEVEL + 1) * 3 + num * 5 + rand () % 4; if (EXP> = (LEVEL + 1) * 30) Draw_UpLevel (); while (EXP> = (LEVEL + 1) * 30) {EXP = EXP-(LEVEL + 1) * 30; LEVEL ++; if (EXP <0) EXP = 0; if (SPEED-10> MAXSPEED) SPEED = SPEED-10;} Draw_Score ();} void Draw_Play () {CtrlWnd_Flash ("in game! "," Press space to pause! ", 4, 0);} void Draw_Food () {// depict Food xSetColor (FWHITE); xSetCursor (Food. x, Food. y); printf ("% 2 s", sharp [SFood]); int I; for (I = 0; I <5; I ++) {if (ExtraFood [I]-> next! = NULL) {xSetColor (FWHITE); xSetCursor (ExtraFood [I]-> x, ExtraFood [I]-> y); printf ("% 2 s ", sharp [SFood]) ;}}void Cls_CtrlWnd () {int m, n; // xSetColor (BBLACK); for (m = 2; m <CTRLWNDHIGH; m ++) {xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 2, m); for (n = 2; n <CTRLWNDWIDTH; n ++) printf ("11 ");}} void Draw_UpLevel () {CtrlWnd_Flash ("", "", 4, 0); CtrlWnd_Flash ("upgraded! "," Speed improved! ", 7,1); CtrlWnd_Flash (" upgraded! "," Speed improved! ", 7,1); CtrlWnd_Flash (" in game! "," Press space to pause! ",);} Void Draw_Tool () {xSetColor (FGREEN); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT +); printf (" item :"); if (Tool_Defense> 0) {xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 7,9); printf ("Invincible shield: %-4d", Tool_Defense);} if (Tool_FiveFood> 0) {xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 4, 10); printf ("[1] food outbreak: %-4d", Tool_FiveFood );} if (Tool_Defense <= 0 & Tool_FiveFood <= 0) {xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 11,8); pri Ntf ("NONE"); CtrlWnd_Flash ("", "", CTRLWNDHIGH-10, 0) ;}/ * Control Area flickering tip */void CtrlWnd_Flash (char *, char * B, int loc, int isflash) {int t; xSetColor (FGREEN); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 3, CTRLWNDHIGH-1-loc ); printf ("% s", a); if (B! = NULL) {xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 3, CTRLWNDHIGH-loc); printf ("% s", B);} if (isflash) {Sleep (500 ); xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 3, CTRLWNDHIGH-1-loc); t = strlen (a); for (; t> 0; t --) printf (""); if (B! = NULL) {xSetCursor (MAP_WIDTH * 2 + CTRLWNDLEFT + 3, CTRLWNDHIGH-loc); t = strlen (B); for (; t> 0; t --) printf ("") ;}sleep (500 );}}


Main. c

/* Wsad or remove multithreading correction death drill into the wall correction conflict with itself vulnerability correction out of control when illegal input add Start Screen add control panel display area */# include "snake. h "int main () {SetConsoleTitle (" Snake V3.0 QQ: 836663997 WSAD or direction key control 1 key use item "); BeginImage (); while (1) // one round-robin game {/* Initial game preparation */system ("cls"); Init (); Start (); while (1) // game subject {Move (); Sleep (SPEED); if (kbhit () snke_ctrl (getch (); if (EatFood () CalcScore (1 ); if (IsPause) Game_Pause (); if (IsDeath () if (snke_defense (); else break;} Game_Over ();} return 0 ;}


Preview:







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.