俄罗斯方块C语言源代码Word下载.docx
《俄罗斯方块C语言源代码Word下载.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块C语言源代码Word下载.docx(22页珍藏版)》请在冰点文库上搜索。
K_P=0x1970
};
/*thedatastructureoftheblock*/
typedefstructtagBlock
charc[4][4];
/*cellfillinfoarray,0-empty,1-filled*/
intx;
/*blockpositioncx[0,BoardWidht-1]*/
inty;
/*blockpositioncy[-4,BoardHeight-1]*/
charcolor;
/*blockcolor*/
charsize;
/*blockmaxsizeinwidthorheight*/
charname;
/*blockname(theblock'
sshape)*/
}Block;
/*game'
sglobalinfo*/
intFrameTime=1300;
intCellSize=18;
intBoardLeft=30;
intBoardTop=30;
/*nextblockgrid*/
intNBBoardLeft=300;
intNBBoardTop=30;
intNBCellSize=10;
/*scoreboardposition*/
intScoreBoardLeft=300;
intScoreBoardTop=100;
intScoreBoardWidth=200;
intScoreBoardHeight=35;
intScoreColor=LIGHTCYAN;
/*infortextpostion*/
intInfoLeft=300;
intInfoTop=200;
intInfoColor=YELLOW;
intBorderColor=DARKGRAY;
intBkGndColor=BLACK;
intGameRunning=true;
intTopLine=BoardHeight-1;
/*topemptyline*/
intTotalScore=100;
charinfo_score[20];
charinfo_help[255];
charinfo_common[255];
/*ourboard,Board[x][y][0]-isFilled,Board[x][y][1]-fillColor*/
unsignedcharBoard[BoardWidth][BoardHeight][2];
charBufferCells[4][4];
/*usedtojudgeifcanrotateblock*/
BlockcurBlock;
/*currentmovingblock*/
BlocknextBlock;
/*nextBlocktoappear*/
/*functionlist*/
intGetKeyCode();
intCanMove(intdx,intdy);
intCanRotate();
intRotateBlock(Block*block);
intMoveBlock(Block*block,intdx,intdy);
voidDrawBlock(Block*block,int,int,int);
voidEraseBlock(Block*block,int,int,int);
voidDisplayScore();
voidDisplayInfo(char*text);
voidGenerateBlock(Block*block);
voidNextBlock();
voidInitGame();
intPauseGame();
voidQuitGame();
/*GetKeyCode*/
int_INNER_HELPERGetKeyCode()
intkey=0;
if(bioskey
(1))
{
key=bioskey(0);
}
returnkey;
}
/*displaytext!
*/
void_INNER_HELPERDisplayInfo(char*text)
setcolor(BkGndColor);
outtextxy(InfoLeft,InfoTop,info_common);
strcpy(info_common,text);
setcolor(InfoColor);
/*createanewblockbykeynumber,
*theblockanchortothetop-leftcornerof4*4cells
*/
void_INNER_HELPERGenerateBlock(Block*block)
intkey=(random(13)*random(17)+random(1000)+random(3000))%7;
block->
size=3;
/*becausemostblocks'
size=3*/
memset(block->
c,0,16);
switch(key)
case0:
name='
T'
;
color=RED;
c[1][0]=1;
c[1][1]=1,block->
c[2][1]=1;
c[1][2]=1;
break;
case1:
L'
color=YELLOW;
c[1][1]=1;
c[1][2]=1,block->
c[2][2]=1;
case2:
J'
color=LIGHTGRAY;
c[0][2]=1;
case3:
z'
color=CYAN;
c[0][0]=1,block->
case4:
5'
color=LIGHTBLUE;
c[1][0]=1,block->
c[2][0]=1;
c[0][1]=1,block->
case5:
o'
color=BLUE;
size=2;
case6:
I'
color=GREEN;
size=4;
c[1][3]=1;
/*getnextblock!
voidNextBlock()
/*copythenextBlocktocurBlock*/
curBlock.size=nextBlock.size;
curBlock.color=nextBlock.color;
curBlock.x=(BoardWidth-4)/2;
curBlock.y=-curBlock.size;
memcpy(curBlock.c,nextBlock.c,16);
/*generatenextBlockandshowit*/
EraseBlock(&
nextBlock,NBBoardLeft,NBBoardTop,NBCellSize);
GenerateBlock(&
nextBlock);
nextBlock.x=1,nextBlock.y=0;
DrawBlock(&
/*rotatetheblock,updatetheblockstructdata*/
int_INNER_HELPERRotateCells(charc[4][4],charblockSize)
chartemp,i,j;
switch(blockSize)
temp=c[0][0];
c[0][0]=c[2][0],c[2][0]=c[2][2],c[2][2]=c[0][2],c[0][2]=temp;
temp=c[0][1];
c[0][1]=c[1][0],c[1][0]=c[2][1],c[2][1]=c[1][2],c[1][2]=temp;
/*only'
blockarivedhere!
c[1][0]=1-c[1][0],c[1][2]=1-c[1][2],c[1][3]=1-c[1][3];
c[0][1]=1-c[0][1],c[2][1]=1-c[2][1],c[3][1]=1-c[3][1];
/*judgeiftheblockcanmovetowardthedirection*/
intCanMove(intdx,intdy)
inti,j,tempX,tempY;
for(i=0;
i<
curBlock.size;
i++)
for(j=0;
j<
j++)
if(curBlock.c[i][j])
/*cannotmoveleftwardorrightward*/
tempX=curBlock.x+i+dx;
if(tempX<
0||tempX>
(BoardWidth-1))returnfalse;
/*makesurexisvalid!
/*cannotmovedownward*/
tempY=curBlock.y+j+dy;
if(tempY>
(BoardHeight-1))returnfalse;
/*yisonlycheckedlowerbound,maybenegative!
!
/*thecellalreadyfilled,wemustcheckY'
supperboundbeforecheckcell!
=0&
&
Board[tempX][tempY][0])returnfalse;
returntrue;
/*judgeiftheblockcanrotate*/
intCanRotate()
/*updatebuffer*/
memcpy(BufferCells,curBlock.c,16);
RotateCells(BufferCells,curBlock.size);
if(BufferCells[i][j])
tempX=curBlock.x+i;
tempY=curBlock.y+j;
(BoardWidth-1))
returnfalse;
(BoardHeight-1))
Board[tempX][tempY][0])
/*drawtheblock*/
void_INNER_HELPERDrawBlock(Block*block,intbdLeft,intbdTop,intcellSize)
inti,j;
setfillstyle(SOLID_FILL,block->
color);
block->
size;
if(block->
c[i][j]&
(block->
y+j)>
=0)
floodfill(
bdLeft+cellSize*(i+block->
x)+cellSize/2,
bdTop+cellSize*(j+block->
y)+cellSize/2,
BorderColor);
/*Rotatetheblock,ifsuccess,returntrue*/
intRotateBlock(Block*block)
intb_success;
size==2)
if((b_success=CanRotate()))
EraseBlock(block,BoardLeft,BoardTop,CellSize);
memcpy(curBlock.c,BufferCells,16);
DrawBlock(block,BoardLeft,BoardTop,CellSize);
returnb_success;
/*eraseablock,onlyfillthefilledcellwithbackgroundcolor*/
void_INNER_HELPEREraseBlock(Block*block,intbdLeft,intbdTop,intcellSize)
setfillstyle(SOLID_FILL,BkGndColor);
y+j>
=0))
/*movebythedirectionifcan,donothingifcannot
*returnvalue:
true-success,false-cannotmovetowardthisdirection
intMoveBlock(Block*block,intdx,intdy)
intb_canmove=CanMove(dx,dy);
if(b_canmove)
curBlock.x+=dx;
curBlock.y+=dy;
returnb_canmove;
/*droptheblocktothebottom!
intDropBlock(Block*block)
while(CanMove(0,1))
curBlock.y++;
return0;
/*returnvalueisassigntotheblock'
salive*/
/*initthegraphicsmode,drawtheboardgrid*/
voidInitGame()
inti,j,gdriver=DETECT,gmode;
structtimesysTime;
/*drawboardcells*/
memset(Board,0,BoardWidth*BoardHeight*2);
memset(nextBlock.c,0,16);
strcpy(info_help,"
P:
PauseGame.--byhoodlum1980"
);
initgraph(&
gdriver,&
gmode,"
"
setcolor(BorderColor);
=BoardWidth;
line(BoardLeft+i*CellSize,BoardTop,BoardLeft+i*CellSize,BoardTop+BoardHeight*CellSize);
=BoardHeight;
line(BoardLeft,BoardTop+i*CellSize,BoardLeft+BoardWidth*CellSize,BoardTop+i*CellSize);
/*drawboardouterborderrect*/
rectangle(BoardLeft-CellSize/4,BoardTop-CellSize/4,
BoardLeft+BoardWidth*CellSize+CellSize/4,
BoardTop+BoardHeight*CellSize+CellSize/4);
/*drawnextblockgrids*/
=4;
line(NBBoardLeft+i*NBCellSize,NBBoardTop,NBBoardLeft+i*NBCellSize,NBBoardTop+4*NBCellSize);
line(NBBoardLeft,NBBoardTop+i*NBCellSize,NBBoardLeft+4*NBCellSize,NBBoardTop+i*NBCellSize);
/*drawscorerect*/
rectangle(ScoreBoardLeft,ScoreBoardTop,ScoreBoardLeft+ScoreBoardWidth,ScoreBoardTop+ScoreBoardHeight);
DisplayScore();
/*setnewseed!
gettime(&
sysTime);
srand(sysTime.ti_hour*3600+sysTime.ti_min*60+sysTime.ti_sec);
NextBlock();
/*createfirstblock*/
setcolor(DARKGRAY);
outtextxy(InfoLeft,InfoTop+20,"
Up-rotateSpace-drop"
outtextxy(InfoLeft,InfoTop+35,"
Left-leftRight-right"
outtextxy(InfoLeft,InfoTop+50,"
Esc-exit"
DisplayInfo(info_help);
/*settheisFilledandfillcolordatatotheboard*/
void_INNER_HELPERFillBoardData()
if(curBlock.c[i][j]&
(curBlock.y+j)>
Board[curBlock.x+i][curBlock.y+j][0]=1;
Board[curBlock.x+i][curBlock.y+j][1]=curBlock.color;
/*drawonelineoftheboard*/
void_INNER_HELPERPaintBoard(