如何用C++编译贪食蛇
1、首先打开C++编译器。
2、然后点击File点击New再点击source File创建。
3、再输入代码:#include<graphics.h>#include<stdlib.h>#include<dos.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011b

5、DELAY(char ch)/*调节游戏速度*/{if(ch=='3'){delay(gamespeed); /*delay是延迟函数*/delay(gamespeed);}else if(ch=='2'){delay(gamespeed);}}Menu()/*游戏开始菜单*/{char ch;printf("Please choose the gamespeed:\n");printf("1-Fast 2-Normal 3-Slow\n");printf("\nPlease Press The numbers..\n");do{ch=getch();}while(ch!='1'&&ch!='2'&&ch!='3');clrscr();return(ch);}

7、void GamePlay(char ch){randomize(); /*随机数发生器*/food.yes=1; /*1代表要出现食蹒效罩翔物,0表示以存在食物*/snake.life=0;snake.direction=1;snake.x[0]=100;snake.y[0]=100;snake.x[1]=110;snake.y[1]=100;snake.node=2;PrScore();while(1) /*可以重复游戏*/{while(!kbhit()) /*在没有按键的情况下蛇自己移动*/{if(food.yes==1) /*需要食物*/{food.x=rand()%400+60;food.y=rand()%350+60; /*使用rand函数随机产生食物坐标*/while(food.x%10!=0)food.x++;while(food.y%10!=0)food.y++; /*判断食物是否出现在整格里*/food.yes=0; /*现在有食物了*/}if(food.yes==0) /*有食物了就要显示出来*/{setcolor(GREEN);rectangle(food.x,food.y,food.x+10,food.y-10);}for(i=snake.node-1;i>0;i--) /*贪吃蛇的移动算法*/{snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1]; /*贪吃蛇的身体移动算法*/}if(key==RIGHT&&snake.direction!=2)snake.direction=1;elseif(key==LEFT&&snake.direction!=1)snake.direction=2;elseif(key==DOWN&&snake.direction!=3)snake.direction=4;}}void GameOver(void){cleardevice();setcolor(RED);settextstyle(0,0,4);outtextxy(200,200,"GAME OVER");getch();}void PrScore(void){char str[10];setfillstyle(SOLID_FILL,YELLOW);bar(50,15,220,35);setcolor(6);settextstyle(0,0,2);sprintf(str,"scord:%d",score);outtextxy(55,20,str);}void Close(void){getch();closegraph();}

10、void GameOver(void){cleardevice();setcolor(RED);settextstyle(0,0,4);outtextxy(200,200,"GAME OVER");getch();}void PrScore(void){char str[10];setfillstyle(SOLID_FILL,YELLOW);bar(50,15,220,35);setcolor(6);settextstyle(0,0,2);sprintf(str,"scord:%d",score);outtextxy(55,20,str);}void Close(void){getch();closegraph();}
