C语言贪吃蛇文档
case UP if(direction!=1)direction=0; break;
case DOWN if(direction!=0)direction=1; break;
case LEFT if(direction!=3)direction=2; break;
outtextxy(3,i(240-s1_y)23+8,s1_choose);
while(bioskey(1)==0); key=bioskey(0); switch(key){
case UP if(i0)j=i--; break;
case DOWN if(i2)j=i++; b
int delay_time=20;延时,单位 10 毫秒
int difficult=0;游戏难度
int game_out=0;
struct snake{蛇结构体 char x; char y;
struct snake previous;前 struct snake next; }head,tail;
barbox(0,-25,BLUE,(320-s1_x)2,24);设置标题框架填充
settextstyle(0,0,1); setcolor(WHITE); outtextxy(5,-17,s1_title);输出标题
while(i3){ barbox(1,i(240-s1_y)23+1,DARKGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置深灰色的条
#includestdio.h #includebios.h #includeconio.h #includedos.h #includegraphics.h #includealloc.h #includestdlib.h #includetime.h
#define LEFT 0x4B00 #define RIGHT 0x4D00 #define UP 0x4800 #define DOWN 0x5000 #define ESC 0x011B #define ENTER 0x1C0D
void s1_window(){进入程序的第一个界面--中间的窗口 int i=0; setviewport(s1_x,s1_y,640-s1_x-1,480-s1_y-1,0);
setcolor(LIGHTBLUE);画两个连在一起的框架 rectangle(-1,-26,(320-s1_x)2,-1); 标题部分 rectangle(-1,-1,(320-s1_x)2,(240-s1_y)2+1);选择部分
printsnake(); cleartime(); key=0; while(1){
gettime(&t_last); if(t_last.ti_hunddelay_time)break;如果超过延时时间就退出 if(bioskey(1)){
key=bioskey(0); switch(key){
setcolor(LIGHTBLUE);画两个连在一起的框架 rectangle(-1,-30,(320-s2_x)2+1,-1); 分值框架 barbox(0,-29,BLUE,(320-s2_x)2+1,28);设置分值框架填充
settextstyle(0,0,1); setcolor(WHITE); outtextxy(5,-17,s2_title);输出标题
if(map[head-y][head-x]==2){如果遇到食物 length++;长度增加 if(length==30){ freesnake(); setviewport(0,0,639,479,0); settextstyle(0,0,3); setcolor(WHITE); outtextxy(206,211,s2_win);显示 you win setcolor(LIGHTRED); outtextxy(205,210,s2_win);显示 you win getch(); clearviewport();清除屏幕内容 game_out=1; return;
head=(struct snake )malloc(sizeof(struct snake));蛇移动一格
head-x=sn-x; head-y=sn-y; switch(direction){
case 0 head-y--; break;
case 1 head-y++; break;
case 2
case ENTER return i;
}
if(j!=i){ barbox(1,j(240-s1_y)23+1,DARKGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置浅灰色的选择条
setcolor(WHITE); outtextxy(3,j(240-s1_y)23+8,s1_choose[j]); } }while(1); }
}
void freesnake(){释放蛇所占的内存 struct snake sn,sn1; sn=head; head=NULL; while(sn!=NULL){free 蛇所占的内存 sn1=sn-next; free(sn); sn=sn1; }
}
void snakemove(){蛇移动 struct snake sn; sn=head;
void gamestart(){游戏进行中 int key; char l[4]; struct time t_last;
setfood(); while(1){
barbox((320-s2_x)2-28,-29,BLUE,27,28);覆盖上次蛇长 sprintf(l,%d,length); setcolor(YELLOW); outtextxy((320-s2_x)2-28,-17,l);输出蛇长
settextstyle(0,0,3); setcolor(CYAN); outtextxy(205,210,s2_fail);显示 game over getch(); clearviewport();清除屏幕内容 game_out=1; return; } else{ head-next=sn; sn-previous=head; head-previous=NULL;
head-x--; break; case 3 head-x++; break; }
if(head-x0 head-x29 游戏失败
freesnake();
head-y0
head-y29
map[head-y][head-x]==1){如果蛇超出地图或者撞到自己,
setviewport(0,0,639,479,0);
} difficult=(length-3)3; delay_time=20-difficult;
map[head-y][head-x]=1;
setfood();再添加一个食物 } else{如果没有遇到食物则清除蛇尾
map[tail-y][tail-x]=0; sn=tail; tail=tail-previous; tail-next=NULL; map[sn-y][sn-x]=0; box(sn-x,sn-y,DARKGRAY); free(sn); map[head-y][head-x]=1; } } }
}
void barbox(int x,int y,int color,int width,int height){填充一定范围的函数 setfillstyle(SOLID_FILL,color); bar(x,y,x+width-1,y+height-1);
}
void box(int x,int y,int color){填充地图小方格函数 barbox(x10+1,y10+1,color,9,9);
setcolor(WHITE);
outtextxy(3,i(240-s1_y)23+8,s1_choose); i++; }
}
void s2_window(){第二个界面--游戏界面 int x,y; setviewport(s2_x,s2_y,640-s2_x-1,480-s2_y-1,0); clearviewport();
}
void initmap(){初始化地图 int x,y; for(y=0;y30;y++) for(x=0;x30;x++) map[y][x]=0;
}
void setfood(){随机产生一个食物 int x,y; do{ x=rand()%30; y=rand()%30; }while(map[y][x]==1); map[y][x]=2; box(x,y,YELLOW);
}
int choose(){在 s1 窗口里的选择框里进行选择 int key,i=0,j;
do{ j=i; barbox(1,i(240-s1_y)23+1,LIGHTGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置浅灰色的选择条 setcolor(BLUE); outtextxy(4,i(240-s1_y)23+9,s1_choose); setcolor(WHITE);
setcolor(LIGHTGRAY); rectangle(-1,0,(320-s2_x)2+1,(240-s2_y)2+1);地图框架 rectangle(0,0,(320-s2_x)2,(240-s2_y)2);
for(y=0;y300;y+=10)地图绘制 for(x=0;x300;x+=10) barbox(x+1,y+1,DARKGRAY,9,9);
}
void printsnake(){显示出蛇