当前位置:神舟问>生活百科>VC6来自.0环境下的俄罗斯方块C语言源代码

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

2023-10-16 20:34:53 编辑:join 浏览量:608

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

#include<stdlib.h>

#include<graphics.h>

#include<bios.h>

#definemDRAW5

#definemLINE6

#definemADOWN7

#definemGEN8

#definemL意宪EFT75

#definemRIGHT77

#definemSPACE57

#至印触大definemDOWN于世立送80

#definemESC1

#defineTIMEINT2

#defineMAXX9

#defineMAXY30

#defineBACKCOLORBLACK

#defineWINX50

#造垂我味层扩河南好县游defineWINY470

#学肥甚呢宽际议植上达defineGAP6

#defineAREAX(WINX+GAP)

#defineAREAY(WINY-GAP)

#defineBOXW15

intoldarea[MAXY+1][MAXX];

intarea防否孔春官第[MAXY+1][MAXX];

intactW,actH,actX,actY;

intcurX,curY,curColor,curW,curH;

intnewX,new弦段圆阶看粮群但息Y,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();

v易统办移南使贵oiddraw();

intgenBox();

intgetKey();

voidlineFull();

intmoveLeft();

intmoveRight();

intmoveDown();

introtate();

intge叶脚思停买十车花鸡铁节tW();

intgetH();

voidclearOldBox();

v设听牛但乎祖苗查继余云oidputNewBox();

intcollision某如海通雨苏娘Rotate(intbox[需映误儿尼米伯粮][4]);

voidgetMessage();

voiddispatchMessage();

inttimeCome();

voidfallDown();

intgameOver();

main()

{

i京调查脱作料就史林nti;

init();

do

{

g沉老若各留防叫措沙新协etMessage();

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;

}

标签:源代码,VC6,C语言

版权声明:文章由 神舟问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.shenzhouwen.com/life/187708.html
热门文章