类拼图游戏
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ENTER 0x1c0d
#include "graphics.h"
#include "stdlib.h"
int o=0,x=3,y=3;
char *menu[]={"RULE","PLAY","ABOUT","QUIT"};
char *ch[4][4]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"," "};
char *temp;
void * buffer,* buffer_t;
unsigned size;
void win(int x,int y,int dx,int dy,int color) /*用bar()画条形图*/
{
setfillstyle(SOLID_FILL,color);
bar(x,y,x+dx,y+dy);
}
void New_map() /*刷新左边的游戏部分*/
{
int i,j;
win(100,100,250,250,15);
for(i=0;i<3;i++)
for(j=0;j<4;j++)
win(110+60*j,110+60*i,50,50,11);
for(j=0;j<3;j++)
win(110+60*j,290,50,50,11);
win(290,290,50,50,11);
win(110+60*y,110+60*x,50,50,WHITE);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{setcolor(RED);
settextstyle(DEFAULT_FONT,0,4);
outtextxy(120+60*j,120+60*i,ch[i][j]);
}
}
void New_menu() /*刷新右边的菜单部分*/
{
int i;
win(360,100,110,250,15);
setcolor(RED);
setlinestyle(0,0,3);
rectangle(362,102,468,348);
settextstyle(DEFAULT_FONT,0,2);
for(i=0;i<4;i++)
{if(i==o)
{win(370,110+60*i,90,50,10);
setcolor(6);
outtextxy(363,128+60*i,"\4");
setcolor(5);
outtextxy(380,128+60*i,menu[i]);
}
else {
win(370,110+60*i,90,50,11);
setcolor(3);
outtextxy(363,128+60*i,"\4");
outtextxy(380,128+60*i,menu[i]);
}
}
}
void New_about() /*菜单中about部分的界面*/
{
win(120,160,220,120,2);
settextstyle(DEFAULT_FONT,0,1);
setcolor(14);
circle(240,220,50);
setcolor(BLUE);
outtextxy(125,180,"Made by HXY");
outtextxy(125,260,"2005-11-12");
setcolor(BLACK);
outtextxy(230,260,"Press enter...");
settextstyle(DEFAULT_FONT,0,6);
setcolor(RED);
outtextxy(200,200,"Abc");
}
void New_rule() /*菜单中rule部分的界面*/
{win(120,160,220,120,4);
settextstyle(DEFAULT_FONT,0,2);
setcolor(14);
outtextxy(170,200,"ABCD");
outtextxy(170,220,"EFGH");
outtextxy(170,240,"IJKL");
outtextxy(170,260,"MNO ");
setcolor(3);
outtextxy(123,170,"The answer is:");
settextstyle(DEFAULT_FONT,0,1);
setcolor(BLACK);
outtextxy(230,264,"Press enter...");
}
void CQ_down() /*按down后进行的操作*/
{
getimage(110+60*y,110+60*(x-1),160+60*y,160+60*(x-1),buffer);
getimage(110+60*y,110+60*x,160+60*y,160+60*x,buffer_t);
putimage(110+60*y,110+60*(x-1),buffer_t,COPY_PUT);
putimage(110+60*y,110+60*x,buffer,COPY_PUT);
temp=ch[x][y];
ch[x][y]=ch[x-1][y];
ch[x-1][y]=temp;
x--;
}
void CQ_up() /*按up后进行的操作*/
{
getimage(110+60*y,110+60*(x+1),160+60*y,160+60*(x+1),buffer);
getimage(110+60*y,110+60*x,160+60*y,160+60*x,buffer_t);
putimage(110+60*y,110+60*(x+1),buffer_t,COPY_PUT);
putimage(110+60*y,110+60*x,buffer,COPY_PUT);
temp=ch[x][y];
ch[x][y]=ch[x+1][y];
ch[x+1][y]=temp;
x++;
}
void CQ_left() /*按left后进行的操作*/
{
getimage(110+60*(y+1),110+60*x,160+60*(y+1),160+60*x,buffer);
getimage(110+60*y,110+60*x,160+60*y,160+60*x,buffer_t);
putimage(110+60*(y+1),110+60*x,buffer_t,COPY_PUT);
putimage(110+60*y,110+60*x,buffer,COPY_PUT);
temp=ch[x][y];
ch[x][y]=ch[x][y+1];
ch[x][y+1]=temp;
y++;
}
void CQ_right() /*按right后进行的操作*/
{
getimage(110+60*(y-1),110+60*x,160+60*(y-1),160+60*x,buffer);
getimage(110+60*y,110+60*x,160+60*y,160+60*x,buffer_t);
putimage(110+60*(y-1),110+60*x,buffer_t,COPY_PUT);
putimage(110+60*y,110+60*x,buffer,COPY_PUT);
temp=ch[x][y];
ch[x][y]=ch[x][y-1];
ch[x][y-1]=temp;
y--;
}
void Wash() /*随机初始游戏部分界面*/
{
int i,j,k;
randomize();
for(i=0;i<100;i++)
{
j=random(15);
k=random(15);
temp=ch[j/4][j%4];
ch[j/4][j%4]=ch[k/4][k%4];
ch[k/4][k%4]=temp;
}
}
void Judge() /*判断玩家是否获胜,并打印结束界面*/
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
if(*ch[i][j]!=65+4*i+j) return;
for(i=3,j=0;j<3;j++)
if(*ch[i][j]!=65+4*i+j) return;
win(120,160,300,120,13);
settextstyle(DEFAULT_FONT,0,2);
setcolor(RED);
outtextxy(130,180,"Congratulations!");
outtextxy(170,220,"Bye bye!");
setcolor(BLACK);
settextstyle(DEFAULT_FONT,0,1);
outtextxy(200,250,"Press any key...");
getch();
closegraph();
exit(0);
}
void Play() /*开始游戏*/
{
int key;
setlinestyle(0,0,3);
setcolor(WHITE);
rectangle(362,102,468,348);
size=imagesize(110,110,160,160);
buffer=malloc(size);
buffer_t=malloc(size);
setcolor(RED);
rectangle(105,105,345,345);
for(;;)
{if(bioskey(1))
{key=bioskey(0);
if(key==ENTER){rectangle(362,102,468,348);
setcolor(WHITE);
rectangle(105,105,345,345);
break;
}
switch(key)
{case UP:if(x==3)break;CQ_up();Judge();break;
case DOWN:if(x==0)break;CQ_down();Judge();break;
case RIGHT:if(y==0)break;CQ_right();Judge();break;
case LEFT:if(y==3)break;CQ_left();Judge();break;
default: break;
}
}
}
}
main()
{int driver=DETECT,mode;
int key;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
cleardevice();
Wash();
New_map();
New_menu();
for(;;) /*菜单部分的选择*/
{
if(bioskey(1))
{key=bioskey(0);
switch(key)
{case UP:if(o==0)o=3;else o--;New_menu();break;
case DOWN:if(o==3)o=0;else o++;New_menu();break;
case ENTER:switch(o)
{case 0:New_rule();
while(getch()!=13);
New_map();
break;
case 1:Play();break;
case 2:New_about();
while(getch()!=13);
New_map();
break;
case 3:closegraph();exit(0);
}
break;
default:break;
}
}
}
}