小游戏-十全十美
程序代码:
#define Up 0x4800 #define Down 0x5000 #define P 0x1970 #define R 0x1372 #define Enter 0x1c0d #define Esc 0x11b #define Right 0x4d00 #define Left 0x4b00 #include<bios.h> #include<stdio.h> #include<dos.h> #include<graphics.h> #include<conio.h> #include<stdlib.h> //定义核心数据 int LargeStartx; int LargeStarty; //大方格的起始位置 int SmallStartx; int SmallStarty; //小方格的起始位置 int array=5; //行列式的大小 int position; //方格的位置用0-array*array-1表示 typedef struct rect2 //定义大方格的属性即移动方格的属性 { int nowx; int nowy; int agox; int agoy; int size;//40 char color; }Large; Large LargeRect; char Attr[10*10]; //记录小方格的状态,用'W'(white)与'R'(red)表示。 typedef struct add //用于改变小方格颜色时,五个方向的增加量(包括当前位置) { int x; int y; int p; }Add; typedef struct rect1 //记录小方格的当前位置=大方格的当前位置-(大方格的大小-小方格的大小)/2,x轴与y轴一样 { int nowx; int nowy; int size; //20 char color; }Small; Small SmallRect; void ChargeColor(int ); void InitData1(); void InitData2(); void DrawLargeRect(int ,int ,int ,char ); void DrawSmallRect(int ,int ,int ,char ); void InitPic(); int Win(); int Judge(int ,int ); void main() { int key; int gdriver=DETECT, gmode; registerbgidriver(EGAVGA_driver); initgraph(&gdriver, &gmode, ""); setbkcolor(BLUE); cleardevice(); InitData1(); InitData2(); InitPic(); do { key=bioskey(0); LargeRect.agox=LargeRect.nowx; LargeRect.agoy=LargeRect.nowy; switch(key) { case Up: { LargeRect.nowy-=LargeRect.size; if(LargeRect.nowy<LargeStarty) {LargeRect.nowy+=LargeRect.size;continue;} setwritemode(1); DrawLargeRect(LargeRect.agox,LargeRect.agoy,LargeRect.size,LargeRect.color); DrawLargeRect(LargeRect.nowx,LargeRect.nowy,LargeRect.size,LargeRect.color); setwritemode(0); position-=array; break; } case Down: { LargeRect.nowy+=LargeRect.size; if(LargeRect.nowy>LargeStarty+LargeRect.size*(array-1)) {LargeRect.nowy-=LargeRect.size;continue;} setwritemode(1); DrawLargeRect(LargeRect.agox,LargeRect.agoy,LargeRect.size,LargeRect.color); DrawLargeRect(LargeRect.nowx,LargeRect.nowy,LargeRect.size,LargeRect.color); setwritemode(0); position+=array; break; } case Left: { LargeRect.nowx-=LargeRect.size; if(LargeRect.nowx<LargeStartx) {LargeRect.nowx+=LargeRect.size;continue;} setwritemode(1); DrawLargeRect(LargeRect.agox,LargeRect.agoy,LargeRect.size,LargeRect.color); DrawLargeRect(LargeRect.nowx,LargeRect.nowy,LargeRect.size,LargeRect.color); setwritemode(0); position-=1; break; } case Right: { LargeRect.nowx+=LargeRect.size; if(LargeRect.nowx>LargeStartx+LargeRect.size*(array-1)) {LargeRect.nowx-=LargeRect.size;continue;} setwritemode(1); DrawLargeRect(LargeRect.agox,LargeRect.agoy,LargeRect.size,LargeRect.color); DrawLargeRect(LargeRect.nowx,LargeRect.nowy,LargeRect.size,LargeRect.color); setwritemode(0); position+=1; break; } case P: { setbkcolor(BLUE); cleardevice(); if(array<10) array++; else array=5; InitData2(); InitPic(); break; } case R: { setbkcolor(BLUE); cleardevice(); InitData2(); InitPic(); break; } case Enter: { SmallRect.nowx=LargeRect.nowx+10; SmallRect.nowy=LargeRect.nowy+10; for(int i=0;i<5;i++) ChargeColor(i); break; } case Esc: { closegraph(); exit(1); } } if(Win()) { outtextxy(100,50,"CONGRATULATIONS!"); sleep(5); setbkcolor(BLUE); cleardevice(); if(array<10) array++; else array=5; InitData2(); InitPic(); } }while(1); } void InitData1() { array=5; LargeStartx=50; LargeStarty=50; SmallStartx=LargeStartx+10; SmallStarty=LargeStarty+10; LargeRect.size=40; LargeRect.color=RED; SmallRect.size=20; } void InitData2() //随着关数的变化会改变的数据 { position=0; LargeRect.nowx=LargeStartx; LargeRect.nowy=LargeStarty; for(int i=0;i<100;i++) Attr[i]='W'; } void DrawSmallRect(int x,int y,int size,char color) { setfillstyle(1,color); bar(x,y,x+size,y+size); } void DrawLargeRect(int x,int y,int size,char color) { setcolor(color); rectangle(x,y,x+size,y+size); } void InitPic() { for(int j=0;j<array;j++) for(int i=0;i<array;i++) DrawSmallRect(SmallStartx+i*40,SmallStarty+j*40,SmallRect.size,WHITE); DrawLargeRect(LargeStartx,LargeStarty,LargeRect.size,LargeRect.color); } void ChargeColor(int Direction) { Add Add; switch(Direction) { case 0: //当前位置 { Add.x=0; Add.y=0; Add.p=0; break; } case 1: //左 { Add.x=-40; Add.y=0; Add.p=-1; break; } case 2: //右 { Add.x=40; Add.y=0; Add.p=1; break; } case 3: //上 { Add.x=0; Add.y=-40; Add.p=-array; break; } case 4: //下 { Add.x=0; Add.y=40; Add.p=array; break; } } if(Judge(Add.x+SmallRect.nowx,Add.y+SmallRect.nowy)) { if(Attr[Add.p+position]=='R') { Attr[Add.p+position]='W'; SmallRect.color=WHITE; } else { Attr[Add.p+position]='R'; SmallRect.color=RED; } DrawSmallRect(SmallRect.nowx+Add.x,SmallRect.nowy+Add.y,SmallRect.size,SmallRect.color); } } int Win() { for(int i=0;i<array*array;i++) if(Attr[i]!='R') return 0; return 1; } int Judge(int x,int y) { if(x>=SmallStartx&&x<=(SmallStartx+(array-1)*40)&&y>=SmallStarty&&(y<=SmallStarty+(array-1)*40)) return 1; else return 0; }