您的位置首页百科问答

VC6.0环境下的俄罗斯方块C语言源代码来自

VC6.0环境下的俄罗斯方块C语言源代码来自

的有关信息介绍如下:

VC6.0环境下的俄罗斯方块C语言源代码来自

#include<stdlib.h>

#include<graphics.h>

#include<bios.h>

#definemDRAW5

#definemLINE6

#definem功是冲开兵ADOWN7

#definemGEN8

#definemLEFT75

#definemRIGHT77

#definemSPACE57

#definemDOWN80

#definemESC1

#defineTIMEINT2

#defineMAXX9

#defineMAXY30

#d360问答efineBACKCOLORBLACK

#defineWINX50

#defineWINY击信赵见470

#defineGAP6

#defineAREAX(WINX+GAP)

#defineAR杨命EAY(WINY-GAP)

#defineBOXW15

intoldarea[MAXY+1][MAXX];

intarea[MAXY+1][MAXX];

intactW,actH,actX,act布括连助吧千介Y;

intcurX,curY,curColor,curW,curH;

intnewX,newY,newColor,newW,newH;

intactive;

intbox[4][4];

intFORCOLOR;

intMESSAGE;

妒收针斤intBOX[7][4觉叶类社][4]={

{

{1,1,1,1},

{0,0,0,0},

{0,0,0,0},

{0,0,0,0}

},{

{1,1,1,0},

{1,0,0,0},

{0,0,0,0},

{0,0,0,0}

},{

{1,1,1,混再来0},

{0,0,1,题顾剂调南术露就算0},

{0,0,0,0},

{0,0,0,0}

},{

{1,1,1,0},

{0,1,0,0},

{0,0十孩喜境困品车足兵粒,0,0},

{0,0,0,0}

},{

{1,1,0,0},

{0,1,1,0},

{0,0,0,0},

{0,0,0,0}

},{

{0,1,1,0},

{五久音示继1,1,0,0},

{0,0,0,0},

{0,0,0,0}

},{

{药入哪图论1,1,0,0},

{1,1,0,0},

{0,0,0,0},

{0,0,0,0}

}

};

voidinit();

voiddraw();

intgenBox(械下斯富);

intgetKey();

v答经也尼关象专知夜oidlineFull();

intmoveLeft();

intmoveRight();

intmoveDown();

introtate();

intgetW();

intgetH();

voidclearOldBox();

voidputNewBox();

i围停百革诉易ntcollisio首运飞强且候达就着神nRotate(intbox[][4歌查法]);

voidgetMessage();

voiddispatchMessage();

inttimeCome();

voidfallDown();

intgameOver();

main()

{

inti;

init();

do

{

getMessage();

dispatchMessage();

}

while(!gameOver());

getch();

closegraph();

}

voidgetMessage()

{

if(MESSAGE)return;

if(timeCome())

{

MESSAGE=mADOWN;

return;

}

if(bioskey(1))

{

MESSAGE=bioskey(0)>>8;

return;

}

}

voiddispatchMessage()

{

switch(MESSAGE)

{

casemLEFT:moveLeft();break;

casemRIGHT:moveRight();break;

casemADOWN:moveDown();break;

casemSPACE:rotate();break;

casemDOWN:fallDown();break;

casemDRAW:draw();break;

casemLINE:lineFull();break;

casemGEN:genBox();break;

casemESC:closegraph();exit(0);

default:MESSAGE=0;

}

}

voidfallDown()

{

while(active)

{

moveDown();draw();

}

MESSAGE=mLINE;

}

inttimeCome()

{

staticlongtm,old;

tm=biostime(0,tm);

if(tm-old<TIMEINT)return0;

else

{

old=tm;return1;

}

}

voidinit()

{

inti,j,x1,y1,x2,y2;

intdriver=DETECT,mode=0;

randomize();

registerbgidriver(EGAVGA_driver);

initgraph(&driver,&mode,"");

cleardevice();

setfillstyle(SOLID_FILL,BLUE);

bar(0,0,639,479);

x1=AREAX;

y1=AREAY-BOXW*MAXY;

x2=AREAX+MAXX*BOXW;

y2=AREAY;

rectangle(--x1,--y1,++x2,++y2);

setfillstyle(SOLID_FILL,BLACK);

bar(++x1,++y1,--x2,--y2);

y1=AREAY-MAXY*BOXW;y2=AREAY;

setcolor(DARKGRAY);

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

{

x1=AREAX+i*BOXW;

line(x1,y1,x1,y2);

}

x1=AREAX;x2=x1+MAXX*BOXW;

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

{

y1=AREAY-j*BOXW;

line(x1,y1,x2,y1);

}

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

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

area[j][i]=oldarea[j][i]=0;

actX=0;actY=0;actW=MAXX-1;actH=MAXY-1;

draw();

MESSAGE=mGEN;

}

intgenBox()

{

inti,j,boxidx;

boxidx=random(7);FORCOLOR=random(7)+1;

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

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

box[j][i]=BOX[boxidx][j][i];

curW=getW();curH=getH();

curX=(MAXX+curW)/2;

if(curX+curW>=MAXX)curX=MAXX-1-curW;

curY=MAXY-1-curH;

newX=curX;newY=curY;actX=curX;actY=curY;

actW=newW=curW;actH=newH=curH;

active=1;

if(collision(box))return0;

putNewBox();

draw();MESSAGE=0;

return1;

}

voidlineFull()

{

introw,col,rowEnd,full,i,j;

rowEnd=newY+newH;

if(rowEnd>=MAXY-1)rowEnd=MAXY-2;

for(row=newY;row<=rowEnd;)

{

full=1;

for(col=0;col<MAXX;col++)

if(!area[row][col]){full=0;break;}

if(!full){++row;continue;}

for(j=row;j<MAXY-1;j++)

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

area[j][i]=area[j+1][i];

actX=0;actY=row;actW=MAXX-1;actH=MAXY-1-row;

draw();rowEnd--;

}

MESSAGE=mGEN;

}

voiddraw()

{

introw,col,x1,y1,x2,y2;

for(row=actY;row<=actY+actH;row++)

for(col=actX;col<=actX+actW;col++)

if(area[row][col]!=oldarea[row][col])

{

if(area[row][col]==0)

setfillstyle(SOLID_FILL,BACKCOLOR);

else

setfillstyle(SOLID_FILL,FORCOLOR);

x1=AREAX+col*BOXW;x2=x1+BOXW;

y1=AREAY-(row+1)*BOXW;y2=y1+BOXW;

bar(++x1,++y1,--x2,--y2);

oldarea[row][col]=area[row][col];

}

MESSAGE=0;

}

intmoveLeft()

{

newX=curX-1;clearOldBox();

if(collision(box))

{

newX=curX;

putNewBox();

MESSAGE=0;

return0;

}

putNewBox();

actW=curW+1;actX=curX=newX;

MESSAGE=mDRAW;

return1;

}

intmoveRight()

{

newX=curX+1;clearOldBox();

if(collision(box))

{

newX=curX;

putNewBox();

MESSAGE=0;

return0;

}

putNewBox();

actW=curW+1;actX=curX;curX=newX;

MESSAGE=mDRAW;

return1;

}

intmoveDown()

{

inti,j;

newY=curY-1;

clearOldBox();

if(collision(box))

{

newY=curY;

putNewBox();

active=0;

MESSAGE=mLINE;

return0;

}

putNewBox();

actH=curH+1;actY=newY;curY=newY;

MESSAGE=mDRAW;

return1;

}

introtate()

{

intnewBox[4][4];

inti,j;

clearOldBox();

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

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

newBox[j][i]=0;

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

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

newBox[curW-i][j]=box[j][i];

newW=curH;newH=curW;

if(collisionRotate(newBox))

{

newW=curW;newH=curH;newX=curX;newY=curY;

putNewBox();

MESSAGE=0;

return0;

}

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

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

box[j][i]=newBox[j][i];

putNewBox();

actH=newH>curH?newH:curH;

actW=curX+actH-newX;

actX=newX;actY=newY;curX=newX;

curY=newY;curW=newW;curH=newH;

MESSAGE=mDRAW;

return1;

}

intgetW()

{

inti,j;

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

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

if(box[j][i])returni;

return0;

}

intgetH()

{

inti,j;

for(j=3;j>0;j--)

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

if(box[j][i])returnj;

return0;

}

voidclearOldBox()

{

inti,j;

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

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

if(box[j][i])

area[curY+j][curX+i]=0;

}

voidputNewBox()

{

inti,j;

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

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

if(box[j][i])

area[newY+j][newX+i]=FORCOLOR;

}

intcollision(intcbox[][4])

{

inti,j;

if(newX<0)return1;

if(newX+newW>=MAXX)return1;

if(newY<0)return1;

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

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

if(area[newY+j][newX+i]&&cbox[j][i])return1;

return0;

}

intcollisionRotate(intcbox[][4])

{

inti,j;

if(newX+newW>=MAXX)newX=MAXX-1-newW;

if(newY+newH>=MAXY)newY=MAXY-1-newH;

if(collision(cbox))return1;

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

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

if(area[newY+j][newX+i])

{

newX-=newW-i+1;gotoL;

}

L:returncollision(cbox);

}

intgameOver()

{

if(!active&&(curY+curH>MAXY-3))return1;

elsereturn0;

}