1。比代码长度.一个字符计 2 分
2。必须完成的功能:
1.方向键控制蛇行方向
2.随机布食
3.吃食蛇长节
4.蛇死法 除吃到自己必死,其它不作限制
3。加分(实际应是减分^_^)
每增加一功能减去10分.比如:蛇身着色,食物闪亮等等
[如有不同意见,跟贴]
就尽量不要太混乱的改了一下 #ifdef _WIN32 #define P Sleep #include <windows.h> #else #define P sleep #endif #define W 79 #define H 23
char M[H][W+1]; typedef struct n{int x,y;struct n*n;}n; n*S=0,b; int c,K;
w(x,y) // new node { n*t=malloc(sizeof(n)); return t->n=0,t->x=x,t->y=y,t; }
a(n**h) // snake add node { n*t=w((*h)->x,(*h)->y); t->n=*h,*h=t,c++,b.x=rand()%W,b.y=rand()%H; }
f(n**h) // free snake nodes { (*h)->n&&(f(&(*h)->n),0),free(*h),*h=0; }
m(n*t) // snake move { t->n&&(m(t->n),t->n->x=t->x,t->n->y=t->y); }
k(n**r) // check snake status { n*h=*r,*t=h->n; for(;t;t=t->n)if(!(t->x-h->x||t->y-h->y))return 0; h->x-b.x||h->y-b.y||a(r); return 1; }
i() // game init { S&&f(&S); S=w(W/2,H/2),a(&S),a(&S),a(&S),c=0,b.n=0,K='a'; }
p() // game print scene { int i; n*t; system("cls"),printf("%d\n",c),memset(M,' ',sizeof(M)); for(t=S;t;t=t->n)M[t->y][t->x]=48; M[b.y][b.x]='X'; for(i=0;i<H;i++)M[i][W]=0,puts(M[i]); }
s() // game step { return S&&(m(S),K=='w'&&(S->y--||(S->y+=H))||K=='a'&&(S->x--||(S->x+=W))|| K=='s'&&(++S->y-H||(S->y=0))||K=='d'&&(++S->x-W||(S->x=0)),1)?k(&S):0; }
int main() { do { for(i();s();)p(),P(c?600/(c+1)+100:500),kbhit()&&(K=getch()); printf("Game Over! You got %d point(S).\nContinue?(Y/N)\n",c),K=getch(); }while(K=='Y'||K=='y'); f(&S); return 0; } 改动了红色部分...
[此贴子已经被作者于2005-1-24 0:46:42编辑过]
直接压成一陀就是这样了: #ifdef _WIN32 #define P Sleep #include <windows.h> #else #define P sleep #endif #define W 79 #define H 23
char M[H][W+1];typedef struct n{int x,y;struct n*n;}n;n*S=0,b;int c,K;w(x,y){n*t =malloc(sizeof(n));return t->n=0,t->x=x,t->y=y,t;}a(n**h){n*t=w((*h)->x,(*h)->y) ;t->n=*h,*h=t,c++,b.x=rand()%W,b.y=rand()%H;}f(n**h){(*h)->n&&(f(&(*h)->n),0), free(*h),*h=0;}m(n*t){t->n&&(m(t->n),t->n->x=t->x,t->n->y=t->y);}k(n**r){n*h=*r, *t=h->n;for(;t;t=t->n)if(!(t->x-h->x||t->y-h->y))return 0;h->x-b.x||h->y-b.y||a( r);return 1;}i(){S&&f(&S);S=w(W/2,H/2),a(&S),a(&S),a(&S),c=0,b.n=0,K='a';}p(){ int i;n*t;system("cls"),printf("%d\n",c),memset(M,' ',sizeof(M));for(t=S;t;t=t-> n)M[t->y][t->x]=48;M[b.y][b.x]='X';for(i=0;i<H;i++)M[i][W]=0,puts(M[i]);}s(){ return S&&(m(S),K=='w'&&(S->y--||(S->y+=H))||K=='a'&&(S->x--||(S->x+=W))||K=='s' &&(++S->y-H||(S->y=0))||K=='d'&&(++S->x-W||(S->x=0)),1)?k(&S):0;}int main(){do{ for(i();s();)p(),P(c?600/(c+1)+100:500),kbhit()&&(K=getch());printf("Game Over!" "You got %d point(S).\nContinue?(Y/N)\n",c),K=getch();}while(K=='Y'||K=='y');f(& S);return 0;}
使用vc6.0编译通过 红色部分,第一次贴时搞错了。呵呵,用vc替换改名还是要小心的...
[此贴子已经被作者于2005-1-24 0:45:16编辑过]