Current location - Health Preservation Learning Network - Slimming men and women - How to use the source code of C language snake?
How to use the source code of C language snake?
The source code of C language snake must be compiled into an EXE file by the corresponding C/C++ compiler before it can run.

Because the operating system we usually use is Windows, and the longest C/C++ compiler in this system is VC++ compiler, so the version commonly used in colleges and universities is VC++6.0.

Let's use VC++6.0 to illustrate the compilation process:

1. Open the snake code in VC++6.0 through the "Open" submenu under the "File" menu.

2. Compile the snake code in VC++6.0 through the "Compile xxxx.xx" submenu under the "Build" menu.

3. Run the snake program in VC++6.0 through the "Execute xxxx.exe" submenu under the "Build" menu.

Attachment: C/C++ snake source code that can run in VC++6 environment (no copyright, write it yourself, welcome to modify and copy it at will).

/*

C/C++ Snake Game, zjlj, 20 15.3. 16

*/

# Definition? Debugging? 0? //When the program is in the debugging stage? Debugging is? 1

# include & ltiostream & gt

# include & ltwindows.h & gt

# include & lttime.h & gt

# include & ltconio.h & gt

Use? Namespace? std

Invalid? Readini (file? **fphead,? int? * score,? Charles? *argv[])? //Create or open the ini file corresponding to the running file, and read the highest record.

{

Charles? File name [200], * pfilename

int? flag=- 1,I;

Strcpy (file name, argv [0]);

for(I = 0; File name [i]! ='\0'; i++)

{

What if? ('.'= = filename[I])flag = 1;

}

if( 1==flag)

{

filename[I- 1]= ' I ';

File name [I-2] =' n';

File name [I-3] =' I';

}

other

{

File name [i] ='.';

filename[I+ 1]= ' I ';

File name [I+2] =' n';

File name [i+3] =' i';

File name [i+4] =' \ 0';

}

for(; File name [i]! =' \ \'&me & gt=0; I-)pfilename = & amp; File name [i];

What if? (? (*fphead=fopen(pfilename," r b+ ")= = NULL)

{

What if? (? (*fphead=fopen(pfilename," WB+")= = NULL)

{

Printf ("Cannot create or open \"%s\" file \n ",pfilename);

System ("suspended");

Exit (0);

}

}

other

{

fread(score,sizeof(int), 1,* fphead);

}

}

Invalid? Writeini (file? **fphead,? int? * score,? Charles? *argv[])? //Open the ini file corresponding to the running file and write the highest record.

{

Charles? File name [200], * pfilename

int? flag=- 1,I;

Strcpy (file name, argv [0]);

for(I = 0; File name [i]! ='\0'; i++)

{

What if? ('.'= = filename[I])flag = 1;

}

if( 1==flag)

{

filename[I- 1]= ' I ';

File name [I-2] =' n';

File name [I-3] =' I';

}

other

{

File name [i] ='.';

filename[I+ 1]= ' I ';

File name [I+2] =' n';

File name [i+3] =' i';

File name [i+4] =' \ 0';

}

for(; File name [i]! =' \ \'&me & gt=0; I-)pfilename = & amp; File name [i];

What if? (? (*fphead=fopen(pfilename," WB+")= = NULL)

{

Printf ("Cannot write to \"%s\" file, the disk is write-protected! \n ",pfilename);

System ("suspended");

Exit (0);

}

other

{

Rewind (* fphead);

fwrite(score,sizeof(int), 1,* fphead);

fclose(* FP head);

}

}

Invalid? gotoxy(int? x,int? Y)// Cursor positioning, the cursor positioning function SetConsoleCursorPosition is that the upper left corner position is 0, and then it extends to the lower left.

{

Coordinates pos

Location. x = 2 * y;

Location. y = x;

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

}

Invalid? Color (int? A)// color function

{

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

}

Invalid? Refresh (int? q[][22],? int? Grade,? int? gamespeed? int? Length, int? Score)? //? Output snake chessboard

{

int? I,j;

for(I = 0; I & lt22; i++)

{

for(j = 0; j & lt22; j++)

{

If(q[i][j]==0)// Output chessboard blank.

{

gotoxy(i,j);

Color (11);

cout & lt& lt"■";

}

If (q [i] [j] =1|| q [i] [j] = = 2)//output chessboard wall.

{?

gotoxy(i,j);

Color (11);

cout & lt& lt"□";

}

If(q[i][j]==3)// output snakehead.

{?

gotoxy(i,j);

Color (14);

cout & lt& lt"★";

}

If(q[i][j]==4)// Output snake.

{?

gotoxy(i,j);

Color (12);

cout & lt& lt"◆";

}

If(q[i][j]==5)// output fruit.

{?

gotoxy(i,j);

Color (12);

cout & lt& lt"●";

}

}

if(i==0)? cout? & lt& lt? ”\ t * * * * * * * * * * * * * * * * * * * * * *”;

if(i== 1)? cout? & lt& lt? "\t grade:"? & lt& lt? Grade; //Display level

if(i==3)? cout? & lt& lt? "\t Automatically advance the time";

if(i==4)? cout? & lt& lt? "\t The interval is:"? & lt& lt? gamespeed? & lt& lt? "lady"; //Display time

if(i==6)? cout? & lt& lt? "\ tWhat is the highest score in history?"? & lt& lt? Score? & lt& lt? "points";

if(i==7)? cout? & lt& lt? \ tWhat's your score now? & lt& lt? (length+(grade-1)*8)* 10? & lt& lt? "points";

if(i==8)? cout? & lt& lt? " \ t * * * * * * * * * * * * * * * * * * * * * * * *;

if(i==9)? cout? & lt& lt? "\ tGame description:";

if(i== 10)? cout? & lt& lt? \t( 1) is controlled by the keyboard';

if(i== 1 1)? cout? & lt& lt? "\ tThe moving direction of the snakehead;" ;

if(i== 12)? cout? & lt& lt? "\t(2) Every time a snake eats a fruit, the snake body";

if(i== 13)? cout? & lt& lt? "t Add a section;" ;

if(i== 14)? cout? & lt& lt? "\t(3) The snake bites itself or hits the wall";

if(i== 15)? cout? & lt& lt? "The wall game is over." ;

if(i== 18)? cout? & lt& lt? " \ t * * * * * * * * * * * * * * * * * * * * * * * *;

if(i== 19)? cout? & lt& lt? "\tC/C++ Language homework:";

What if (i==20)? cout? & lt& lt? \tzjlj,20 15.03. 16? ;

}

}

int? main(int? argc,? Charles? *argv[]){

int? tcsQipan[22][22]; ? //? The snake chessboard is a two-dimensional array (such as 22*22, including walls).

int? I,j,score,directiontemp

Documents? * fpini//*fpini? Information file

readini(& amp; fpini,? & Score? argv); //Read the highest record of the ini file.

What if? (Score & lt0)// If the highest score is less than zero and set to zero, the initial file is negative.

Score = 0;

while( 1)

{

for(I = 1; I & lt=20; i++)

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

tcsQipan[I][j]= 0; //The coordinates corresponding to the snake chessboard are marked with the symbol 0 in the middle blank part.

for(I = 0; I & lt=2 1; i++)

tcsQipan[0][i]? =? tcsQipan[2 1][i]? =? 1; ? //The coordinates corresponding to the snake chessboard are marked with the signs of 1 on the upper and lower walls.

for(I = 1; I & lt=20; i++)

tcsQipan[i][0]? =? tcsQipan[i][2 1]? =? 2; ? //The corresponding coordinates of the snake chessboard are marked with signs on the left and right walls.

int? tcszoo Biao[2][500]; ? //snake coordinate array

for(I = 0; ? I<4; ? i++)

{

tcszoobao[0][I]? =? 1; //X coordinates of snake body and snake head

tcszoobao[ 1][I]? =? Me? +? 1; //Y coordinates of snake body and snake head

}

int? Boss? =? 3. Tail? =? 0; //Represents the snake head and the array offset of the snake head.

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

tcsQipan[ 1][I]= 4; //snake body

tcsQipan[ 1][4]= 3; //snakehead

int? x 1,? y 1; //? Random fruit

srand(time(0)); //Set random seed

do

{

x 1 = rand()% 20+ 1;

y 1 = rand()% 20+ 1;

}

while(tcsQipan[x 1][y 1]! =0); //If you don't reproduce the fruit in the blank space,

tcsQipan[x 1][y 1]= 5; //The coordinates corresponding to the snake chessboard are marked with the symbol of Fruit 5.

Color (12);

Cout & lt& lt" \ n \ n \ t \ t \ t \ Snake game is about to start? !" & lt& ltendl// Ready to start.

Dragon? start,starttemp

int? Grade? =? 1,? Length? =? 4; ? //Set the initial level and initial length of the snake.

int? gamespeed? =? 500; ? //Set the initial forwarding time interval

for(I = 3; I & gt=0; I-)

{

Start = clock ();

while(clock()-start & lt; = 1000);

System ("CLS");

If (i>0)

cout? & lt& lt? \ n \ n \ t \ t \ t \ Enter the countdown? & lt& lt? Me? & lt& lt? endl? //Countdown display

other

Refresh (tcsQipan, grade, gamespeed, length, score); ? //Initial chessboard display

}

int? timeover= 1,other key = 1; //Initialize timeout and critical judgment parameters.

Charles? Direction? =? 77; ? //? In the initial setting, move to the right.

int? x = tcszuobao[0][head],y = tcszuobao[ 1][head]; //Save the coordinates of snakehead into x, y variables.

While( 1)// Run a game.

{

Start? =? clock();

while((time over =((start temp = clock())-start & lt; = gamespeed))& amp; & amp! kb hit()); //If a key is pressed or the time exceeds the automatic advance interval, the cycle ends.

If (direction = = 72 | | direction = = 80 | | direction ==75? ||| Direction ==77)

Directiontemp = direction; //Keep the last arrow key.

//start temp = gamespeed+start-start temp; //Retention time

If (timeout)

{

# If? (Debugging == 1)

Direction? =? getch(); //Debugging code

# Otherwise

If ((direction? =getch())==-32)

Direction? =? getch();

#endif

}

# If? (DEBUG== 1)// Debugging code

Start = clock ();

while(clock()-start & lt; =2000);

gotoxy(24,4);

cout? & lt& lt? "\ tpype ASCII code"

#endif

If (! (Direction = = 72 | | Direction = = 80 | | Direction ==75? ||| Direction ==77))

{

other key = 0; //? This key is not an arrow key, and otherkey is set to 0.

}

other

{

other key = 1; //? This key is the arrow key and the other key is set to 1.

}

If (direction ==72? & amp& amp? Directiontemp==80)// Ignore the reverse key.

{

Direction = 32;

other key = 0;

//Start? =? clock();

//while(clock()-start & lt; = start temp);

}

Or what? If (direction ==80? & amp& amp? directiontemp==72)

{

Direction = 32; //Set the key as a non-directional key.

other key = 0; //? This key is a non-directional key, and other keys are set to 0.

//? Start? =? clock();

//while(clock()-start & lt; = start temp); //Compensate the waiting time

}

Or what? If (direction ==75? & amp& amp? directiontemp==77)

{

Direction = 32;

other key = 0;

//Start? =? clock();

//while(clock()-start & lt; = start temp);

}

Or what? If (direction ==77? & amp& amp? directiontemp==75)

{

Direction = 32;

other key = 0;

//Start? =? clock();

//while(clock()-start & lt; = start temp);

}

Switch (Direction)//Determine the direction key.

{

Case? 72:? x=? tcszuobao[0][head]- 1; ? y=? tcszuobao[ 1][head]; Break; ? //? up

Case? 80:? x=? tcszuobao[0][head]+ 1; ? y=? tcszuobao[ 1][head]; Break; ? //? downwards

Case? 75:? x=? tcszoobao[0][head]; ? y=? tcszuobao[ 1][head]- 1; Break; ? //? towards the left

Case? 77:? x=? tcszoobao[0][head]; ? y=? Tcszuobao[ 1][ head]+1; Break; ? //? Go right.

Default:? Break;

}

if(x==0? ||? x==2 1? ||y==0? ||? y==2 1)? //? The snakehead hit the wall and ended the game.

{?

gotoxy(22, 12);

cout? & lt& lt? "\ tThe game is over!" ? & lt& lt? endl

if(score & gt; = (length+(grade-1) * 8) *10)//Judge whether the record is broken.

{

gotoxy( 10,7);

Color (12);

cout? & lt& lt? "Can't get through? Come on! " ? & lt& lt? endl

fclose(fpini); //Close the ini file

}

other

{

gotoxy( 10,7);

Color (12);

cout? & lt& lt? Congratulations on breaking the record? & lt& lt? endl

Score = (length+(grade-1) * 8) *10;

write ini(& amp; fpini,? & Score? argv); The highest record written in ini file

}

gotoxy(23, 12);

cout? & lt& lt? Press enter to restart, press ESC to exit the game? & lt& lt? Endl// display prompt

Break; //Quit the game

}

if(tcsQipan[x][y]! = 0 & amp& amp! (x = = x 1 & amp; & ampy = = y 1)& amp; & amptcsQipan[x][y]! =3)? //The snake head touches the snake body, ending the game.

{

gotoxy(22, 12);

cout? & lt& lt? "\ tThe game is over!" ? & lt& lt? endl

if(score & gt; = (length+(grade-1) * 8) *10)//Judge whether the record is broken.

{

gotoxy( 10,7);

Color (12);

cout? & lt& lt? "Can't get through? Come on! " ? & lt& lt? endl

fclose(fpini); //Close the ini file

}

other

{

gotoxy( 10,7);

Color (12);

cout? & lt& lt? Congratulations on breaking the record? & lt& lt? endl

Score = (length+(grade-1) * 8) *10;

write ini(& amp; fpini,? & Score? argv); The highest record written in ini file

}

gotoxy(23, 12);

cout? & lt& lt? Press enter to restart, press ESC to exit the game? & lt& lt? Endl// display prompt

Break; //Quit the game

}

/*

The core algorithm starts when the game is running.

*/

if(x==x 1? & amp& amp? y==y 1)? //? Eat fruit, length plus 1

{

Length? ++;

If (length > = 8)//If the length is greater than or equal to 8, recalculate the length and add 1 to the grade.

{

Length? -=? 8; //Recalculate the length

Grade? ++; //Grade plus 1

if(gamespeed & gt; 50)// The fastest control speed is 50.

gamespeed? =? 550? -? Grade? *? 50; ? //? Change the automatic propulsion interval

}

tcsQipan[x][y]=? 3; //The coordinates corresponding to the snake chessboard are now changed to snake head symbol 3.

tcsQipan[tcszuobao[0][head]][tcszuobao[ 1][head]]? =? 4; //The coordinate corresponding to the greedy snake chessboard is changed from the original snake head symbol to the snake body symbol 4.

Boss? =? (head+ 1)% 400; //Prevent the array from going out of bounds

tcszoobao[0][head]? =? x; //X coordinate of the snake head

tcszoobao[ 1][head]? =? y; //Y coordinate of the snake head

Make//random fruit

{

x 1 = rand()% 20+ 1;

y 1 = rand()% 20+ 1;

}

while(tcsQipan[x 1][y 1]! =0); //If you don't reproduce the fruit in the blank space,

tcsQipan[x 1][y 1]= 5; //The coordinates corresponding to the snake chessboard are marked with the symbol of Fruit 5.

gotoxy(22, 12);

cout? & lt& lt? "\ tThe game is in progress!" ? & lt& lt? endl

Refresh (tcsQipan, grade, gamespeed, length, score);

}

Or what? //? Don't eat fruit

{?

If (other key)

{

tcsQipan? [tcszuobao[0][tail]][tcszuobao[ 1][tail]]= 0;

tail =(tail+ 1)% 400; //Prevent the array from going out of bounds

tcsQipan? [tcszuobao[0][head]][tcszuobao[ 1][head]]= 4;

head =(head+ 1)% 400; //Prevent the array from going out of bounds

tcszuobao[0][head]= x; //X coordinate of the snake head

tcszuobao[ 1][head]= y; //Y coordinate of the snake head

tcsQipan[tcszuobao[0][head]][tcszuobao[ 1][head]]= 3;

gotoxy(22, 12);

cout? & lt& lt? "\ tThe game is in progress!" ? & lt& lt? endl

Refresh (tcsQipan, grade, gamespeed, length, score);

}

other

{

gotoxy(22, 12);

cout? & lt& lt? "\ tGame paused!" ? & lt& lt? endl

}

}

/*

The core algorithm of the game runtime is over.

*/

}

while( 1)

{

And (! kb hit());

If ((direction? = getch()= = 13)// Enter to start the next game.

Break;

If (direction? ==27)// Press ESC to exit the game.

Exit (0);

}

System ("CLS"); //Clear the screen and start again.

}

Return? 0;

}