1、这里我们定义一个CBin类描述Tetris游戏的矩形框。对矩形框进行分析,它应该有三个私有的数据成员为:image,width和height.Cbin类将Tetris游戏的矩形框描述成为一个二维数组image,变量width和height存储了image的维数。接下来为CBin类添加五个成员函数:构造函数:CBin(unsigned int w,unsigned int h),析构函数:Cbin(), void getImage(unsigned char*destImage), void setImage(unsigned char*srcImage),unsigned int remove
2、FullIines(). 然后分别在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记录了特定点的坐标, c
3、olour为砖块的颜色值. 为CIBrick重载7个虚构函数:int shifLeft(CBin*bin), int shifRight(CBin*bin),int shifDown(CBin*bin), int rotateClockwise(CBin*bin), int checkCollision (CBin*bin),void operator(unsigned char*binImage),void putAtTop(unsigned int newOrient,unsigned int new PosX).第二步:仿照CIBrick,完成CLBrick, CSBrick,CTBri
4、ck, CFSBrick,CFLBrick, COBrick类的实现,同样将类定义代码写在brick.h文件中,类实现代码写在brick.h文件中.3. 进一步完善(1)实现砖块的三维化在试图类定义(NewTetrisView .h)中添加两个公有的成员函数声明。在视图类的实现文件(NewTetrisView .cpp)中,添加这两个函数的实现代码。修改试图类的DrawImage函数,添加代码。(2) 使用双缓冲技术解决屏幕闪烁 修改视图类的OnDraw函数。修改视图类的DrawImage函数。4背景音乐的实现。三、 代码实现(代码省略部分参照实验指导书)1. 然后分别在bin.h和bin.c
5、pp中完成Cbin类的定义和函数实现:在文件bin.h中添加三个成员变量和六个成员函数并初始化(代码省略) unsigned char*imgage; unsigned int width; unsigned int height; CBin(unsigned int w,unsigned int h); CBin(); unsigned int getWidth()return width;; unsigned int getHeight()return height; void getImage(unsigned char*destImage); void setImage(unsigne
6、d char*srcImage); unsigned int removeFullLines(); ; CBrick 类的说明如下(文件brick.h):class CBrickprotected:unsigned int orientation;/表示砖块的状态unsigned int posX; /砖块特定点的x坐标unsigned int posY; /砖块特定点的y坐标unsigned chai colour; /砖块的颜色public: unsigned int getOrientation()return Orientation; unsigned int getPosX()ret
7、urn posX; unsigned int getPosY()return posY; unsigned char getColour()return colour; void setOrientation(unsigned int newOrient)orientation=newOrient; void setPosX(unsigned int newPosX)posX=newPosX; void setPosY(unsigned int newPosY)posY=newPosY; void setColour(unsigned char newColour)colour=newColo
8、ur; virtual int shiftLeft(CBin* bin)=0; virtual int shiftRight(CBin* bin)=0; virtual int shiftDown(CBin* bin)=0; virtual int rotateClockwise(CBin* bin)=0; virtual int checkCollision(CBin* bin)=0; virtual void operator(unsigned char* binImage)=0; virtual void putAtTop(unsigned int newOrient,unsigned
9、int newPosX)=0; CIBrick需要重载7个虚函数,CIBrick类的定义如下(文件brick .h):class CIBrick:public CBrickpublic: int shiftLeft(CBin*bin); int shiftRight(CBin*bin); int shiftDown(CBin*bin); int rotateClockwise(CBin*bin); int checkCollision(CBin*bin); void operator(unsigned char*binImage); void putAtTop(unsigned int new
10、Orient,unsigned int newPosX);在后面实现CLBrick, CSBrick,CTBrick, CFSBrick,CFLBrick, COBrick的6种方块实现中按照上面的步骤重载7个上述虚函数。后面只将各个类型方块实现的代码列出来。/I型砖块代码的实现部分:CIBrick类型砖块:(黑色方块为程序中所选的坐标点)CIBrick类的实现代码(代码省略)/L型砖块代码的实现部分:CLBrick类型砖块:(左移、右移、上键、下键即以下4个消息响应函数与I型砖块代码的实现部分相同,这里就不列出来了)shiftLeft(CBin*bin)shiftDown(CBin*bin)
11、shiftRight(CBin*bin)rotateClockwise(CBin*bin)int CLBrick:checkCollision(CBin *bin)unsigned int width; unsigned char*image; unsigned int orientation; unsigned int posX; unsigned int posY; width=bin-getWidth(); height=bin-getHeight(); image=new unsigned char*height; for(unsigned int i=0;igetImage(imag
12、e); orientation=getOrientation(); posX=getPosX(); posY=getPosY(); if(orientation=0) if(posX+1width-1)|(posYheight-1) return 0; if(imageposY-2posX!=0)|(imageposY-1posX!=0)|(imageposYposX!=0)|(imageposYposX+1!=0) if(orientation=1)1)|(posX+2width-1)|(posY+1 if(imageposYposX!=0)|(imageposYposX+2!=0)|(im
13、ageposY+1posX! if(orientation=2) if(posX1)|(posY+2 if(imageposYposX-1!=0)|(imageposY+2posX! if(orientation=3)2)|(posX1)|(posY if(imageposYposX-2!=0)|(imageposYposX-1! return 1;void CLBrick:operator(unsigned char* binImage)unsigned int orientation; unsigned char colour; colour=getColour(); binImagepo
14、sY-2posX=colour; binImageposY-1posX=colour; binImageposYposX=colour; binImageposYposX+1=colour; binImageposYposX=colour; binImageposYposX+2=colour; binImageposY+1posX=colour; binImageposYposX-1=colour; binImageposY+2posX=colour; binImageposYposX-1=colour; binImageposYposX-2=colour;putAtTop(unsigned
15、int newOrient, unsigned int newPosX)setPosX(newPosX); setOrientation(newOrient); switch(newOrient) case 0: setPosY(2);break; case 1: setPosY(0); case 2: case 3: setPosY(1); CSBrick类型砖块:/S型砖块代码的实现部分:(左移、右移、上键、下键消息响应函数与I型砖块代码的实现部分相同,这里就不列出来了)int CSBrick: if(posX if(imageposYposX!=0)|(imageposY+1posX-1
16、! if(imageposY-1posX-1! if(imageposYposX-1!=0)|(imageposY-1posX+1!if(posX+1(unsigned char *binImage) binImageposY+1posX-1=colour; binImageposY-1posX-1=colour; binImageposY-1posX+1=colour; binImageposY-1posX=colour; binImageposY+1posX+1=colour;/T型砖块代码的实现部分:CTBrick类型砖块:int CTBrick: if(imageposY-1posX!width-
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1