The realization of Snake C language

Source: Internet
Author: User
Tags goto

The program at run time also has some errors not to find out, in the left and right side of the wall will be one before the end game

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Char map[8][16];//game Map
int h=8,l=16;//the height and length of the map
void Mapinitial ();//initialization of the map
void Cretefood ();//randomly produce food
void Getdirection ();//Read Direction
void Smove ();//Movement of snakes
void move ();//Snake head, Snake tail movement (included in Smove)
void Show ();//Refresh Show Map
void Gameover ();//Judge whether the game is over or not
int slength=2;//The length of the snake
int dx[4]={-1,1,0,0};//-1 means moving up, 1 means moving down (for the movement of the Snake head)
int dy[4]={0,0,-1,1};//-1 for left motion, 1 for right motion
int over=0;
const char shead= ' @ ';//Snake head
const char stail= ' * ';//Snake tail
const char food= ' $ '; Food

struct {
int x;
An int y;//x,y is used to determine the position of a snake.
int direction;//The direction of the movement of the snake head by using the 1,2,3,4 to describe the upper and lower left
}snake[8*16];//information about each location of the snake. Snake[0] denotes a snake head

int main (void)
{

Mapinitial ();
while (1)
{
Smove ();

if (over==1)
{
printf ("Game over \");
return 0;

}
}



}

void Mapinitial ()
{

int i=0,j=0;

for (i=0;ifor (j=0;j<l;j++)
Map[i][j]= '. '; /map memory with '. ' Fill


int x=0,y=0;
Srand (Time (0));
H:
X=rand ()%H; X represents the row, and Y represents the column
Y=rand ()%l;
if (x{
map[x][y]=shead;//for the first time randomly generated snake head
map[x+1][y]=stail;//!! Snakes are born with a small tail
}
Else
Goto h;
Snake[0].x=x;
Snake[0].y=y;
snake[1].x=x+1;
Snake[1].y=y;

Cretefood ();

for (i=0;i{for (j=0;j<l;j++)
printf ("%c", Map[i][j]);
printf ("\ n");}

Getch ();//Get the first direction to move it, the keys in the smove change direction

}

void Cretefood ()
{
int x, y;
Here: the//srand () function provides the seed for the RAND function, which causes the random number of the rand () function to change
Srand (Time (0)); Time (0) returns the number of seconds in the current%60
X=rand ()%H;
Y=rand ()%l;
if (map[x][y]== '. ')
Map[x][y]= ' $ ';
Else
goto here;
}


void Getdirection ()
{
Char key;//Read direction

_sleep (500);//The Snake head to pause after moving, because the player can see the snake to a certain position to have the time to press the button, so that the player presses the button and then the next coordinate to change position
Otherwise, the player may press the button at a certain point, but the computer has been run to the next time so that the snake can only make the next turn.
//!! And the position of the pause should be in the computer read the button before the player in the pause time to make the operation can be the first time in this round to be reflected!!

if (Kbhit ()!=0)
{

while (Kbhit ()! = 0)
Key=getch ();


Switch (key)
{

Case 72:snake[0].direction = 0;//on
Break

Case 80:snake[0].direction = 1; Under
Break

Case 75:snake[0].direction = 2; Left
Break

Case 77:snake[0].direction = 3;//Right
Break

}

}
}


void Smove ()
{
Getdirection ();//Get keys, no buttons move in one Direction, others turn
Move ();//Snake head, snake tail moving separately
Show ();//Refresh Map

}

void Move ()
{
int sum=slength;//the length of the current snake

int x=snake[0].x+dx[snake[0].direction];
Int y=snake[0]. Y+dy[snake[0].direction];

int p=snake[0].x;
int q=snake[0].y;//p,q indicates the position before the snake head is moved

if (map[x][y]== '. ')
{
map[snake[0].x][snake[0].y]= '. ';
map[x][y]= ' @ ';
}//not eat food, continue to move

else if (map[x][y]== ' $ ')
{
map[snake[0].x][snake[0].y]= ' * ';
map[x][y]= ' @ ';
Snake[slength].x=p;
snake[slength].y=q;//New Snake length is the original snake head part
Cretefood ();
slength++;
}//Eat food, the original place into a snake tail, create new food, the snake grows
else
{
over=1;//snake itself collision game End
printf ("Game Over");
return 0;
}

Gameover ();

Snake[0].x=x;
snake[0].y=y;//The position of the snake head after moving

///////////////////////////////////////////////////////// The above is the movement of the snake head and the snake head to eat food length plus 1
/////////////////////////////////////////////////////////(the original food into a snake head, the original snake head into the tail of the snake)


int i;
if (sum==slength)//when there is no food to eat, the movement of the snake body.
{

for (i=1;i<slength;i++)
{
if (i==1)
Map[snake[i].x][snake[i].y]= '. '; /Put a blank so that the total length of the move is unchanged
if (i = = Slength-1)
{
snake[i].x = p;
SNAKE[I].Y = q;

}
Else
{
snake[i].x = snake[i+1].x;
SNAKE[I].Y = Snake[i+1].y;//i+1 is the part of the I Front

}

map[snake[i].x] [snake[i].y] = ' * ';
}//for
}//if



}

void Show ()
{

System ("CLS");
int i=0,j=0;

for (i = 0; i < H; i++)
{
for (j = 0; J < L; J + +)
printf ("%c", Map[i][j]);
printf ("\ n");
}

}

void Gameover ()
{
if (snake[0].x<0| | snake[0].x>=h| | snake[0].y<0| | SNAKE[0].Y>=L)
Over=1;
}

The realization of Snake C language

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.