Traditional Culture Encyclopedia - Traditional culture - Seek C++ game source code ~

Seek C++ game source code ~

The following is the snake source code:

# include & ltiostream.h & gt

# include & ltwindows.h & gt

# include & lttime.h & gt

# include & ltstdlib.h & gt

# include & ltconio.h & gt

# Define N 2 1

Void gotoxy(int x, int y)// position function

{

Coordinate position;

Location. X = 2 * x

Location. Y = y

SetConsoleCursorPosition(GetStdHandle(STD _ OUTPUT _ HANDLE),pos);

}

Void color(int a)// color function

{

SetConsoleTextAttribute(GetStdHandle(STD _ OUTPUT _ HANDLE),a);

}

Void init(int apple[2])// Initialize function (Initialize fence, display information, apple)

{

int i,j; //Initialize the fence

int wall[N+2][N+2]= { { 0 } };

for(I = 1; I < = n; i++)

{

for(j = 1; j & lt= N; j++)

wall[I][j]= 1;

}

Color (11);

for(I = 0; I<N+2; i++)

{

for(j = 0; j & ltn+2; j++)

{

if(wall[i][j])

cout & lt& lt"■";

else cout & lt& lt"□" ;

}

cout & lt& ltendl

}

gotoxy(N+3, 1); //Display information

Color (20);

Cout & lt& lt "move southwest" endl

gotoxy(N+3,2);

Color (20);

Cout & lt& lt "Press any key to pause"

gotoxy(N+3,3);

Color (20);

Cout & lt& lt "score:"

apple[0]= rand()% N+ 1; //Apple

apple[ 1]= rand()% N+ 1;

Gotoxy (Apple [0], Apple [1]);

Color (12);

cout & lt& lt"●" & lt; & ltendl

}

int main()

{

int i,j;

int * * snake = NULL

int apple[2];

Int score = 0;

int tail[2];

int len = 3;

char ch = ' p

Srand ((unsigned) time (empty));

Init (apple);

snake=(int**)realloc(snake,sizeof(int *)* len);

for(I = 0; I & ltleni++)

snake[I]=(int *)malloc(sizeof(int)* 2);

for(I = 0; I & ltleni++)

{

snake[I][0]= N/2;

snake[I][ 1]= N/2+I;

gotoxy(snake[i][0],snake[I][ 1]);

Color (14);

cout & lt& lt“★”& lt; & ltendl

}

While( 1)// enters the message loop.

{

tail[0]= snake[len- 1][0];

Tail [1]= snake [len-1] [1];

gotoxy(tail[0],tail[ 1]);

Color (11);

cout & lt& lt"■" & lt; & ltendl

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 (14);

cout & lt& lt“★”& lt; & ltendl

}

if(kbhit())

{

gotoxy(0,N+2);

ch = getche();

}

Switch (channel)

{

case ' w ':snake[0][ 1]-; Break;

case ' s ':snake[0][ 1]++; Break;

Case "A": Snake [0] [0]-; Break;

Case "d": snake [0] [0]++; Break;

Default: break

}

gotoxy(snake[0][0],snake[0][ 1]);

Color (14);

cout & lt& lt“★”& lt; & ltendl

Sleep (ABS(200-0.5 * points));

if(snake[0][0]= = apple[0]& amp; & Snake [0] [1] = = Apple [1])/After eating the apple, the score of the snake increases 1, and the length of the snake increases1.

{

Score++;

len++;

snake=(int**)realloc(snake,sizeof(int *)* len);

snake[len- 1]=(int *)malloc(sizeof(int)* 2);

apple[0]= rand()% N+ 1;

apple[ 1]= rand()% N+ 1;

Gotoxy (Apple [0], Apple [1]);

Color (12);

cout & lt& lt"●" & lt; & ltendl

gotoxy(N+5,3);

Color (20);

Cout & lt& lt score & lt& ltendl

}

If (snake [0] [1] = = 0 || snake [0] [1] = = n || snake [0] [0] = = 0 || snake [0] = = n)//Failure after hitting the fence.

{

gotoxy(N/2,N/2);

Color (30);

Cout & lt& lt "failed! ! ! "& lt& ltendl

for(I = 0; I & ltleni++)

Free (snake [I]);

Sleep (infinite);

Exit (0);

}

}

Returns 0;

}