C + + snake

Source: Internet
Author: User

#include <iostream> #include <cstdio> #include <cstdlib> #include <ctime> #include <conio.h >using namespace Std;typedef struct{int x, y;}                                          Point;char map[22][22];                     Define a 22*22 map (with border) point snake[400], food, Next;                                                 Define snakes, food, next snake head position int head, tail;                     subscript int grade, length, autotime for storing snake head and tail of snake;                                                 Game level, snake length, automatic advance time required char direction; Forward direction//define inline functions with inline save program runtime call cost//refresh map inline void Update (char map[][22], int grade, int length, int autotime) {Syst                                          EM ("CLS");       Clear screen int i, J;       printf ("\ n");              for (i = 0; i <; i++) {printf ("\ t");              for (j = 0; J <; J + +) printf ("%c", Map[i][j]);              if (i = = 0) printf ("\ t level:%d", grade);             if (i = = 2)        printf ("\ t is length:%d", length);              if (i = = 6) printf ("\ t forward Speed:%d", autotime);       printf ("\ n"); }}//welcome interface inline void Hello () {puts ("\n\n\n\t\t\t snake game is about to begin! Please use the arrow keys to start the game!                                          ");       Ready to start double start;                            for (int i = 1; I >= 0; i--) {start = (double) clock ()/clocks_per_sec;       Gets the time that the program runs so far while (double) clock ()/Clocks_per_sec-start <= 1);                                                                      After 1 seconds, if (i) {System ("CLS");                            Clear screen printf ("\n\n\n\t\t\t Entry countdown:%d\n", i);                            Countdown} else Update (map, grade, length, autotime);                                                          Refresh map}}//randomly generated food position inline void f () {srand (int (time (0)));            Call seed function do{food.x = rand ()% 20 + 1;       Food.y = rand ()% 20 + 1;       } while (Map[food.x][food.y]! = ");                                                               MAP[FOOD.X][FOOD.Y] = '! '; Food is "!"}       Initialize the inline void init () {int i, J;       for (i = 1; I <=, i++) for (j = 1; J <=, J + +) map[i][j] = ";       for (i = 0; I <=; i++) map[0][i] = map[21][i] = map[i][0] = map[i][21] = ' * ';                                          Boundary map[1][1] = map[1][2] = ' O ';                                                      Snake body (including snake tail) map[1][3] = ' @ '; Head of snakehead = 2;                                                   tail = 0; At the beginning of the head and tail subscript snake[head].x = 1;                                SNAKE[HEAD].Y = 3; At the beginning of the snake head on the map subscript snake[tail].x = 1;                                SNAKE[TAIL].Y = 1; At the beginning of the snake tail on the map subscript snake[1].x = 1;  SNAKE[1].Y = 2;                                   At the beginning of the snake body on the map subscript f (); Randomly generated food location grade = 1; length = 3;                               Autotime = 500;                                                      Start level, length, auto advance time Direction = 77;       The initial motion direction to the right}//pre-forward inline int GO () {bool Timeover = true;                     Double start = (double) clock ()/clocks_per_sec; Get the time that the program runs so far//automatically after 1 seconds or wait 1 seconds for keyboard input while (Timeover = (double) clock ()/Clocks_per_sec-start <= Autoti       me/1000.0)) &&!_kbhit ());              Keyboard input if (timeover) {_getch ();                                                               Direction = _getch (); Get direction} switch (direction) {case 72:next.x = snake[head].x-1;              Next.y = SNAKE[HEAD].Y;       Up break; Case 80:next.x = snake[head].x + 1; Next.y = Snake[heAD].Y;       Downward break; Case 75:next.x = snake[head].x;              Next.y = snake[head].y-1;       Break left; Case 77:next.x = snake[head].x;              Next.y = snake[head].y + 1;       Break right;                                                               Default:puts ("\tgame over!");       Press the non-directional key game failed return 0; } if (Next.x = = 0 | | Next.x = = 21 | | Next.y = = 0 | |              Next.y = = 21)//hit the border {puts ("\tgame over!");       return 0; } if (Map[next.x][next.y]! = "' &&! (              Next.x = = Food.x&&next.y = = food.y)//Eat yourself {puts ("\tgame over!");       return 0;                                                                                                  } if (length = = 400)              Longest snake length {puts ("\tgood game!");       return 0; } RETUrn 1;}                                                                      Eat to food inline void EAT () {length++;                                   length increased by 1 int _grade = LENGTH/10 + 1;              Compute level if (_grade! = Grade) {grade = _grade;                     if (autotime >=) autotime = 550-grade * 50;                                          Increase the first level of automatic time minus 50 milliseconds} Map[next.x][next.y] = ' @ ';              Snake head position change map[snake[head].x][snake[head].y] = ' O ';                                          The original snake head position changes to the snake body Head = (head + 1)% 400; Snake head subscript plus 1 snake[head].x = next.x;       Snake[head].y = Next.y;                                                                             Snake head subscript change f ();                     Randomly generated food location Update (map, grade, length, autotime);              Refresh the map}//didn't eat food. inline void FAILURE () {map[snake[tail].x][snake[tail].y] = "; The original position of the snake tail becomes "" tail = (tail + 1) % 400;                                          Snake tail subscript plus 1 Map[next.x][next.y] = ' @ ';              Snake head position change map[snake[head].x][snake[head].y] = ' O ';                                          The original snake head position changes to the snake body Head = (head + 1)% 400;                                                 Snake head subscript plus 1 snake[head].x = next.x;       Snake head subscript Change snake[head].y = Next.y;                     Update (map, grade, length, autotime);                                   Refresh map}//main function int main () {init ();                            Initialize Hello (); Welcome interface while (1) {if (GO ())//pre-forward {if (Nex              T.x = = Food.x&&next.y = = food.y) EAT ();       Eat to Food else FAILURE ();              No food to eat} else return 0; Failure or victory, game over} RETUrn 0;} 

  

C + + snake

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.