Snake C + + implementation

Source: Internet
Author: User

Recently did not turn over the once written program. It's really offensive. Once really do not understand anything. Just have a program is quite interesting, sophomore time wrote a snake game. Although the program is very ugly, there are a lot of loopholes. But it was also the program that made me really start to like programming. Not much to say. Look at the code first.


#include <iostream> #include <windows.h> #include <ctime> #include <cstdlib> #include < Conio.h>using namespace Std;int N;     The size of the wall, written here in order to control int foods[2];//Food's horizontal ordinate int m=1; Control speed void Gotoxy (int x,int y)//Position function, after input coordinates. The cursor moves to that location for output {COORD pos;p os.   X=2*x; Because the following output graphs are accounted for two position Pos. Y=y; SetConsoleCursorPosition (GetStdHandle (Std_output_handle), POS);//cursor Control position}void color (int a)//color function { Setconsoletextattribute (GetStdHandle (Std_output_handle), a);//So there's just one count.}void init () {//init wall for (int i=0;i<=   n;i++) for (int j=0;j<=n;j++) {Gotoxy (i, j); if (i==0| | i==n| | j==0| |   J==n) {Gotoxy (i, j);   Color (10); cout<< "";} Fence else {Gotoxy (i, j); color (3); cout<< "-";}     Can play the area}}void guize () {gotoxy (5,8); Color (2);   cout<< "welcome you to play this very fun snake game";     Gotoxy (5,9);    Color (2); cout<< "This game is made by Lu Ximing. More rough, please forgive me ";     Gotoxy (n+1,1); Color (12); cout<< "Please press W,s,a,d to control the direction";     Gotoxy (n+1,2); Color (12);      cout<< "Please press SPACEBAR to pause";     Gotoxy (n+1,3); Color (12); cout<< "Your score:"; Sleep (2000);     Gotoxy (5,8); Color (2);   cout<< "";     Gotoxy (5,9);    Color (2); cout<< "";} void food () {//Fruit generation. Use time to do variable Srand ((unsigned) (NULL)), Foods[0]=rand ()% (n-1) +1;//fruit horizontal foods[1]=rand ()% (n-1) +1;//Fruit's ordinate}int m    Ain () {char again= ' r ';    while (again== ' R ') {int err = 0; The input must be correctly inferred Do{err++;if (err>1) {gotoxy (0,0);cout<< "The range you entered is not correct, please enter again"; Sleep (500);}    Gotoxy (0,0);cout<< "Please set the width of the fence, the range is 10-25, press ENTER to determine:";cin>>n; Gotoxy (0,1);cout<< "Please set the difficulty, 1-Simple 2-medium 3-difficult. Press the ENTER key to determine: ";cin>>m;    Gotoxy (0,0);cout<< ""; Gotoxy (0,1); Sleep (;cout<<) ""; while (n<=9| | n>=26| | m<=0| |     M&GT;=4);     int * * snake=null;//defines a snake that is used to control the movement of the snake coordinates int score=0;int tail[2];       The horizontal ordinate of the tail int len=3;     The length of the initial snake char ch= ' P '; The parameter used to control the direction char sH= ' P ';      Used to suppress the movement of the snake in the opposite direction int yw=1;    Used to infer whether to bite yourself int count=1; Control level guize (); init ();//Wall snake= (int**) realloc (Snake,4*len);//For snake request Len length 4 bytes space for (int i=0;i<len;i++) snake [I]=new int[2];for (i=0;i<len;i++) {snake[i][0]=n/2; snake[i][1]=n/2+i; Gotoxy (snake[i][0],snake[i][1]); color (14 );       cout<< "★" <<endl;//with ★ to form the part of the snake}//So that the formation of a 3-length snake s1:food (); Produces a fruit for (i=len-1;i>=0;i--) if (foods[0]==snake[i][0]&&foods[1]==snake[i][1])//prevents the fruit from being long on the snake body goto s1;        Gotoxy (Foods[0],foods[1]);        Color (4); cout<< "" <<endl;              int zt=0; Used to pause while (1)//formally enter the game stage {tail[0]=snake[len-1][0];//Tail of the horizontal axis tail[1]=snake[len-1][1];//tail of the ordinate gotoxy (tail[0],tail[1     ]);      Color (3);cout<< "-";   Gotoxy (0,4);        Color (10); cout<< "", if (ch!= ') {for (i=len-1;i>0;i--) {Snake[i][0]=snake[i-1][0];snake[i][1]=snake[i-1][1];gotoxy ( SNAKE[I][0],SNAKE[I][1]); color;cout<< "★" <<endl; The previous section is replaced by a later section}} if (Kbhit ())//infer if there is aKey Press {gotoxy (0,n+2); Ch=getch ();} if ((sh== ' a ' &&ch== ' d ') | | (sh== ' d ' &&ch== ' a ') | | (sh== ' W ' &&ch== ' s ') | | (sh== ' s ' &&ch== ' W ') | |  (ch!= ' A ' &&ch!= ' W ' &&ch!= ' d ' &&ch!= ' s ' &&ch!= '))    Ch=sh; Suppress snake reverse move switch (CH)//move Direction {case ' W ': Snake[0][1]--;break;case ' s ': Snake[0][1]++;break;case ' a ': snake[0][0]--;break;                  Case ' d ': snake[0][0]++;break;case ": break;   Press the SPACEBAR to pause} sh=ch; Gotoxy (snake[0][0],snake[0][1]); Color (+);cout<< "★" <<endl; Sleep (ABS (300/m-0.1*score-5*count));//The addition of fractions will get faster if (snake[0][0]==foods[0]&&snake[0][1]==foods[1])// After eating apples, the snake scores plus 1.     The serpent grows 1{score+=5; After eating the fruit the scores also correspond to add len++;snake= (int**) realloc (Snake,4*len);//For snake new application len length of 4 bytes of space. Since Len has changed snake[len-1]=new Int[2];s2:food (); Generates a new fruit for (i=len-1;i>=0;i--) if (foods[0]==snake[i][0]&&foods[1]==snake[i][1])//prevents the fruit from being long on the snake body goto S2;        Gotoxy (Foods[0],foods[1]);        Color (4);   cout<< "" <<endl; Gotoxy (n+7,3); color (;cout<)<score<<endl;if (score%50==0) Count++;color (2*count), Gotoxy (n+2,4);cout<< "section" <<count<<               "Off";}            Yw=1;   for (i=1;i<len;i++) if (score>=5&&snake[i][1]==snake[0][1]&&snake[i][0]==snake[0][0])//Bite Yourself       yw=0; if (ch== ') zt=score;if ((YW==0&AMP;&AMP;SCORE&GT;ZT) | | snake[0][1]==0| | snake[0][1]==n| | snake[0][0]==0| | snake[0][0]==n)//bite yourself or hit the fence after the failure {gotoxy (1,N/2); color (); if (score<100) cout<< "So fast dead, really failed. To refuel Ah, rookie level "<<endl;else if (score<200) cout<<" The game is not bad.    Continue to work, Master level "<<endl;else cout<<" play so good, is simply the Great God level "<<endl; delete []snake;      Sleep (+); goto s;   }}s:for (i=0;i<=n;i++) for (int j=0;j<=n;j++) {Gotoxy (i, j); cout<< "";} Gotoxy (1,n/2+1); color (;cout<<) "Press R again.     Other key cancel: "; Gotoxy (13,n/2+1); color (n); Cin>>again;system ("CLS");} return 0;}

The results are as follows:



Can see. The function is realized, and I think it's kinda fun. But there are several obvious flaws in this program:

1. The code is too ugly, all kinds of incorrect qi, I think I now see a bit of difficulty.

2. This program can only be performed correctly under Visual c++6.0, and errors such as redefinition and undefined may occur in other environments. Just look at the for loop I wrote and I should know what's going on.

3, a lot of Goto Ah, C + + was originally to avoid goto, but once was thought to be good, and did not think there is anything wrong!

Alas, think of how ignorant you have been, just a celebrity is not said, life is to constantly find himself is a SB process, although do not know who said, but I want to study is so!

Snake C + + implementation

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.