Beans game on the console interface

Source: Internet
Author: User

The ESC key can exit the game.

Main. cpp

# Include "Lib. H "# pragma onceextern int level; int main () {food; wall; body curpos; Hall; int iexit = 0; while (1) {If (iexit) break; Init (& food, & wall, & curpos, & Hall); makewall (& Wall); While (1) {sleep (200 ); int res = move (& food, & wall, & curpos); if (1 = res) // restart break; else if (-1 = res) // The user exits {iexit = 1; break;} showscore (& curpos, & Food) ;}} return 0 ;}

Lib. h

# Include <stdio. h> # include <stdlib. h> # include <windows. h> # include <time. h> // identify the direction key pressed by the user # define key_upwm_user + 1 # define key_downwm_user + 2 # define keys + 3 # define key_rightwm_user + 4 // identify the current direction of the user # define dire_upwm_user + 5 # define dire_downwm_user + 6 # define layout + 7 # define layout + 8 // identify the element type to be printed # define print_wallwm_user + 9 # define print_foodwm_user + 10 # define print_bodywm_user + 11 # define print_cleanwm_user + 12 # define knock_wallwm_user + 13 # define knock_foodwm_user + 14 struct POS {int X; int y ;}; struct wall {pos POS [9999]; int Len ;}; struct food {pos POS [8]; int Len; int ishidden ;}; struct body {pos Pos; int direction ;}; struct Hall {pos POS [200]; int Len ;}; void Init (food * pfood, wall * pwall, body * pbody, hall * phall); // perform some initialization operations void print (Pos * POs, int type, int IX = 0); // complete the printing function void makewall (wall * pwall ); // generate the wall void makefood (hall * phall, food * pfood); // generate the int move (food * pfood, wall * pwall, body * pbody ); // The user moves void showscore (body * pbody, food * pfood); // display level and remaining number of beans int isover (Pos pos1, POS pos2, int type ); // determine whether point 2 overlaps int isknock (wall * pwall, body * pbody, food * pfood, int type); // determine whether to hit the wall or eat beans int getkey (); // obtain the user button

Lib. cpp

# Include "Lib. H "# pragma oncehandle hmain_out; handle hmain_in; lele_cursor_info Info; int ibeans = 0; int level = 1; // extern Short wall [17] [24]; short wall [17] [24] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1, -, 0, 0,-, 0,-1,-1,-, 0,-,-, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-, 0, 0, 0,-, 0, 0, 0,-1,-1,-1,-1,-1,-,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-, 0,-, 0, -, 0,-,-1,-1,-1,-, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1, -1,-, 0, 0,-, 0, 0,-,-, 0, 0, 0, 0, 0, 0,-, 0, 0, 0, 0, 0, 0, 0, 0,-, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-,-, 0, 0, 0, 0, 0, 0,-, 0, 0, 0, 0, 0,-, 0,-, 0, 0, 0, 0, 0, 0, 0, 0, -, 0,-,-1,-1,-1,-1,-,-1,-, 0, 0, 0,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-, 0, 0, 0, 0, 0,-, 0, 0, 0, 0, 0,-, 0, 0, 0, 0, 0,-, 0, 0, 0,-1,-1,-1, -, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1, -1,-, 0, 0, 0, 0, 0, 0, 0,-, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /*********************************** function: * determine whether a user is colliding with a wall or eating food ******************************* * ***/INT isknock (wall * pwall, body * pbody, food * pfood, int type) {If (knock_wall = type) {int I; for (I = 0; I <pwall-> Len; ++ I) {If (isover (pbody-> POs, pwall-> POS [I], knock_wall) return 1; // collision with the wall} else if (knock_food = type) {int I; for (I = 1; I <= pfood-> Len; ++ I) {If (isover (pfood-> POS [I], pbody-> POs, knock_wall) return I; // collision with food} return 0 ;} /*********************************** function: * move a user **********************************/int move (food * pfood, wall * pwall, body * pbody) {body prepos = * pbody; // POS prepos = pbody-> Pos; int Ikey = getkey (); If (-1 = Ikey) // The user exits return-1; if (Ikey) {pbody-> direction = Ikey + 4; Ikey = 0;} If (0 = Ikey) {If (direction_up = pbody-> direction) -- pbody-> POS. y; else if (direction_down = pbody-> direction) ++ pbody-> POS. y; else if (direction_left = pbody-> direction) -- pbody-> POS. x; else if (direction_right = pbody-> direction) ++ pbody-> POS. x;} If (isknock (pwall, pbody, pfood, knock_wall) {* pbody = prepos; return 0;} int IX = isknock (pwall, pbody, pfood, knock_food ); if (ix) {++ ibeans; // Delete the first-eaten food int I; for (I = IX; I <= (pfood-> len-1 ); + + I) pfood-> POS [I] = pfood-> POS [I + 1]; -- (pfood-> Len); If (0 = pfood-> Len) // The Authority completes {++ level; return 1 ;}} print (& prepos. POs, print_clean); // first Delete the previous output print (& pbody-> POs, print_body); Return 0 ;} /*********************************** function: * determine whether two points overlap ********************************** */INT isover (Pos pos1, pos pos2, int type) {If (knock_wall = type) // pos1, the body. pos2, The wallif (pos1.x = pos2.x & pos1.y = pos2.y) | (pos2.x + 1 = pos1.x & pos2.y = pos1.y) return 1; return 0 ;} /*********************************** function: * generate a wall *********************************/void makewall (wall * pwall) {int X, Y; int IX = 0; For (x = 0; x <= 16; ++ X) {for (y = 0; y <= 23; ++ y) {If (0 = wall [x] [Y]) {pwall-> POS [ix]. X = 2 * Y; pwall-> POS [ix]. y = x; print (& pwall-> POS [ix ++], print_wall) ;}} pwall-> Len = IX; // update the number of walls }/******************************** * ** features: * Complete initialization ***********************************/ void Init (food * pfood, wall * pwall, body * pbody, hall * phall) {// obtain the standard console input and output handle hmain_out = getstdhandle (std_output_handle); hmain_in = getstdhandle (std_input_handle ); // hide the cursor getconsolecursorinfo (hmain_out, & info); info. bvisible = false; setconsolecursorinfo (hmain_out, & info); // initialize the struct pfood-> Len = 0; pwall-> Len = 0; phall-> Len = 0; // change the console size system ("Mode con Cols = 50 lines = 21"); // The corridor struct value is int X, Y; int IX = 0; For (x = 0; x <17; ++ X) {for (y = 0; y <24; ++ y) {If (Wall [x] [Y]) // non-wall {phall-> POS [ix]. X = 2 * Y; phall-> POS [ix ++]. y = x ;}} phall-> Len = IX; pbody-> POS. X = 2; pbody-> POS. y = 1; pbody-> direction = direction_down; printf ("% d \ n", phall-> POS [0]. x, phall-> POS [0]. y); // output food int I; makefood (phall, pfood); for (I = 1; I <= 7; ++ I) {print (& pfood-> POS [I], print_food, I );}} /*********************************** function: * obtain the user's key ***********************************/ int getkey () {If (assign (vk_up) return key_up; If (getasynckeystate (vk_down) return key_down; If (getasynckeystate (vk_left) return key_left; If (getasynckeystate (vk_right )) return key_right; If (getasynckeystate (vk_escape) Return-1; // The user exits return 0; // The user does not have valid buttons }/******************************** * ** features: * Complete the print function ***********************************/ void print (Pos * P, int type, int IX) {coord Pos; POS. X = p-> X; POS. y = p-> Y; setconsolecursorposition (hmain_out, POS); If (print_wall = type) printf ("■"); else if (print_food = type) printf ("% d", IX); else if (print_body = type) printf ("\ 1"); else if (print_clean = type) printf ("");} /*********************************** function: * display level and number of remaining beans ********************************* **/void showscore (body * pbody, food * pfood) {coord Pos; POS. X = 0; POS. y = 18; setconsolecursorposition (hmain_out, POS); printf ("\ tbeans left: % d | pos: x = % d, y = % d \ n ", pfood-> Len, pbody-> POS. x, pbody-> POS. y); printf ("\ t \ tlevel: % d", level); // For (INT I = 1; I <= pfood-> Len; ++ I) // printf ("(x = % d, y = % d)", pfood-> POS [I]. x, pfood-> POS [I]. Y );} /*********************************** function: * generate food *********************************/void makefood (hall * phall, food * pfood) {srand (unsigned) Time (null); int tot; For (TOT = 1; Tot <= 7; ++ ToT) {int ixfood = rand () * phall-> Len/rand_max; pfood-> POS [tot]. X = phall-> POS [ixfood]. x; pfood-> POS [tot]. y = phall-> POS [ixfood]. y;} pfood-> Len = 7 ;}

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.