C ++: Snake and Snake

Source: Internet
Author: User

C ++: Snake and Snake

1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 # include <ctime> 5 # include <conio. h> 6 using namespace std; 7 8 typedef struct {int x, y;} Point; 9 10 char map [22] [22]; // define a 22*22 map (including borders) 11 Point snake [400], food, Next; // define the position of snake, food, and Next snake head 12 int head, tail; // The subscript 13 int grade, length, and autotime used to store the snake head and tail; // the time required for the game level, snake length, and automatic forward 14 char direction; // forward direction 15 16 // use inline to define inline functions to save the call overhead during running 17 // Refresh the map 18 inline void Update (char map [] [22], int grade, int length, int autotime) 19 {20 system ("cls "); // clear screen 21 int I, j; 22 printf ("\ n"); 23 for (I = 0; I <22; I ++) 24 {25 printf ("\ t"); 26 for (j = 0; j <22; j ++) 27 printf ("% c ", map [I] [j]); 28 if (I = 0) 29 printf ("\ t level: % d", grade ); 30 if (I = 2) 31 printf ("\ t length: % d", length); 32 if (I = 6) 33 printf ("\ t automatic forward time"); 34 if (I = 8) 35 printf ("\ T interval: % d ms", autotime); 36 printf ("\ n"); 37} 38} 39 40 // welcome page 41 inline void hello () 42 {43 puts ("\ n \ t Snake game is about to begin! "); // Prepare to start 44 double start; 45 for (int I = 3; I> = 0; I --) 46 {47 start = (double) clock () /CLOCKS_PER_SEC; // obtain the program's current running time 48 while (double) clock ()/CLOCKS_PER_SEC-start <= 1 ); // after 1 second, 49 if (I> 0) 50 {51 system ("cls "); // clear screen 52 printf ("\ n \ t enter countdown: % d \ n", I ); // countdown 53} 54 else 55 Update (map, grade, length, autotime ); // refresh map 56} 57} 58 59 // randomly generated food location 60 inline void f () 61 {62 srand (Int (time (0); // call the seed function 63 do {64 food. x = rand () % 20 + 1; 65 food. y = rand () % 20 + 1; 66} while (map [food. x] [food. y]! = ''); 67 map [food. x] [food. y] = '! '; // The food is "!" 68} 69 70 // initialize 71 inline void init () 72 {73 int I, j; 74 for (I = 1; I <= 20; I ++) 75 for (j = 1; j <= 20; j ++) 76 map [I] [j] = ''; 77 for (I = 0; I <= 21; I ++) 78 map [0] [I] = map [21] [I] = map [I] [0] = map [I] [21] = '*'; // border 79 map [1] [1] = map [1] [2] = 'O'; // snake body (including snake tails) 80 map [1] [3] = '@'; // snake header 81 head = 2; tail = 0; // subscript of the header and tail 82 snake [head] at the start. x = 1; snake [head]. y = 3; // At the beginning, the subscript of the snake header on the map is 83 snake [tail]. X = 1; snake [tail]. y = 1; // The subscript 84 snake on the map at the beginning [1]. x = 1; snake [1]. y = 2; // The subscript 85 f () of the snake on the map at the beginning; // the random food location 86 grade = 1; length = 3; autotime = 500; // start level, length, and automatic forward time 87 ction = 77; // The initial direction of motion 88} 89 90 // pre-forward 91 inline int GO () 92 {93 bool timeover = true; 94 double start = (double) clock ()/CLOCKS_PER_SEC; // get the current running time of the program 95 96 // automatically enter 97 while (timeover = (double) clock () after 1 second or within 1 second () /CLOCKS_PER_SEC-start <= autotime/1000.0 ))&&! _ Kbhit (); 98 // enter 99 if (timeover) 100 {101 _ getch (); 102 direction = _ getch () on the keyboard (); // obtain the direction 103} 104 switch (direction) 105 {106 case 72: 107 Next. x = snake [head]. x-1; Next. y = snake [head]. y; // up 108 break; 109 case 80: 110 Next. x = snake [head]. x + 1; Next. y = snake [head]. y; // 111 break down; 112 case 75: 113 Next. x = snake [head]. x; Next. y = snake [head]. y-1; // 114 break to the left; 115 case 77: 116 Next. x = snake [head]. X; Next. y = snake [head]. y + 1; // 117 break to the right; 118 default: 119 puts ("\ tGame over! "); // Failed to press the arrow key 120 return 0; 121} 122 if (Next. x = 0 | Next. x = 21 | Next. y = 0 | Next. y = 21) // hit the boundary 123 {124 puts ("\ tGame over! "); 125 return 0; 126} 127 if (map [Next. x] [Next. y]! = ''&&! (Next. x = food. x & Next. y = food. y) // eat yourself 128 {129 puts ("\ tGame over! "); 130 return 0; 131} 132 if (length = 400) // The longest snake length is 133 {134 puts (" \ tGood game! "); 135 return 0; 136} 137 return 1; 138} 139 140 // 141 inline void EAT () 142 {143 length ++; // The length is increased by 1144 int _ grade = length/10 + 1; // The calculation level is 145 if (_ grade! = Grade) 146 {147 grade = _ grade; 148 if (autotime> = 100) 149 autotime = 550-grade * 50; // increase the first-level automatic time minus 50 milliseconds 150} 151 map [Next. x] [Next. y] = '@'; // The Position of the snake header changes by 152 map [snake [head]. x] [snake [head]. y] = 'O'; // The original head of the snake body changes to 153 head = (head + 1) % 400; // The subscripts of the snake header add 1154 snake [head]. x = Next. x; snake [head]. y = Next. y; // The subscripts of the snake header change by 155 f (); // a random 156 Update (map, grade, length, autotime) Food position is generated ); // refresh map 157} 158 159 // 160 inline void FAILURE () 161 {162 map [snake [tail]. x] [snake [tail]. y] = ''; // the original position of the snake tail is changed to" "163 tail = (tail + 1) % 400; // Add 1164 map [Next. x] [Next. y] = '@'; // The Position of the snake header changes by 165 map [snake [head]. x] [snake [head]. y] = 'O'; // The original head of the snake body changes to 166 head = (head + 1) % 400; // The subscripts of the snake header add 1167 snake [head]. x = Next. x; // The snake header subscript changes by 168 snake [head]. y = Next. y; 169 Update (map, grade, length, autotime); // refresh map 170} 171 172 // main function 173 int main () 174 {175 init (); // initialize 176 hello (); // Welcome Page 177 while (1) 178 {179 if (GO () // pre-forward 180 {181 if (Next. x = food. x & Next. y = food. y) 182 EAT (); // food 183 else184 FAILURE (); // food 185} 186 else187 return 0; // failed or victory, game ended 188} 189 return 0; 190} 191 //

 

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.