Current location - Health Preservation Learning Network - Healthy weight loss - Seek the snake code operation of C language.
Seek the snake code operation of C language.
//? ConsoleApplication 1.cpp:? Defines the entry point for the console application.

//

# Contains? " stdafx.h "

# Contains? & ltstdio.h & gt

# Contains? & ltwindows.h & gt

# Contains? & lttime.h & gt

# Contains? & ltconio.h & gt

# Definition? Frame _ Height? 20? //Definition? Map? size

# Definition? Frame _ width? 40

# Definition? Up? What? //Definition? Operation? key

# Definition? Come down? “s”

# Definition? Left? one

# Definition? Right? enlighten

int? Me? j,? k;

Charles? ch? =? Upward; ? //Initial? direction

int? Grow up? =? 0; ? //logo:? What if? Snakes? grow

struct? Food? {

int? x;

int? y;

} food;

struct? Snakes? {

int? x[50];

int? y[50];

int? len

int? Speed;

} snake; ? //snake[0]? Is it? head

Invalid? init _ map(void);

Invalid? Update _ food (void);

Invalid? move _ snake(void);

int? is _ alive(void);

Invalid? get _ speed(void);

Invalid? gotoxy(int? x,? int? y);

int? Master ()

{

init _ map(); ? //Initialize the map

What time? ( 1)

{

Update _ Food ();

//Does it produce food?

get _ speed(); ? //Get speed

move _ snake(); ? //Move the snake's body

Sleep (snake.speed);

//moving speed

What if? (! (is_alive()))

//Snake's life and death (hit the wall or eat)

Break;

}

Printf ("Game? It's over! ”);

_ getch();

Return? 0;

}

//initialization

Invalid? Initializing mapping (invalid)

{

//Initial? food

srand(time(NULL));

food.x? =? Rand ()? %? (frame_height? -? 2)? +? 1;

Food. y? =? Rand ()? %? (frame_width? -? 2)? +? 1;

gotoxy(food.x,? food . y);

printf("!" );

//Initial? snake

snake.x[0]? =? Frame _ Height? /? 2;

snake.y[0]? =? Frame _ width? /? 2;

gotoxy(snake.x[0],? snake . y[0]);

printf(“@”);

Snack lane? =? 3;

Snakes. Speed? =? 200;

For what? (k? =? 1; ? k & ltsnake.len? k++)

{

snake.x[k]? =? snake.x[k? -? 1]? +? 1;

snake.y[k]? =? snake.y[k? -? 1];

gotoxy(snake.x[k],? snake . y[k]);

printf(“@”);

}

//Initial? bar

For what? (j? =? 0; ? J< frame _ width; ? j++)

{

gotoxy(0,j);

printf(" # ");

gotoxy(frame_height? -? 1,? j);

printf(" # ");

}

For what? (me? =? 1; ? Me< frame _ height? -? 1; ? i++)

{

Gotoxy (I, 0);

printf(" # ");

Gotoxy (me, frame _ width? -? 1);

printf(" # ");

}

}

//Generate? food

Invalid? Update _ Food ()

{

What if? (snake.x[0]? ==? Food & snake.y [0]? ==? food.y)

{

srand(time(NULL));

food.x? =? Rand ()? %? (frame_height? -? 2)? +? 1;

Food. y? =? Rand ()? %? (frame_width? -? 2)? +? 1;

gotoxy(food.x,? food . y);

printf("!" );

snake . len++;

Grow up? =? 1;

}

}

//Move? snake

Invalid? move_snake()

{

What if? (_kbhit())

ch? =? _ getch();

What if? (! Growth)

{

gotoxy(snake.x[snake.len? -? 1],? snake . y【snake . len? -? 1]);

printf("? );

}

For what? (k? =? Snack lane? -? 1; ? k & gt0; ? k -)

{

snake.x[k]? =? snake.x[k? -? 1];

snake.y[k]? =? snake.y[k? -? 1];

}

Switch? (ch)

{

Case? Up:? snake . x[0]-; ? Break;

Case? Down:? snake . x[0]++; ? Break;

Case? Left:? snake . y[0]-; ? Break;

Case? Right? snake . y[0]++; ? Break;

Default:? Break;

}

gotoxy(snake.x[0],? snake . y[0]);

printf(“@”);

Grow up? =? 0;

gotoxy(frame_height,? 0);

}

//Yes? alive

int? is_alive(void)

{

What if? (snake.x[0]? ==? 0? ||? snake.x[0]? ==? Frame _ Height? -? 1? ||? snake.y[0]? ==? Frame _ width? -? 1? ||? snake.y[0]? ==? 0)

Return? 0;

For what? (k? =? 1; ? k & ltsnake.len? k++)

What if? (snake.x[k]? ==? snake.x[0]? & amp& amp? snake.y[k]? ==? snake.y[0])

Return? 0;

Return? 1;

}

//Speed? up

Invalid? Get _ Speed (invalid)

{

What if? (snake.len? & lt=? 6)

Snakes. Speed? =? 200;

Or what? What if? (snake.len? & lt=? 10)

Snakes. Speed? =? 100;

Or what? What if? (snake.len? & lt=? 20)

Snakes. Speed? =? 50;

Or what? What if? (snake.len? & lt=? 30)

Snakes. Speed? =? 30;

Or what? Snakes. Speed? =? 20;

}

//Move? cursor

Invalid? gotoxy(int? x,? int? y)

{

Handle? hout

Coordinates cor

Haute? =? GetStdHandle(STD _ OUTPUT _ HANDLE);

Oh, my god x? =? y;

Oh, my god y? =? x;

SetConsoleCursorPosition(hout,cor);

}