Traditional Culture Encyclopedia - Traditional festivals - Looking for snakes code to run in VC++6.0
Looking for snakes code to run in VC++6.0
#include < stdio, h >
#include < process. h >
#include < Windows. h >
#include < conio. h >
#include < time. h >
< p>#Include < stdlib. H >#defineWIDTH40
#defineHEIGH12
Enumeration of directions
Left,
Right,
Up
Down
};
StructFood{//Food
Intx;
Inty;
}};
{//Draw the snake body
intx;
inty;
structNode*next;
}}};
structSnake{//snake attribute du
intlenth; //length
enumdirectiondir; //direction
};
structFood*food; //food
structSnake*snake; // Snake Attributes
structNode*snode, *tail; // snake body
intSPEECH = 200;
intscore = 0; // score
intsmark = 0; // eat food mark zhi mark
inttimes = 0;
intSTOP = 0;
voidInitfood(); //generate food
voidInitsnake(); //construct snake
voidEatfood(); //head forward
voidAddnode(intx, inty); //add snake body
voiddisplay(structNode*shead); //display snake body sitting dao marker
voidmove(); //snake move
voiddraw(); //draw snake
voidHomepage(); //homepage
< p>voidkeybordhit(); //monitor keyboard keysvoidAddtail(); //eat food
voidgotoxy(intx, inty); //locate the cursor
{
COORDpos;
Pos. X = x -1;
pos.Y = y-1;
SetConsoleCursorPosition(GetStdHandle(STD__OUTPUT__HANDLE), pos);
}
voidInitsnake()//construct snake
{
inti;
snake = (structSnake*)malloc(sizeof(structSnake));
tail = (structNode*)malloc (sizeof(structNode));
food = (structFood*)malloc(sizeof(structFood));
snake->lenth=5;//initial length 5
snake->dir= RIGHT;//initial snake head direction RIGHT
for(i=2;i<=snake->lenth+2;i++)//add 5 nodes
{
Addnode(i, 2);
}
}
voidInitfood( )//generate food
{
structNode*p=snode;
intmark=1;
srand((unsigned)time(NULL));//generate a random number seeded with time
while(1)
{<
food->x=rand()%(WIDTH-2)+2;//food X coordinate
food->y=rand()%(HEIGH-2)+2;//food Y coordinate
while (p!=NULL)
{
if (( food->x==p->x)&&(food->y==p->y))//If the food is generated on the snake
{// then regenerate the food
mark = 0; //food generation is invalid
break;
}
p=p->next;
}
if (mark == 1) // if the food is not on the snake, generate the food, otherwise regenerate the food
{
gotoxy(food->x,food->y);
printf( "%c", 3);
break;
}
mark = 1;
p=snode;
}
}
voidmove()//move
{
structNode *q, *p = snode;
if(snake->dir==RIGHT)
{
Addnode(p->x+1,p->y);
if(smark==0)
{
while(p->next!=NULL)
{
q=p;
p=p->next;
}
q->next=NULL;
free(p);
}
}
if(snake->dir==LEFT)
{
Addnode(p->x-1,p->y);
if(smark==0)
{
while(p->next!=NULL)
{
q=p;
p=p->next;
}
q->next=NULL;
free(p);
}
}
if(snake->dir==UP)
< p>{Addnode(p->x,p->y-1);
if(snake->dir==0)
{
while(p->next!=NULL)
{
q=p;
p=p-> next;
}
q->next=NULL;
free(p);
}
}
if(snake->dir==DOWN)
{
Addnode(p->x,p- >y+1);
if (smark == 0)
{
while (p->next!=NULL)
{
q=p;
p=p->next;
}
q->next = NULL;
free(p);
}
}
}
}
voidAddnode(intx, inty)//add snake
{
structNode*newnode = (structNode*) malloc(sizeof(structNode));
structNode*p=snode;
newnode->next=snode;
newnode->x=x;
newnode->y=y;<
snode = newnode; //node added to the head of the snake
if(x<2||x>=WIDTH||y<2||y>=HEIGH)//hit the boundary
{
STOP = 1;
gotoxy (10, 19);
printfrom the end of the snake. p>
printf("Hit the wall, game over, exit with any key! \n"); //fail
__getch();
free(snode); //free memory
free(snake);
exit(0);
}
while (p! = NULL) //hits the self
{
if (p->next!=NULL)
if ((p->x==x)&&(p->y==y))
{
STOP = 1;
gotoxy(10, 19);
< p>printf ("Hit itself, game over, exit with any key! \n"); //fail__getch();
free(snode); //free memory
free(snake);
exit(0);
}
p=p->next;
}
}
voidEatfood() //Eat food
{
Addtail();
score++;
}
voidAddtail() //Add snake's tail
{
structNode*newnode = (structNode*)malloc(sizeof(structNode));
structNode*p=snode;
tail->next=newnode;
newnode-& gt;x=50;
newnode->y=20;
newnode->next=NULL;//node added to the head of the snake
tail=newnode;//new tail
}
voiddraw()╱/draw snake
{
structNode *p = snode;
inti, j;
while (p!=NULL)
{
gotoxy(p->x,p->y);
printf("% c", 2);
tail = p;
p=p->next;
}
if(snode->x==food->x&&&snode->y==food->y)// Snake head coordinates equal to food coordinates
{
smark = 1;
Eatfood();//increase node
Initfood();//generate food
}
if (smark == 0)
{
gotoxy( tail->x,tail->y);//not eat the food to clear the tail node
printf("%c", '');//if eat the food, not clear the tail node
}
else
{
times = 1;
}
if((smark==1)&&(times==1))
{
gotoxy(tail->x,tail->y);//not eating food clears the tail node before
printf("%c", '');//not clearing tail node if food is eaten
smark = 0;
}
gotoxy(50, 12);
printf("food: %d,%d",food-& gt;x,food->y);
gotoxy(50, 5);
printf("score: %d",score);
gotoxy(50, 7);
printf( "Speed: %d", SPEECH);
gotoxy (15, 14);
printf ("Press o to accelerate");
gotoxy (15, 15);<
printf("Press p to slow down");
gotoxy (15, 16);
printf("Press space to pause");
}
voidHideCursor()//Hide Cursor
{
CONSOLE__CURSOR__INFOcursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_ HANDLE), &cursor_info);
}
voidHomepage() //draw homepage
{
intx,y;
HideCursor(); //hide the cursor
printf ( "--------- ---------- ---------- ---------- -\n");
printf ("|\t\t\t\t \t \t \n");
printf ("|\t\t \t \t \t \t \n ");
printf("|\t\t\t |t \n");
printf("|\t\t\t |t \n");
printf ("|\t\t \t \t |t \n");
printf ("|\t \t \t \t \n");
printf ("|\t \t \t \t \t \t \n");
printf ("|\t \t \t \t \t \t \n");
printf ("|\t |\t\t\t\t|\n");
printf ("|\t\t\t\t|\n");
printf ("|\t\t\t\t|\n ");
printf("|\t\t \t |t \n");
printf("--") ---------- ---------- ---------- --------\n");
gotoxy (5, 13);
printf ("Start the game with any key! Press W. A. S. D to control the direction");
__getch();
Initsnake();
Initfood();
gotoxy (5, 13);
printf( ");
}
voidkeybordhit() //monitor keyboard
{
charch;
if (___kbhit())
{
ch = getch();
switch(ch)
{
case 'W':
case 'w':if(snake->dir==DOWN)//If the original direction is DOWN, and it is invalid to press in the opposite direction
{
break;
}
else
snake->dir=UP;break;
case 'A':
case 'a':if(snake->dir==RIGHT)//If the original direction is RIGHT, then pressing the opposite direction is invalid
{
break;
Begins the process by pressing the opposite direction. >}}
else
snake->dir=LEFT;break;
case 'S':
case 's':if(snake->dir==UP)//if the original direction is up,and pressing the opposite direction is not valid
{
break;
}
else
snake->dir=DOWN;break;
case 'D':
case 'd':if(snake->dir==LEFT)//if the original direction was left, and pressing the opposite direction is invalid
{If the original direction was left, and pressing the opposite direction is invalid
cause 'd' is not valid.
{
break;
}
else
snake->dir=RIGHT;break;
case'O':
case'o':
if(SPEECH>=150)// Speed up
{
SPEECH = SPEECH-50;
}
break;
case'P':
case'p':
if(SPEECH<=400)//Speed down
{
SPEECH = SPEECH + 50;
}
break;
case''':
pause
gotoxy (15, 18);
printf("Game has been paused, press any key to Resume game");
system("pause>nul");
gotoxy (15, 18);
printf("");
break;
< p>default: break;}
}
}
intmain (void)//entry to the program
{
Homepage();
while(!STOP)
{
keybordhit(); //monitor keyboard keys
move(); //change of snake's coordinates
draw(); //redrawing of snake
Sleep(SPEECH); //temporarily hangs the thread
}
return0;
}
Extension:
Notes:
1. Code converts source information into symbols that can be easily communicated or stored. Decoding (decoding) is the process of reducing and translating code symbols into a form that the receiver can understand.
2. One reason for coding is to communicate in situations where ordinary language (spoken or written) is difficult. For example, a flag can convey a particular message with a particular mark, and another person standing at a distance can interpret the mark to reproduce the message.
- Related articles
- Self-expression Analysis of Traditional Dietary Activities
- What are the top ten cuisines in Hangzhou?
- What are the ways of spreading culture?
- Famous sayings of contemporary celebrities
- What are the history, beauty, products and customs of Taiwan Province Province?
- Briefly explain the difference and connection between character and morality.
- Steamed steamed buns with cold water or boiled water?
- Information about characteristic houses.
- Who is the working emperor of China?
- The practice of lobster tail