| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1520 人关注过本帖
标题:俄罗斯方块源码
只看楼主 加入收藏
pthouge
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2006-3-10
收藏
 问题点数:0 回复次数:7 
俄罗斯方块源码

这个源码没有加注释,看不懂,不知道是否有人有加过注释的源码,或者是帮我读一下,加一下注释
#include <stdlib.h>
#include <graphics.h>
#include <bios.h>
#define mDRAW 5
#define mLINE 6
#define mADOWN 7
#define mGEN 8
#define mLEFT 75
#define mRIGHT 77
#define mSPACE 57
#define mDOWN 80
#define mESC 1
#define TIMEINT 2
#define MAXX 9
#define MAXY 30
#define BACKCOLOR BLACK
#define WINX 50
#define WINY 470
#define GAP 6
#define AREAX (WINX+GAP)
#define AREAY (WINY-GAP)
#define BOXW 15

int oldarea[MAXY+1][MAXX];
int area[MAXY+1][MAXX];
int actW,actH,actX,actY;
int curX,curY,curColor,curW,curH;
int newX,newY,newColor,newW,newH;
int active;
int box[4][4];
int FORCOLOR;
int MESSAGE;
int BOX[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}
}
};


void init();
void draw();
int genBox();
int getKey();
void lineFull();
int moveLeft();
int moveRight();
int moveDown();
int rotate();
int getW();
int getH();
void clearOldBox();
void putNewBox();
int collisionRotate(int box[][4]);
void getMessage();
void dispatchMessage();
int timeCome();
void fallDown();
int gameOver();

main()
{
int i;
init();
do
{
getMessage();
dispatchMessage();
}
while(!gameOver());

getch();
closegraph();
}

void getMessage()
{
if(MESSAGE) return;
if(timeCome())
{
MESSAGE=mADOWN;
return;
}
if(bioskey(1))
{
MESSAGE=bioskey(0)>>8;
return;
}
}

void dispatchMessage()
{
switch(MESSAGE)
{
case mLEFT: moveLeft();break;
case mRIGHT: moveRight();break;
case mADOWN: moveDown();break;
case mSPACE: rotate();break;
case mDOWN: fallDown(); break;
case mDRAW: draw();break;
case mLINE: lineFull();break;
case mGEN: genBox();break;
case mESC: closegraph(); exit(0);
default: MESSAGE=0;
}
}

void fallDown()
{
while(active)
{
moveDown(); draw();
}

MESSAGE=mLINE;
}

int timeCome()
{

static long tm, old;
tm=biostime(0,tm);
if(tm-old<TIMEINT) return 0;
else
{
old=tm; return 1;
}
}

void init()
{
int i,j,x1,y1,x2,y2;
int driver=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;
}

int genBox()
{
int i,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)) return 0;
putNewBox();
draw(); MESSAGE=0;
return 1;
}

void lineFull()
{
int row,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;
}

void draw()
{
int row,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;
}

int moveLeft()
{
newX=curX-1; clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();
actW=curW+1; actX=curX=newX;
MESSAGE=mDRAW;
return 1;
}

int moveRight()
{
newX=curX+1; clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();
actW=curW+1; actX=curX; curX=newX;
MESSAGE=mDRAW;
return 1;
}

int moveDown()
{
int i,j;
newY=curY-1;
clearOldBox();
if(collision(box))
{
newY=curY;
putNewBox();
active=0;
MESSAGE=mLINE;
return 0;
}
putNewBox();
actH=curH+1; actY=newY; curY=newY;
MESSAGE=mDRAW;
return 1;
}

int rotate()
{
int newBox[4][4];
int i,j;
clearOldBox();

for(j=0;j<4;j++)
for(i=0;i<4;i++)
newBox[j][i]=0;

for(j=0;j<=curH;j++)
for(i=0;i<=curW;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;
return 0;
}

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;
return 1;
}

int getW()
{
int i,j;
for(i=3;i>0;i--)
for(j=0;j<4;j++)
if(box[j][i]) return i;
return 0;
}

int getH()
{
int i,j;
for(j=3;j>0;j--)
for(i=0;i<4;i++)
if(box[j][i]) return j;
return 0;
}


void clearOldBox()
{
int i,j;
for(j=0;j<=curH; j++)
for(i=0;i<=curW; i++)
if(box[j][i])
area[curY+j][curX+i]=0;
}

void putNewBox()
{
int i,j;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(box[j][i])
area[newY+j][newX+i]=FORCOLOR;
}

int collision(int cbox[][4])
{
int i,j;
if(newX<0) return 1;
if(newX+newW>=MAXX) return 1;
if(newY<0) return 1;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(area[newY+j][newX+i]&&cbox[j][i]) return 1;
return 0;
}

int collisionRotate(int cbox[][4])
{
int i,j;
if(newX+newW>=MAXX) newX=MAXX-1-newW;
if(newY+newH>=MAXY) newY=MAXY-1-newH;
if(collision(cbox)) return 1;
for(i=0;i<=newW;i++)
for(j=0;j<=newH;j++)
if(area[newY+j][newX+i])
{
newX-=newW-i+1; goto L;
}
L: return collision(cbox);
}

int gameOver()
{
if(!active &&(curY+curH>MAXY-3)) return 1;
else return 0;
}

搜索更多相关主题的帖子: define 俄罗斯方块 源码 include 注释 
2006-04-22 08:04
errorer
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-4-19
收藏
得分:0 
以下是引用pthouge在2006-4-22 8:04:00的发言:

这个源码没有加注释,看不懂,不知道是否有人有加过注释的源码,或者是帮我读一下,加一下注释
#include <stdlib.h>
#include <graphics.h>
#include <bios.h>
#define mDRAW 5
#define mLINE 6
#define mADOWN 7
#define mGEN 8
#define mLEFT 75
#define mRIGHT 77
#define mSPACE 57
#define mDOWN 80
#define mESC 1
#define TIMEINT 2
#define MAXX 9
#define MAXY 30
#define BACKCOLOR BLACK
#define WINX 50
#define WINY 470
#define GAP 6
#define AREAX (WINX+GAP)
#define AREAY (WINY-GAP)
#define BOXW 15

int oldarea[MAXY+1][MAXX];
int area[MAXY+1][MAXX];
int actW,actH,actX,actY;
int curX,curY,curColor,curW,curH;
int newX,newY,newColor,newW,newH;
int active;
int box[4][4];
int FORCOLOR;
int MESSAGE;
int BOX[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}
}
};/*定义全局,边界大小,颜色等/*


void init();/*申明函数(游戏初始化)*/
void draw();/*画图函数(用于做出一个动作后下一个游戏画面)*/
int genBox();/**/
int getKey();/**/
void lineFull();/**/
int moveLeft();/*向左运动函数*/
int moveRight();/*向右运动函数*/
int moveDown();/*向下运动函数*/
int rotate();/**/
int getW();/**/
int getH();/**/
void clearOldBox();/**/
void putNewBox();/**/
int collisionRotate(int box[][4]);/**/
void getMessage();/**/
void dispatchMessage();/*处理键盘信息函数,依据信息的不同而执行相应的动作*/
int timeCome();/**/
void fallDown();/**/
int gameOver();/*游戏结束函数*/

main()
{
int i;
init();/*调用此函数,游戏初始化*/
do
{
getMessage();/*调用此函数(接收键盘信息)*/
dispatchMessage();/*调用此函数,根据键盘信息执行动作*/

!gameOver());

getch();
closegraph();
}

void getMessage()
{
if(MESSAGE) return;
if(timeCome())
{
MESSAGE=mADOWN;
return;
}
if(bioskey(1))
{
MESSAGE=bioskey(0)>>8;
return;
}
}

void dispatchMessage()
{
switch(MESSAGE)
{
case mLEFT: moveLeft();break;
case mRIGHT: moveRight();break;
case mADOWN: moveDown();break;
case mSPACE: rotate();break;
case mDOWN: fallDown(); break;
case mDRAW: draw();break;
case mLINE: lineFull();break;
case mGEN: genBox();break;
case mESC: closegraph(); exit(0);
default: MESSAGE=0;/*对接收过来的信息进行分类,不同的参数,调用执行不同的函数*/

void fallDown()/*向下运动函数*/
{
while(active)
{
moveDown(); /*调用函数,做向下运动*/
draw();/*调用函数,画出下一时刻的游戏画面*/
}

MESSAGE=mLINE;/*把这个全局附值给message*/
}

int timeCome()
{

static long tm, old;
tm=biostime(0,tm);
if(tm-old<TIMEINT) return 0;
else
{
old=tm; return 1;
}
}

void init()
{
int i,j,x1,y1,x2,y2;
int driver=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;
}/*此函数画出游戏初形式*/

int genBox()
{
int i,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)) return 0;
putNewBox();
draw(); MESSAGE=0;
return 1;
}

void lineFull()
{
int row,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;
}

void draw()
{
int row,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;
}

int moveLeft()
{
newX=curX-1; clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();
actW=curW+1; actX=curX=newX;
MESSAGE=mDRAW;
return 1;
}

int moveRight()
{
newX=curX+1; clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();
actW=curW+1; actX=curX; curX=newX;
MESSAGE=mDRAW;
return 1;
}

int moveDown()
{
int i,j;
newY=curY-1;
clearOldBox();
if(collision(box))
{
newY=curY;
putNewBox();
active=0;
MESSAGE=mLINE;
return 0;
}
putNewBox();
actH=curH+1; actY=newY; curY=newY;
MESSAGE=mDRAW;
return 1;
}

int rotate()
{
int newBox[4][4];
int i,j;
clearOldBox();

for(j=0;j<4;j++)
for(i=0;i<4;i++)
newBox[j][i]=0;

for(j=0;j<=curH;j++)
for(i=0;i<=curW;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;
return 0;
}

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;
return 1;
}

int getW()
{
int i,j;
for(i=3;i>0;i--)
for(j=0;j<4;j++)
if(box[j][i]) return i;
return 0;
}

int getH()
{
int i,j;
for(j=3;j>0;j--)
for(i=0;i<4;i++)
if(box[j][i]) return j;
return 0;
}


void clearOldBox()
{
int i,j;
for(j=0;j<=curH; j++)
for(i=0;i<=curW; i++)
if(box[j][i])
area[curY+j][curX+i]=0;
}

void putNewBox()
{
int i,j;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(box[j][i])
area[newY+j][newX+i]=FORCOLOR;
}

int collision(int cbox[][4])
{
int i,j;
if(newX<0) return 1;
if(newX+newW>=MAXX) return 1;
if(newY<0) return 1;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(area[newY+j][newX+i]&&cbox[j][i]) return 1;
return 0;
}

int collisionRotate(int cbox[][4])
{
int i,j;
if(newX+newW>=MAXX) newX=MAXX-1-newW;
if(newY+newH>=MAXY) newY=MAXY-1-newH;
if(collision(cbox)) return 1;
for(i=0;i<=newW;i++)
for(j=0;j<=newH;j++)
if(area[newY+j][newX+i])
{
newX-=newW-i+1; goto L;
}
L: return collision(cbox);
}

int gameOver()
{
if(!active &&(curY+curH>MAXY-3)) return 1;
else return 0;
}

不好意思,有点别扭呀。
假如我要在else return 0;后面打个**
那么下面那排的}就没了,5555555555,请求高手教我怎么做呀(记得原来有次遇到这种情况,解决了,但没记住方法)


思想有多远,就能走多远!
2006-04-22 10:37
zljackson
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-4-17
收藏
得分:0 
在哪里改变方格的下降速度呢??
2006-04-22 22:02
cdmalcl
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:4091
专家分:524
注 册:2005-9-23
收藏
得分:0 
以下是引用pthouge在2006-4-22 8:04:00的发言:

这个源码没有加注释,看不懂,不知道是否有人有加过注释的源码,或者是帮我读一下,加一下注释
#define mESC 1
#define TIMEINT 2 //这里改 越小越快
#define MAXX 9

2006-04-23 03:20
cdmalcl
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:4091
专家分:524
注 册:2005-9-23
收藏
得分:0 

#include <stdlib.h>
#include <graphics.h>
#include <bios.h>
#include <math.h>

#define mDRAW 5
#define mLINE 6
#define mADOWN 7
#define mGEN 8
#define mLEFT 75 /*左键*/
#define mRIGHT 77 /*右键*/
#define mSPACE 57 /*空格键*/
#define mDOWN 80 /*下键*/
#define mUP 72 /*上键*/
#define mADD 13 /*增速减*/
#define mMINU 12 /*减速键*/
#define mESC 1 /*退出键*/
#define mENTER 28 /*回车键*/
#define TIMEINT 9 /*时间分度常量(游戏速度初始值)*/
#define NEXTBOXX 20 /*预显示方块图象位置参考坐标的X常量*/
#define NEXTBOXY 30 /*预显示方块图象位置参考坐标的常量*/
#define MAXX 9 /*有效区横轴方格个数*/
#define MAXY 25 /*有效区纵轴方格个数*/
#define BACKCOLOR BLACK /*有效区方格背景颜色*/
#define WINX 250 /*有效区顶坐标X值*/
#define WINY 470 /*有效区底坐标Y值*/
#define GAP 6 /*校正值*/
#define AREAX (WINX+GAP)
#define AREAY (WINY-GAP)
#define BOXW 18 /*方块长(宽)度*/
#define FILLBOX 5 /*方块填充方式*/
#define CHAR_C 2 /*分数字体大小*/
#define NB_COLOR 12 /*预显示方块显示区域背景颜色*/
#define S_LINE 100 /*关口界线*/

#define SP_X WINX+BOXW*MAXX+80 /*速度显示条X轴位置*/
#define SP_Y 90 /*速度显示条Y轴位置*/
#define SP_COLOR 6 /*速度显示条背景颜色*/

int oldarea[MAXY+1][MAXX];
int area[MAXY+1][MAXX];
int actW,actH,actX,actY;
int curX,curY,curColor,curW,curH;
int newX,newY,newColor,newW,newH;
int active;
int box[4][4],box_next[4][4];
int boxidx1,boxidx2;
int FORCOLOR;
int MESSAGE;
int speed=TIMEINT;
int sorce;
int BOX[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}
}
};/*定义全局,边界大小,颜色等*/

void getMessage(); /*得到键盘按键记录*/
void dispatchMessage(); /*处理键盘的信息和控制方块的不同状态*/
void fallDown(); /*让方块一次性到底*/
int timeCome(); /*时间控制,以此控制方块下落的速度*/
void init(); /*对游戏的一些数据和画面进行初试化*/
int genBox(); /*下一方块设定*/
void lineFull(); /*减行操作*/
void draw(); /*绘制方块*/
int moveLeft(); /*向左运动函数*/
int moveRight(); /*向右运动函数*/
int moveDown(); /*向下运动函数*/
int rotate(); /*改变方块的形状*/
int getW(); /*返回方块最右端列值*/
int getH(); /*返回方块最下端行值*/
void clearOldBox(); /*清理方块变量*/
void putNewBox(); /*装载方块变量*/
int collision(int cbox[][4]); /*方块形状变化判断*/
int collisionRotate(int box[][4]); /*方块形状变化判断*/
int gameOver(); /*游戏结束函数*/

int bkview(); /*绘制特性显示图形背景*/
int draw_next(); /*下一个出现的方块图形的绘制*/
int speedview(); /*显示游戏速度*/

main()
{
init();
do
{
setspeed();
getMessage();
if(MESSAGE==mADD){speed-=(speed>1);speedview();}
dispatchMessage();

}
while(!gameOver());

getch();
closegraph();
}

void getMessage()
{
if(MESSAGE) return;
if(timeCome())
{
MESSAGE=mADOWN;
return;
}
if(bioskey(1))
{
MESSAGE=bioskey(0)>>8;
if(MESSAGE>=5&&MESSAGE<=8)MESSAGE=0;
return;
}
}

void dispatchMessage()
{
int i;

switch(MESSAGE)
{
case mLEFT: moveLeft();break;

case mRIGHT: moveRight();break;

case mADOWN: moveDown();break;

case mUP:
case mSPACE: rotate();break;

case mDOWN: moveDown(); break;

case mDRAW: draw();break;

case mENTER: fallDown();break;

case mLINE: lineFull();break;

case mGEN: genBox();break;

case mMINU: speed+=(speed<9);speedview();break;

case mESC: closegraph(); exit(0);

default: MESSAGE=0;
}
}

void fallDown()
{
while(active)
{
moveDown();
draw();
}

MESSAGE=mLINE;
}

int timeCome()
{
static long tm, old;

tm=biostime(0,tm);

if(tm-old<speed) return 0;
else
{
old=tm;
return 1;
}
}

void init()
{
int i,j,x1,y1,x2,y2;
int driver=DETECT, mode=0;

sorce=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;

bkview();
draw_next();

draw();
MESSAGE=mGEN;
}

int genBox()
{
int i,j;

draw_next();
FORCOLOR=boxidx1+2;

for(j=0;j<4-(boxidx1==2);j++)
for(i=0;i<4;i++)
box[j][i]=BOX[boxidx1][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)) return 0;
putNewBox();

draw();
MESSAGE=0;
return 1;
}

void lineFull()
{
int row,col, rowEnd,full,i,j;
int num=0;
char *sorce_char;

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--;
num++;
}
sorce+=num;
if(num)
{
ltoa(sorce,sorce_char,10);
for(j=0;j<16+CHAR_C*5;j++)
for(i=0;i<110+CHAR_C*5;i++)
putpixel(NEXTBOXX+i,NEXTBOXY+j+190,1);
setcolor(12);
outtextxy(NEXTBOXX+5,NEXTBOXY+190,sorce_char);
}
MESSAGE=mGEN;
}

void draw()
{
int row,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(FILLBOX,BACKCOLOR);
else
setfillstyle(FILLBOX,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;
}

int moveLeft()
{
newX=curX-1;

clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();

actW=curW+1;
actX=curX=newX;
MESSAGE=mDRAW;
return 1;
}

int moveRight()
{
newX=curX+1;
clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();

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

MESSAGE=mDRAW;
return 1;
}

int moveDown()
{
int i,j;
newY=curY-1;
clearOldBox();
if(collision(box))
{
newY=curY;
putNewBox();

active=0;
MESSAGE=mLINE;
return 0;
}
putNewBox();
actH=curH+1;
actY=newY;
curY=newY;

MESSAGE=mDRAW;
return 1;
}

int rotate()
{
int newBox[4][4];
int i,j;

clearOldBox();

for(j=0;j<4;j++)
for(i=0;i<4;i++)
newBox[j][i]=0;

for(j=0;j<=curH;j++)
for(i=0;i<=curW;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;
return 0;
}

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;
return 1;
}

int getW()
{
int i,j;
for(i=3;i>0;i--)
for(j=0;j<4;j++)
if(box[j][i]) return i;
return 0;
}

int getH()
{
int i,j;
for(j=3;j>0;j--)
for(i=0;i<4;i++)
if(box[j][i]) return j;
return 0;
}

void clearOldBox()
{
int i,j;
for(j=0;j<=curH; j++)
for(i=0;i<=curW; i++)
if(box[j][i])
area[curY+j][curX+i]=0;
}

void putNewBox()
{
int i,j;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(box[j][i])
area[newY+j][newX+i]=FORCOLOR;
}

int collision(int cbox[][4])
{
int i,j;
if(newX<0) return 1;
if(newX+newW>=MAXX) return 1;
if(newY<0) return 1;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(area[newY+j][newX+i]&&cbox[j][i]) return 1;
return 0;
}

int collisionRotate(int cbox[][4])
{
int i,j;
if(newX+newW>=MAXX)
newX=MAXX-1-newW;
if(newY+newH>=MAXY)
newY=MAXY-1-newH;
if(collision(cbox)) return 1;

for(i=0;i<=newW;i++)
for(j=0;j<=newH;j++)
if(area[newY+j][newX+i])
{
newX-=newW-i+1; goto L;
}
L: return collision(cbox);
}

int gameOver()
{
if(!active &&(curY+curH>MAXY-3))
return 1;
else
return 0;
}

接下

2006-04-23 12:28
cdmalcl
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:4091
专家分:524
注 册:2005-9-23
收藏
得分:0 

int bkview()
{
int i;
float l;

settextstyle(1,0,CHAR_C);
setcolor(3);

/***************预显示方块背景*******************/
for(i=1;i<11;i++)
{
setcolor(i/2+3);
rectangle(NEXTBOXX-10+i,NEXTBOXY+i,NEXTBOXX+BOXW*7-i,NEXTBOXY+BOXW*6+6-i);
}
setfillstyle(1,NB_COLOR);
floodfill(NEXTBOXX+20,NEXTBOXY+20,getcolor());
for(i=1;i<11;i++)
{
setcolor(i/5+3);
rectangle(NEXTBOXX-15+i,NEXTBOXY+175+i,NEXTBOXX+151-i+CHAR_C*10,NEXTBOXY+221-i+CHAR_C*5);
}
/**************速度显示背景绘制*****************/
outtextxy(SP_X-5,SP_Y-CHAR_C*5+10,"speed:");
for(i=1;i<11;i++)
{
setcolor(i/5+8);
rectangle(SP_X-5+i,SP_Y+70+i,SP_X+46-i+CHAR_C*10,SP_Y+26-i+CHAR_C*5);

}
rectangle(SP_X+6,SP_Y+72,SP_X+35+CHAR_C*10,SP_Y+24+CHAR_C*5);
setfillstyle(1,SP_COLOR);
floodfill(SP_X+8,SP_Y+60,getcolor());
speedview();
/***************成绩显示背景绘制***************/
outtextxy(NEXTBOXX-8,NEXTBOXY+160-CHAR_C*5,"SORCE:");
setcolor(12);
outtextxy(NEXTBOXX+5,NEXTBOXY+190,"0");
/***************游戏进行区的边框**************/
for(l=MAXX;l<470;l+=0.2)putpixel(AREAX-3+cos(l/3)*3,l,2);
for(l=MAXX;l<470;l+=0.2)putpixel(AREAX-3+sin(l/3)*3,l,2);
for(l=MAXX;l<470;l+=0.2)putpixel(AREAX+MAXX*BOXW+GAP/2+1+cos(l/3)*3,l,2);
for(l=MAXX;l<470;l+=0.2)putpixel(AREAX+MAXX*BOXW+GAP/2+1+sin(l/3)*3,l,2);

}

int draw_next()
{

int i,j,Color;
int gunk=0;

boxidx1=boxidx2;
boxidx2=random(7);
FORCOLOR=boxidx2+2;

if(boxidx1==0)
gunk=-5;
else
if(boxidx1==6)
gunk=5;

Color=getcolor();
setcolor(NB_COLOR);

for(j=0+(boxidx2==1);j<4;j++)
for(i=0;i<4;i++)
if(BOX[boxidx1][3-j][i])
{
rectangle(i*BOXW+29+NEXTBOXX+gunk,j*BOXW+NEXTBOXY,(i+1)*BOXW+29+NEXTBOXX+gunk,(j+1)*BOXW+NEXTBOXY);
setfillstyle(1,NB_COLOR);
floodfill(i*BOXW+31+NEXTBOXX+gunk,j*BOXW+2+NEXTBOXY,NB_COLOR);
}
if(boxidx2==0)
gunk=-5;
else
if(boxidx2==6)
gunk=5;
else
gunk=0;

setcolor(FORCOLOR);
for(j=0+(boxidx2==2);j<4;j++)
for(i=0;i<4;i++)
if(BOX[boxidx2][3-j][i])
{
rectangle(i*BOXW+29+NEXTBOXX+gunk,j*BOXW+NEXTBOXY,(i+1)*BOXW+29+NEXTBOXX+gunk,(j+1)*BOXW+NEXTBOXY);
setfillstyle(FILLBOX,FORCOLOR);
floodfill(i*BOXW+31+NEXTBOXX+gunk,j*BOXW+2+NEXTBOXY,FORCOLOR);
}
}

int setspeed()
{
int s;

s=sorce/S_LINE+1;
if(s>9)
speed=1;
else
if(speed>10-s)
{
speed-=(speed>1);
speedview();
}

}

int speedview()
{
int i,j;
char *speed_char;
int cr;

for(j=0;j<15+CHAR_C*4;j++)
for(i=0;i<10+CHAR_C*4;i++)
putpixel(SP_X+i+23+2*(speed==9),SP_Y+40+j,SP_COLOR);

cr=getcolor();
setcolor(10);
ltoa(10-speed,speed_char,10);
outtextxy(SP_X+25+2*(speed==9),SP_Y+43,speed_char);
setcolor(cr);
MESSAGE=0;
}

2006-04-23 12:28
cdmalcl
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:4091
专家分:524
注 册:2005-9-23
收藏
得分:0 

蓝色的地方是我后加的东西

2006-04-23 12:29
高阁逆风
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:508
专家分:321
注 册:2006-4-29
收藏
得分:0 
厉害厉害!!

      上天安排我做了个多情的人,却又安排我遭遇了无数绝情的人,所以我最终把自己磨练成了一个滥情的人。别人是人见人爱,我是见人爱人.......
2006-05-06 18:10
快速回复:俄罗斯方块源码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.107085 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved