当前位置: 代码迷 >> C语言 >> 求助高人,蛇问题!
  详细解决方案

求助高人,蛇问题!

热度:314   发布时间:2006-03-03 22:40:00.0
求助高人,蛇问题!

高人好:请帮我看看下面的程序怎么回事,为什么只能吃一个方块。
#include<graphics.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
struct menu{
int left,top,right,bottom;
};
struct menu guest[11];
struct menu food={200,200,210,210}; file://食物坐标
int index=5;
void draw_guest(struct menu t[11]); file://绘制主人
void process_program(void); file://处理函数
void move_guest(struct menu t[11]); file://移
void delete_last_element(struct menu t[11]); file://删除数组的最后一个元素
void conversion_array_element(struct menu t[11]); file://转换数组元素
void set_top_element(struct menu t[11]); file://向上走
void set_bottom_element(struct menu t[11]); file://向下走
void set_left_element(struct menu t[11]); file://向左走
void set_right_element(struct menu t[11]); file://向右走
void renew_set_array(struct menu t[11]); file://重新设置数组
void renew_appear_food(void); file://重新出现实物
int main(void)
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc"); file://图形初始化
srand(time(NULL)); file://随机数
draw_guest(guest);
process_program();
closegraph();
return 0;
}
void draw_guest(struct menu t[11])
{
int i;
setcolor(2);
for(i=1;i<=index;i++) file://首先画5个方块
{
t[i].left=100+i*10-10;
t[i].top=100;
t[i].right=110+i*10-10;
t[i].bottom=110;
rectangle(t[i].left,t[i].top,t[i].right,t[i].bottom);
}
}
void process_program(void)
{
move_guest(guest);
}
void move_guest(struct menu t[11])
{
char ch;
setfillstyle(1,3); file://set food color
bar(food.left,food.top,food.right,food.bottom); file://made food
while(1)
{
if(kbhit())
{
ch=getch();
if(ch==119) file://ch=w
{
delete_last_element(t);
conversion_array_element(t);
set_top_element(t);
renew_set_array(t);
}
if(ch==115) file://ch=s
{
delete_last_element(t);
conversion_array_element(t);
set_bottom_element(t);
renew_set_array(t);
}
if(ch==97) file://ch=a
{
delete_last_element(t);
conversion_array_element(t);
set_left_element(t);
renew_set_array(t);
}
if(ch==100) file://ch=d
{
delete_last_element(t);
conversion_array_element(t);
set_right_element(t);
renew_set_array(t);
}
file://下面为当二者相与的时候
if(t[index].left==food.left && t[index].top==food.top && t[index].right==food.right && t[index].bottom==food.bottom)
{
setfillstyle(1,0);
bar(food.left,food.top,food.right,food.bottom); file://guest eat up food
index++;
t[index].left=t[index-1].left+10;
t[index].top=t[index-1].top;
t[index].right=t[index-1].right+10;
t[index].bottom=t[index-1].bottom;
renew_appear_food();
}
if(ch==27) file://ch=ESC
break;
}//if
}//while
}
void delete_last_element(struct menu t[11])
{
setcolor(0);
rectangle(t[1].left,t[1].top,t[1].right,t[1].bottom);
}
void conversion_array_element(struct menu t[11])
{
int i;
for(i=2;i<=index;i++)
t[i-1]=t[i];
}
void set_top_element(struct menu t[11])
{
t[index].top=t[index].top-10;
t[index].bottom=t[index].bottom-10;
}
void set_bottom_element(struct menu t[11])
{
t[index].top=t[index].top+10;
t[index].bottom=t[index].bottom+10;
}
void set_left_element(struct menu t[11])
{
t[index].left=t[index].left-10;
t[index].right=t[index].right-10;
}
void set_right_element(struct menu t[11])
{
t[index].left=t[index].left+10;
t[index].right=t[index].right+10;
}
void renew_set_array(struct menu t[11])
{
int i;
setcolor(2);
for(i=1;i<=index;i++)
rectangle(t[i].left,t[i].top,t[i].right,t[i].bottom);
}
void renew_appear_food(void)
{
int i,j;
i=100+rand()%400;
i=i/10*10;
j=100+rand()%400;
j=j/10*10;
setfillstyle(1,3);
bar(i,j,i+10,j+10);
}


----------------解决方案--------------------------------------------------------
下次别用file 来注释,如果你想让更多的人来帮助你
----------------解决方案--------------------------------------------------------
file不是我写上的,TC3.0就这样吧,我直接粘上来的
----------------解决方案--------------------------------------------------------
  相关解决方案