当前位置: 代码迷 >> C语言 >> 大家好 能帮帮小妹吗
  详细解决方案

大家好 能帮帮小妹吗

热度:264   发布时间:2006-03-02 11:18:00.0
好的程序并不是有多复杂,而是大家都一看就明白.当然啦,也不是人人都想的到.
不过,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;
}


----------------解决方案--------------------------------------------------------
郁闷怎么都这么谦虚啊
编游戏我至今还没找到门在哪里!妹妹竟然这么说
看来我的智商该从新格式化了
伤心啊!
----------------解决方案--------------------------------------------------------
以下是引用梦想中国在2006-3-4 11:34:00的发言:

这是我学3天编出来的:请你发贴时把时间去掉,OK?


?? 说我嘛
不过,我初试了一下,你的程序在文件编写方面好像有些问题。


----------------解决方案--------------------------------------------------------
说楼主
----------------解决方案--------------------------------------------------------
  相关解决方案