双人版俄罗斯方块游戏实验报告Word文档格式.docx
《双人版俄罗斯方块游戏实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《双人版俄罗斯方块游戏实验报告Word文档格式.docx(25页珍藏版)》请在冰豆网上搜索。
(5)UnsignedintremoveFullLines()
检查image,如果任何一行完全填满,则删除这一行,并让上面一行的数据下移,返回删除的总行数。
设计代码如下:
classCBin{
private:
unsignedchar**image;
unsignedintwidth;
unsignedintheight;
public:
CBin(unsignedintw,unsignedinth);
~CBin();
unsignedintgetWidth(){returnwidth;
};
unsignedintgetHeight(){returnheight;
voidgetImage(unsignedchar**destImage);
voidsetImage(unsignedchar**srcImage);
unsignedintremoveFullLines();
};
CBin:
:
CBin(unsignedintw,unsignedinth)
{
width=w;
height=h;
image=newunsignedchar*[height];
for(unsignedinti=0;
i<
height;
i++)
{
image[i]=newunsignedchar[width];
for(unsignedintj=0;
j<
width;
j++)
image[i][j]=0;
}
}
CBin:
~CBin()
for(unsignedinti=0;
i++){
deleteimage[i];
delete[]image;
voidCBin:
getImage(unsignedchar**destImage)
destImage[i][j]=image[i][j];
setImage(unsignedchar**srcImage)
image[i][j]=srcImage[i][j];
unsignedintCBin:
removeFullLines()
unsignedintflag,EmptyLine=0;
unsignedinti,j,m;
for(i=0;
flag=0;
for(j=0;
{
if(image[i][j]==0)
flag=1;
}
//一行完全被填满
if(flag==0)
for(j=0;
{
image[i][j]=0;
}
//消除整行
for(m=i;
m>
0;
m--)
for(j=0;
{
image[m][j]=image[m-1][j];
}
for(j=0;
image[0][j]=0;
EmptyLine++;
//记录消的行数
i--;
returnEmptyLine;
CBrick类的成员函数:
(1)intshiftLeft(CBin*bin
将砖块在游戏的矩形框内左移一位。
注意座椅可能产生冲突,左边有赚快活碰到墙,如果不能左移返回0值,如果能左移返回1值。
(2)intshiftRight(CBin*bin)
同shiftLeft,右移。
(3)intshiftDown(CBin*bin)
同shiftLeft,下移。
(4)introtateClockwise(CBin*bin)
同shiftLeft,顺时针旋转。
(5)intcheckCollision(CBin*bin)
检查移动或旋转过程中是否有冲突,有冲突返回0,否则返回1。
(6)intcheckCollision(CBin*bin)
重载运算符>
>
,通过设置映射到游戏矩形的二维数组binImage,设置砖块的颜色,这里假设binimage是一个合法的大侠合适的二维数组。
(7)voidoperator>
(unsignedchar*binimage)
(8)putAtTop(unsignedintnewOrient,unsignedintnewPosX)
通过设置砖块特定点的Y坐标(posY)是的砖块位于矩形的最上方,砖块的X坐标和砖块的状态由newPosX和newOrient确定。
注意posY的值依赖于砖块的状态
(9)putAtMid(unsignedintnewOrient,unsignedintnewPosX)
与putAtTop一致,只是将坐标设置在面板中间。
下面是一个具体的砖块设计代码:
classCCBrick:
publicCBrick{
intshiftLeft(CBin*bin);
intshiftRight(CBin*bin);
intshiftDown(CBin*bin);
introtateClockwise(CBin*bin);
intcheckCollision(CBin*bin);
voidoperator>
(unsignedchar**binImage);
voidputAtTop(intnewOrient,intnewPosX);
voidputAtMid(intnewOrient,intnewPosX);
/////////////////////////
intCCBrick:
shiftLeft(CBin*bin)
intposX;
posX=getPosX();
setPosX(posX-1);
if(checkCollision(bin)==0){
setPosX(posX);
return0;
return1;
shiftDown(CBin*bin)
intposY;
posY=getPosY();
setPosY(posY+1);
setPosY(posY);
shiftRight(CBin*bin)
setPosX(posX+1);
if(checkCollision(bin)==0){
setPosX(posX);
rotateClockwise(CBin*bin)
intorientation,oldOrientation;
orientation=getOrientation();
oldOrientation=orientation;
if(orientation==3)
orientation=0;
else
orientation=orientation+1;
setOrientation(orientation);
setOrientation(oldOrientation);
checkCollision(CBin*bin)
intwidth;
intheight;
intorientation;
width=bin->
getWidth();
height=bin->
getHeight();
for(inti=0;
bin->
getImage(image);
if(orientation==0)
if((posX<
0)||
((posX+1)>
width-1)||
(posY<
1)||
(posY+1>
height-1))
return0;
if((image[posY-1][posX]!
=0)||
(image[posY-1][posX+1]!
(image[posY][posX]!
(image[posY+1][posX]!
(image[posY+1][posX+1]!
=0))
if(orientation==1)
if((posX<
((posY+1)>
if((image[posY+1][posX+1]!
(image[posY][posX+1]!
(image[posY][posX-1]!
(image[posY+1][posX-1]!
if(orientation==2)
(posX>
if((image[posY-1][posX-1]!
(image[posY-1][posX]!
if(orientation==3)
(posX+1>
((posY)>
if((image[posY-1][posX+1]!
(image[posY-1][posX-1]!
voidCCBrick:
operator>
(unsignedchar**binImage)
unsignedcharcolour;
colour=getColour();
binImage[posY-1][posX+1]=colour;
binImage[posY-1][posX]=colour;
binImage[posY][posX]=colour;
binImage[posY+1][posX]=colour;
binImage[posY+1][posX+1]=colour;
binImage[posY][posX+1]=colour;
binImage[posY][posX-1]=colour;
binImage[posY+1][posX-1]=colour;
}
binImage[posY-1][posX-1]=colour;
binImage[posY][posX]=colour;
binImage[posY-1][posX]=colour;
binImage[posY-1][posX+1]=colour;
putAtTop(intnewOrient,intnewPosX)
setPosX(newPosX);
setOrientation(newOrient);
switch(newOrient)
case0:
setPosY
(1);
break;
case1:
setPosY(0);
case2:
case3:
putAtMid(intnewOrient,intnewPosX)
setPosX(newPosX-3);
setPosY(9);
setPosY(8);
(1).定义相关的变量并在构造函数中初始化。
(2).添加DrawImage(CBin*bin,unsignedchar**image,CDC*pDC)函数,用来绘制游戏砖块。
这其中要绘制四个面板,传递四个参数。
两个主面板,两个与侧面板。
部分代码如下:
voidCNewTerisView:
DrawImage(CBin*bin,CBin*bin1,CBin*bin2,CBin*bin3,unsignedchar**image,unsignedchar**image1,unsignedchar**image2,unsignedchar**image3,CDC*pDC)
CRectrect;
//创建背景位图画刷
CBitmapbitmap;
bitmap.LoadBitmap(IDB_BITMAP24);
CBrushbrush;
brush.CreatePatternBrush(&
bitmap);
GetClientRect(&
rect);
pDC->
FillRect(rect,&
brush);
//右边玩家分数的输出
CRectre;
FillSolidRect(re,RGB(210,255,255));
charbuf[100];
sprintf(buf,"
%d"
numLines*10);
TextOut(330,90,buf);
//左边玩家分数的输出
CRectre1;
FillSolidRect(re1,RGB(230,255,100));
charbuf1[100];
sprintf(buf1,"
numLines1*10);
TextOut(440,90,buf1);
CRectrc,rc1,rc2,rc3;
//将载入的位图资源放在数组中
intBit[8]={IDB_BITMAP11,IDB_BITMAP12,IDB_BITMAP13,IDB_BITMAP14,IDB_BITMAP15,IDB_BITMAP16,IDB_BITMAP17,IDB_BITMAP18};
intb,b1,b2;
for(i=0;
i<
height1;
i++)
for(j=0;
j<
width1;
j++)
rc1=CRect(j*nSize+300,i*nSize,(j+1)*nSize+300,(i+1)*nSize);
//矩形的区域
if(image1[i][j]!
=0)
{
b=image1[i][j];
b1=b;
CBitmapbitmap1;
//左边玩家预测砖块的位图填充
bitmap1.LoadBitmap(Bit[b]);
bitmap1.SetBitmapDimension(20,20);
CBrushbrush1;
brush1.CreatePatternBrush(&
bitmap1);
pDC->
FillRect(rc1,&
brush1);
rc=CRect(j*nSize+80,i*nSize,(j+1)*nSize+80,(i+1)*nSize);
if(image[i][j]!
b=image[i][j];
//左边玩家主面板砖块的位图填充
FillRect(rc,&
(3).在菜单中添加“开始”、“难度”、“暂停”、“游戏模式”等菜单,
A.为“开始”菜单添加消息响应函数。
初始化面板,并设置定时器。
代码如下:
voidCNewTerisView:
OnGameStart()
//TODO:
Addyourcommandhandlercodehere
gameOver=0;
brickInFlight=0;
numLines=0;
//------------
gameOver1=0;
brickInFlight1=0;
numLines1=0;
for(unsignedinti=0;
20;
for(unsignedintj=0;
10;
outputImage[i][j]=0;
outputImage1[i][j]=0;
outputImage2[i][