C 语言编写的《推箱子》游戏
发两个Turbo C2.0写的《推箱子》和《贪吃蛇》游戏,附件里有可执行文件和代码,有兴趣的朋友可以看看。
贪吃蛇与推箱子游戏.rar
(56.19 KB)
·
·
·
程序在TC2.0、Win-TC下测试通过。
程序代码:
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <dos.h> #define SIZE 20 #define KEY_UP 0x4800 #define KEY_DOWN 0x5000 #define KEY_LEFT 0x4b00 #define KEY_RIGHT 0x4d00 #define KEY_ESC 0x011b #define Re 0x1372 int Step=0,right_Step,top_Step; void cheshi(int a[][6],int n,int m) { int i,j; for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("%d ",a[i][j]); printf("\n"); } } void Coloring(int x,int y,int subscript) { switch(subscript) { case 0: { setfillstyle(1,DARKGRAY); break; } case 1: { setfillstyle(6,YELLOW); break; } case 2: { setfillstyle(8,DARKGRAY); break; } case 3: { setfillstyle(1,CYAN); break; } case 4: { setfillstyle(1,RED); break; } case 5: { setfillstyle(1,CYAN); break; } case 6: { setfillstyle(1,RED); break; } } bar(x,y,x+SIZE,y+SIZE); } void display(int guan[][6],int imax,int jmax) { int gdriver = DETECT, gmode, errorcode; int left, top, right, bottom,x,y; int i,j; 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-jmax*SIZE+jmax*2+2; top=getmaxy()/2-((imax/2*SIZE)+(imax/2+4)); right=left+jmax*SIZE+jmax*2+2; bottom=getmaxy()/2+(((imax-imax/2)*SIZE)+((imax-imax/2)+4)); right_Step=right; top_Step=top; rectangle(left,top,right,bottom); for(i=0;i<imax;i++) { for(j=0;j<jmax;j++) { x=left+(j+1)*2+j*SIZE; y=top+(i+1)*2+i*SIZE; Coloring(x,y,guan[i][j]); } } setcolor(YELLOW); outtextxy(left,top-20,"UserYuH:BOX"); setcolor(WHITE); outtextxy(right+10,top,"Guan:"); outtextxy(right+10,top+20,"Step:"); rectangle(left,bottom+4,left+82,bottom+20); settextjustify(LEFT_TEXT,TOP_TEXT); outtextxy(left+4,bottom+8,"R:refresh"); rectangle(left+86,bottom+4,left+162,bottom+20); settextjustify(LEFT_TEXT,TOP_TEXT); outtextxy(left+92,bottom+8,"Esc:exit"); } void CopyGuan(int guan[][6],int guan_all[][6],int imax,int jmax) { int i,j; for(i=0;i<imax;i++) for(j=0;j<jmax;j++) guan[i][j]=guan_all[i][j]; } void select_guan(int guan_all[][6],int guan[][6],int subi_j[],int ijmax[],int guan_num,int *fulfil) { int i,j; switch(guan_num) { case 1: { *fulfil=2; subi_j[0]=2,subi_j[1]=0; ijmax[0]=6,ijmax[1]=4; CopyGuan(guan,guan_all,ijmax[0],ijmax[1]); break; } case 2: { *fulfil=2; subi_j[0]=2,subi_j[1]=0; ijmax[0]=5,ijmax[1]=5; CopyGuan(guan,guan_all,ijmax[0],ijmax[1]); break; } case 3: { *fulfil=1; subi_j[0]=3,subi_j[1]=3; ijmax[0]=6,ijmax[1]=6; break; } case 4: { *fulfil=0; subi_j[0]=1,subi_j[1]=1; ijmax[0]=6,ijmax[1]=6; break; } case 5: { *fulfil=0; subi_j[0]=4,subi_j[1]=2; ijmax[0]=5,ijmax[1]=6; break; } case 6: { *fulfil=1; subi_j[0]=0,subi_j[1]=4; ijmax[0]=6,ijmax[1]=5; break; } case 7: { *fulfil=0; subi_j[0]=2,subi_j[1]=4; ijmax[0]=6,ijmax[1]=5; break; } case 8: { *fulfil=0; subi_j[0]=0,subi_j[1]=4; ijmax[0]=6,ijmax[1]=6; break; } case 9: { *fulfil=0; subi_j[0]=3,subi_j[1]=1; ijmax[0]=4,ijmax[1]=6; break; } case 10: { *fulfil=1; subi_j[0]=3,subi_j[1]=3; ijmax[0]=6,ijmax[1]=5; break; } case 11: { *fulfil=1; subi_j[0]=4,subi_j[1]=1; ijmax[0]=6,ijmax[1]=5; break; } case 12: { *fulfil=0; subi_j[0]=0,subi_j[1]=2; ijmax[0]=5,ijmax[1]=6; break; } case 13: { *fulfil=1; subi_j[0]=2,subi_j[1]=3; ijmax[0]=6,ijmax[1]=5; break; } case 14: { *fulfil=0; subi_j[0]=3,subi_j[1]=0; ijmax[0]=5,ijmax[1]=6; break; } case 15: { *fulfil=1; subi_j[0]=0,subi_j[1]=3; ijmax[0]=6,ijmax[1]=6; break; } case 16: { *fulfil=0; subi_j[0]=0,subi_j[1]=3; ijmax[0]=5,ijmax[1]=5; break; } case 17: { *fulfil=0; subi_j[0]=3,subi_j[1]=5; ijmax[0]=4,ijmax[1]=6; break; } case 18: { *fulfil=1; subi_j[0]=0,subi_j[1]=1; ijmax[0]=6,ijmax[1]=6; break; } case 19: { *fulfil=0; subi_j[0]=5,subi_j[1]=4; ijmax[0]=6,ijmax[1]=6; break; } case 20: { *fulfil=2; subi_j[0]=1,subi_j[1]=4; ijmax[0]=5,ijmax[1]=6; break; } } CopyGuan(guan,guan_all,ijmax[0],ijmax[1]); } void Eixt() { exit(0); } void Move(int point,int guan[][6],int subi_j[],int imax,int jmax,int *fulfil) { int i,j,*ir,*jr,left,top; left=getmaxx()/2-jmax*SIZE+jmax*2+2; top=getmaxy()/2-((imax/2*SIZE)+(imax/2+4)); ir=&subi_j[0],jr=&subi_j[1]; i=*ir,j=*jr; switch(point) { case 1: { if(i>0)i--; else break; if(guan[i][j]==0) { if(guan[*ir][j]==6) { guan[i][j]=4; guan[*ir][j]=2; } else { guan[*ir][j]=0; guan[i][j]=4; } Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Coloring(left+(j+1)*2+j*SIZE,top+(*ir+1)*2+*ir*SIZE,guan[*ir][j]); *ir=i; Step++; } else if(guan[i][j]==3) { if(i>0 && (guan[i-1][j]==0||guan[i-1][j]==2)) { if(guan[i-1][j]==0) guan[i-1][j]=3; else { guan[i-1][j]=5; (*fulfil)++; } guan[i][j]=0; Coloring(left+(j+1)*2+j*SIZE,top+((i-1)+1)*2+(i-1)*SIZE,guan[i-1][j]); Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Step++; } } else if(guan[i][j]==5) { if(i>0 && (guan[i-1][j]==0||guan[i-1][j]==2)) { if(guan[i-1][j]==0) guan[i-1][j]=3; else { guan[i-1][j]=5; (*fulfil)++; } guan[i][j]=2; Coloring(left+(j+1)*2+j*SIZE,top+((i-1)+1)*2+(i-1)*SIZE,guan[i-1][j]); Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Step++; (*fulfil)--; } } else if(guan[i][j]==2) { guan[i][j]=6; if(guan[*ir][j]==4) guan[*ir][j]=0; else guan[*ir][j]=2; Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Coloring(left+(j+1)*2+j*SIZE,top+(*ir+1)*2+*ir*SIZE,guan[*ir][j]); *ir=i; Step++; } break; } case 2: { if(i<imax-1)i++; else break; if(guan[i][j]==0) { if(guan[*ir][j]==6) { guan[i][j]=4; guan[*ir][j]=2; } else { guan[*ir][j]=0; guan[i][j]=4; } Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Coloring(left+(j+1)*2+j*SIZE,top+(*ir+1)*2+*ir*SIZE,guan[*ir][j]); *ir=i; Step++; } else if(guan[i][j]==3) { if(i<imax-1 && (guan[i+1][j]==0||guan[i+1][j]==2)) { if(guan[i+1][j]==0) guan[i+1][j]=3; else { guan[i+1][j]=5; (*fulfil)++; } guan[i][j]=0; Coloring(left+(j+1)*2+j*SIZE,top+((i+1)+1)*2+(i+1)*SIZE,guan[i+1][j]); Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Step++; } } else if(guan[i][j]==5) { if(i<imax-1 && (guan[i+1][j]==0||guan[i+1][j]==2)) { if(guan[i+1][j]==0) guan[i+1][j]=3; else { guan[i+1][j]=5; (*fulfil)++; } guan[i][j]=2; Coloring(left+(j+1)*2+j*SIZE,top+((i+1)+1)*2+(i+1)*SIZE,guan[i+1][j]); Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Step++; (*fulfil)--; } } else if(guan[i][j]==2) { guan[i][j]=6; if(guan[*ir][j]=4) guan[*ir][j]=0; else guan[*ir][j]=2; Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Coloring(left+(j+1)*2+j*SIZE,top+(*ir+1)*2+*ir*SIZE,guan[*ir][j]); *ir=i; Step++; } break; } case 3: { if(j>0)j--; else break; if(guan[i][j]==0) { if(guan[i][*jr]==6) { guan[i][j]=4; guan[i][*jr]=2; } else { guan[i][*jr]=0; guan[i][j]=4; } Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Coloring(left+(*jr+1)*2+*jr*SIZE,top+(i+1)*2+i*SIZE,guan[i][*jr]); *jr=j; Step++; } else if(guan[i][j]==3) { if(j>0 && (guan[i][j-1]==0||guan[i][j-1]==2)) { if(guan[i][j-1]==0) guan[i][j-1]=3; else { guan[i][j-1]=5; (*fulfil)++; } guan[i][j]=0; Coloring(left+((j-1)+1)*2+(j-1)*SIZE,top+(i+1)*2+i*SIZE,guan[i][j-1]); Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Step++; } } else if(guan[i][j]==5) { if(j>0 && (guan[i][j-1]==0||guan[i][j-1]==2)) { if(guan[i][j-1]==0) guan[i][j-1]=3; else { guan[i][j-1]=5; (*fulfil)++; } guan[i][j]=2; Coloring(left+((j-1)+1)*2+(j-1)*SIZE,top+(i+1)*2+i*SIZE,guan[i][j-1]); Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Step++; (*fulfil)--; } } else if(guan[i][j]==2) { guan[i][j]=6; if(guan[i][*jr]==4) guan[i][*jr]=0; else guan[i][*jr]=2; Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Coloring(left+(*jr+1)*2+*jr*SIZE,top+(i+1)*2+i*SIZE,guan[i][*jr]); *jr=j; Step++; } break; } case 4: { if(j<jmax-1)j++; else break; if(guan[i][j]==0) { if(guan[i][*jr]==6) { guan[i][j]=4; guan[i][*jr]=2; } else { guan[i][*jr]=0; guan[i][j]=4; } Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Coloring(left+(*jr+1)*2+*jr*SIZE,top+(i+1)*2+i*SIZE,guan[i][*jr]); *jr=j; Step++; } else if(guan[i][j]==3) { if(j<jmax-1 && (guan[i][j+1]==0||guan[i][j+1]==2)) { if(guan[i][j+1]==0) guan[i][j+1]=3; else { guan[i][j+1]=5; (*fulfil)++; } guan[i][j]=0; Coloring(left+((j+1)+1)*2+(j+1)*SIZE,top+(i+1)*2+i*SIZE,guan[i][j+1]); Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Step++; } } else if(guan[i][j]==5) { if(j<jmax-1 && (guan[i][j+1]==0||guan[i][j+1]==2)) { if(guan[i][j+1]==0) guan[i][j+1]=3; else { guan[i][j+1]=5; (*fulfil)++; } guan[i][j]=2; Coloring(left+((j+1)+1)*2+(j+1)*SIZE,top+(i+1)*2+i*SIZE,guan[i][j+1]); Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Step++; (*fulfil)--; } } else if(guan[i][j]==2) { guan[i][j]=6; if(guan[i][*jr]==4) guan[i][*jr]=0; else guan[i][*jr]=2; Coloring(left+(j+1)*2+j*SIZE,top+(i+1)*2+i*SIZE,guan[i][j]); Coloring(left+(*jr+1)*2+*jr*SIZE,top+(i+1)*2+i*SIZE,guan[i][*jr]); *jr=j; Step++; } break; } } } void YouWin(void) { setcolor(RED); rectangle(getmaxx()/2-50,getmaxy()/2-20,getmaxx()/2+50,getmaxy()/2+15); settextjustify(LEFT_TEXT,TOP_TEXT); setcolor(WHITE); outtextxy(getmaxx()/2-46,getmaxy()/2-16,"YOU WIN!"); outtextxy(getmaxx()/2-46,getmaxy()/2,"Next:Enter"); getch(); clrscr(); } int main(void) { int guan[6][6],subi_j[2],ijmax[2],guan_num=1,key,fulfil=0,point; char s[10]; int guan_all[20][6][6]={ 0,0,2,0,0,0,0,1,0,5,0,0,4,0,3,0,0,0,0,1,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,2,0,5,0,0,0,0,1,0,0,0,4,3,5,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,0, 1,1,0,0,1,1,1,0,0,0,0,1,0,2,0,2,3,1,0,0,3,4,0,0,1,1,1,5,1,0,1,1,1,0,0,0, 0,0,0,1,0,0,0,4,0,0,3,0,1,3,1,1,2,2,1,0,2,1,0,0,1,3,0,0,0,0,1,0,0,1,1,1, 1,0,0,0,0,1,1,2,1,1,3,0,0,2,2,3,0,0,0,0,1,3,0,0,0,0,4,0,1,1,0,0,0,0,0,0, 1,1,1,0,4,0,0,0,5,3,0,0,0,0,0,0,0,0,1,0,2,1,1,0,1,3,0,1,1,0,1,0,2,1,1,0, 2,1,1,1,2,0,0,1,0,0,2,0,0,3,3,0,4,0,0,0,3,0,0,0,0,0,1,0,0,0,0,0,1,1,1,0, 1,1,1,0,4,1,1,1,1,0,0,0,2,0,1,3,3,0,0,0,0,0,0,1,2,0,0,3,1,1,1,2,0,0,1,1, 0,2,2,1,1,1,0,3,0,0,0,0,0,0,1,3,1,0,0,4,0,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,0,0,2,0,0,3,0,1,0,0,0,5,3,0,0,0,0,2,1,4,0,0,0,0,0,0,1,0,0,0,0,1,1,0, 2,0,0,1,1,0,0,1,0,0,0,0,0,2,0,1,0,0,0,3,5,3,0,0,1,4,0,1,1,0,1,0,0,1,1,0, 1,0,4,0,1,1,1,0,2,0,0,0,2,0,3,2,3,0,1,3,1,0,1,1,1,0,0,0,1,1,0,0,0,0,0,0, 1,0,0,0,1,0,0,3,1,0,1,0,0,2,0,4,1,0,0,5,0,0,0,0,1,0,1,3,0,0,1,2,0,0,1,0, 1,0,0,1,1,1,2,2,3,0,0,2,0,1,3,0,3,0,4,0,0,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, 0,0,0,4,1,1,0,3,1,0,1,1,0,5,0,3,0,0,0,0,0,1,1,0,1,2,0,0,2,0,1,1,0,0,0,1, 0,0,0,4,1,0,0,0,1,0,0,0,2,0,0,3,0,0,0,3,3,1,2,0,1,1,0,0,2,0,0,0,0,0,0,0, 1,1,2,0,1,1,0,2,0,0,1,1,0,0,0,3,3,0,1,0,2,0,3,4,0,0,0,0,0,0,0,0,0,0,0,0, 1,4,2,0,0,1,0,3,3,5,0,1,0,0,1,0,0,1,0,0,1,0,0,2,1,1,1,0,1,0,1,1,1,0,0,0, 1,1,1,1,0,0,1,1,1,1,3,2,1,1,0,0,2,0,1,1,0,0,1,2,0,3,0,0,3,0,0,0,0,1,4,0, 0,0,0,1,1,1,0,1,0,5,4,1,0,0,5,0,0,0,1,1,3,0,0,0,1,1,0,0,0,2,0,0,0,0,0,0 }; while(guan_num<=20) { loop1: select_guan(guan_all[guan_num-1],guan,subi_j,ijmax,guan_num,&fulfil); display(guan,ijmax[0],ijmax[1]); itoa(guan_num,s,10); setcolor(YELLOW); outtextxy(right_Step+50,top_Step,s); Step=0; while(fulfil!=3) { if(bioskey(1))key=bioskey(0); else key=0; if(key) switch(key) { case KEY_UP: point=1; break; case KEY_DOWN: point=2; break; case KEY_LEFT: point=3; break; case KEY_RIGHT: point=4; break; case KEY_ESC: Eixt(); case Re: clrscr(); goto loop1; break; /*default: printf("%x\n",key);*/ } if(point) { Move(point,guan,subi_j,ijmax[0],ijmax[1],&fulfil); point=0; itoa(Step,s,10); setfillstyle(1,0x0000); bar(right_Step+50,top_Step+20,right_Step+100,top_Step+40); setcolor(YELLOW); outtextxy(right_Step+50,top_Step+20,s); } } if(fulfil==3) { YouWin(); guan_num++; } } getch(); closegraph(); return 0; }
[ 本帖最后由 UserYuH 于 2009-11-15 11:49 编辑 ]