#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
char s1_title[]=Snake game;
char s1_choose[3][11]={start game,author,exit};
char s2_title[]=Snake game,made by HungryAnt!;
char s2_fail[]=Game over!;
char s2_win[]=You win!!!;
int s1_x=320-45;
int s1_y=240-33;
int s2_x=320-150;
int s2_y=240-150;
int s3_x=320-120;
int s3_y=240-50;
char map[30][30];空地0,蛇身1,食物2
int length;
int direction=2;上,下,左,右0,1,2,3
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;
void initsnake(){蛇初始化
struct snake sn;
head=sn=(struct snake )malloc(sizeof(struct snake));蛇头
sn-x=14;
sn-y=15;
map[15][14]=1;
sn-previous=NULL;
sn-next=(struct snake )malloc(sizeof(struct snake));
sn-next-previous=sn;
sn=sn-next;
sn-x=15;
sn-y=15;
map[15][15]=1;
sn-next=(struct snake )malloc(sizeof(struct snake));
sn-next-previous=sn;
sn=sn-next;
sn-x=16;
sn-y=15;
map[15][16]=1;
sn-next=NULL;
tail=sn;蛇尾
}
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);
}
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);
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++;
break;
case ESC
exit(0);
break;
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 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);选择部分
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);设置深灰色的条
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();
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);输出标题
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 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);
}
void printsnake(){显示出蛇
struct snake sn=head;
box(sn-x,sn-y,LIGHTBLUE);
sn=head-next;
while(sn!=NULL){
box(sn-x,sn-y,LIGHTGREEN);
sn=sn-next;
}
}
void cleartime(){时间设置为0
struct time time_0;
time_0.ti_min=0;
time_0.ti_hour=0;
time_0.ti_hund=0;
time_0.ti_sec=0;
settime(&time_0);
}
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;
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
head-x--;
break;
case 3
head-x++;
break;
}
if(head-x0 head-x29 head-y0 head-y29 map[head-y][head-x]==1){如果蛇超出地图或者撞到自己,游戏失败
freesnake();
setviewport(0,0,639,479,0);
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;
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;
}
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 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);输出蛇长
printsnake();
cleartime();
key=0;
while(1){
gettime(&t_last);
if(t_last.ti_hunddelay_time)break;如果超过延时时间就退出
if(bioskey(1)){
key=bioskey(0);
switch(key){
case UP
if(direction!=1)direction=0;
break;
case DOWN
if(direction!=0)direction=1;
break;
case LEFT
if(direction!=3)direction=2;
break;
case RIGHT
if(direction!=2)direction=3;
break;
}
break;
}
}
snakemove();
if(key==ESC){
freesnake();
game_out=1;
setviewport(0,0,639,479,0);
clearviewport();
return;
}
if(game_out==1)return;
}
}
void s2(){游戏
length=3;
direction=2;
game_out
评论0