小游戏源代码.docx
《小游戏源代码.docx》由会员分享,可在线阅读,更多相关《小游戏源代码.docx(19页珍藏版)》请在冰豆网上搜索。
小游戏源代码
/************************************
*Desc:
俄罗斯方块游戏
*By:
hoodlum1980
*Email
*Date:
22:
30
************************************/
#include<>
#include<>
#include<>
#include<>
#include<>
#include<>
#definetrue1
#definefalse0
#defineBoardWidth12
#defineBoardHeight23
#define_INNER_HELPER/*innerhelpermethod*/
/*ScanCodesDefine*/
enumKEYCODES
{
K_ESC=0x011b,
K_UP=0x4800,/*upwardarrow*/
K_LEFT=0x4b00,
K_DOWN=0x5000,
K_RIGHT=0x4d00,
K_SPACE=0x3920,
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);
outtextxy(InfoLeft,InfoTop,info_common);
}
/*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:
block->name='T';
block->color=RED;
block->c[1][0]=1;
block->c[1][1]=1,block->c[2][1]=1;
block->c[1][2]=1;
break;
case1:
block->name='L';
block->color=YELLOW;
block->c[1][0]=1;
block->c[1][1]=1;
block->c[1][2]=1,block->c[2][2]=1;
break;
case2:
block->name='J';
block->color=LIGHTGRAY;
block->c[1][0]=1;
block->c[1][1]=1;
block->c[1][2]=1,block->c[0][2]=1;
break;
case3:
block->name='z';
block->color=CYAN;
block->c[0][0]=1,block->c[1][0]=1;
block->c[1][1]=1,block->c[2][1]=1;
break;
case4:
block->name='5';
block->color=LIGHTBLUE;
block->c[1][0]=1,block->c[2][0]=1;
block->c[0][1]=1,block->c[1][1]=1;
break;
case5:
block->name='o';
block->color=BLUE;
block->size=2;
block->c[0][0]=1,block->c[1][0]=1;
block->c[0][1]=1,block->c[1][1]=1;
break;
case6:
block->name='I';
block->color=GREEN;
block->size=4;
block->c[1][0]=1;
block->c[1][1]=1;
block->c[1][2]=1;
block->c[1][3]=1;
break;
}
}
/*getnextblock!
*/
voidNextBlock()
{
/*copythenextBlocktocurBlock*/
=;
=;
=(BoardWidth-4)/2;
=;
memcpy,,16);
/*generatenextBlockandshowit*/
EraseBlock(&nextBlock,NBBoardLeft,NBBoardTop,NBCellSize);
GenerateBlock(&nextBlock);
=1,=0;
DrawBlock(&nextBlock,NBBoardLeft,NBBoardTop,NBCellSize);
}
/*rotatetheblock,updatetheblockstructdata*/
int_INNER_HELPERRotateCells(charc[4][4],charblockSize)
{
chartemp,i,j;
switch(blockSize)
{
case3:
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;
break;
case4:
/*only'I'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];
break;
}
}
/*judgeiftheblockcanmovetowardthedirection*/
intCanMove(intdx,intdy)
{
inti,j,tempX,tempY;
for(i=0;i<;i++)
{
for(j=0;j<;j++)
{
if[i][j])
{
/*cannotmoveleftwardorrightward*/
tempX=+i+dx;
if(tempX<0||tempX>(BoardWidth-1))returnfalse;/*makesurexisvalid!
*/
/*cannotmovedownward*/
tempY=+j+dy;
if(tempY>(BoardHeight-1))returnfalse;/*yisonlycheckedlowerbound,maybenegative!
!
!
!
*/
/*thecellalreadyfilled,wemustcheckY'supperboundbeforecheckcell!
*/
if(tempY>=0&&Board[tempX][tempY][0])returnfalse;
}
}
}
returntrue;
}
/*judgeiftheblockcanrotate*/
intCanRotate()
{
inti,j,tempX,tempY;
/*updatebuffer*/
memcpy(BufferCells,,16);
RotateCells(BufferCells,;
for(i=0;i<;i++)
{
for(j=0;j<;j++)
{
if(BufferCells[i][j])
{
tempX=+i;
tempY=+j;
if(tempX<0||tempX>(BoardWidth-1))
returnfalse;
if(tempY>(BoardHeight-1))
returnfalse;
if(tempY>=0&&Board[tempX][tempY][0])
returnfalse;
}
}
}
returntrue;
}
/*drawtheblock*/
void_INNER_HELPERDrawBlock(Block*block,intbdLeft,intbdTop,intcellSize)
{
inti,j;
setfillstyle(SOLID_FILL,block->color);
for(i=0;isize;i++)
{
for(j=0;jsize;j++)
{
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)
{
chartemp,i,j;
intb_success;
if(block->size==2)
returntrue;
if((b_success=CanRotate()))
{
EraseBlock(block,BoardLeft,BoardTop,CellSize);
memcpy,BufferCells,16);
DrawBlock(block,BoardLeft,BoardTop,CellSize);
}
returnb_success;
}
/*eraseablock,onlyfillthefilledcellwithbackgroundcolor*/
void_INNER_HELPEREraseBlock(Block*block,intbdLeft,intbdTop,intcellSize)
{
inti,j;
setfillstyle(SOLID_FILL,BkGndColor);
for(i=0;isize;i++)
{
for(j=0;jsize;j++)
{
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);
}
}
}
}
/*movebythedirectionifcan,donothingifcannot
*returnvalue:
true-success,false-cannotmovetowardthisdirection
*/
intMoveBlock(Block*block,intdx,intdy)
{
intb_canmove=CanMove(dx,dy);
if(b_canmove)
{
EraseBlock(block,BoardLeft,BoardTop,CellSize);
+=dx;
+=dy;
DrawBlock(block,BoardLeft,BoardTop,CellSize);
}
returnb_canmove;
}
/*droptheblocktothebottom!
*/
intDropBlock(Block*block)
{
EraseBlock(block,BoardLeft,BoardTop,CellSize);
while(CanMove(0,1))
{
++;
}
DrawBlock(block,BoardLeft,BoardTop,CellSize);
return0;/*returnvalueisassigntotheblock'salive*/
}
/*initthegraphicsmode,drawtheboardgrid*/
voidInitGame()
{
inti,j,gdriver=DETECT,gmode;
structtimesysTime;
/*drawboardcells*/
memset(Board,0,BoardWidth*BoardHeight*2);
memset,0,16);
strcpy(info_help,"P:
PauseGame.--byhoodlum1980");
initgraph(&gdriver,&gmode,"");
setcolor(BorderColor);
for(i=0;i<=BoardWidth;i++)
{
line(BoardLeft+i*CellSize,BoardTop,BoardLeft+i*CellSize,BoardTop+BoardHeight*CellSize);
}
for(i=0;i<=BoardHeight;i++)
{
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*/
for(i=0;i<=4;i++)
{
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*3600+*60+;
GenerateBlock(&nextBlock);
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()
{
inti,j;
for(i=0;i<;i++)
{
for(j=0;j<;j++)
{
if[i][j]&&+j)>=0)
{
Board[+i][+j][0]=1;
Board[+i][+j][1]=;
}
}
}
}
/*drawonelineoftheboard*/
void_INNER_HELPERPaintBoard()
{
inti,j,fillcolor;
for(j=max((TopLine-4),0);j{
for(i=0;i{
fillcolor=Board[i][j][0]?
Board[i][j][1]:
BkGndColor;
setfillstyle(SOLID_FILL,fillcolor);
floodfill(BoardLeft+i*CellSize+CellSize/2,BoardTop+j*CellSize+CellSize/2,BorderColor);
}
}
}
/*checkifonelineiffilledfullandincreasethetotalScore!
*/
void_INNER_HELPERCheckBoard()
{
inti,j,k,score=10,sum=0,topy,lines=0;
/*wefindthetopemptyline!
*/
j=topy=BoardHeight-1;
do
{
sum=0;
for(i=0;i<