*連連看遊戲C語言源代碼
*/# include & lt;stdio.h & gt
# include & ltgraphics.h & gt
# include & ltstdlib.h & gt
# include & ltmath.h & gt
# include & ltdos.h & gt#定義真1
# define false 0/* -全局變量。
int BkGndColor = BLACK
int BorderColor = LIGHTGRAY
int LineColor = LIGHTBLUE/*消除壹對框時的連接顏色*/
/* Pb - ProgressBar */
int PbColor = LIGHTGREEN
int PbY = 4;
int Pb height = 4;
int PbValue/*進度條的百分比,初始值為100。*/
啟動時間長;/*秒的開始時間,只計算分、秒*/
總時間長;/*整個遊戲的最大秒數* * *!,*//* BoardDatas:小型板*/
/* Board[x][y][0] - 0:空,1:已填充*/
/* Board[x][y][1] -單元格的鍵;*/
無符號char板[10][10][2];
int CellSize = 30
int BoardX = 20
int BoardY = 60
int board width = 10;
int board height = 10;
int CellColor =白色;
int SelColor = BLUE/*單元格的邊框矩形顏色*/
int CurColor = RED/* curCell的邊框矩形顏色*/
int EraColor =青色;/*用來擦除單元格的顏色!*/
int PairsCount/*我們在板上放置了多少對*/*用於存儲邏輯坐標(索引)*/
typedef結構_標記單元
{
char x;
char y;
}細胞;細胞selCell,curCell/*緩存上壹次選擇的位置和當前位置!*//*掃描代碼定義*/
枚舉鍵碼
{
K_ESC =0x011b,
K_UP =0x4800,/*向上箭頭*/
K_LEFT =0x4b00,
K_DOWN =0x5000,
K_RIGHT =0x4d00,
K_SPACE =0x3920,
K_P =0x1970,
K_RETURN =0x1c0d,/* Enter */
};/* -函數列表*/
void init game(char * bgi path);
void play game();
void quit game();
void InitProgressBar();
void UpdateProgressBar(int percent);
void DrawCell(int key,int x,int y,int color);
void EraseCell(int x,int y);
void DrawBorderRect(CELL *c,int color);
void DrawGameOver(char * info);
int get key code();
int FindPath(CELL *c1,CELL * C2);
/*畫消去框時的連接路徑!,用指定的顏色!*/
void DrawPath(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int color);/* -功能實現*/。
*首先判斷水平方向,找出兩點所在的水平直線活動範圍。
*計算這兩條線段在垂直方向的* * *相同面積!!!,
*遍歷該區域,判斷兩條線段之間是否可以建立壹條公共垂直線,如果可以,則連接兩點;
*然後是垂直方向判斷,類似。無論兩點是否在壹條直線上,
*可以使用這個算法,因為兩個共線的點只是兩個點是矩形的對角點的特例。
*//*找到了兩個單元格之間的路徑,並成功返回true */
int FindPath(單元格*c1,單元格*c2)
{
int i,j,path,min1,max1,min2,max2,left,right,top,bottom
/* - (0)判斷是否點擊了同壹塊!- */
如果(板[c 1->;x][c 1->;y][1]!= Board[C2->;x][C2->;y][1])
返回false
/* - (1)求水平面積!- */
min 1 = max 1 = c 1->;x;
min 2 = max 2 = C2-& gt;x;
while(min 1-1 & gt;= 0 & amp& ampboard[min 1-1][c 1->;y][0]= = 0)min 1-;
while(min 2-1 & gt;= 0 & amp& ampboard[min 2-1][C2->;y][0]= = 0)min 2-;
left=max(min1,min 2);/*左邊距*/
while(max 1+1 & lt;紙板寬度和尺寸。& ampboard[max 1+1][c 1->;y][0]= = 0)max 1++;
while(max 2+1 & lt;紙板寬度和尺寸。& ampboard[max 2+1][C2->;y][0]= = 0)max 2++;
right=min(max1,max 2);/*右邊界*/*檢查兩條水平線之間是否有共同的垂直連線!*/
/*可以在邊緣連接*/
if(left==0)
{
/*左邊緣連接*/
draw path(c 1->;x,c 1->;y,-1,c 1-& gt;y,-1,C2-& gt;y,C2-& gt;x,c2->y,line color);
延時(6000);
draw path(c 1->;x,c 1->;y,-1,c 1-& gt;y,-1,C2-& gt;y,C2-& gt;x,c2->y,BkGndColor);/*刪除線!*/
返回true
}
if(right==(BoardWidth-1))
{
draw path(c 1->;x,c 1->;y,板寬,c 1->;y,板寬,c2->y,C2-& gt;x,c2->y,line color);
延時(6000);
draw path(c 1->;x,c 1->;y,板寬,c 1->;y,板寬,c2->y,C2-& gt;x,c2->y,BkGndColor);/*刪除線!*/
返回true
} for(I =左;我& lt=對;i++)
{
path = 0;/*算算垂直常用垂直線的長度!*/
for(j = min(c 1->;y,C2-& gt;y)+1;j & ltmax(c 1->;y,C2-& gt;y);j++)
{
path+= Board[I][j][0];
if(路徑& gt0)破;
}
如果(路徑==0)
{
draw path(c 1->;x,c 1->;y,I,c 1-& gt;y,I,C2-& gt;y,C2-& gt;x,c2->y,line color);
延時(6000);
draw path(c 1->;x,c 1->;y,I,c 1-& gt;y,I,C2-& gt;y,C2-& gt;x,c2->y,BkGndColor);/*刪除線!*/
返回true
}
}/* - (2)求垂直公共* * *區!- */
min 1 = max 1 = c 1->;y;
min 2 = max 2 = C2-& gt;y;
while(min 1-1 & gt;= 0 & amp& amp板[c 1->;x][min 1-1][0]= = 0)min 1-;
while(min 2-1 & gt;= 0 & amp& ampboard[C2->;x][min 2-1][0]= = 0)min 2-;
top=max(min1,min 2);
while(max 1+1 & lt;木板高度和高度。& amp板[c 1->;x][max 1+1][0]= = 0)max 1++;
while(max 2+1 & lt;木板高度和高度。& ampboard[C2->;x][max 2+1][0]= = 0)max 2++;
bottom=min(max1,max 2);/*檢查兩條垂直線之間是否有共同的垂直連線!*/
/*可以在邊緣連接*/
if(top==0)
{
/*在頂部消除*/
draw path(c 1->;x,c 1->;y,c 1->;x,-1,C2-& gt;x,-1,C2-& gt;x,c2->y,line color);
延時(6000);
draw path(c 1->;x,c 1->;y,c 1->;x,-1,C2-& gt;x,-1,C2-& gt;x,c2->y,BkGndColor);/*刪除線!*/
返回true
}
if(bottom = =(board height-1))
{
draw path(c 1->;x,c 1->;y,c 1->;x,板高,C2-& gt;x,板高,C2-& gt;x,c2->y,line color);
延時(6000);
draw path(c 1->;x,c 1->;y,c 1->;x,板高,C2-& gt;x,板高,C2-& gt;x,c2->y,BkGndColor);/*刪除線!*/
返回true
} for(j = top;j & lt=底部;j++)
{
path = 0;/*統計層面常用垂直線的長度!*/
for(I = min(c 1->;x,c2->x)+1;我& ltmax(c 1->;x,c2->x);i++)
{
path+= Board[I][j][0];
if(路徑& gt0)破;
}
如果(路徑==0)
{
/*水平公共垂直線*/
draw path(c 1->;x,c 1->;y,c 1->;x,j,C2-& gt;x,j,C2-& gt;x,c2->y,line color);
延時(6000);
draw path(c 1->;x,c 1->;y,c 1->;x,j,C2-& gt;x,j,C2-& gt;x,c2->y,BkGndColor);/*刪除線!*/
返回true
}
}/*到了這裏就意味著沒有通路*/
返回false
}
/*獲取密鑰代碼*/
int GetKeyCode()
{
int key = 0;
if(bioskey(1))
{
key = BIOS key(0);
}
回車鍵;
}/*畫消去框時的連接路徑!,用指定的顏色!,坐標是單元格邏輯坐標!*/
void DrawPath(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int color)
{
setcolor(顏色);
move to(BoardX+CellSize/2+CellSize * x 1,BoardY+CellSize/2+CellSize * y 1);
line to(board x+CellSize/2+CellSize * x2,board y+CellSize/2+CellSize * y2);
line to(board x+CellSize/2+CellSize * x3,board y+CellSize/2+CellSize * y3);
line to(board x+CellSize/2+CellSize * x4,board y+CellSize/2+CellSize * y4);
}
/*祝賀信息,用戶已經成功完成遊戲!*/
void DrawGameOver(char* info)
{
/*計算棋盤中心點*/
int CX = BoardX+CellSize * board width/2;
int cy = BoardY+CellSize * board height/2;
struct text settings type textInfos;
/*獲取以前的文本信息*/
gettextsettings(& amp;textInfos);
setcolor(深灰色);
setfillstyle(SOLID_FILL,藍色);
/*文本中心*/
長方形(cx-102,cy-22,cx+102,cy+22);
漫填(cx,cy,深灰色);
長方形(cx-100,cy-20,cx+100,cy+20);
settextjustify(CENTER_TEXT,CENTER _ TEXT);
setcolor(淺藍色);
outtextxy(cx,cy,info);
/*恢復原始文本設置*/
settextjustify(textInfos.horiz,textinfos . vert);
}/*在顏色為*/的單元格上繪制焦點矩形
/*用指定的顏色繪制選定的外部邊框*/
void DrawBorderRect(單元格*c,內部顏色)
{
setcolor(顏色);
矩形(BoardX+(c-& gt;x)*CellSize+1,BoardY+(c-& gt;y)*CellSize+1,board x+(c-& gt;x+1)*CellSize-2,BoardY+(c-& gt;y+1)* cell size-2);
矩形(BoardX+(c-& gt;x)*單元格大小,BoardY+(c-& gt;y)*CellSize,board x+(c-& gt;x+1)*CellSize-1,BoardY+(c-& gt;y+1)* cell size-1);
}/*在x和y上用指定顏色的鍵畫單元格,鍵隨機在2,3,4,5,6,7,8,9,10,11之間*/
void DrawCell(int key,int x,int y,int color)
{
setcolor(顏色);
矩形(BoardX+x*CellSize+2,BoardY+y*CellSize+2,BoardX+(x+1)*CellSize-3,BoardY+(y+1)* CellSize-3);
setfillstyle(key,color);
floodfill(BoardX+x*CellSize+3,BoardY+y*CellSize+3,color);
}/*擦除單元格*/
void EraseCell(int x,int y)
{
setcolor(era color);
矩形(BoardX+x*CellSize+2,BoardY+y*CellSize+2,BoardX+(x+1)*CellSize-3,BoardY+(y+1)* CellSize-3);
setfillstyle(SOLID_FILL,BkGndColor);
floodfill(BoardX+x*CellSize+3,BoardY+y*CellSize+3,era color);
set color(BkGndColor);
矩形(BoardX+x*CellSize+2,BoardY+y*CellSize+2,BoardX+(x+1)*CellSize-3,BoardY+(y+1)* CellSize-3);
}
/*初始化進度條*/
void InitProgressBar()
{
int width = CellSize * BoardWidth
/*進度條邊框矩形*/
set color(border color);
矩形(BoardX-2,PbY-2,BoardX+width+2,PbY+Pb height+2);/*畫壹個值= 100%進度條*/
set color(Pb color);
矩形(BoardX,PbY,BoardX+width,PbY+Pb height);
setfillstyle(SOLID_FILL,Pb color);
floodfill(BoardX+1,PbY+1,Pb color);
}