📄 game.cpp
字号:
}
void CGame::PutPannel2(void)
{
//To do:
int x,t;
RECT rect;
rect.left=480;
rect.right=640;
rect.top=0;
rect.bottom=480;
pScreen->ClearDDSurface(&rect,0);
pScreen->PutSpt(500,50,62);
for(x=0;x<player1->GetLife()-1;x++)
pScreen->PutSpt(550+x*15,52,66);
if(player1->GetLife()<=0)
pScreen->PutSpt(550,53,85);
pScreen->PutSpt(500,75,64);
t=player1->GetTotal();
for(x=5;x>0;x--,t/=10)
pScreen->PutSpt(550+x*12,78,t%10+49);
if(twoplayer)
{
pScreen->PutSpt(500,150,63);
for(x=0;x<player2->GetLife()-1;x++)
pScreen->PutSpt(550+x*15,152,66);
if(player2->GetLife()<=0)
pScreen->PutSpt(550,153,85);
pScreen->PutSpt(500,175,64);
t=player2->GetTotal();
for(x=5;x>0;x--,t/=10)
pScreen->PutSpt(550+x*12,178,t%10+49);
}
pScreen->PutSpt(500,300,65);
pScreen->PutSpt(580,325,numEnemy/10+49);
pScreen->PutSpt(595,325,numEnemy%10+49);
pScreen->PutSpt(500,400,47);
pScreen->PutSpt(522,405,Current/10+49);
pScreen->PutSpt(537,405,Current%10+49);
pScreen->PutSpt(552,400,48);
pScreen->PutSpt(540,460,89);
}
void CGame::OnMapEdit()
{
//To do:
int i,j;
if(pScreen->BeReady()==false)
{
pScreen->RestoreDD();
return;
}
switch(Occasion)
{
case 2:
if(!BeModified()) break;
pScreen->ClearDDSurface(NULL,0);
PutPannel();
for(i=0;i<32;i++)
for(j=0;j<32;j++)
if(*(CurrMap+i+(j<<5))) pScreen->PutSpt(i*15,j*15,*(CurrMap+i+(j<<5)));
pScreen->PutSpt(12*15,30*15,1);
pScreen->PutSpt(18*15,30*15,9);
pScreen->PutSpt(0,0,21);
pScreen->PutSpt(15*15,0,21);
pScreen->PutSpt(30*15,0,21);
/* if(LClick==1) pScreen->PutSpt(485,409,46);
else pScreen->PutSpt(485,409,41);
if(LClick==2) pScreen->PutSpt(559,409,47);
else pScreen->PutSpt(559,409,42);
if(LClick==3) pScreen->PutSpt(485,444,44);
else pScreen->PutSpt(485,444,39);
if(LClick==4) pScreen->PutSpt(559,444,45);
else pScreen->PutSpt(559,444,40);
*/
if(MouseStyle&&MouseX<480) pScreen->PutSpt(MouseX/15*15,MouseY/15*15,MouseStyle);
pScreen->PutSpt(MouseX,MouseY,46);
pScreen->Flip();
SetModify(false);
break;
case 6:
/* timer++;
if(timer>=32)
{
timer=0;
Occasion=2;
LClick=0;
SetModify();
break;
}
pScreen->lpDDPalette->SetEntries(0,(timer-1)<<3,8,pScreen->DefaultPE+((timer-1)<<3));
*/
PALETTEENTRY temp[256];
for(i=0;i<256;i++)
temp[i].peBlue=temp[i].peFlags=temp[i].peGreen=temp[i].peRed=0;
for(i=0;i<64;i++)
{
for(j=0;j<256;j++)
{
if(temp[j].peBlue>pScreen->DefaultPE[j].peBlue-4) temp[j].peBlue=pScreen->DefaultPE[j].peBlue;
else temp[j].peBlue+=4;
if(temp[j].peGreen>pScreen->DefaultPE[j].peGreen-4) temp[j].peGreen=pScreen->DefaultPE[j].peGreen;
else temp[j].peGreen+=4;
if(temp[j].peRed>pScreen->DefaultPE[j].peRed-4) temp[j].peRed=pScreen->DefaultPE[j].peRed;
else temp[j].peRed+=4;
}
pScreen->lpDD->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN,NULL);
pScreen->lpDDPalette->SetEntries(0,0,256,temp);
}
timer=0;
Occasion=2;
LClick=0;
SetModify(true);
break;
case 9:
/* PALETTEENTRY temp[256];
int i,j;
*/ pScreen->lpDDPalette->GetEntries(0,0,256,temp);
for(i=0;i<64;i++)
{
for(j=0;j<256;j++)
{
if(temp[j].peBlue<4) temp[j].peBlue=0;
else temp[j].peBlue-=4;
if(temp[j].peGreen<4) temp[j].peGreen=0;
else temp[j].peGreen-=4;
if(temp[j].peRed<4) temp[j].peRed=0;
else temp[j].peRed-=4;
}
pScreen->lpDD->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN,NULL);
pScreen->lpDDPalette->SetEntries(0,0,256,temp);
}
for(i=0;i<256;i++)
{
temp[i].peBlue=0;
temp[i].peFlags=0;
temp[i].peGreen=0;
temp[i].peRed=0;
}
pScreen->lpDDPalette->SetEntries(0,0,256,temp);
pScreen->ClearDDSurface(NULL,0);
pMap->LoadMap(MAPFILE);
Current=pMap->GetNum();
pMap->GetSpt(Current,CurrMap);
PutPannel();
for(i=0;i<32;i++)
for(j=0;j<32;j++)
if(*(CurrMap+i+(j<<5))) pScreen->PutSpt(i*15,j*15,*(CurrMap+i+(j<<5)));
pScreen->PutSpt(12*15,30*15,1);
pScreen->PutSpt(18*15,30*15,9);
pScreen->PutSpt(0,0,21);
pScreen->PutSpt(15*15,0,21);
pScreen->PutSpt(30*15,0,21);
pScreen->Flip();
LClick=0;
timer=0;
Occasion=6;
MouseStyle=0;
break;
}
}
int CGame::OnKey(void)
{
//To do:
if(KeyPGD)
{
Current++;
if(Current>pMap->GetNum()) Current=1;
Occasion=14;
return 0;
}
if(KeyPGU)
{
Current--;
if(Current<=0) Current=pMap->GetNum();
Occasion=14;
return 0;
}
if(KeyDel)
{
int i,s;
for(i=0;i<MAXENEMY;i++)
{
s=Enemy[i].GetStatus();
if(s==ST_READY||s==ST_FORWARD||s==ST_FIRE) break;
}
if(i<MAXENEMY) Enemy[i].SetStatus(ST_DYING);
}
bool p1ok=false;
if(player1->GetStatus()==ST_READY)
{
int d=player1->GetDirection();
if((Keyboard1&KEY_FIRE)&&!player1->GetBomb()->BeActive())
{
// if(player1->GetStatus()==ST_READY)
player1->SetStatus(ST_FIRE);
p1ok=true;
}
if(!p1ok&&(Keyboard1&KEY_UP))
{
if(d==0) player1->SetStatus(ST_FORWARD);
else player1->SetDirection(0);
// Keyboard1=0;
p1ok=true;
}
if(!p1ok&&(Keyboard1&KEY_LEFT))
{
if(d==1) player1->SetStatus(ST_FORWARD);
else player1->SetDirection(1);
// Keyboard1=0;
p1ok=true;
}
if(!p1ok&&(Keyboard1&KEY_DOWN))
{
if(d==2) player1->SetStatus(ST_FORWARD);
else player1->SetDirection(2);
// Keyboard1=0;
p1ok=true;
}
if(!p1ok&&(Keyboard1&KEY_RIGHT))
{
if(d==3) player1->SetStatus(ST_FORWARD);
else player1->SetDirection(3);
// Keyboard1=0;
p1ok=true;
}
if(!p1ok&&player1->GetStatus()!=ST_DEAD) player1->SetStatus(ST_READY);
}
if(!twoplayer||player2->GetLife()<=0) return 1;
// if(player2->GetStatus()!=ST_READY) return;
int direction;
if((Mouse2&KEY_FIRE)&&(!player2->GetBomb()->BeActive()))
{
player2->SetStatus(ST_FIRE);
// Mouse2&=239;
return 1;
}
if(Mouse2==KEY_FORWARD)
{
// dx=MouseX-OldX;dy=MouseY-OldY;
int tx,ty;
tx=OldX-player2->GetX();
ty=OldY-player2->GetY();
if(abs(dx)>=abs(dy))
if(dx>0) direction=3;
else direction=1;
else if(dy>0) direction=2;
else direction=0;
// MouseMove=false;
if(player2->GetDirection()!=direction)
{
player2->SetDirection(direction);
return 1;
}
else
{
if(!player2->Able(direction))
{
player2->SetStatus(ST_READY);
Mouse2=0;
return 1;
}
switch(direction)
{
case 0:
if(ty>=0)
{
Mouse2=0;
return 1;
}
break;
case 1:
if(tx>=0)
{
Mouse2=0;
return 1;
}
break;
case 2:
if(ty<2)
{
Mouse2=0;
return 1;
}
break;
case 3:
if(tx<2)
{
Mouse2=0;
return 1;
}
break;
}
player2->SetStatus(ST_FORWARD);
return 1;
}
}
// else
/* if(Mouse2==KEY_FORWARD)
{
player2->SetStatus(ST_FORWARD);
return 1;
// Mouse2=0;
}
*/
return 1;
}
CGame::~CGame()
{
//To do:
}
void CGame::SetWnd(HWND hWnd)
{
hGameWnd=hWnd;
}
//Defines of the class CTank:
CTank::CTank():Life(TANKLIFE),Total(0),Status(0),autotimer(0),Power(1),OriX(-1),OriY(-1)
{
//To do:
pScreen=GetTheScreen();
pGame=GetTheGame();
pCurrMap=pGame->GetCurrMap();
pImageMap=pGame->GetCurrImage();
bomb.SetTank(this);
}
int CTank::GetLife(void)
{
//To do:
return Life;
}
int CTank::GetX(void)
{
return TankX;
}
int CTank::GetY(void)
{
return TankY;
}
void CTank::SetPosition(int x,int y)
{
TankX=x;
if(OriX<0) OriX=x;
TankY=y;
if(OriY<0) OriY=y;
}
int CTank::GetTotal(void)
{
//To do:
return Total;
}
void CTank::AddTotal(int t)
{
Total+=t;
}
void CTank::SetLife(int l)
{
Life=l;
}
void CTank::SetDirection(int d)
{
Direction=d;
}
int CTank::GetStatus(void)
{
return Status;
}
void CTank::SetStatus(int s)
{
Status=s;
}
void CTank::SetBase(int b)
{
base=b;
}
void CTank::Settime(int t)
{
timer=t;
}
int CTank::GetDirection(void)
{
return Direction;
}
void CTank::SetToBeEnemy(bool b)
{
IsEnemy=b;
}
bool CTank::BeEnemy(void)
{
return IsEnemy;
}
void CTank::ReSet(void)
{
bomb.SetActive(false);
}
void CTank::SetTotal(int t)
{
Total=t;
}
bool CTank::Able(int d)
{
//To do:
bool res=true;
unsigned char *p=pCurrMap+TankX+(TankY<<5);
unsigned char *p2=pImageMap+TankX+(TankY<<5);
switch(d)
{
case 0:
if(TankY<=0) res=false;
else if(((*(p-32)!=30)&&(*(p-32)!=32))||((*(p-31)!=30)&&(*(p-31)!=32))) res=false;
if((*(p2-32)!=0)||(*(p2-31)!=0)) res=false;
break;
case 1:
if(TankX<=0) res=false;
else if(((*(p-1)!=30)&&(*(p-1)!=32))||((*(p+31)!=30)&&(*(p+31)!=32))) res=false;
if((*(p2-1)!=0)||(*(p2+31)!=0)) res=false;
break;
case 2:
if(TankY>=30) res=false;
else if(((*(p+64)!=30)&&(*(p+64)!=32))||((*(p+65)!=30)&&(*(p+65)!=32))) res=false;
if((*(p2+64)!=0)||(*(p2+65)!=0)) res=false;
break;
case 3:
if(TankX>=30) res=false;
else if(((*(p+2)!=30)&&(*(p+2)!=32))||((*(p+34)!=30)&&(*(p+34)!=32))) res=false;
if((*(p2+2)!=0)||(*(p2+34)!=0)) res=false;
break;
}
return res;
}
void CTank::SetAutotime(int t)
{
autotimer=t;
}
CBomb* CTank::GetBomb(void)
{
return &bomb;
}
void CTank::Auto(void)
{
//To do:
int i;
if(autotimer<=0)
{
i=rand()%4;
switch(i)
{
case 0:
case 1:
//Go forward
if(Status==ST_READY) Status=ST_FORWARD;
autotimer=rand()%10;
break;
case 2:
//Change Direction
if(Status==ST_READY)
Direction=rand()%4;
break;
case 3:
//Fire
if(Status==ST_READY)
Status=ST_FIRE;
break;
}
}
else autotimer--;
Action();
}
void CTank::Action(void)
{
//To do:
if(Status==ST_DEAD) return;
unsigned char *p=pImageMap+TankX+(TankY<<5);
if(Status!=ST_SLEEP&&Status!=ST_WAKE&&Status!=ST_BORN&&Status!=ST_DYING)
{
if(*p==4||*(p+1)==4||*(p+32)==4||*(p+33)==4)
{
Status=ST_DYING;
timer=0;
}
*p=*(p+1)=*(p+32)=*(p+33)=0;
}
switch(Status)
{
case ST_DEAD:
return;
case ST_SLEEP:
timer=0;
return;
case ST_WAKE:
timer++;
if(timer>=WAITTIME)
{
Status=ST_BORN;
timer=0;
}
return;
case ST_DYING:
Life--;
pScreen->PutSpt(TankX*15,TankY*15,82);
*p=*(p+1)=*(p+32)=*(p+33)=0;
if(IsEnemy) pGame->ReduceEnemy();
if(Life<=0) Status=ST_DEAD;
else
{
Status=ST_BORN;
SetPosition(OriX,OriY);
}
return;
case ST_BORN:
timer++;
if(timer>=12)
{
Status=ST_READY;
timer=0;
}
// pScreen->PutSpt(0,0,67);
pScreen->PutSpt(TankX*15,TankY*15,67+(timer/2)%3);
return;
case ST_READY:
pScreen->PutSpt(TankX*15,TankY*15,base+(Direction<<1));
timer=0;
break;
case ST_FIRE:
pScreen->PutSpt(TankX*15,TankY*15,base+(Direction<<1));
if(bomb.BeActive()==false)
{
bomb.SetActive(true);
bomb.SetDirection(Direction);
switch(Direction)
{
case 0:
bomb.SetXY(TankX,TankY-1);
break;
case 1:
bomb.SetXY(TankX-1,TankY);
break;
case 2:
bomb.SetXY(TankX,TankY+1);
break;
case 3:
bomb.SetXY(TankX+1,TankY);
break;
}
}
Status=ST_READY;
break;
case ST_FORWARD:
if(!Able(Direction))
{
timer++;
pScreen->PutSpt(TankX*15,TankY*15,base+(Direction<<1)+timer%2);
Status=ST_READY;
break;
}
switch(Direction)
{
case 0:
if(TankY>0)
{
if(timer==0) pScreen->PutSpt(TankX*15,TankY*15-5,base+(Direction<<1)+timer%2);
if(timer==1)
{
pScreen->PutSpt(TankX*15,TankY*15-10,base+(Direction<<1)+timer%2);
TankY--;
}
if(timer==2) pScreen->PutSpt(TankX*15,TankY*15,base+(Direction<<1)+timer%2);
}
else
pScreen->PutSpt(TankX*15,TankY*15,base+(Direction<<1)+timer%2);
// timer++;
break;
case 1:
if(TankX>0)
{
if(timer==0) pScreen->PutSpt(TankX*15-5,TankY*15,base+(Direction<<1)+timer%2);
if(timer==1)
{
pScreen->PutSpt(TankX*15-10,TankY*15,base+(Direction<<1)+timer%2);
TankX--;
}
if(timer==2) pScreen->PutSpt(TankX*15,TankY*15,base+(Direction<<1)+timer%2);
}
else pScreen->PutSpt(TankX*15,TankY*15,base+(Direction<<1)+timer%2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -