| 導購 | 订阅 | 在线投稿
分享
 
 
 

俺用c語言寫的一個天下算法最爛的俄羅斯方塊程序

2006-12-17 09:28:43  編輯來源:互聯網  简体版  手機版  移動版  評論  字體: ||
 

俺用c語言寫的一個天下算法最爛的俄羅斯方塊程序

俺用c語言寫的一個天下算法最爛的俄羅斯方塊程序

/*tc3下調試通過,編譯前將turboc目錄下的egavga.bgi考到源程序所在目錄下。

另要在 */

#include <graphics.h>/* 定義boland圖形庫*/

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include <time.h>

#define ESC 0x11b /*定義鍵盤掃描碼*/

#define UP 0x4800

#define DOWN 0x5000

#define LEFT 0x4b00

#define F1 0x3b00

#define RIGHT 0x4d00

#define YES 0x1579

#define NO 0x316e

#define RESTART 0x1372

/********************定義全局變量*****************************/

int grid[20][10];/*網格數組,0表示該格無方塊,1則有運動方塊,2則有靜止方塊*/

int level,score,interval;/*level--關數,score--消去的層數,

interval--下降的時間間隔(單位毫秒)*/

/**************************定義每種方塊的形狀***********************************/

static int I_SHAPE1[1][4]={1,1,1,1};/*I代表長條方塊,O--方形*/

static int I_SHAPE2[4][1]={1,

1,

1,

1};

static int O_SHAPE[2][2]={1,1,

1,1};

static int T_SHAPE1[2][3]={1,1,1,

0,1,0};

static int T_SHAPE2[3][2]={0,1,

1,1,

0,1};

static int T_SHAPE3[2][3]={0,1,0,

1,1,1};

static int T_SHAPE4[3][2]={1,0,

1,1,

1,0};

static int L_SHAPE1[3][2]={1,0,

1,0,

1,1};

static int L_SHAPE2[2][3]={1,1,1,

1,0,0};

static int L_SHAPE3[3][2]={1,1,

0,1,

0,1};

static int L_SHAPE4[2][3]={0,0,1,

1,1,1};

static int J_SHAPE1[3][2]={0,1,

0,1,

1,1 };

static int J_SHAPE2[2][3]={1,0,0,

1,1,1};

static int J_SHAPE3[3][2]={1,1,

1,0,

1,0 };

static int J_SHAPE4[2][3]={1,1,1,

0,0,1};

static int Z_SHAPE1[2][3]={1,1,0,

0,1,1};

static int Z_SHAPE2[3][2]={0,1,

1,1,

1,0 };

static int N_SHAPE1[2][3]={0,1,1,

1,1,0};

static int N_SHAPE2[3][2]={1,0,

1,1,

0,1 };

/***************************定義方塊的結構體*****************************/

struct block

{char type;/*方塊的類型,共有I,O,T,L,J,Z,N七種類型*/

int shape;/*每種方塊的具體形狀*/

int x;/*方塊最左上方的橫坐標*/

int y;/*方塊最左上方的橫坐標*/

int color;/*方塊的顔色*/

};

/***************************函數申明***********************************/

int ini_graph();/*初始化連接圖形庫graphic.h的驅動*/

int check_bottom();/*判斷方塊是否到容器底或下方已有別的方塊,有則返回1,否則返回0*/

int check_top();/*判斷方塊是否到容器頂,也是遊戲結束標志*/

int check_lborder();/*判斷方塊是否到容器左邊緣或左邊已有別的方塊*/

int check_rborder();/*判斷方塊是否到容器右邊緣或右邊已有別的方塊*/

int del_line();/*判斷一行是否已滿,如滿則消格,返回1,否則返回0*/

void initial();/*初始化所有全局變量和界面*/

void restart();/*遊戲結束後詢問用戶是退出還是重玩*/

void help();/*顯示按鍵的幫助信息*/

void showscore();/*顯示分數的界面*/

void test();/*測試grid[i][j]用的調試函數*/

void drawbar();/*畫遊戲的容器*/

void ini_grid();/*畫容器裏的格子*/

void show_next();/*在屏幕右上方顯示下一個方塊的信息*/

void drawblock();/*畫方塊*/

void clear_block();/*清除方塊*/

void printscore();/*打印分數和關數*/

struct block create();/*隨機産生一個新的方塊*/

struct block change();/*旋轉變換函數*/

/***************************主函數**********************************/

void main()

{int key;/* 用戶按鍵*/

struct block nextblock,mblock;/*nextblock--下一個方塊,mblock--現在的方塊*/

initial();

help(420,200);

showscore();

printscore();

nextblock=create();

loop: if(check_top(mblock)) restart();/*如果方塊沒到容器頂,進行loop循環*/

printscore() ;

mblock=nextblock;

nextblock=create();

show_next(nextblock);

while(1)

{ if(bioskey(1)!=0) key=bioskey(0);

else key=0;

switch(key)

{ case DOWN:{ if(check_bottom(mblock)==1)

{ if(! del_line(mblock.y))

{ mblock.color=DARKGRAY;

drawblock(mblock);}goto loop;}

clear_block(mblock);

mblock.y=mblock.y+20;

drawblock(mblock);

}

break;

case UP: mblock=change(mblock);break;

case LEFT: if(!check_lborder(mblock)&&!check_bottom(mblock))

{clear_block(mblock);mblock.x-=20;} break;

case RIGHT:if(!check_rborder(mblock)&&!check_bottom(mblock))

{clear_block(mblock);mblock.x+=20;}

break;

case F1:test();break;

case ESC: setfillstyle(1, DARKGRAY);

bar(0,465,640,480);

outtextxy(5,469,"Are you sure to exit (Y/N)...");

while(1)

{ key=bioskey(0);

if (key==NO)

{initial();help(420,200);showscore();printscore();goto loop;}

if(key==YES)

{closegraph();exit(1);}

} break;

default:{ if(check_bottom(mblock)==1)

{ if(! del_line(mblock.y))

{ mblock.color=DARKGRAY;

drawblock(mblock);

}

goto loop;

}

clear_block(mblock);

mblock.y=mblock.y+20;

drawblock(mblock);

delay(interval);

}

}/*switch*/

}/*while*/

}/*main*/

/*初始化所有全局變量和界面*/

void initial()

{int i,j;

score=0;

interval=500;

for(i=0;i<20;i++)

for(j=0;j<10;j++)

grid[i][j]=0;

ini_graph();

drawbar();

ini_grid();

}

/*畫遊戲的容器*/

void drawbar()

{int left,top,right,bottom;

left=200;

top= 40;

right=400;

bottom=440;

setfillstyle(1,LIGHTBLUE);

bar(left,top,right,bottom);

bar(480,40,560,120);

}

/*在容器畫 20*10 的格子 */

void ini_grid()

{int i,j;

for(i=0;i<10;i++)

for(j=0;j<20;j++)

{rectangle(200+i*20,40+j*20,200+i*20+20,40+j*20+20);/*畫主容器的格子*/

}

for(i=0;i<4;i++)

for(j=0;j<4;j++)

rectangle(480+i*20,40+j*20,480+i*20+20,40+j*20+20);/*畫顯示下一個方塊的格子*/

}

/*隨機産生一個新的方塊*/

struct block create()

{struct block newblock ;

int r;

r=random(7);

switch(r)

{case(0):newblock.type='I';newblock.color=5; break;

case(1):newblock.type='O';newblock.color=BLUE; break;

case(2):newblock.type='T';newblock.color=BROWN; break;

case(3):newblock.type='L';newblock.color=YELLOW; break;

case(4):newblock.type='J';newblock.color=LIGHTGREEN; break;

case(5):newblock.type='Z';newblock.color=GREEN; break;

case(6):newblock.type='N';newblock.color=RED; break;

}

newblock.shape=1;

newblock.x=300;

newblock.y=40;

return newblock;

}

/*消格函數,傳遞參數爲方塊的左上方的縱坐標值,如有消格,返回0,否則返回1*/

int del_line(int ty)

{int i,j ,b,i0,k,ret;

ret=0;

if (ty>=440) return 0;

i0=(ty-40)/20;

if(i0==19||i0==18||i0==17)b=19;

else b=i0+3;

for(k=i0;k<=b;k++)

{ for(j=0;j<10;j++)

if(!grid[k][j]) break;

if(j==10) {score++;

ret=1;

for(i=k;i>0;i--)

for(j=0;j<10;j++)

{ grid[i][j]=grid[i-1][j];

if(!grid[i][j])

{setfillstyle(1,LIGHTBLUE);

setcolor(WHITE);

rectangle(200+j*20,40+i*20,200+j*20+20,40+i*20+20);

floodfill(200+j*20+10,40+i*20+10,WHITE);

}

else {setfillstyle(9,DARKGRAY);

setcolor(DARKGRAY);

rectangle(200+j*20,40+i*20,200+j*20+20,40+i*20+20);

floodfill(200+j*20+10,40+i*20+10,DARKGRAY);

}

}

}

}

return ret;

}

/*判斷方塊是否到容器左邊界或左方已有別的方塊,有則返回1,否則返回0

傳遞參數爲當前的的block結構實例變量, */

int check_lborder(struct block cblock)

{int i0,j0;

j0=(cblock.x-200)/20;

i0=(cblock.y-40)/20;

if(cblock.x<=200) return 1;/*已經到達容器左邊界*/

switch(cblock.type)

{case('I'):if(grid[i0][j0-1]) return 1; break;/*左邊已有其他靜止的方格*/

case('O'):if(grid[i0][j0-1]) return 1; break;

case('L'): switch(cblock.shape)

{case(1):if(grid[i0][j0-1]||grid[i0+1][j0-1]||grid[i0+2][j0-1])

return 1; break;

case(2):if(grid[i0][j0-1]||grid[i0+1][j0-1])

return 1; break;

case(3):if(grid[i0][j0-1]||grid[i0+1][j0]||grid[i0+2][j0])

return 1; break;

case(4): if(grid[i0][j0-1]||grid[i0+1][j0-1])

return 1;break;

} break;

case('J'):switch(cblock.shape)

{case(1):if(grid[i0][j0]||grid[i0+1][j0]||grid[i0+2][j0-1])

return 1; break;

case(2):if(grid[i0][j0-1]||grid[i0-1][j0-1])

return 1; break;

case(3):if(grid[i0][j0-1]||grid[i0+1][j0-1]||grid[i0+2][j0-1])

return 1; break;

case(4): if(grid[i0][j0-1]||grid[i0+1][j0+1])

return 1; break;

} break;

case('T'):break;

case('Z'):break;

case('N'):break;

}

return 0;

}

/*判斷方塊是否到容器右邊界或右方已有別的方塊,有則返回1,否則返回0

傳遞參數爲當前的的block結構實例變量, */

int check_rborder(struct block cblock)

{int i0,j0;

j0=(cblock.x-200)/20;/*把方塊的左上方的坐標x,y換算成相應數組grid[i][j]的下標i0,j0*/

i0=(cblock.y-40)/20;

switch(cblock.type)

{case('I'):if(cblock.shape==1)

{if(cblock.x+80>=400||grid[i0][j0+4]) return 1;}/*已經到達容器右邊界或左邊已有方塊*/

else {if(cblock.x+20>=400||grid[i0][j0+1])return 1;}

break;

case('O'): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2])

return 1; break;

case('T'): switch(cblock.shape)

{case(1): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+2])

return 1;break;

case(3): if(cblock.x+60>=400||grid[i0+1][j0+3]||grid[i0][j0+2])

return 1;break;

case(2):if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+2])

return 1;break;

case(4):if(cblock.x+40>=400||grid[i0][j0+1]||grid[i0+2][j0+1]||grid[i0+1][j0+2])

return 1;break;

}

break;

case('L'):switch(cblock.shape)

{case(1): if(cblock.x+40>=400||grid[i0+2][j0+2]||grid[i0][j0+1]||grid[i0+1][j0+1])

return 1;break;

case(2): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+3])

return 1;break;

case(3): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+2])

return 1;break;

case(4): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+3])

return 1;break;

} break;

case('J'):switch(cblock.shape)

{case(1): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+2])

return 1;break;

case(2):if(cblock.x+60>=400||grid[i0][j0+1]||grid[i0+1][j0+3])

return 1;break;

case(3): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+1]||grid[i0+2][j0+1])

return 1;break;

case(4): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+3])

return 1;break;

} break;

case('Z'): switch(cblock.shape)

{case(1): if(cblock.x+60>=400||grid[i0][j0+2]||grid[i0+1][j0+3])

return 1;break;

case(2):if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+1])

return 1;break;

}break;

case('N'): switch(cblock.shape)

{case(1): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+2])

return 1;break;

case(2):if(cblock.x+40>=400||grid[i0][j0+1]||grid[i0+1][j0+2]||grid[i0+2][j0+2])

return 1;break;

} break;

}

return 0;

}

/*旋轉變換函數,本遊戲的核心程序之一,

傳遞參數爲當前的的block結構實例變量,

判斷當前位置是否能變換後 ,若能

返回變換後的block實例變量,否則

返回就原來傳進來的block實例變量 */

struct block change(struct block cblock)

{int i,j,i0,j0;

j0=(cblock.x-200)/20;/*把方塊的左上方的坐標x,y換算成相應數組grid[i][j]的下標i0,j0*/

i0=(cblock.y-40)/20;

switch(cblock.type)

{case('I'):switch(cblock.shape)

{ case(1):if (grid[i0+2][j0+1]||grid[i0+3][j0+1])

return cblock;

clear_block(cblock); cblock.x+=20; cblock.shape++;break;

case(2):if (grid[i0][j0-1]||grid[i0][j0+1] || grid[i0][j0+2]||cblock.x>=360||cblock.x<=200)

return cblock;/*如果變換後的格子上已經有其他方塊,

或變換後方塊超出則不變換*/

clear_block(cblock); cblock.x-=20;/*否則清楚原來的方塊,計算出變換新的坐標*/

cblock.shape--;/*同一類型的方塊進入下一個形狀*/

break;

}

break;

case('T'):switch(cblock.shape)

{case(1):if(grid[i0-1][j0+1]) return cblock;

clear_block(cblock);cblock.y-=20;

cblock.shape++; break;

case(2):if(cblock.x>=360||grid[i0+1][j0+2]) return cblock;

clear_block(cblock);cblock.shape++;break;

case(3):if(grid[i0-1][j0+1]||grid[i0][j0+2])

return cblock;

clear_block(cblock);cblock.x+=20;cblock.y-=20;

cblock.shape++;break;

case(4):if(cblock.x<=200||grid[i0+1][j0-1])

return cblock;

clear_block(cblock);cblock.y+=20;cblock.x-=20;

cblock.shape=1; break;

} break;

case('L'):switch(cblock.shape)

{case(1):if(grid[i0+2][j0-1]||grid[i0+1][j0+1]||cblock.x<=200)

return cblock;

clear_block(cblock);cblock.y+=20; cblock.x-=20;

cblock.shape++; break;

case(2): if(grid[i0-1][j0]||grid[i0-1][j0+1]||grid[i0+1][j0+1])

return cblock;

clear_block(cblock);cblock.y-=20;

cblock.shape++;break;

case(3): if(grid[i0+2][j0-1]||grid[i0+2][j0]||cblock.x<=200)

return cblock;

clear_block(cblock);cblock.y+=20;cblock.x-=20;

cblock.shape++;break;

case(4): if(grid[i0][j0+1]||grid[i0-1][j0+1])

return cblock;

clear_block(cblock);cblock.y-=20;cblock.x+=20;

cblock.shape=1; break;

}

break;

case('J'): switch(cblock.shape)

{case(1):if(grid[i0+1][j0]||grid[i0+2][j0+2]||cblock.x>=360)

return cblock;

clear_block(cblock);cblock.y+=20;

cblock.shape++; break;

case(2): if(grid[i0-1][j0+1]||grid[i0-1][j0])

return cblock;

clear_block(cblock);cblock.y-=20;

cblock.shape++;break;

case(3):if(grid[i0+1][j0+1]||grid[i0+2][j0+1]||grid[i0+1][j0-1])

return cblock;

clear_block(cblock);cblock.y+=20;cblock.x-=20;

if(grid[i0-1][j0]||grid[i0-1][j0+1]||cblock.x<=200)

return cblock;

cblock.shape++;break;

case(4): if(grid[i0-1][j0-2]||grid[i0+1][j0+1])

return cblock;

clear_block(cblock);cblock.y-=20;cblock.x+=20;

cblock.shape=1; break;

}

break;

case('Z'):switch(cblock.shape)

{ case(1):if(grid[i0-1][j0+1]||grid[i0+1][j0])

return cblock;

clear_block(cblock);cblock.y-=20;

cblock.shape++;break;

case(2): if(grid[i0+2][j0+1]||grid[i0+2][j0+2])

return cblock;

clear_block(cblock);cblock.y+=20;

cblock.shape--;break;

}break;

case('N'):switch(cblock.shape)

{ case(1):if(grid[i0][j0]||grid[i0-1][j0])

return cblock;

clear_block(cblock); cblock.y-=20;

cblock.shape++;break;

case(2):if(grid[i0+2][j0]||grid[i0+1][j0+2]||cblock.x>=360)

return cblock;

clear_block(cblock); cblock.y+=20; cblock.shape--;break;

}break;

}

return cblock;/*返回變換後的方塊*/

}

/*畫方塊函數,傳遞參數爲當前block實例變量*/

void drawblock(struct block drblock)

{int i0,j0,i,j;

j0=(drblock.x-200)/20;/*把方塊的左上方的坐標x,y換算成相應數組grid[i][j]的下標i0,j0*/

i0=(drblock.y-40)/20;

setfillstyle(9,drblock.color);

setcolor(drblock.color);

switch(drblock.type)

{case('I'):if(drblock.shape==1)

{ for(j=0;j<4;j++)

{ rectangle(drblock.x+j*20,drblock.y,drblock.x+j*20+20,drblock.y+20);/*畫方塊格子*/

floodfill(drblock.x+j*20+10,drblock.y+10,drblock.color);/*蔓延填充方塊*/

grid[i0][j0+j]+=I_SHAPE1[0][j];/*將定義的形狀與grid數組相加*/

} /*這樣有格子的相應grid數組元素就加1*/

}

if(drblock.shape==2)

{ for(i=0;i<4;i++)

{ rectangle(drblock.x,drblock.y+20*i,drblock.x+20,drblock.y+20*i+20);

floodfill(drblock.x+10,drblock.y+20*i+10,drblock.color);

grid[i0+i][j0]+=I_SHAPE2[i][0];

}

}break;

case('O'):for(i=0;i<2;i++)

for(j=0;j<2;j++)

{rectangle(drblock.x+i*20,drblock.y+j*20,drblock.x+i*20+20,drblock.y+j*20+20);

floodfill(drblock.x+i*20+10,drblock.y+j*20+10,drblock.color);

grid[i0+i][j0+j]+=O_SHAPE[i][j];

}

break;

case('T'): switch(drblock.shape)

{case(1):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{grid[i0+i][j0+j]+=T_SHAPE1[i][j] ;

if(T_SHAPE1[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

case(2):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{grid[i0+i][j0+j]+=T_SHAPE2[i][j] ;

if(T_SHAPE2[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

case(3):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{grid[i0+i][j0+j]+=T_SHAPE3[i][j] ;

if(T_SHAPE3[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

}break ;

case(4): for(i=0;i<3;i++)

for(j=0;j<2;j++)

{grid[i0+i][j0+j]+=T_SHAPE4[i][j] ;

if(T_SHAPE4[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

} break;

case('L'): switch(drblock.shape)

{case(1):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{grid[i0+i][j0+j]+=L_SHAPE1[i][j] ;

if(L_SHAPE1[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

case(2):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{grid[i0+i][j0+j]+=L_SHAPE2[i][j] ;

if(L_SHAPE2[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

case(3):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{grid[i0+i][j0+j]+=L_SHAPE3[i][j] ;

if(L_SHAPE3[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

}break ;

case(4): for(i=0;i<2;i++)

for(j=0;j<3;j++)

{grid[i0+i][j0+j]+=L_SHAPE4[i][j] ;

if(L_SHAPE4[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

}

break;

case('J'): switch(drblock.shape)

{case(1):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{grid[i0+i][j0+j]+=J_SHAPE1[i][j] ;

if(J_SHAPE1[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

case(2):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{grid[i0+i][j0+j]+=J_SHAPE2[i][j] ;

if(J_SHAPE2[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

case(3):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{grid[i0+i][j0+j]+=J_SHAPE3[i][j] ;

if(J_SHAPE3[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break;

case(4): for(i=0;i<2;i++)

for(j=0;j<3;j++)

{grid[i0+i][j0+j]+=J_SHAPE4[i][j] ;

if(J_SHAPE4[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

} break;

case('Z'):switch(drblock.shape)

{ case(1):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{grid[i0+i][j0+j]+=Z_SHAPE1[i][j] ;

if(Z_SHAPE1[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

case(2):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{grid[i0+i][j0+j]+=Z_SHAPE2[i][j] ;

if(Z_SHAPE2[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

} break;

case('N'):switch(drblock.shape)

{ case(1):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{grid[i0+i][j0+j]+=N_SHAPE1[i][j] ;

if(N_SHAPE1[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

case(2):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{grid[i0+i][j0+j]+=N_SHAPE2[i][j] ;

if(N_SHAPE2[i][j]==1)

{rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);

floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);

}

} break ;

} break;

}

}

/*清楚原來的方塊,用原來容器的方格填充

傳遞參數爲當前的block實例變量*/

void clear_block(struct block cblock)

{ int i,j,i0,j0;

j0=(cblock.x-200)/20;/*把方塊的左上方的坐標x,y換算成相應數組grid[i][j]的下標i0,j0*/

i0=(cblock.y-40)/20;

setfillstyle(1,LIGHTBLUE);/*設置填充色爲原容器格子的顔色*/

setcolor(WHITE);/*恢複原來的前景色*/

switch(cblock.type)

{case('I'):if(cblock.shape==1)

{ for(j=0;j<4;j++)

{rectangle(cblock.x+j*20,cblock.y,cblock.x+j*20+20,cblock.y+20);/*畫原容器的格子*/

floodfill(cblock.x+j*20+10,cblock.y+10,WHITE);/*蔓延填充*/

grid[i0][j0+j]=0;/*清零,即該格已經沒有方塊*/

}

}

if(cblock.shape==2)

{ for(i=0;i<4;i++)

{ rectangle(cblock.x,cblock.y+20*i,cblock.x+20,cblock.y+20*i+20);

floodfill(cblock.x+10,cblock.y+20*i+10,WHITE);

grid[i0+i][j0]=0;

}

}break;

case('O'):for(i=0;i<2;i++)

for(j=0;j<2;j++)

{rectangle(cblock.x+i*20,cblock.y+j*20,cblock.x+i*20+20,cblock.y+j*20+20);

floodfill(cblock.x+i*20+10,cblock.y+j*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

break;

case('T'): switch(cblock.shape)

{case(1):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{ if(T_SHAPE1[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0 ; }

} break ;

case(2):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{ if(T_SHAPE2[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0 ;

}

} break ;

case(3):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{ if(T_SHAPE3[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

}break ;

case(4): for(i=0;i<3;i++)

for(j=0;j<2;j++)

{ if(T_SHAPE4[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0; }

} break ;

} break;

case('L'): switch(cblock.shape)

{case(1):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{if(L_SHAPE1[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

case(2):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{if(L_SHAPE2[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

case(3):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{if(L_SHAPE3[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

}break ;

case(4): for(i=0;i<2;i++)

for(j=0;j<3;j++)

{if(L_SHAPE4[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

}

break;

case('J'): switch(cblock.shape)

{case(1):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{ if(J_SHAPE1[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

case(2):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{ if(J_SHAPE2[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

case(3):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{if(J_SHAPE3[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break;

case(4): for(i=0;i<2;i++)

for(j=0;j<3;j++)

{ if(J_SHAPE4[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

} break;

case('Z'):switch(cblock.shape)

{ case(1):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{ if(Z_SHAPE1[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

case(2):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{if(Z_SHAPE2[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

} break;

case('N'):switch(cblock.shape)

{ case(1):for(i=0;i<2;i++)

for(j=0;j<3;j++)

{ if(N_SHAPE1[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

case(2):for(i=0;i<3;i++)

for(j=0;j<2;j++)

{ if(N_SHAPE2[i][j]==1)

{rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);

floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);

grid[i0+i][j0+j]=0;

}

} break ;

} break;

}

}

/*判斷方塊是否到容器頂,也是遊戲結束標志,如到頂則返回1,否則返回0*/

int check_top(struct block tblock)

{ if(tblock.y==40) return 1;

else return 0;

}

/*判斷方塊是否到容器底或下方已有別的方塊,有則返回1,否則返回0*/

int check_bottom(struct block nblock)

{ int j,i0,j0;

j0=(nblock.x-200)/20;

i0=(nblock.y-40)/20;

switch(nblock.type)

{case('I'): if(nblock.shape==1)

{if(nblock.y==420) return 1;/*已經到達容器底部*/

for(j=0;j<4;j++)

if(grid[i0+1][j0+j]) return 1;/*I型方塊下面一格已有靜止方塊*/

}

if(nblock.shape==2)

{if(nblock.y==360) return 1;

if(grid[i0+4][j0]) return 1;

}

break;

case('O'): if(nblock.y==400) return 1;

for(j=0;j<2;j++)

if (grid[i0+2][j0+j]) return 1;

break;

case('T'):switch(nblock.shape)

{case(1):if(nblock.y==400) return 1;

if(grid[i0+2][j0+1]||grid[i0+1][j0]||grid[i0+1][j0+2])

return 1;

break;

case(2):if(nblock.y==380) return 1;

if(grid[i0+3][j0+1]||grid[i0+2][j0]) return 1; break;

case(3):if(nblock.y==400) return 1;

for(j=0;j<3;j++)

if(grid[i0+2][j0+j]) return 1; break;

case(4):if(nblock.y==380) return 1;

if(grid[i0+3][j0]||grid[i0+2][j0+1]) return 1;break;

} break;

case('L'): switch(nblock.shape)

{case(1):if (nblock.y>=380) return 1;

if (grid[i0+3][j0]||grid[i0+3][j0+1])

return 1; break;

case(2):if(nblock.y>=400) return 1;

if(grid[i0+2][j0]||grid[i0+1][j0+1]||grid[i0+1][j0+2])

return 1; break;

case(3):if (nblock.y>=380) return 1;

if (grid[i0+3][j0+1]||grid[i0+1][j0])

return 1; break;

case(4):if (nblock.y>=400) return 1;

for(j=0;j<3;j++)

if(grid[i0+2][j0+j])

return 1; break;

}

break;

case('J'):switch(nblock.shape)

{case(1):if (nblock.y>=380) return 1;

if (grid[i0+3][j0]||grid[i0+3][j0+1])

return 1; break;

case(2):if (nblock.y>=400) return 1;

for(j=0;j<3;j++)

if(grid[i0+2][j0+j])

return 1; break;

case(3):if (nblock.y>=380) return 1;

if (grid[i0+3][j0]||grid[i0+1][j0+1])

return 1; break;

case(4):if (nblock.y>=400) return 1;

if (grid[i0+2][j0+2]||grid[i0+1][j0]||grid[i0+1][j0+1])

return 1; break;

} break;

case('Z'):if (nblock.shape==1)

{ if(nblock.y==400) return 1;

if(grid[i0+1][j0]||grid[i0+2][j0+1]||grid[i0+2][j0+2])

return 1; }

if (nblock.shape==2)

{ if(nblock.y==380) return 1;

if(grid[i0+3][j0]||grid[i0+2][j0+1])

return 1; } break;

case('N'):if(nblock.shape==1)

{ if(nblock.y==400) return 1;

if(grid[i0+1][j0+2]||grid[i0+2][j0+1]||grid[i0+2][j0])

return 1;

}

if(nblock.shape==2)

{ if(nblock.y==380) return 1;

if(grid[i0+2][j0]||grid[i0+3][j0+1])

return 1;

}

break;

}

return 0;

}

/*在屏幕右上方顯示下一個方塊的信息*/

void show_next(struct block nextblock)

{int i,j,g[4][4];

setfillstyle(1,LIGHTBLUE);

setcolor(WHITE);

for(i=0;i<4;i++)

for(j=0;j<4;j++)

{ rectangle(480+i*20,40+j*20,480+i*20+20,40+j*20+20);

floodfill( 480+i*20+10,40+20*j+10,WHITE);

}

setfillstyle(9,nextblock.color);

setcolor(nextblock.color);

switch (nextblock.type)

{case('I'): for(j=0;j<4;j++)

{rectangle(480+j*20,60,480+j*20+20,60+20);

floodfill(480+j*20+10,60+10,nextblock.color);

}

break;

case('O'): for(i=0;i<2;i++)

for(j=0;j<2;j++)

{ rectangle(500+20*i,60+20*j,500+20*i+20,60+20*j+20);

floodfill(500+20*i+10,60+20*j+10,nextblock.color);

}break ;

case('T'): for(i=0;i<2;i++)

for(j=0;j<3;j++)

{g[i][j]=T_SHAPE1[i][j];

if(g[i][j]==1)

{ rectangle(480+j*20,60+i*20,480+j*20+20,60+i*20+20);

floodfill(480+j*20+10,60+i*20+10,nextblock.color);

}

}

break;

case('L'): for(i=0;i<3;i++)

for(j=0;j<2;j++)

{g[i][j]=L_SHAPE1[i][j];

if(g[i][j]==1)

{ rectangle(500+j*20,40+i*20,500+j*20+20,40+i*20+20);

floodfill(500+j*20+10,40+i*20+10,nextblock.color);

}

}break;

case('J') : for(i=0;i<3;i++)

for(j=0;j<2;j++)

{g[i][j]=J_SHAPE1[i][j];

if(g[i][j]==1)

{ rectangle(500+j*20,40+i*20,500+j*20+20,40+i*20+20);

floodfill(500+j*20+10,40+i*20+10,nextblock.color);

}

}break;

case('Z'): for(i=0;i<2;i++)

for(j=0;j<3;j++)

{g[i][j]=Z_SHAPE1[i][j];

if(g[i][j]==1)

{ rectangle(500+j*20,60+i*20,500+j*20+20,60+i*20+20);

floodfill(500+j*20+10,60+i*20+10,nextblock.color);

}

}break;

case('N'): for(i=0;i<2;i++)

for(j=0;j<3;j++)

{g[i][j]=N_SHAPE1[i][j];

if(g[i][j]==1)

{ rectangle(500+j*20,60+i*20,500+j*20+20,60+i*20+20);

floodfill(500+j*20+10,60+i*20+10,nextblock.color);

}

}break;

}

}

/*test the grid array's value*/

void test()

{int i,j;

gotoxy(1,1);

for (i=0;i<20;i++)

{for (j=0;j<10;j++)

printf ("%2d",grid[i][j]);

printf ("\n");

}

}

/*initial the graphic driver*/

int ini_graph(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

int left, top, right, bottom;

/* initialize graphics and local variables */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); /* terminate with an error code */

}

return 0;

}

/*show the game information on right*/

void help(int xs,int ys){

setcolor(15);

rectangle(xs-3,ys-3,xs+200,ys+150);

line (xs-3,ys+26,xs+200,ys+26);

line (xs-3,ys+72,xs+200,ys+72);

line(xs+39,ys+26,xs+39,ys+72);

setcolor(12);

outtextxy(xs+60,ys+8,"Welcome!!");

setcolor(14);

outtextxy(xs,ys+45,"HELP:");

outtextxy(xs+55,ys+30,"->-Left ;<--Right");

outtextxy(xs+55,ys+45,"^-Change");

outtextxy(xs+55,ys+60,"ESC-Exit;R-Restar");

outtextxy(xs,ys+75,"Russia Diamonds ");

outtextxy(xs,ys+90,"CopyRight By FANZ.com");

outtextxy(xs,ys+105,"Date:15/12/2001");

outtextxy(xs,ys+120,"E-mail:Fanz@263.net");

}

/*show score and level*/

void showscore(){

int cur[8];

cur[0]=420;

cur[1]=360;

cur[2]=cur[0] ;

cur[3]=cur[1]+50;

cur[4]=cur[2]+203;

cur[5]=cur[3];

cur[6]=cur[4];

cur[7]=cur[5]-50;

setcolor(LIGHTBLUE);

setfillstyle(7,LIGHTBLUE);

fillpoly(4,cur);

setcolor(13);

outtextxy(480,380,"Score:");

outtextxy(480,390,"Level:");

}

/*顯示並刷新分數和關數*/

void printscore()

{ char sscore[6];

char slevel[2];

settextstyle(0,0,0);

level=score/100+1;

if (level>=5) level=1;

interval=550-level*50;

showscore();

setcolor(RED);

sprintf(sscore,"%d",score);

sprintf(slevel,"%d",level);

outtextxy (540,390,slevel);

outtextxy (540,380,sscore);

}

/*在屏幕右上方顯示下一個方塊的信息*/

void restart()

{ int key;

delay(1000);

setfillstyle(1,10);

settextstyle(0,0,6);

setcolor(3);

outtextxy(102,152,"GAME OVER");

setcolor(3);

outtextxy(99,149,"GAME OVER");

setcolor(10);

outtextxy(96,146,"GAME OVER");

delay(1000);

setcolor(0);

settextstyle(0,0,2);

outtextxy(222,242,"R__Again!");

outtextxy(232,282,"ESC__Exit!");

setcolor(10);

outtextxy(220,240,"R__Again!");

outtextxy(230,280,"ESC__Exit!");

while(1)

{ key=bioskey(0);

if (key==RESTART)

{initial();return;}

if(key==ESC)

{closegraph();exit(1);}

}

}

 
俺用c語言寫的一個天下算法最爛的俄羅斯方塊程序 俺用c語言寫的一個天下算法最爛的俄羅斯方塊程序 /*tc3下調試通過,編譯前將turboc目錄下的egavga.bgi考到源程序所在目錄下。 另要在 */ #include <graphics.h>/* 定義boland圖形庫*/ #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <time.h> #define ESC 0x11b /*定義鍵盤掃描碼*/ #define UP 0x4800 #define DOWN 0x5000 #define LEFT 0x4b00 #define F1 0x3b00 #define RIGHT 0x4d00 #define YES 0x1579 #define NO 0x316e #define RESTART 0x1372 /********************定義全局變量*****************************/ int grid[20][10];/*網格數組,0表示該格無方塊,1則有運動方塊,2則有靜止方塊*/ int level,score,interval;/*level--關數,score--消去的層數, interval--下降的時間間隔(單位毫秒)*/ /**************************定義每種方塊的形狀***********************************/ static int I_SHAPE1[1][4]={1,1,1,1};/*I代表長條方塊,O--方形*/ static int I_SHAPE2[4][1]={1, 1, 1, 1}; static int O_SHAPE[2][2]={1,1, 1,1}; static int T_SHAPE1[2][3]={1,1,1, 0,1,0}; static int T_SHAPE2[3][2]={0,1, 1,1, 0,1}; static int T_SHAPE3[2][3]={0,1,0, 1,1,1}; static int T_SHAPE4[3][2]={1,0, 1,1, 1,0}; static int L_SHAPE1[3][2]={1,0, 1,0, 1,1}; static int L_SHAPE2[2][3]={1,1,1, 1,0,0}; static int L_SHAPE3[3][2]={1,1, 0,1, 0,1}; static int L_SHAPE4[2][3]={0,0,1, 1,1,1}; static int J_SHAPE1[3][2]={0,1, 0,1, 1,1 }; static int J_SHAPE2[2][3]={1,0,0, 1,1,1}; static int J_SHAPE3[3][2]={1,1, 1,0, 1,0 }; static int J_SHAPE4[2][3]={1,1,1, 0,0,1}; static int Z_SHAPE1[2][3]={1,1,0, 0,1,1}; static int Z_SHAPE2[3][2]={0,1, 1,1, 1,0 }; static int N_SHAPE1[2][3]={0,1,1, 1,1,0}; static int N_SHAPE2[3][2]={1,0, 1,1, 0,1 }; /***************************定義方塊的結構體*****************************/ struct block {char type;/*方塊的類型,共有I,O,T,L,J,Z,N七種類型*/ int shape;/*每種方塊的具體形狀*/ int x;/*方塊最左上方的橫坐標*/ int y;/*方塊最左上方的橫坐標*/ int color;/*方塊的顔色*/ }; /***************************函數申明***********************************/ int ini_graph();/*初始化連接圖形庫graphic.h的驅動*/ int check_bottom();/*判斷方塊是否到容器底或下方已有別的方塊,有則返回1,否則返回0*/ int check_top();/*判斷方塊是否到容器頂,也是遊戲結束標志*/ int check_lborder();/*判斷方塊是否到容器左邊緣或左邊已有別的方塊*/ int check_rborder();/*判斷方塊是否到容器右邊緣或右邊已有別的方塊*/ int del_line();/*判斷一行是否已滿,如滿則消格,返回1,否則返回0*/ void initial();/*初始化所有全局變量和界面*/ void restart();/*遊戲結束後詢問用戶是退出還是重玩*/ void help();/*顯示按鍵的幫助信息*/ void showscore();/*顯示分數的界面*/ void test();/*測試grid[i][j]用的調試函數*/ void drawbar();/*畫遊戲的容器*/ void ini_grid();/*畫容器裏的格子*/ void show_next();/*在屏幕右上方顯示下一個方塊的信息*/ void drawblock();/*畫方塊*/ void clear_block();/*清除方塊*/ void printscore();/*打印分數和關數*/ struct block create();/*隨機産生一個新的方塊*/ struct block change();/*旋轉變換函數*/ /***************************主函數**********************************/ void main() {int key;/* 用戶按鍵*/ struct block nextblock,mblock;/*nextblock--下一個方塊,mblock--現在的方塊*/ initial(); help(420,200); showscore(); printscore(); nextblock=create(); loop: if(check_top(mblock)) restart();/*如果方塊沒到容器頂,進行loop循環*/ printscore() ; mblock=nextblock; nextblock=create(); show_next(nextblock); while(1) { if(bioskey(1)!=0) key=bioskey(0); else key=0; switch(key) { case DOWN:{ if(check_bottom(mblock)==1) { if(! del_line(mblock.y)) { mblock.color=DARKGRAY; drawblock(mblock);}goto loop;} clear_block(mblock); mblock.y=mblock.y+20; drawblock(mblock); } break; case UP: mblock=change(mblock);break; case LEFT: if(!check_lborder(mblock)&&!check_bottom(mblock)) {clear_block(mblock);mblock.x-=20;} break; case RIGHT:if(!check_rborder(mblock)&&!check_bottom(mblock)) {clear_block(mblock);mblock.x+=20;} break; case F1:test();break; case ESC: setfillstyle(1, DARKGRAY); bar(0,465,640,480); outtextxy(5,469,"Are you sure to exit (Y/N)..."); while(1) { key=bioskey(0); if (key==NO) {initial();help(420,200);showscore();printscore();goto loop;} if(key==YES) {closegraph();exit(1);} } break; default:{ if(check_bottom(mblock)==1) { if(! del_line(mblock.y)) { mblock.color=DARKGRAY; drawblock(mblock); } goto loop; } clear_block(mblock); mblock.y=mblock.y+20; drawblock(mblock); delay(interval); } }/*switch*/ }/*while*/ }/*main*/ /*初始化所有全局變量和界面*/ void initial() {int i,j; score=0; interval=500; for(i=0;i<20;i++) for(j=0;j<10;j++) grid[i][j]=0; ini_graph(); drawbar(); ini_grid(); } /*畫遊戲的容器*/ void drawbar() {int left,top,right,bottom; left=200; top= 40; right=400; bottom=440; setfillstyle(1,LIGHTBLUE); bar(left,top,right,bottom); bar(480,40,560,120); } /*在容器畫 20*10 的格子 */ void ini_grid() {int i,j; for(i=0;i<10;i++) for(j=0;j<20;j++) {rectangle(200+i*20,40+j*20,200+i*20+20,40+j*20+20);/*畫主容器的格子*/ } for(i=0;i<4;i++) for(j=0;j<4;j++) rectangle(480+i*20,40+j*20,480+i*20+20,40+j*20+20);/*畫顯示下一個方塊的格子*/ } /*隨機産生一個新的方塊*/ struct block create() {struct block newblock ; int r; r=random(7); switch(r) {case(0):newblock.type='I';newblock.color=5; break; case(1):newblock.type='O';newblock.color=BLUE; break; case(2):newblock.type='T';newblock.color=BROWN; break; case(3):newblock.type='L';newblock.color=YELLOW; break; case(4):newblock.type='J';newblock.color=LIGHTGREEN; break; case(5):newblock.type='Z';newblock.color=GREEN; break; case(6):newblock.type='N';newblock.color=RED; break; } newblock.shape=1; newblock.x=300; newblock.y=40; return newblock; } /*消格函數,傳遞參數爲方塊的左上方的縱坐標值,如有消格,返回0,否則返回1*/ int del_line(int ty) {int i,j ,b,i0,k,ret; ret=0; if (ty>=440) return 0; i0=(ty-40)/20; if(i0==19||i0==18||i0==17)b=19; else b=i0+3; for(k=i0;k<=b;k++) { for(j=0;j<10;j++) if(!grid[k][j]) break; if(j==10) {score++; ret=1; for(i=k;i>0;i--) for(j=0;j<10;j++) { grid[i][j]=grid[i-1][j]; if(!grid[i][j]) {setfillstyle(1,LIGHTBLUE); setcolor(WHITE); rectangle(200+j*20,40+i*20,200+j*20+20,40+i*20+20); floodfill(200+j*20+10,40+i*20+10,WHITE); } else {setfillstyle(9,DARKGRAY); setcolor(DARKGRAY); rectangle(200+j*20,40+i*20,200+j*20+20,40+i*20+20); floodfill(200+j*20+10,40+i*20+10,DARKGRAY); } } } } return ret; } /*判斷方塊是否到容器左邊界或左方已有別的方塊,有則返回1,否則返回0 傳遞參數爲當前的的block結構實例變量, */ int check_lborder(struct block cblock) {int i0,j0; j0=(cblock.x-200)/20; i0=(cblock.y-40)/20; if(cblock.x<=200) return 1;/*已經到達容器左邊界*/ switch(cblock.type) {case('I'):if(grid[i0][j0-1]) return 1; break;/*左邊已有其他靜止的方格*/ case('O'):if(grid[i0][j0-1]) return 1; break; case('L'): switch(cblock.shape) {case(1):if(grid[i0][j0-1]||grid[i0+1][j0-1]||grid[i0+2][j0-1]) return 1; break; case(2):if(grid[i0][j0-1]||grid[i0+1][j0-1]) return 1; break; case(3):if(grid[i0][j0-1]||grid[i0+1][j0]||grid[i0+2][j0]) return 1; break; case(4): if(grid[i0][j0-1]||grid[i0+1][j0-1]) return 1;break; } break; case('J'):switch(cblock.shape) {case(1):if(grid[i0][j0]||grid[i0+1][j0]||grid[i0+2][j0-1]) return 1; break; case(2):if(grid[i0][j0-1]||grid[i0-1][j0-1]) return 1; break; case(3):if(grid[i0][j0-1]||grid[i0+1][j0-1]||grid[i0+2][j0-1]) return 1; break; case(4): if(grid[i0][j0-1]||grid[i0+1][j0+1]) return 1; break; } break; case('T'):break; case('Z'):break; case('N'):break; } return 0; } /*判斷方塊是否到容器右邊界或右方已有別的方塊,有則返回1,否則返回0 傳遞參數爲當前的的block結構實例變量, */ int check_rborder(struct block cblock) {int i0,j0; j0=(cblock.x-200)/20;/*把方塊的左上方的坐標x,y換算成相應數組grid[i][j]的下標i0,j0*/ i0=(cblock.y-40)/20; switch(cblock.type) {case('I'):if(cblock.shape==1) {if(cblock.x+80>=400||grid[i0][j0+4]) return 1;}/*已經到達容器右邊界或左邊已有方塊*/ else {if(cblock.x+20>=400||grid[i0][j0+1])return 1;} break; case('O'): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]) return 1; break; case('T'): switch(cblock.shape) {case(1): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+2]) return 1;break; case(3): if(cblock.x+60>=400||grid[i0+1][j0+3]||grid[i0][j0+2]) return 1;break; case(2):if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+2]) return 1;break; case(4):if(cblock.x+40>=400||grid[i0][j0+1]||grid[i0+2][j0+1]||grid[i0+1][j0+2]) return 1;break; } break; case('L'):switch(cblock.shape) {case(1): if(cblock.x+40>=400||grid[i0+2][j0+2]||grid[i0][j0+1]||grid[i0+1][j0+1]) return 1;break; case(2): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+3]) return 1;break; case(3): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+2]) return 1;break; case(4): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+3]) return 1;break; } break; case('J'):switch(cblock.shape) {case(1): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+2]) return 1;break; case(2):if(cblock.x+60>=400||grid[i0][j0+1]||grid[i0+1][j0+3]) return 1;break; case(3): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+1]||grid[i0+2][j0+1]) return 1;break; case(4): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+3]) return 1;break; } break; case('Z'): switch(cblock.shape) {case(1): if(cblock.x+60>=400||grid[i0][j0+2]||grid[i0+1][j0+3]) return 1;break; case(2):if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+1]) return 1;break; }break; case('N'): switch(cblock.shape) {case(1): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+2]) return 1;break; case(2):if(cblock.x+40>=400||grid[i0][j0+1]||grid[i0+1][j0+2]||grid[i0+2][j0+2]) return 1;break; } break; } return 0; } /*旋轉變換函數,本遊戲的核心程序之一, 傳遞參數爲當前的的block結構實例變量, 判斷當前位置是否能變換後 ,若能 返回變換後的block實例變量,否則 返回就原來傳進來的block實例變量 */ struct block change(struct block cblock) {int i,j,i0,j0; j0=(cblock.x-200)/20;/*把方塊的左上方的坐標x,y換算成相應數組grid[i][j]的下標i0,j0*/ i0=(cblock.y-40)/20; switch(cblock.type) {case('I'):switch(cblock.shape) { case(1):if (grid[i0+2][j0+1]||grid[i0+3][j0+1]) return cblock; clear_block(cblock); cblock.x+=20; cblock.shape++;break; case(2):if (grid[i0][j0-1]||grid[i0][j0+1] || grid[i0][j0+2]||cblock.x>=360||cblock.x<=200) return cblock;/*如果變換後的格子上已經有其他方塊, 或變換後方塊超出則不變換*/ clear_block(cblock); cblock.x-=20;/*否則清楚原來的方塊,計算出變換新的坐標*/ cblock.shape--;/*同一類型的方塊進入下一個形狀*/ break; } break; case('T'):switch(cblock.shape) {case(1):if(grid[i0-1][j0+1]) return cblock; clear_block(cblock);cblock.y-=20; cblock.shape++; break; case(2):if(cblock.x>=360||grid[i0+1][j0+2]) return cblock; clear_block(cblock);cblock.shape++;break; case(3):if(grid[i0-1][j0+1]||grid[i0][j0+2]) return cblock; clear_block(cblock);cblock.x+=20;cblock.y-=20; cblock.shape++;break; case(4):if(cblock.x<=200||grid[i0+1][j0-1]) return cblock; clear_block(cblock);cblock.y+=20;cblock.x-=20; cblock.shape=1; break; } break; case('L'):switch(cblock.shape) {case(1):if(grid[i0+2][j0-1]||grid[i0+1][j0+1]||cblock.x<=200) return cblock; clear_block(cblock);cblock.y+=20; cblock.x-=20; cblock.shape++; break; case(2): if(grid[i0-1][j0]||grid[i0-1][j0+1]||grid[i0+1][j0+1]) return cblock; clear_block(cblock);cblock.y-=20; cblock.shape++;break; case(3): if(grid[i0+2][j0-1]||grid[i0+2][j0]||cblock.x<=200) return cblock; clear_block(cblock);cblock.y+=20;cblock.x-=20; cblock.shape++;break; case(4): if(grid[i0][j0+1]||grid[i0-1][j0+1]) return cblock; clear_block(cblock);cblock.y-=20;cblock.x+=20; cblock.shape=1; break; } break; case('J'): switch(cblock.shape) {case(1):if(grid[i0+1][j0]||grid[i0+2][j0+2]||cblock.x>=360) return cblock; clear_block(cblock);cblock.y+=20; cblock.shape++; break; case(2): if(grid[i0-1][j0+1]||grid[i0-1][j0]) return cblock; clear_block(cblock);cblock.y-=20; cblock.shape++;break; case(3):if(grid[i0+1][j0+1]||grid[i0+2][j0+1]||grid[i0+1][j0-1]) return cblock; clear_block(cblock);cblock.y+=20;cblock.x-=20; if(grid[i0-1][j0]||grid[i0-1][j0+1]||cblock.x<=200) return cblock; cblock.shape++;break; case(4): if(grid[i0-1][j0-2]||grid[i0+1][j0+1]) return cblock; clear_block(cblock);cblock.y-=20;cblock.x+=20; cblock.shape=1; break; } break; case('Z'):switch(cblock.shape) { case(1):if(grid[i0-1][j0+1]||grid[i0+1][j0]) return cblock; clear_block(cblock);cblock.y-=20; cblock.shape++;break; case(2): if(grid[i0+2][j0+1]||grid[i0+2][j0+2]) return cblock; clear_block(cblock);cblock.y+=20; cblock.shape--;break; }break; case('N'):switch(cblock.shape) { case(1):if(grid[i0][j0]||grid[i0-1][j0]) return cblock; clear_block(cblock); cblock.y-=20; cblock.shape++;break; case(2):if(grid[i0+2][j0]||grid[i0+1][j0+2]||cblock.x>=360) return cblock; clear_block(cblock); cblock.y+=20; cblock.shape--;break; }break; } return cblock;/*返回變換後的方塊*/ } /*畫方塊函數,傳遞參數爲當前block實例變量*/ void drawblock(struct block drblock) {int i0,j0,i,j; j0=(drblock.x-200)/20;/*把方塊的左上方的坐標x,y換算成相應數組grid[i][j]的下標i0,j0*/ i0=(drblock.y-40)/20; setfillstyle(9,drblock.color); setcolor(drblock.color); switch(drblock.type) {case('I'):if(drblock.shape==1) { for(j=0;j<4;j++) { rectangle(drblock.x+j*20,drblock.y,drblock.x+j*20+20,drblock.y+20);/*畫方塊格子*/ floodfill(drblock.x+j*20+10,drblock.y+10,drblock.color);/*蔓延填充方塊*/ grid[i0][j0+j]+=I_SHAPE1[0][j];/*將定義的形狀與grid數組相加*/ } /*這樣有格子的相應grid數組元素就加1*/ } if(drblock.shape==2) { for(i=0;i<4;i++) { rectangle(drblock.x,drblock.y+20*i,drblock.x+20,drblock.y+20*i+20); floodfill(drblock.x+10,drblock.y+20*i+10,drblock.color); grid[i0+i][j0]+=I_SHAPE2[i][0]; } }break; case('O'):for(i=0;i<2;i++) for(j=0;j<2;j++) {rectangle(drblock.x+i*20,drblock.y+j*20,drblock.x+i*20+20,drblock.y+j*20+20); floodfill(drblock.x+i*20+10,drblock.y+j*20+10,drblock.color); grid[i0+i][j0+j]+=O_SHAPE[i][j]; } break; case('T'): switch(drblock.shape) {case(1):for(i=0;i<2;i++) for(j=0;j<3;j++) {grid[i0+i][j0+j]+=T_SHAPE1[i][j] ; if(T_SHAPE1[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; case(2):for(i=0;i<3;i++) for(j=0;j<2;j++) {grid[i0+i][j0+j]+=T_SHAPE2[i][j] ; if(T_SHAPE2[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; case(3):for(i=0;i<2;i++) for(j=0;j<3;j++) {grid[i0+i][j0+j]+=T_SHAPE3[i][j] ; if(T_SHAPE3[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } }break ; case(4): for(i=0;i<3;i++) for(j=0;j<2;j++) {grid[i0+i][j0+j]+=T_SHAPE4[i][j] ; if(T_SHAPE4[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; } break; case('L'): switch(drblock.shape) {case(1):for(i=0;i<3;i++) for(j=0;j<2;j++) {grid[i0+i][j0+j]+=L_SHAPE1[i][j] ; if(L_SHAPE1[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; case(2):for(i=0;i<2;i++) for(j=0;j<3;j++) {grid[i0+i][j0+j]+=L_SHAPE2[i][j] ; if(L_SHAPE2[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; case(3):for(i=0;i<3;i++) for(j=0;j<2;j++) {grid[i0+i][j0+j]+=L_SHAPE3[i][j] ; if(L_SHAPE3[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } }break ; case(4): for(i=0;i<2;i++) for(j=0;j<3;j++) {grid[i0+i][j0+j]+=L_SHAPE4[i][j] ; if(L_SHAPE4[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; } break; case('J'): switch(drblock.shape) {case(1):for(i=0;i<3;i++) for(j=0;j<2;j++) {grid[i0+i][j0+j]+=J_SHAPE1[i][j] ; if(J_SHAPE1[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; case(2):for(i=0;i<2;i++) for(j=0;j<3;j++) {grid[i0+i][j0+j]+=J_SHAPE2[i][j] ; if(J_SHAPE2[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; case(3):for(i=0;i<3;i++) for(j=0;j<2;j++) {grid[i0+i][j0+j]+=J_SHAPE3[i][j] ; if(J_SHAPE3[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break; case(4): for(i=0;i<2;i++) for(j=0;j<3;j++) {grid[i0+i][j0+j]+=J_SHAPE4[i][j] ; if(J_SHAPE4[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; } break; case('Z'):switch(drblock.shape) { case(1):for(i=0;i<2;i++) for(j=0;j<3;j++) {grid[i0+i][j0+j]+=Z_SHAPE1[i][j] ; if(Z_SHAPE1[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; case(2):for(i=0;i<3;i++) for(j=0;j<2;j++) {grid[i0+i][j0+j]+=Z_SHAPE2[i][j] ; if(Z_SHAPE2[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; } break; case('N'):switch(drblock.shape) { case(1):for(i=0;i<2;i++) for(j=0;j<3;j++) {grid[i0+i][j0+j]+=N_SHAPE1[i][j] ; if(N_SHAPE1[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; case(2):for(i=0;i<3;i++) for(j=0;j<2;j++) {grid[i0+i][j0+j]+=N_SHAPE2[i][j] ; if(N_SHAPE2[i][j]==1) {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20); floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color); } } break ; } break; } } /*清楚原來的方塊,用原來容器的方格填充 傳遞參數爲當前的block實例變量*/ void clear_block(struct block cblock) { int i,j,i0,j0; j0=(cblock.x-200)/20;/*把方塊的左上方的坐標x,y換算成相應數組grid[i][j]的下標i0,j0*/ i0=(cblock.y-40)/20; setfillstyle(1,LIGHTBLUE);/*設置填充色爲原容器格子的顔色*/ setcolor(WHITE);/*恢複原來的前景色*/ switch(cblock.type) {case('I'):if(cblock.shape==1) { for(j=0;j<4;j++) {rectangle(cblock.x+j*20,cblock.y,cblock.x+j*20+20,cblock.y+20);/*畫原容器的格子*/ floodfill(cblock.x+j*20+10,cblock.y+10,WHITE);/*蔓延填充*/ grid[i0][j0+j]=0;/*清零,即該格已經沒有方塊*/ } } if(cblock.shape==2) { for(i=0;i<4;i++) { rectangle(cblock.x,cblock.y+20*i,cblock.x+20,cblock.y+20*i+20); floodfill(cblock.x+10,cblock.y+20*i+10,WHITE); grid[i0+i][j0]=0; } }break; case('O'):for(i=0;i<2;i++) for(j=0;j<2;j++) {rectangle(cblock.x+i*20,cblock.y+j*20,cblock.x+i*20+20,cblock.y+j*20+20); floodfill(cblock.x+i*20+10,cblock.y+j*20+10,WHITE); grid[i0+i][j0+j]=0; } break; case('T'): switch(cblock.shape) {case(1):for(i=0;i<2;i++) for(j=0;j<3;j++) { if(T_SHAPE1[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0 ; } } break ; case(2):for(i=0;i<3;i++) for(j=0;j<2;j++) { if(T_SHAPE2[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0 ; } } break ; case(3):for(i=0;i<2;i++) for(j=0;j<3;j++) { if(T_SHAPE3[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } }break ; case(4): for(i=0;i<3;i++) for(j=0;j<2;j++) { if(T_SHAPE4[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; } break; case('L'): switch(cblock.shape) {case(1):for(i=0;i<3;i++) for(j=0;j<2;j++) {if(L_SHAPE1[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; case(2):for(i=0;i<2;i++) for(j=0;j<3;j++) {if(L_SHAPE2[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; case(3):for(i=0;i<3;i++) for(j=0;j<2;j++) {if(L_SHAPE3[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } }break ; case(4): for(i=0;i<2;i++) for(j=0;j<3;j++) {if(L_SHAPE4[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; } break; case('J'): switch(cblock.shape) {case(1):for(i=0;i<3;i++) for(j=0;j<2;j++) { if(J_SHAPE1[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; case(2):for(i=0;i<2;i++) for(j=0;j<3;j++) { if(J_SHAPE2[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; case(3):for(i=0;i<3;i++) for(j=0;j<2;j++) {if(J_SHAPE3[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break; case(4): for(i=0;i<2;i++) for(j=0;j<3;j++) { if(J_SHAPE4[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; } break; case('Z'):switch(cblock.shape) { case(1):for(i=0;i<2;i++) for(j=0;j<3;j++) { if(Z_SHAPE1[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; case(2):for(i=0;i<3;i++) for(j=0;j<2;j++) {if(Z_SHAPE2[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; } break; case('N'):switch(cblock.shape) { case(1):for(i=0;i<2;i++) for(j=0;j<3;j++) { if(N_SHAPE1[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; case(2):for(i=0;i<3;i++) for(j=0;j<2;j++) { if(N_SHAPE2[i][j]==1) {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20); floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE); grid[i0+i][j0+j]=0; } } break ; } break; } } /*判斷方塊是否到容器頂,也是遊戲結束標志,如到頂則返回1,否則返回0*/ int check_top(struct block tblock) { if(tblock.y==40) return 1; else return 0; } /*判斷方塊是否到容器底或下方已有別的方塊,有則返回1,否則返回0*/ int check_bottom(struct block nblock) { int j,i0,j0; j0=(nblock.x-200)/20; i0=(nblock.y-40)/20; switch(nblock.type) {case('I'): if(nblock.shape==1) {if(nblock.y==420) return 1;/*已經到達容器底部*/ for(j=0;j<4;j++) if(grid[i0+1][j0+j]) return 1;/*I型方塊下面一格已有靜止方塊*/ } if(nblock.shape==2) {if(nblock.y==360) return 1; if(grid[i0+4][j0]) return 1; } break; case('O'): if(nblock.y==400) return 1; for(j=0;j<2;j++) if (grid[i0+2][j0+j]) return 1; break; case('T'):switch(nblock.shape) {case(1):if(nblock.y==400) return 1; if(grid[i0+2][j0+1]||grid[i0+1][j0]||grid[i0+1][j0+2]) return 1; break; case(2):if(nblock.y==380) return 1; if(grid[i0+3][j0+1]||grid[i0+2][j0]) return 1; break; case(3):if(nblock.y==400) return 1; for(j=0;j<3;j++) if(grid[i0+2][j0+j]) return 1; break; case(4):if(nblock.y==380) return 1; if(grid[i0+3][j0]||grid[i0+2][j0+1]) return 1;break; } break; case('L'): switch(nblock.shape) {case(1):if (nblock.y>=380) return 1; if (grid[i0+3][j0]||grid[i0+3][j0+1]) return 1; break; case(2):if(nblock.y>=400) return 1; if(grid[i0+2][j0]||grid[i0+1][j0+1]||grid[i0+1][j0+2]) return 1; break; case(3):if (nblock.y>=380) return 1; if (grid[i0+3][j0+1]||grid[i0+1][j0]) return 1; break; case(4):if (nblock.y>=400) return 1; for(j=0;j<3;j++) if(grid[i0+2][j0+j]) return 1; break; } break; case('J'):switch(nblock.shape) {case(1):if (nblock.y>=380) return 1; if (grid[i0+3][j0]||grid[i0+3][j0+1]) return 1; break; case(2):if (nblock.y>=400) return 1; for(j=0;j<3;j++) if(grid[i0+2][j0+j]) return 1; break; case(3):if (nblock.y>=380) return 1; if (grid[i0+3][j0]||grid[i0+1][j0+1]) return 1; break; case(4):if (nblock.y>=400) return 1; if (grid[i0+2][j0+2]||grid[i0+1][j0]||grid[i0+1][j0+1]) return 1; break; } break; case('Z'):if (nblock.shape==1) { if(nblock.y==400) return 1; if(grid[i0+1][j0]||grid[i0+2][j0+1]||grid[i0+2][j0+2]) return 1; } if (nblock.shape==2) { if(nblock.y==380) return 1; if(grid[i0+3][j0]||grid[i0+2][j0+1]) return 1; } break; case('N'):if(nblock.shape==1) { if(nblock.y==400) return 1; if(grid[i0+1][j0+2]||grid[i0+2][j0+1]||grid[i0+2][j0]) return 1; } if(nblock.shape==2) { if(nblock.y==380) return 1; if(grid[i0+2][j0]||grid[i0+3][j0+1]) return 1; } break; } return 0; } /*在屏幕右上方顯示下一個方塊的信息*/ void show_next(struct block nextblock) {int i,j,g[4][4]; setfillstyle(1,LIGHTBLUE); setcolor(WHITE); for(i=0;i<4;i++) for(j=0;j<4;j++) { rectangle(480+i*20,40+j*20,480+i*20+20,40+j*20+20); floodfill( 480+i*20+10,40+20*j+10,WHITE); } setfillstyle(9,nextblock.color); setcolor(nextblock.color); switch (nextblock.type) {case('I'): for(j=0;j<4;j++) {rectangle(480+j*20,60,480+j*20+20,60+20); floodfill(480+j*20+10,60+10,nextblock.color); } break; case('O'): for(i=0;i<2;i++) for(j=0;j<2;j++) { rectangle(500+20*i,60+20*j,500+20*i+20,60+20*j+20); floodfill(500+20*i+10,60+20*j+10,nextblock.color); }break ; case('T'): for(i=0;i<2;i++) for(j=0;j<3;j++) {g[i][j]=T_SHAPE1[i][j]; if(g[i][j]==1) { rectangle(480+j*20,60+i*20,480+j*20+20,60+i*20+20); floodfill(480+j*20+10,60+i*20+10,nextblock.color); } } break; case('L'): for(i=0;i<3;i++) for(j=0;j<2;j++) {g[i][j]=L_SHAPE1[i][j]; if(g[i][j]==1) { rectangle(500+j*20,40+i*20,500+j*20+20,40+i*20+20); floodfill(500+j*20+10,40+i*20+10,nextblock.color); } }break; case('J') : for(i=0;i<3;i++) for(j=0;j<2;j++) {g[i][j]=J_SHAPE1[i][j]; if(g[i][j]==1) { rectangle(500+j*20,40+i*20,500+j*20+20,40+i*20+20); floodfill(500+j*20+10,40+i*20+10,nextblock.color); } }break; case('Z'): for(i=0;i<2;i++) for(j=0;j<3;j++) {g[i][j]=Z_SHAPE1[i][j]; if(g[i][j]==1) { rectangle(500+j*20,60+i*20,500+j*20+20,60+i*20+20); floodfill(500+j*20+10,60+i*20+10,nextblock.color); } }break; case('N'): for(i=0;i<2;i++) for(j=0;j<3;j++) {g[i][j]=N_SHAPE1[i][j]; if(g[i][j]==1) { rectangle(500+j*20,60+i*20,500+j*20+20,60+i*20+20); floodfill(500+j*20+10,60+i*20+10,nextblock.color); } }break; } } /*test the grid array's value*/ void test() {int i,j; gotoxy(1,1); for (i=0;i<20;i++) {for (j=0;j<10;j++) printf ("%2d",grid[i][j]); printf ("\n"); } } /*initial the graphic driver*/ int ini_graph(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int left, top, right, bottom; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } return 0; } /*show the game information on right*/ void help(int xs,int ys){ setcolor(15); rectangle(xs-3,ys-3,xs+200,ys+150); line (xs-3,ys+26,xs+200,ys+26); line (xs-3,ys+72,xs+200,ys+72); line(xs+39,ys+26,xs+39,ys+72); setcolor(12); outtextxy(xs+60,ys+8,"Welcome!!"); setcolor(14); outtextxy(xs,ys+45,"HELP:"); outtextxy(xs+55,ys+30,"->-Left ;<--Right"); outtextxy(xs+55,ys+45,"^-Change"); outtextxy(xs+55,ys+60,"ESC-Exit;R-Restar"); outtextxy(xs,ys+75,"Russia Diamonds "); outtextxy(xs,ys+90,"CopyRight By FANZ.com"); outtextxy(xs,ys+105,"Date:15/12/2001"); outtextxy(xs,ys+120,"E-mail:Fanz@263.net"); } /*show score and level*/ void showscore(){ int cur[8]; cur[0]=420; cur[1]=360; cur[2]=cur[0] ; cur[3]=cur[1]+50; cur[4]=cur[2]+203; cur[5]=cur[3]; cur[6]=cur[4]; cur[7]=cur[5]-50; setcolor(LIGHTBLUE); setfillstyle(7,LIGHTBLUE); fillpoly(4,cur); setcolor(13); outtextxy(480,380,"Score:"); outtextxy(480,390,"Level:"); } /*顯示並刷新分數和關數*/ void printscore() { char sscore[6]; char slevel[2]; settextstyle(0,0,0); level=score/100+1; if (level>=5) level=1; interval=550-level*50; showscore(); setcolor(RED); sprintf(sscore,"%d",score); sprintf(slevel,"%d",level); outtextxy (540,390,slevel); outtextxy (540,380,sscore); } /*在屏幕右上方顯示下一個方塊的信息*/ void restart() { int key; delay(1000); setfillstyle(1,10); settextstyle(0,0,6); setcolor(3); outtextxy(102,152,"GAME OVER"); setcolor(3); outtextxy(99,149,"GAME OVER"); setcolor(10); outtextxy(96,146,"GAME OVER"); delay(1000); setcolor(0); settextstyle(0,0,2); outtextxy(222,242,"R__Again!"); outtextxy(232,282,"ESC__Exit!"); setcolor(10); outtextxy(220,240,"R__Again!"); outtextxy(230,280,"ESC__Exit!"); while(1) { key=bioskey(0); if (key==RESTART) {initial();return;} if(key==ESC) {closegraph();exit(1);} } }
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有