全部博文(165)
分类:
2007-05-09 10:33:26
贪吃蛇游戏--结构化编程
/*c语言编程--重要的的是结构化的编程思想*/
#include
#include
#include
#include
#include
#include
#define FIELD_WIDTH 300 /*就做20个格子的*/
#define FIELD_HEIGHT 300
#define FIELD_LEFT getmaxx()/2-FIELD_WIDTH/2
#define FIELD_TOP getmaxy()/2-FIELD_HEIGHT/2
#define CELL_NUMBER 15
#define CELL_WIDTH FIELD_HEIGHT/CELL_NUMBER
#define TAIJI_CX getmaxx()/2
#define TAIJI_CY 50
#define TAIJI_R 24
#define TAIJI_r 7
struct Rect
{ int left;
int top;
int right;
int bottom;
}field;
struct BeanStruct
{ int h;
int l;
}bean;
struct SnakeStruct
{ int h;
int l;
int direction;
int length;
}snake[100];
/*int direction;这是一个错误*/
main()
{
int driver=DETECT,mode=0,errorcode;
int i,key;
void Welcome();
void GameInit();
void SnakeTurn(int direction);
void SnakeMove();
void EatBean();
void Isdead();
initgraph(&driver,&mode,"c:\\turboc2");
Welcome();
GameInit();
while(1)
{ if(kbhit())
{ key=SpecialKey();
if(key==72) SnakeTurn(1);/*1:上,2:下,3:左,4:右*/
if(key==80) SnakeTurn(2);
if(key==75) SnakeTurn(3);
if(key==77) SnakeTurn(4);
}
else
{
SnakeMove();
EatBean();
Isdead();
for(i=0;i<=15;i++)
delay(80000);
}
}
}
/**************************************1层函数*********************************************/
void Welcome()
{ setfillstyle(SOLID_FILL,YELLOW);
bar(0,0,getmaxx(),getmaxy());
setfillstyle(SOLID_FILL,WHITE);
bar(80,80,getmaxx()-80,getmaxy()-80);
setfillstyle(SOLID_FILL,GREEN);
bar(100,100,getmaxx()-100,getmaxy()-100);
setcolor(BLACK);
settextstyle(1, 0, 3); /*三重笔划字体, 水平放大8倍*/
outtextxy(140, 140, "GAME: SNAKE ");
outtextxy(140, 180, "MAKER: HOU ");
settextstyle(1, 0, 6); /*三重笔划字体, 水平放大8倍*/
outtextxy(140, 220, "WELCOM");
setcolor(BLUE);
rectangle(0,0,getmaxx(),getmaxy());
rectangle(5,5,getmaxx()-5,getmaxy()-5);
getch();
cleardevice();
}
void GameInit()
{ void FieldInit();
void BeanInit();
void SnakeInit();
FieldInit();
SnakeInit();
BeanInit();
getch();
}
void SnakeTurn(int direction)/*1.看能不能变方向。2。变方向*/
{ if((direction==1)&&(snake[0].direction!=2)) snake[0].direction=1;
if((direction==2)&&(snake[0].direction!=1)) snake[0].direction=2;
if((direction==3)&&(snake[0].direction!=4)) snake[0].direction=3;
if((direction==4)&&(snake[0].direction!=3)) snake[0].direction=4;
}
void SnakeMove()
/*0.擦尾,1。移动身子;2。移动头。3,画头 */
/*另一种思维是全擦全画 */
/*问是先移头还是先移身子 */
/*移动身子的时候怎么移动? */
{ int i;
setfillstyle(SOLID_FILL,BROWN);
bar(FIELD_LEFT+snake[snake[0].length-1].l*CELL_WIDTH+1,FIELD_TOP+snake[snake[0].length-1].h*CELL_WIDTH+1,FIELD_LEFT+(snake[snake[0].length-1].l+1)*CELL_WIDTH-1,FIELD_TOP+(snake[snake[0].length-1].h+1)*CELL_WIDTH-1);
for(i=snake[0].length;i>=1;i--)
{ snake[i].h=snake[i-1].h;
snake[i].l=snake[i-1].l;
}
if(snake[0].direction==1) snake[0].h=snake[0].h-1;
if(snake[0].direction==2) snake[0].h=snake[0].h+1;
if(snake[0].direction==3) snake[0].l=snake[0].l-1;
if(snake[0].direction==4) snake[0].l=snake[0].l+1;
setfillstyle(SOLID_FILL,GREEN);
bar(FIELD_LEFT+snake[0].l*CELL_WIDTH+1,FIELD_TOP+snake[0].h*CELL_WIDTH+1,FIELD_LEFT+(snake[0].l+1)*CELL_WIDTH-1,FIELD_TOP+(snake[0].h+1)*CELL_WIDTH-1);
}
void EatBean()/*1.判断撞上了吗?2。长度加一,3。位置赋上什么?!*/
{ void BeanInit();
if((snake[0].h==bean.h)&&(snake[0].l==bean.l))
{
BeanInit();
/* snake[snake[0].length].h=bean.h;
snake[snake[0].length].l=bean.l;*//*这样是错误的现象*/
snake[snake[0].length].h=snake[snake[0].length-1].h;
snake[snake[0].length].l=snake[snake[0].length-1].l;
snake[0].length=snake[0].length+1;
}
}
void Isdead()/*几种情况下死?*/
{ int i,isDead=0;
for(i=1;i
isDead=1;
}
if((snake[0].h<0)||(snake[0].h>=CELL_NUMBER)||(snake[0].l<0)||(snake[0].l>=CELL_NUMBER))
isDead=1;
if(isDead==1)
{ setcolor(10);
settextstyle(1, 0, 3); /*三重笔划字体, 水平放大8倍*/
outtextxy(20, 120, "YOU LOST");
getch();
closegraph();
exit(1);
}
}
/**************************************二层函数*********************************************/
void FieldInit()
{ void PainTaiJi();
/*数值初始化*/
int i;
field.left=getmaxx()/2- FIELD_WIDTH /2;
field.right=getmaxx()/2+ FIELD_WIDTH /2;
field.top=getmaxy()/2-FIELD_HEIGHT/2;
field.bottom=getmaxy()/2+FIELD_HEIGHT/2;
setcolor(BLUE);
rectangle(0,0,getmaxx(),getmaxy());
rectangle(2,2,getmaxx()-2,getmaxy()-2);
/*画一个场背景*/
setfillstyle(SOLID_FILL,BROWN);
bar(field.left,field.top,field.right,field.bottom);
/*画一个场地外边*/
setcolor(BLUE);
rectangle(field.left,field.top,field.right,field.bottom);
rectangle(field.left-5,field.top-5,field.right+5,field.bottom+5);
setfillstyle(SOLID_FILL, LIGHTGRAY);
floodfill(field.left-1,field.top-1,BLUE);
/*画一个场地格子*/
setlinestyle(0,0,2);
/*画横条*/
for(i=0;i<=CELL_NUMBER;i++)
{ line(field.left,field.top+i*CELL_WIDTH,field.right,field.top+i*CELL_WIDTH);
}
/*画树条*/
for(i=0;i<=CELL_NUMBER;i++)
{ line(field.left+i*CELL_WIDTH,field.top,field.left+i*CELL_WIDTH,field.bottom);
}
setcolor(WHITE);
settextstyle(1, 0, 5); /*三重笔划字体, 水平放大8倍*/
outtextxy(getmaxx()/2-FIELD_WIDTH/2+10, getmaxy()-80, "MAKER : HOU");
PainTaiJi();
}
void SnakeInit()
{ int i;
snake[0].h=5;
snake[0].l=5;
snake[0].direction=1;
snake[0].length=3;
snake[1].h=5;
snake[1].l=6;
snake[2].h=5;
snake[2].l=7;
setcolor(CYAN);
setfillstyle(SOLID_FILL, GREEN);
for(i=0;i<=1;i++)
bar(FIELD_LEFT+snake[i].l*CELL_WIDTH+1,FIELD_TOP+snake[i].h*CELL_WIDTH+1,FIELD_LEFT+(snake[i].l+1)*CELL_WIDTH-1,FIELD_TOP+(snake[i].h+1)*CELL_WIDTH-1);
}
void BeanInit()
/*讨论:是否应该有豆动这个函数?????*//*生成成功?有没有和蛇重叠*/
/*或者说是豆的生成,如果被蛇吃了的话,可以在生成就行了,一样的函数*/
{ int i,ss=1,sum=0;
static cnum=0;
if(cnum!=0)
{ setfillstyle(SOLID_FILL,BROWN);
floodfill(FIELD_LEFT+bean.l*CELL_WIDTH+CELL_WIDTH/2,FIELD_TOP+bean.h*CELL_WIDTH+CELL_WIDTH/2,BLUE);
}
while(ss==1)
{
randomize();
bean.h=random(CELL_NUMBER);
bean.l=random(CELL_NUMBER);
for(i=0;i
sum=sum+1;
}
if(sum==snake[0].length) ss=0;
sum=0;
}
setcolor(CYAN);
setfillstyle(SOLID_FILL,GREEN);
circle(FIELD_LEFT+bean.l*CELL_WIDTH+CELL_WIDTH/2,FIELD_TOP+bean.h*CELL_WIDTH+CELL_WIDTH/2,CELL_WIDTH/2-1);
floodfill(FIELD_LEFT+bean.l*CELL_WIDTH+CELL_WIDTH/2,FIELD_TOP+bean.h*CELL_WIDTH+CELL_WIDTH/2,CYAN);
}
/**************************************三层函数*********************************************/
void PainTaiJi()
{ /*画外面的圆*/
setcolor(LIGHTGRAY);
circle(TAIJI_CX,TAIJI_CY,TAIJI_R);
arc(TAIJI_CX,TAIJI_CY-TAIJI_R/2,90,270,TAIJI_R/2);
arc(TAIJI_CX,TAIJI_CY+TAIJI_R/2,270,90,TAIJI_R/2);
/*画二个圆弧*/
setfillstyle(SOLID_FILL,WHITE);
floodfill(TAIJI_CX,TAIJI_CY-1, LIGHTGRAY);
setfillstyle(SOLID_FILL,BLACK);
floodfill(TAIJI_CX,TAIJI_CY+1, LIGHTGRAY);
/*画下面的小圈*/
setcolor(LIGHTGRAY);
circle(TAIJI_CX,TAIJI_CY+TAIJI_R/2,TAIJI_r);
setfillstyle(SOLID_FILL,WHITE);
floodfill(TAIJI_CX,TAIJI_CY+TAIJI_R/2,LIGHTGRAY);
/*画上面的小圈*/
setcolor(LIGHTGRAY);
circle(TAIJI_CX,TAIJI_CY-TAIJI_R/2,TAIJI_r);
setfillstyle(SOLID_FILL,BLACK);
floodfill(TAIJI_CX,TAIJI_CY-TAIJI_R/2,LIGHTGRAY);
}
/* ===============================公共函数====================================== */
int SpecialKey()
{ int key;
while(bioskey(1)==0);
key=bioskey(0);
if (key==0x11b) abort();
key=key&0xff?key&0xff:key>>8;
return(key);
}