俄罗斯方块文档格式.docx
《俄罗斯方块文档格式.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块文档格式.docx(34页珍藏版)》请在冰豆网上搜索。
这里我们定义一个CBin类描述Tetris游戏的矩形框。
对矩形框进行分析,它应该有三个私有的数据成员为:
image,width和height.Cbin类将Tetris游戏的矩形框描述成为一个二维数组image,变量width和height存储了image的维数。
接下来为CBin类添加五个成员函数:
构造函数:
CBin(unsignedintw,unsignedinth),
析构函数:
~Cbin(),
voidgetImage(unsignedchar**destImage),
voidsetImage(unsignedchar**srcImage),
unsignedintremoveFullIines().
然后分别在bin.h和bin.cpp中完成Cbin类的定义和函数实现。
(2)Tetris游戏的砖块类:
第一步:
建立类:
CIBrick,CLBrick,CSBrick,CTBrick,CFSBrick,CFLBrick,COBrick,它们的基类都是CBrick类。
它们都有有四个数据成员:
orientation,posX,posy,colour.Orientation表示”1”砖块的四个状态,可能取值为{0,1,2,3},由状态0到状态1是”1”砖块固定一个特定点顺时针旋转90度.依次类推,状态3的下一个状态是状态0.posX,posy记录了特定点的坐标,colour为砖块的颜色值.
为CIBrick重载7个虚构函数:
intshifLeft(CBin*bin),
intshifRight(CBin*bin),
intshifDown(CBin*bin),
introtateClockwise(CBin*bin),
intcheckCollision(CBin*bin),
voidoperator>
>
(unsignedchar**binImage),
voidputAtTop(unsignedintnewOrient,unsignedintnewPosX).
第二步:
仿照CIBrick,完成CLBrick,CSBrick,CTBrick,CFSBrick,CFLBrick,COBrick类的实现,同样将类定义代码写在brick.h文件中,类实现代码写在brick.h文件中.
3.进一步完善
(1)实现砖块的三维化
在试图类定义(NewTetrisView.h)中添加两个公有的成员函数声明。
在视图类的实现文件(NewTetrisView.cpp)中,添加这两个函数的实现代码。
修改试图类的DrawImage函数,添加代码。
(2)使用双缓冲技术解决屏幕闪烁
修改视图类的OnDraw函数。
修改视图类的DrawImage函数。
4.背景音乐的实现。
三、代码实现(代码省略部分参照实验指导书)
1.然后分别在bin.h和bin.cpp中完成Cbin类的定义和函数实现:
在文件bin.h中添加三个成员变量和六个成员函数并初始化(代码省略)
unsignedchar**imgage;
unsignedintwidth;
unsignedintheight;
CBin(unsignedintw,unsignedinth);
~CBin();
unsignedintgetWidth(){returnwidth;
};
unsignedintgetHeight(){returnheight;
voidgetImage(unsignedchar**destImage);
voidsetImage(unsignedchar**srcImage);
unsignedintremoveFullLines();
};
CBrick类的说明如下(文件brick.h):
classCBrick{
protected:
unsignedintorientation;
//表示砖块的状态
unsignedintposX;
//砖块特定点的x坐标
unsignedintposY;
//砖块特定点的y坐标
unsignedchaicolour;
//砖块的颜色
public:
unsignedintgetOrientation(){returnOrientation;
unsignedintgetPosX(){returnposX;
unsignedintgetPosY(){returnposY;
unsignedchargetColour(){returncolour;
voidsetOrientation(unsignedintnewOrient){orientation=newOrient;
voidsetPosX(unsignedintnewPosX){posX=newPosX;
voidsetPosY(unsignedintnewPosY){posY=newPosY;
voidsetColour(unsignedcharnewColour){colour=newColour;
virtualintshiftLeft(CBin*bin)=0;
virtualintshiftRight(CBin*bin)=0;
virtualintshiftDown(CBin*bin)=0;
virtualintrotateClockwise(CBin*bin)=0;
virtualintcheckCollision(CBin*bin)=0;
virtualvoidoperator>
(unsignedchar**binImage)=0;
virtualvoidputAtTop(unsignedintnewOrient,unsignedintnewPosX)=0;
}
◆CIBrick需要重载7个虚函数,CIBrick类的定义如下(文件brick.h):
classCIBrick:
publicCBrick
{public:
intshiftLeft(CBin*bin);
intshiftRight(CBin*bin);
intshiftDown(CBin*bin);
introtateClockwise(CBin*bin);
intcheckCollision(CBin*bin);
voidoperator>
(unsignedchar**binImage);
voidputAtTop(unsignedintnewOrient,unsignedintnewPosX);
在后面实现CLBrick,CSBrick,CTBrick,CFSBrick,CFLBrick,COBrick的6种方块实现中按照上面的步骤重载7个上述虚函数。
后面只将各个类型方块实现的代码列出来。
//I型砖块代码的实现部分:
CIBrick类型砖块:
(黑色方块为程序中所选的坐标点)
CIBrick类的实现代码(代码省略)
//L型砖块代码的实现部分:
CLBrick类型砖块:
(左移、右移、上键、下键即以下4个消息响应函数与I型砖块代码的实现部分相同,这里就不列出来了)
◆shiftLeft(CBin*bin)◆shiftDown(CBin*bin)◆shiftRight(CBin*bin)◆rotateClockwise(CBin*bin)
intCLBrick:
:
checkCollision(CBin*bin)
{unsignedintwidth;
unsignedchar**image;
unsignedintorientation;
unsignedintposX;
unsignedintposY;
width=bin->
getWidth();
height=bin->
getHeight();
image=newunsignedchar*[height];
for(unsignedinti=0;
i<
height;
i++)
{
image[i]=newunsignedchar[width];
}
bin->
getImage(image);
orientation=getOrientation();
posX=getPosX();
posY=getPosY();
if(orientation==0)
{if((posX+1<
1)||(posX+1>
width-1)||(posY<
2)||(posY>
height-1))
return0;
if((image[posY-2][posX]!
=0)||(image[posY-1][posX]!
=0)||(image[posY][posX]!
=0)||(image[posY][posX+1]!
=0))
if(orientation==1)
1)||(posX+2>
width-1)||(posY+1<
1)||(posY+1>
if((image[posY][posX]!
=0)||(image[posY][posX+2]!
=0)||(image[posY+1][posX]!
if(orientation==2)
{if((posX<
1)||(posX>
1)||(posY+2>
if((image[posY][posX-1]!
=0)||(image[posY+2][posX]!
if(orientation==3)
2)||(posX>
1)||(posY>
if((image[posY][posX-2]!
=0)||(image[posY][posX-1]!
return1;
voidCLBrick:
operator>
(unsignedchar**binImage)
{unsignedintorientation;
unsignedcharcolour;
colour=getColour();
{binImage[posY-2][posX]=colour;
binImage[posY-1][posX]=colour;
binImage[posY][posX]=colour;
binImage[posY][posX+1]=colour;
{binImage[posY][posX]=colour;
binImage[posY][posX+2]=colour;
binImage[posY+1][posX]=colour;
binImage[posY][posX-1]=colour;
binImage[posY+2][posX]=colour;
{binImage[posY][posX-1]=colour;
binImage[posY][posX-2]=colour;
putAtTop(unsignedintnewOrient,unsignedintnewPosX)
{setPosX(newPosX);
setOrientation(newOrient);
switch(newOrient)
{case0:
setPosY
(2);
break;
case1:
setPosY(0);
case2:
case3:
setPosY
(1);
CSBrick类型砖块:
//S型砖块代码的实现部分:
(左移、右移、上键、下键消息响应函数与I型砖块代码的实现部分相同,这里就不列出来了)
intCSBrick:
if((posX<
if((image[posY][posX]!
=0)||(image[posY+1][posX-1]!
if((image[posY-1][posX-1]!
if((image[posY][posX-1]!
=0)||(image[posY-1][posX+1]!
if((posX+1<
if((image[posY-1][posX]!
=0)||(image[posY+1][posX+1]!
voidCSBrick:
operator>
(unsignedchar**binImage)
binImage[posY+1][posX-1]=colour;
{binImage[posY-1][posX-1]=colour;
{binImage[posY-1][posX+1]=colour;
{binImage[posY-1][posX]=colour;
binImage[posY+1][posX+1]=colour;
//T型砖块代码的实现部分:
CTBrick类型砖块:
intCTBrick:
if((image[posY-1][posX]!
width-