再发一贴:TC 2.0 下的《贪吃蛇》游戏
只为有兴趣,又写了这一程序,上次发的《推箱子》有些朋友没看到,为了兴趣的朋友都能看到,我都生成了应用程序,放在附件里。多谢支持!
《贪吃蛇》可执行文件.rar
(25.64 KB)
《推箱子》可执行文件.rar
(24.73 KB)
程序代码:
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <time.h> #include <dos.h> #define INTR 0x1C #define SIZE 5 #define KEY_UP 0x4800 #define KEY_DOWN 0x5000 #define KEY_LEFT 0x4b00 #define KEY_RIGHT 0x4d00 #define KEY_ESC 0x011b void interrupt ( *oldhandler)(void); int count=1,speed=0,pointing=0,score,food; int left,top; void interrupt handler(void) { disable(); count++; enable(); oldhandler(); } void Coloring(int i,int j)/* 食物填上绿色 */ { setfillstyle(1,GREEN); bar(left+1+SIZE*j,top+1+SIZE*i,left+j*SIZE+SIZE,top+i*SIZE+SIZE); } void SuiJiFood(int venues[][40],int suijisu)/* 随机生成5个食物 */ { int i,j,k; for(k=0;k<suijisu;k++) { i=rand()%40; j=rand()%40; if(venues[i][j]==1)k--; else { venues[i][j]=2; Coloring(i,j); } } food=suijisu; } int GameOver(void)/* 游戏结束,按回车重新开始,按Esc键退出 */ { char ch; setcolor(RED); rectangle(getmaxx()/2-50,getmaxy()/2-20,getmaxx()/2+70,getmaxy()/2+15); settextjustify(LEFT_TEXT,TOP_TEXT); setcolor(WHITE); outtextxy(getmaxx()/2-30,getmaxy()/2-16,"GameOver!"); outtextxy(getmaxx()/2-46,getmaxy()/2,"Continue:Enter"); while(1) { ch=getch(); if(ch==13) return 1; else if(ch==27) { closegraph(); exit(0); } } return 0; } void ShowScore()/* 显示得分 */ { char s[10]; itoa(score,s,10); setfillstyle(1,0); bar(left+260,top+20,left+300,top+50); setcolor(YELLOW); outtextxy(left+260,top+20,s); } void ShowGuan()/* 显示关 */ { char s[10]; itoa(speed+1,s,10); setfillstyle(1,0); bar(left+260,top,left+300,top+10); setcolor(YELLOW); outtextxy(left+260,top,s); } int Judge(int i,int j,int snake[][2],int venues[][40],int *n,int *snakelen) /* 判断蛇是否撞上墙、蛇身、食物。 */ { int k; if( i<0 || i>39 || j<0 || j>39 || venues[i][j]==1)/* 撞上墙或蛇身显示游戏结束 */ if(GameOver()) return 1; if(venues[i][j]==2)/* 撞上食物,加分,加蛇身上度,减去一个食物 */ { if(*n==*snakelen)(*n)++; (*snakelen)++; score+=10; if(score==200) { speed++; ShowGuan(); } food--; ShowScore(); } for(k=*n;k>0;k--)/* 数据型式的蛇身移动 */ { snake[k][0]=snake[k-1][0]; snake[k][1]=snake[k-1][1]; } snake[0][0]=i; snake[0][1]=j; venues[i][j]=1; return 0; } void Clearsnake(int venues[][40])/* 清除数据型式的蛇 */ { int i,j; for(i=0;i<40;i++) for(j=0;j<40;j++) venues[i][j]=0; } int main(void) { int gdriver = DETECT, gmode, errorcode; int right, bottom,suijisu=5; int key,point=0,snake[200][2]={0},snakelen=10,venues[40][40]={0}; time_t t; int i,j,n,iend,jend,s; initgraph(&gdriver, &gmode, ""); errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } left=getmaxx()/2-100; top=getmaxy()/2-100; right=getmaxx()/2+102; bottom=getmaxy()/2+102; oldhandler = getvect(INTR); setvect(INTR, handler); srand((unsigned)time(&t)); while(1) { rectangle(left,top,right,bottom);/* 画个100*102的方框 */ setfillstyle(1,DARKGRAY); bar(left+1,top+1,right-1,bottom-1);/* 给方框填色 */ /* 下面是显示一些信息 */ setcolor(YELLOW); outtextxy(left,top-20,"UserYuH:TanChiShe"); setcolor(WHITE); outtextxy(right+10,top,"Guan:"); outtextxy(right+10,top+20,"Score:"); rectangle(left,bottom+4,left+82,bottom+20); settextjustify(LEFT_TEXT,TOP_TEXT); outtextxy(left+4,bottom+8,"Esc:exit"); SuiJiFood(venues,suijisu);/* 调用随机生成5个食物的函数 */ switch(rand()%4)/* 随机选择蛇的出场方向 */ { case 0: i=-1; j=rand()%40; pointing=2;/* 2表示从方框上边拈出,方向:向下 */ break; case 1: j=-1; i=rand()%40; pointing=4;/* 4表示从方框左边拈出,方向:向右 */ break; case 2: i=40; j=rand()%40; pointing=1;/* 1表示从方框下边拈出,方向:向上 */ break; case 3: j=40; i=rand()%40; pointing=3;/* 3表示从方框右边拈出,方向:向左 */ break; } snakelen=10; count=0; score=0; n=0; ShowScore(); ShowGuan(); while(1) { if(count>1-speed) { count=0; switch(pointing) { case 1:i--;break; /* 上 */ case 2:i++;break; /* 下 */ case 3:j--;break; /* 左 */ case 4:j++;break; /* 右 */ } if(Judge(i,j,snake,venues,&n,&snakelen)) { Clearsnake(venues); speed=0; setfillstyle(1,0); bar(left-105,top-105,right+60,bottom+20); break; } setfillstyle(1,RED);/* 给新的蛇头填色 */ bar(left+1+SIZE*j,top+1+SIZE*i,left+SIZE*j+SIZE,top+SIZE*i+SIZE); if(n==snakelen) { iend=snake[snakelen][0]; jend=snake[snakelen][1]; venues[iend][jend]=0; setfillstyle(1,DARKGRAY);/* 把蛇尾涂掉 */ bar(left+1+SIZE*jend,top+1+SIZE*iend,left+SIZE*jend+SIZE,top+SIZE*iend+SIZE); } else n++; if(food==0)/* 食物为0,再生成5个食物 */ { SuiJiFood(venues,suijisu); } } if(bioskey(1))key=bioskey(0);/* 下面是从键盘获取键值 */ else key=0; if(key) switch(key) { case KEY_UP: if(pointing!=2) point=1; break; case KEY_DOWN: if(pointing!=1) point=2; break; case KEY_LEFT: if(pointing!=4) point=3; break; case KEY_RIGHT: if(pointing!=3) point=4; break; case KEY_ESC: exit(0); } if(point) { pointing=point; point=0; } } } setvect(INTR, oldhandler); getch(); closegraph(); return 0; }
[ 本帖最后由 UserYuH 于 2009-10-1 12:07 编辑 ]