不过,6楼说得不错,注释绝对不能少.
这是我学3天编出来的:请你发贴时把时间去掉,OK?
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
struct menu{
int left,top,right,bottom;
};
struct menu guest[100];
struct menu food;
int index=5;
void draw_guest(struct menu t[100]);
void process_program(void);
void move_guest(struct menu t[100]);
void delete_last_element(struct menu t[100]);
void conversion_array_element(struct menu t[100]);
void set_top_element(struct menu t[100]);
void set_bottom_element(struct menu t[100]);
void set_left_element(struct menu t[100]);
void set_right_element(struct menu t[100]);
void renew_set_array(struct menu t[100]);
void renew_appear_food(struct menu *f);
void output_score(void);
void set_screen(void);
int compare(struct menu t[100]);
int main(void)
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc");
srand(time(NULL));
set_screen();
draw_guest(guest);
process_program();
closegraph();
return 0;
}
void draw_guest(struct menu t[100])
{
int i;char text[]="Press Tab key,exit program.";
outtextxy(200,20,text);
setcolor(2);
for(i=1;i<=index;i++)
{
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[100])
{
char ch,text[]="USER Score:";
setcolor(15);
outtextxy(530,70,text);
renew_appear_food(&food);
while(1)
{
if(kbhit())
{
ch=getch();
if(ch==9) file://if ch equal tab key,exit program
break;
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);
}
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(&food);
set_screen();
output_score(); file://record score function
}
if(compare(t))
{
setcolor(15);
outtextxy(100,250,"The snake is deid,press anykey exit program");
getch();
break;
}
}//if
}//while
}
void delete_last_element(struct menu t[100])
{
setcolor(0);
rectangle(t[1].left,t[1].top,t[1].right,t[1].bottom);
}
void conversion_array_element(struct menu t[100])
{
int i;
for(i=2;i<=index;i++)
t[i-1]=t[i];
}
void set_top_element(struct menu t[100])
{
t[index].top=t[index].top-10;
t[index].bottom=t[index].bottom-10;
}
void set_bottom_element(struct menu t[100])
{
t[index].top=t[index].top+10;
t[index].bottom=t[index].bottom+10;
}
void set_left_element(struct menu t[100])
{
t[index].left=t[index].left-10;
t[index].right=t[index].right-10;
}
void set_right_element(struct menu t[100])
{
t[index].left=t[index].left+10;
t[index].right=t[index].right+10;
}
void renew_set_array(struct menu t[100])
{
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(struct menu *f)
{
f->left=100+rand()%300;
f->left=f->left/10*10;
f->top=70+rand()%300;
f->top=f->top/10*10;
f->right=f->left+10;
f->bottom=f->top+10;
setfillstyle(1,15);
bar(f->left,f->top,f->right,f->bottom);
}
void output_score(void)
{
static int i=0;
char string[20]; file://attention array
i++;
setfillstyle(1,0);
bar(540,80,570,110);
sprintf(string,"%d",i);
setcolor(15);
outtextxy(550,100,string);
}
void set_screen(void)
{
setcolor(15);
setlinestyle(1,0,3);
rectangle(50,50,500,400);
}
int compare(struct menu t[100])
{
int i,flag=0;
for(i=1;i<index;i++)
{
if(t[i].left==t[index].left && t[i].top==t[index].top && t[i].right==t[index].right && t[i].bottom==t[index].bottom)
flag=1;
if(t[index].left==t[index-2].left && t[index].top==t[index-2].top && t[index].right==t[index-2].right && t[index].bottom==t[index-2].bottom)
flag=0;
}
return flag;
}