?? 拼圖.c
字號:
#include "graphics.h" /* 圖形函數的包含文件 */
#include "dos.h" /* 結構體t的包含文件 */
#include "stdlib.h" /* 隨機函數的包含文件 */
#include "math.h" /* 數學函數的包含文件 */
#define LEFT 0x4b00 /* 定義left鍵的掃描碼 */
#define RIGHT 0x4d00 /* 定義right鍵的掃描碼 */
#define DOWN 0x5000 /* 定義down鍵的掃描碼 */
#define UP 0x4800 /* 定義up鍵的掃描碼 */
#define ESC 0x011b /* 定義Esc鍵的掃描碼 */
#define F5 0x3f00 /* 定義F5鍵的掃描碼 */
struct Block
{
int num,x,y;
}block[25];
int times=0;
void init_gr(void) /* BGI初始化 */
{
int gd = DETECT, gm = 0; /* 和gd = VGA,gm = VGAHI是同樣效果 */
registerbgidriver(EGAVGA_driver);/* 注冊BGI驅動后可以不需要.BGI文件的支持運行 */
initgraph(&gd, &gm, "");
}
void init_information()
{
setcolor(12);
rectangle(99,99,251,251);
rectangle(98,98,252,252);
rectangle(97,97,253,253);
setcolor(14);
line(254,99,254,255);
line(255,99,255,255);
line(99,254,255,254);
line(99,255,255,255);
setcolor(14);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
setcolor(RED);
outtextxy(50,420,"Programmed by ddpie @ FleaHouse.");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(70,30,"Picture Mosaic");
setcolor(9);
rectangle(30,60,330,380);
rectangle(32,62,328,378);
rectangle(34,64,326,376);
}
void back(struct Block here)
{
char *n;
setfillstyle(1,5);
bar(here.x+2,here.y+2,here.x+28,here.y+28);
n=(char *)malloc(3);
sprintf(n,"%d",here.num);
setcolor(14);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(here.x+10,here.y+10,n);
free(n);
}
void init_block()
{
int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
int k=5*i+j;
block[k].num=k;
block[k].x=100+30*j;
block[k].y=100+30*i;
}
for(i=24;i>0;i--)
{
block[i].x=block[i-1].x;
block[i].y=block[i-1].y;
}
block[0].x=block[0].y=220;
for(i=1;i<26;i++)
back(block[i]);
}
void overcast(struct Block here)
{
setfillstyle(0,0);
bar(here.x+2,here.y+2,here.x+28,here.y+28);
}
void shuffle()
{
int times,i;
srand(time(0)); /* 設置時間種子 */
for(times=0;times<10000;times++)
{
int ran;
ran=rand()%4;
switch(ran)
{
case 0:
for(i=1;i<26;i++)
if(block[i].x==block[0].x+30&&block[i].y==block[0].y)
break;
if(i<26)
{
block[i].x-=30;
block[0].x+=30;
overcast(block[0]);
back(block[i]);
}
break;
case 1:
for(i=1;i<26;i++)
if(block[i].x==block[0].x-30&&block[i].y==block[0].y)
break;
if(i<26)
{
block[i].x+=30;
block[0].x-=30;
overcast(block[0]);
back(block[i]);
}
break;
case 2:
for(i=1;i<26;i++)
if(block[i].x==block[0].x&&block[i].y==block[0].y+30)
break;
if(i<26)
{
block[i].y-=30;
block[0].y+=30;
overcast(block[0]);
back(block[i]);
}
break;
case 3:
for(i=1;i<26;i++)
if(block[i].x==block[0].x&&block[i].y==block[0].y-30)
break;
if(i<26)
{
block[i].y+=30;
block[0].y-=30;
overcast(block[0]);
back(block[i]);
}
break;
}
}
}
void main()
{
int i,key,win=0,flag=0;
long time_start,time_end,time_now_1,time_now_2=0;
struct time t;
init_gr();
init_information();
init_block();
outtextxy(80,280,"Press any key to shuffle");
bioskey(0);
setfillstyle(0,0);
bar(75,275,275,300);
outtextxy(130,280,"Shuffling...");
shuffle();
setfillstyle(0,0);
bar(75,275,275,300);
outtextxy(110,280,"Now you can play!");
outtextxy(130,320,"Good luck!");
for(i=1;i<26;i++)
back(block[i]);
key=bioskey(0);
setfillstyle(0,0);
bar(75,275,275,340);
setcolor(14);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(110,320,"Game playing.....");
gettime(&t);
time_start=t.ti_hour*3600+t.ti_min*60+t.ti_sec; /* 記錄當前時間 */
while(key!=ESC)
{
if(kbhit())
{
setfillstyle(0,0);
bar(200,85,260,95);
{
char *here;
here=(char *)malloc(20);
sprintf(here,"Pedometer: %d",times);
outtextxy(120,85,here);
free(here);
}
key=bioskey(0);
switch(key)
{
case LEFT:
for(i=1;i<26;i++)
if(block[i].x==block[0].x+30&&block[i].y==block[0].y)
{
times++;
break;
}
if(i<26)
{
block[i].x-=30;
block[0].x+=30;
overcast(block[0]);
back(block[i]);
}
break;
case RIGHT:
for(i=1;i<26;i++)
if(block[i].x==block[0].x-30&&block[i].y==block[0].y)
{
times++;
break;
}
if(i<26)
{
block[i].x+=30;
block[0].x-=30;
overcast(block[0]);
back(block[i]);
}
break;
case UP:
for(i=1;i<26;i++)
if(block[i].x==block[0].x&&block[i].y==block[0].y+30)
{
times++;
break;
}
if(i<26)
{
block[i].y-=30;
block[0].y+=30;
overcast(block[0]);
back(block[i]);
}
break;
case DOWN:
for(i=1;i<26;i++)
if(block[i].x==block[0].x&&block[i].y==block[0].y-30)
{
times++;
break;
}
if(i<26)
{
block[i].y+=30;
block[0].y-=30;
overcast(block[0]);
back(block[i]);
}
break;
default:break;
}
}
gettime(&t);
time_end=t.ti_hour*3600+t.ti_min*60+t.ti_sec; /* 記錄當前時間 */
time_now_1=time_end-time_start;
if(time_now_1!=time_now_2)
{
setfillstyle(0,0);
bar(160,70,260,80);
{
char *here;
here=(char *)malloc(20);
sprintf(here,"Time: %lds",time_now_1);
outtextxy(120,70,here);
free(here);
}
}
time_now_2=time_now_1;
for(i=1;i<26;i++)
if(((i-1)%5)*30+100==block[i].x&&((i-1)/5)*30+100==block[i].y)
flag++;
if(flag==24)
{
setfillstyle(0,0);
bar(105,315,245,330);
setcolor(14);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(100,280,"You win!!!");
getch();
break;
}
flag=0;
}
closegraph();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -