www.pudn.com > russion_vc.zip > Player.cpp
// Player.cpp: implementation of the Player class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Russion.h"
#include "Player.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
int Pane_Backup[7][4][4]={ 0,0,1,0,
0,0,1,0,
0,0,1,0,
0,0,1,0,
2,2,0,0,
2,2,0,0,
0,0,0,0,
0,0,0,0,
0,0,3,0,
0,3,3,0,
0,3,0,0,
0,0,0,0,
4,0,0,0,
4,4,0,0,
0,4,0,0,
0,0,0,0,
0,5,0,0,
5,5,5,0,
0,0,0,0,
0,0,0,0,
6,6,0,0,
0,6,0,0,
0,6,0,0,
0,0,0,0,
7,0,0,0,
7,7,7,0,
0,0,0,0,
0,0,0,0 };
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Player::Player()
{
lu.x=50;
lu.y=108;
rd.x=50+24*10;
rd.y=50+24*18;
Marks=0;
Step=1;
speed=1;
Active=false;
}
Player::~Player()
{
}
bool Player::Turn()
{
//转动
TRACE("\n转动\n");
// if(TurnCheck())
switch (mPane.list)
{
case 0:
{
int i,j;
int buf[4][4];
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
{
buf[j][3-i]=mPane.matrix[i][j];
if(mPane.matrix[i][j]!=0)
if(Back[j+mPane.x][3-i+mPane.y]!=0)
return false;
}
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
mPane.matrix[i][j]=buf[i][j];
}
break;
case 1:
{
int i,j;
int buf[4][4];
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
{
buf[j][1-i]=mPane.matrix[i][j];
if(mPane.matrix[i][j]!=0)
if(Back[j+mPane.x][1-i+mPane.y]!=0)
return false;
}
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
mPane.matrix[i][j]=buf[i][j];
}
break;
default:
{
int i,j;
int buf[4][4];
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
buf[j][2-i]=mPane.matrix[i][j];
if(mPane.matrix[i][j]!=0)
if(Back[j+mPane.x][2-i+mPane.y]!=0)
return false;
}
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
mPane.matrix[i][j]=buf[i][j];
}
break;
}
mPane.lastx=mPane.x;
mPane.lasty=mPane.y;
mPane.lastoffset=mPane.offset;
return true;
}
bool Player::TurnCheck()
{
//检查是否能转动
for(int i=0; i<=3; i++)
for(int j=0; j<=3; j++)
if(mPane.matrix[i][j]!=0)
{
if(Back[j+mPane.x][3-i+mPane.y]!=0)
{
TRACE("\nTurn failed\n");
TRACE(" detect matrix (i: %d ,j: %d )(x: %d ,y: %d ) , Back( %d , %d ) %d\n",i,j,mPane.x,mPane.y,(j+mPane.x),(3-i+mPane.y),Back[j+mPane.x][3-i+mPane.y]);
for(j=0;j<=MAXY+1;j++)
{
TRACE("\n");
for(i=0;i<=MAXX+1;i++)
TRACE("%d ",Back[i][j]);
}
return false;
}
}
return true;
}
bool Player::DownStep()
{
//向下移动一象素, 返回bool为无法移动
TRACE("\n Player::向下移动\n");
if(mPane.offset>=PANE_LENGTH)
{
for(int j=3;j>=0;j--)
for(int i=3;i>=0;i--)
if( mPane.matrix[i][j]!=0 )
if(Back[mPane.x+i][mPane.y+j+2]!=0) return false;
mPane.lastx=mPane.x;
mPane.lasty=mPane.y;
mPane.lastoffset=mPane.offset;
mPane.y++;
mPane.offset=0;
TRACE("\n移动一格\n");
}
else
{
TRACE("\n移动一象素\n");
mPane.lastx=mPane.x;
mPane.lasty=mPane.y;
mPane.lastoffset=mPane.offset;
mPane.offset++;
}
return true;
}
bool Player::DownLine()
{
//向下移动一象素, 返回bool为无法移动
TRACE("\n Player::向下移动\n");
if(mPane.offset!=0)
{
mPane.lasty=mPane.y;
mPane.y++;
mPane.lastoffset=mPane.offset;
mPane.offset=0;
TRACE("\n移动一格\n");
for(int j=0;j<=3;j++)
for(int i=0;i<=3;i++)
if( mPane.matrix[i][j]!=0 )
if(Back[mPane.x+i][mPane.y+j+1]!=0)
{
TRACE("\nDown failed\n");
return false;
}
}
else
{
for(int j=0;j<=3;j++)
for(int i=0;i<=3;i++)
if( mPane.matrix[i][j]!=0 )
{
TRACE(" detect matrix ( %d , %d ) , Back( %d , %d )",i,j,mPane.x+i,mPane.y+j+1);
if(Back[mPane.x+i][mPane.y+j+1]!=0)
{
TRACE("\nDown failed\n");
return false;
}
}
mPane.lastx=mPane.x;
mPane.lasty=mPane.y;
mPane.lastoffset=mPane.offset;
mPane.y++;
mPane.offset=0;
TRACE("\n移动一格\n");
}
return true;
}
bool Player::Left()
{
//向左移动一个
TRACE("\n Player::向左移动一个\n");
int add;
if(mPane.offset==0) add=0;
else add=1;
for(int j=3;j>=0;j--)
for(int i=3;i>=0;i--)
if( mPane.matrix[i][j]!=0 )
if(Back[i-1+mPane.x][j+mPane.y]!=0 || Back[i-1+mPane.x][j+add+mPane.y]!=0)
{
TRACE("\nLeft failed\n");
return false;
}
mPane.lastx=mPane.x;
mPane.lasty=mPane.y;
mPane.lastoffset=mPane.offset;
mPane.x--;
return true;
}
bool Player::Right()
{
mPane.lastx=mPane.x;
mPane.lasty=mPane.y;
//向右移动一格
TRACE("\n Player::向右移动一格\n");
int add;
if(mPane.offset==0) add=0;
else add=1;
{
for(int j=3;j>=0;j--)
for(int i=3;i>=0;i--)
if( mPane.matrix[i][j]!=0 )
if(Back[i+1+mPane.x][j+mPane.y]!=0 || Back[i+1+mPane.x][j+add+mPane.y]!=0)
{
TRACE("\nRight failed\n");
return false;
}
mPane.lastx=mPane.x;
mPane.lasty=mPane.y;
mPane.lastoffset=mPane.offset;
mPane.x++;
return true;
}
}
void Player::Stop()
{
Active=false;
for (int i=0;i<=3;i++)
for (int j=0;j<=3;j++)
if(mPane.matrix[i][j]!=0)
Back[i+mPane.x][j+mPane.y]=mPane.matrix[i][j];
}
int Player::Clear()
{
//检查是否消去,返回消去的行数,-1为错误
bool clear=false;
int num=0;
int y=0;
for( y = mPane.y-1; y<=((mPane.y+3)1;j--)
for(int i=1;i<=MAXX;i++)
{
Back[i][j]=Back[i][j-1];
}
for(int i=1;i<=MAXX;i++)
Back[i][1]=0;
num++;
}
clear=true;
}
switch(num)
{
case 0:
return 0;
case 1:
Marks+=10;
break;
case 2:
Marks+=20;
break;
case 3:
Marks+=40;
break;
case 4:
Marks+=70;
break;
default:
TRACE("\nError in clear !\n");
return -1;
}
return num;
// redraw
}
void Player::InitNewPane()
{
//初始化一个新的方块
TRACE("\n Player::初始化一个新的方块\n");
mPane.x=3;
mPane.y=1;
mPane.lastx=3;
mPane.lasty=1;
mPane.offset=0;
mPane.lastoffset=0;
mPane.list=pre.list;
int i=0,j=0;
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
mPane.matrix[i][j]=pre.matrix[i][j];
CTime t;
CTime time=t.GetCurrentTime();
pre.list=(time.GetSecond()) % 7;
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
pre.matrix[i][j]=Pane_Backup[pre.list][i][j];
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
if(mPane.matrix[i][j]!=0)
if(Back[3+i][j]!=0)
{
GameOver();
return;
};
Active=true;
}
void Player::GetPoint(int PaneX,int PaneY,int& PixX,int& PixY)
{
PixX=(PaneX-1)*MICRO_PANE_LENGTH*PANE_LENGTH+lu.x;
PixY=(PaneY-1)*MICRO_PANE_LENGTH*PANE_LENGTH+lu.y;
// PixX=(mPane.x-1)*MICRO_PANE_LENGTH*PANE_LENGTH+lu.x;
// PixY=(mPane.y-1)*MICRO_PANE_LENGTH*PANE_LENGTH+lu.y+mPane.offset*MICRO_PANE_LENGTH;
}
/*
void Player::ReDrawPane()
{
}
*/
void Player::InitGame()
{
TRACE("\nGAME START !!!\n");
Marks=0;
//背景置0
int i,j;
for(j=0;j<=MAXY;j++)
for(i=0;i<=MAXX;i++)
Back[i][j]=0;
//底壁置1
for(i=0;i<=MAXX;i++)
Back[i][MAXY+1]=1;
//左右壁置1
for(j=0;j<=MA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