Snake C + + implementation

Source: Internet
Author: User

Recently did not turn over the previous written procedures, really unsightly, before really what do not understand, but there is a program is very interesting, sophomore writing a snake game, although the program is very ugly, there are many loopholes, but also this program let me really began to like the programming, not many said, first look at the code!


#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 entering coordinates, the cursor will move to that position for output {COORD pos;p os.   X=2*x; Because the graphics output below are two 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 only one parameter.}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 made by Lu Ximing, relatively 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); Ao.or (12); cout<< "Your score:"; Sleep (2000);     Gotoxy (5,8); Color (2);   cout<< "";     Gotoxy (5,9);    Color (2); cout<< "";} void food () {//Fruit generation, Time variable Srand ((unsigned) (NULL)), Foods[0]=rand ()% (n-1) +1;//fruit horizontal foods[1]=rand ()% (n-1) +1;/    /Fruit Ordinate}int main () {char again= ' r ';    while (again== ' R ') {int err = 0; Input must be correctly judged Do{err++;if (err>1) {gotoxy (0,0);cout<< "You entered the wrong range, please re-enter"; 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-hard, press ENTER 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 '; parameter to control the direction char sh=' P ';      Used to suppress the movement of the snake in the opposite direction int yw=1;    Used to determine 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 the following section} if (Kbhit ())///To determine if there are buttons pressedUnder {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 increase in the score will become faster if (snake[0][0]==foods[0]&&snake[0][1]==foods[1])//     After eating apples, the snake scores plus 1, and the snake grows 1{score+=5; After eating the fruit scores also increase len++;snake= (int**) realloc (Snake,4*len);//For snake new application len length of 4 bytes space, because 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 (n); cout<&lT;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<<" Play is also good, continue to work hard, master level "<<endl;else    cout<< "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 keys cancel:"; Gotoxy (13,n/2+1); color (n); Cin>>again;system ("CLS");} return 0;}

The results are as follows:



As you can see, the functionality is implemented, and I think it's fun, but there are a few obvious flaws in the program:

1. The code is too ugly, all kinds of misalignment, I am now looking at a bit of difficulty.

2. This program can only be run correctly under visual c++6.0, there may be redefinition and undefined errors in other environments, so look carefully 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 it was used to feel good, and did not feel anything wrong!

Alas, think of how ignorant I was before, but a famous person did not say well, life is to constantly discover the past 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.