/************************************
*Desc:俄罗斯方块游戏
*By:hoodlum1980
:30
************************************/
#include<stdio.h>
#include<bios.h>
#include<dos.h>
#include<graphics.h>
#include<string.h>
#include<stdlib.h>
#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*/
curBlock.size=nextBlock.size;
lor;
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(&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];