#include "Game.h"
CGame::CGame()
{
time_t timer;
time(&timer);
srand(unsigned int(timer));
}
void CGame::OnInit(char * MapName)
{
m_start=true ;
m_deuce=false;
m_win =false;
m_lost =false;
m_over =false;
m_timeOld=0;
m_timer=timeGetTime();
this->m_pSBuf[PIRATE]->Play(1);
this->m_pSBuf[START]->Play();
this->DoMap(MapName);
p[0].x=(float)p[0].xmap*big;
p[0].y=(float)p[0].ymap*big;
p[0].way=0;
p[0].speed=2.0;
p[0].step=8;
p[0].counter=0;
p[0].type=1;
p[0].pin=false;
p[0].cast=false;
p[0].kick=false;
p[0].bomb=1;
p[0].power=1;
p[0].presswaykey=35;
p[0].status=free;
p[0].time=1;
p[1].x=(float)p[1].xmap*big;
p[1].y=(float)p[1].ymap*big;
p[1].way=0;
p[1].speed=2.0;
p[1].step=8;
p[1].counter=0;
p[1].type=2;
p[1].pin=false;
p[1].cast=false;
p[1].kick=false;
p[1].bomb=1;
p[1].power=1;
p[1].presswaykey=35;
p[1].status=free;
p[1].time=1;
}
bool CGame::DoMapFile(char * MapName)
{
if(MapName==NULL)
return false;
CFile file;
if( file.Open( MapName , CFile::modeRead ) )
{
file.SeekToBegin();
file.Read(maptype ,maxx*maxy*sizeof(int));
file.Read(mapstatus,maxx*maxy*sizeof(int));
file.Read(px,8*sizeof(int));
file.Read(py,8*sizeof(int));
file.Close();
}
else
return false;
for(int i=0;i<maxx;i++)
for(int j=0;j<maxy;j++)
{
table[j][i].type = maptype[j][i];
table[j][i].status=mapstatus[j][i];
}
position=new POINT[8];
for(i=0;i<8;i++)
{
position[i].x=px[i];
position[i].y=py[i];
}
return true;
}
void CGame::DoMap(char * MapName)
{
int i,j;
if(!this->DoMapFile(MapName))
{
//------------------------------------------------------------------------------------//
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
int map[maxy][maxx]={03,03,03,03,00,00,00,03,00,00,00,03,03,03,03, // 0
03,01,03,00,02,02,02,00,02,02,02,00,03,01,03, // 1
03,03,00,02,00,03,00,03,00,03,00,02,00,03,03, // 2
03,00,02,00,03,03,03,03,03,03,03,00,02,00,03, // 3
03,00,02,03,03,03,03,03,03,03,03,03,02,00,03, // 4
03,00,02,00,03,03,03,03,03,03,03,00,02,00,03, // 5
03,00,02,03,03,03,01,01,01,03,03,03,02,00,03, // 6
03,03,00,02,00,03,03,03,03,03,00,02,00,03,03, // 7
03,03,03,00,02,03,03,03,03,03,02,00,03,03,03, // 8
03,03,03,03,00,02,00,03,00,02,00,03,03,03,03, // 9
03,03,00,03,03,00,02,02,02,00,03,03,00,03,03, //10
03,01,00,03,03,03,00,00,00,03,03,03,00,01,03, //11
03,00,00,03,03,03,03,03,03,03,03,03,00,00,03, //12
};
///////////////////////////
/* 海盗船14 */
///////////////////////////
table[06][06].status=1;
table[01][01].status=2;
table[01][13].status=2;
table[11][13].status=2;
table[11][01].status=2;
POINT pos[]={{4,7},{10,7},{1,12},{13,12},{1,5},{13,5},{5,0},{9,0}};
//------------------------------------------------------------------------------------//
for(i=0;i<maxx;i++)
for(j=0;j<maxy;j++)
table[j][i].type=map[j][i];
position=pos;
}
for(i=0;i<maxx;i++)
for(j=0;j<maxy;j++)
{
table[j][i].counter=0;
table[j][i].xoffset=0;
table[j][i].yoffset=0;
table[j][i].move=false;
table[j][i].tag=0;
if(table[j][i].type!=1)
table[j][i].status=0;
if(table[j][i].type==2||table[j][i].type==3)
{
int k=rand()%100;
if(k>=0&&k<2)
table[j][i].tag=6;
if(k>=3&&k<7)
table[j][i].tag=5;
if(k>=7&&k<11)
table[j][i].tag=4;
if(k>=11&&k<16)
table[j][i].tag=3;
if(k>=16&&k<31)
table[j][i].tag=2;
if(k>=31&&k<41)
table[j][i].tag=1;
}
}
i=rand()%8;
j=rand()%8;
while(i==j)
{
j=rand()%8;
}
p[0].xmap=(int)position[i].x;
p[0].ymap=(int)position[i].y;
p[1].xmap=(int)position[j].x;
p[1].ymap=(int)position[j].y;
}
void CGame::Create(HWND hWnd,HINSTANCE hInstance)
{
hwnd=hWnd;
hdc=::GetDC(hWnd);
this->m_pInput=new CInput;
this->m_pInput->Create(hWnd,hInstance);
this->m_pDraw=new CDraw;
this->m_pDraw->Create(hWnd);
this->m_pOpla1=new CPlain;
this->m_pOpla1->Create(this->m_pDraw->GetpDD(),800,600);
this->m_pOpla1->DrawBitmap(".\\Picture\\pic.bmp",800,600);
this->m_pOpla2=new CPlain;
this->m_pOpla2->Create(this->m_pDraw->GetpDD(),1040,248);
this->m_pOpla2->DrawBitmap(".\\Picture\\player.bmp",1040,248);
this->m_pOpla3=new CPlain;
this->m_pOpla3->Create(this->m_pDraw->GetpDD(),240,320);
this->m_pOpla3->DrawBitmap(".\\Picture\\word.bmp",240,320);
this->m_pOpla4=new CPlain;
this->m_pOpla4->Create(this->m_pDraw->GetpDD(),800,600);
this->m_pOpla4->DrawBitmap(".\\Picture\\back.bmp",800,600);
this->m_pOpla5=new CPlain;
this->m_pOpla5->Create(this->m_pDraw->GetpDD(),1024,768);
this->m_pOpla5->DrawBitmap(".\\Picture\\background.bmp",1024,768);
this->m_pSound=new CSound;
this->m_pSound->Create(hWnd);
char * s[]={".\\Sound\\appear.wav", //出现道具
".\\Sound\\detonation.wav", //.......
".\\Sound\\deuce.wav", //平局
".\\Sound\\die.wav", //死亡
".\\Sound\\get.wav", //得到东西
".\\Sound\\lay.wav", //放下泡泡
".\\Sound\\lost.wav", //失败
".\\Sound\\pirate.wav", //背景声音
".\\Sound\\start.wav", //开始
".\\Sound\\win.wav"}; //胜利
for(int i=0;i<MMUN;i++)
{
this->m_pSBuf[i]=new CSoundBuffer;
this->m_pSBuf[i]->CreateBuffer(this->m_pSound->GetpDS(),s[i]);
this->m_pSBuf[i]->SetVolume(-500);
}
m_bHelp=false;
this->OnInit();
}
CGame::~CGame()
{
}
void CGame::Render()
{
this->DoInput();
this->DoPrush();
this->DoBomb();
this->DoPlayer();
this->DoDraw();
}
void CGame::FPS()
{
static float m_fps;
m_timeNew = timeGetTime();
if (m_timeNew >= m_timeOld + 250)
{ //每半秒就更新一下fps
m_fps = (float) m_counter * 1000.0f / (m_timeNew - m_timeOld); //计算fps,fps_count是刷新次数统计
m_counter = 0; //清空刷新次数
m_timeOld = m_timeNew; //使过去的时间等于当前的时间
}
m_counter++;
char str[256];
sprintf(str,"FPS: %f",m_fps);
if(this->m_bHelp)
{
m_pDraw->DrawText(NULL,str,19,40);
sprintf(str,"P1: x:%d y:%d power:%d bomb:%d speed:%d",(int)p[0].x,(int)p[0].y,p[0].power,p[0].bomb,(int)p[0].speed);
m_pDraw->DrawText(NULL,str,19,60);
sprintf(str,"P2: x:%d y:%d power:%d bomb:%d speed:%d",(int)p[1].x,(int)p[1].y,p[1].power,p[1].bomb,(int)p[1].speed);
m_pDraw->DrawText(NULL,str,19,80);
sprintf(str,"F1 for help show/hide");
m_pDraw->DrawText(NULL,str,19,100);
sprintf(str,"F2 for reset");
m_pDraw->DrawText(NULL,str,19,120);
}
if(60-((int)(m_timeNew-m_timer)/1000)%60<10)
sprintf(str,"0%d:0%d",2-(int)(m_timeNew-m_timer)/1000/60,59-((int)(m_timeNew-m_timer)/1000)%60);
else
sprintf(str,"0%d:%d ",2-(int)(m_timeNew-m_timer)/1000/60,59-((int)(m_timeNew-m_timer)/1000)%60);
m_pDraw->DrawText(NULL,str,720,74,RGB(255,130,10));
if(2-(int)(m_timeNew-m_timer)/1000/60==0&&59-((int)(m_timeNew-m_timer)/1000)%60==0)
this->m_deuce=true;
}
void CGame::DoInput()
{
int n=0;
m_pInput->GetKeyboardState();
if(m_pInput->IsKeyDownToggle(DIK_F1))
{
if(this->m_bHelp)
this->m_bHelp=false;
else
this->m_bHelp=true;
}
if(m_pInput->IsKeyDownToggle(DIK_F2))
this->OnInit();
if(m_pInput->IsKeyDownToggle(DIK_F12))
{
}
for(int index=0;index<PNUM;index++)
{
if((index==0&&m_pInput->IsKeyDownToggle(DIK_LCONTROL))||(index==1&&m_pInput->IsKeyDownToggle(DIK_RCONTROL)))
{
if(p[index].status==die&&p[in