Manager.cpp
上传用户:semy2100
上传日期:2013-01-22
资源大小:3633k
文件大小:10k
源码类别:

射击游戏

开发平台:

Visual C++

  1. /********************************************************************
  2. *                        空运爆发                                    *
  3. *                       Version 2.0                                  *
  4. * 本游戏全部代码由:                                                 *
  5. *                   冲天实验室   梁锋 编写                           *
  6. *                    邮箱:lenwine@126.com                           *
  7. *                bbs: http://free2.e-168.cn/lenwind (正在开发中...)  *
  8. **********************************************************************/
  9. //////////////////////////////////////////////////////////////////////
  10. #include "stdafx.h"
  11. const int Manager::Imglong=::GetSystemMetrics(SM_CXSCREEN);
  12. const int Manager::Imgtall=::GetSystemMetrics(SM_CYSCREEN);
  13. const int Manager::printtime=20;
  14. const double Manager::PI=3.1415926535897932;
  15. //////////////////////////////////////////////////////////////////////
  16. Manager::Manager(HWND mhwnd):MgrFace(mhwnd)
  17. {
  18. Time=GetTickCount();//毫秒
  19. ZeroMemory(&st,sizeof(st));
  20. st.EndTime=Time;
  21. ts=1.0;
  22.    
  23. srand(time(0));
  24. OneKey=0;
  25. TwoKey=0;
  26. flat=1;
  27. flat2=1;
  28.        oWindow=new OutScreen(this);
  29.    oPeratorPlace=new OperatorPlace(this,Imglong/2-100,Imgtall-50,0);
  30.    oPeratorPlace2=new OperatorPlace(this,Imglong/2+100,Imgtall-50,1);
  31.    oPeratorbomb=new BombSet(this);
  32.    oEnermybomb=new BombSet(this);
  33.    
  34.    oEnermyPlace=new EnermyPlace(this);
  35.    oHit=new ObjectHit(this);
  36.        oWindow->beginsp();
  37. //初始化DirectSound对象
  38.    if(!InitializeDirectSound(MgrFace))
  39. {
  40. ::MessageBox(MgrFace,"Error!",NULL,MB_OK);
  41. }
  42. //创建声音缓冲区
  43. m_lPlay=new MusicPlace("disco.wav",TRUE,m_lpDS);
  44. ms_play=new MusicPlace("mpy.wav",TRUE,m_lpDS);//旋风音
  45. co_play=new MusicPlace("co.wav",TRUE,m_lpDS);
  46. cs_play=new MusicPlace("bom.wav",TRUE,m_lpDS);
  47. //播放背景音乐
  48. m_lPlay->ActionPlay();
  49. }
  50. Manager::~Manager()
  51. {
  52. ClearDSBuffer();
  53.     delete oWindow;
  54. }
  55. //刷新游戏
  56. void Manager::Update()
  57. {
  58. int t=GetTickCount();
  59. //=============================================
  60. /** if(bActive)
  61. {
  62. //计算帧频率
  63. static int frame=0, nt, ot=0;
  64. nt=t;
  65. if (nt > ot+1000)
  66. {
  67. ot=nt;
  68. fps=frame;
  69. frame=0;
  70. }
  71. //画面延时
  72. static int NewCount, OldCount=0;
  73. NewCount=GetTickCount(); 
  74. if (NewCount > OldCount+speed)
  75. {
  76. frame++;
  77. OldCount=NewCount;
  78. UpdateFrame();//更新画面
  79. }
  80. }**/
  81. //=============================================
  82. if(t-Time<printtime)
  83. return;
  84. Time=t;
  85. //难度增加
  86. if(oPeratorPlace->result>=flat*200)
  87. {
  88. oEnermyPlace->AddStress();
  89. flat++;
  90. }
  91. switch(oPeratorPlace->bombStyle)
  92. {
  93. case 0:
  94. i=0;
  95. break;
  96. case 1:
  97. i=1;
  98. break;
  99. case 2:
  100. i=2;
  101. break;
  102. default:
  103. break;
  104. }
  105. oWindow->Refresh();//清屏
  106. oHit->Update();
  107. oEnermybomb->Update();
  108. oPeratorbomb->Update();
  109. oEnermyPlace->Update();
  110. oPeratorPlace->Update();
  111. oPeratorPlace2->Update2();
  112. //oWindow->paintsp();
  113. Updatest();
  114. OutStatus();
  115. oWindow->PmOutPut();//将所有角色输出
  116. }
  117. void Manager::Updatest()
  118. {
  119. st.ObjectValue++;
  120. if(Time-st.EndTime>=1000)
  121. {
  122. st.st=st.ObjectValue;
  123. st.ObjectValue=0;
  124. st.EndTime=Time;
  125. ts=(double)1000/(printtime*st.st);
  126. }
  127. }
  128. //显示信息
  129. void Manager::OutStatus()//在客户区TOP上列出所有信息
  130. {
  131. HDC hdc=oWindow->GetCloseDDC();
  132. ostringstream message;
  133. message<<"红蓝活:  "<<oPeratorPlace->alive;
  134. message<<" 红蓝敌: "<<oPeratorPlace->decedent<<"  红蓝分 "<<oPeratorPlace->result;
  135. message<<"  红蓝级  "<<flat;
  136.     message<<"<<空运爆发>>";
  137.    
  138. SetBkMode(hdc,TRANSPARENT);
  139. SetTextColor(hdc,RGB(130,100,10));
  140. TextOut(hdc, 5, Imgtall-20, message.str().c_str(), message.str().length());
  141. }
  142. void Manager::EnermyShoot(Brock *bBrock)//敌方发射
  143. {
  144. oEnermybomb->Firing(bBrock->bombStyle,
  145. bBrock->GetX()+bBrock->Bitlong/2,
  146. bBrock->GetY()+bBrock->Bittall/2,
  147. bBrock->spx,bBrock->spy);
  148. }
  149. void Manager::OperatorShoot(OperatorPlace *oPeratorPlace,int rb)//我方发射
  150. {
  151.     oPeratorbomb->Firing(oPeratorPlace->bombStyle,
  152. oPeratorPlace->GetX()+oPeratorPlace->Bitlong/2,
  153. oPeratorPlace->GetY()+oPeratorPlace->Bittall/2,
  154. oPeratorPlace->spx,oPeratorPlace->spy);
  155. }
  156. void Manager::OperatorShoot2(OperatorPlace *oPeratorPlace2,int rb)//我方发射
  157. {
  158.     oPeratorbomb->Firing(oPeratorPlace2->bombStyle,
  159. oPeratorPlace2->GetX()+oPeratorPlace2->Bitlong/2,
  160. oPeratorPlace2->GetY()+oPeratorPlace2->Bittall/2,
  161. oPeratorPlace2->spx,oPeratorPlace2->spy);
  162. }
  163. void Manager::PrintMember(MemberStatus *mStatus)//输出所有成员
  164. {
  165. oWindow->PaintOutPut(mStatus->GetX(),
  166. mStatus->GetY(),
  167. mStatus->Bitlong,
  168. mStatus->Bittall,
  169. mStatus->bx,
  170. mStatus->by,false);
  171. }
  172. void Manager::KeyDown(WPARAM KeyCode)//定义上、下、左、右 双键方向
  173. {
  174. switch(KeyCode)
  175. {
  176. case VK_LEFT: 
  177. // oWindow->rightsp();//背景向右走
  178. case VK_RIGHT: 
  179. // oWindow->leftsp();//背景向左走
  180. case VK_UP:
  181. // oWindow->downsp();//背景向下走
  182. case VK_DOWN:
  183. if(KeyCode==TwoKey||KeyCode==OneKey)return;
  184. if(OneKey && TwoKey)return;
  185. OneKey=TwoKey;
  186. TwoKey=KeyCode;
  187.                           /** 判断双键按下的操作 **/
  188. if((OneKey==VK_LEFT && TwoKey==VK_UP)||(OneKey==VK_UP && TwoKey==VK_LEFT))
  189. oPeratorPlace->Start(OperatorPlace::MOVLU);
  190. else if((OneKey==VK_LEFT && TwoKey==VK_DOWN)||(OneKey==VK_DOWN && TwoKey==VK_LEFT))
  191. oPeratorPlace->Start(OperatorPlace::MOVLD);
  192. else if((OneKey==VK_RIGHT && TwoKey==VK_UP)||(OneKey==VK_UP && TwoKey==VK_RIGHT))
  193. oPeratorPlace->Start(OperatorPlace::MOVRU);
  194. else if((OneKey==VK_RIGHT && TwoKey==VK_DOWN)||(OneKey==VK_DOWN && TwoKey==VK_RIGHT))
  195. oPeratorPlace->Start(OperatorPlace::MOVRD);
  196. else
  197. oPeratorPlace->Start((OperatorPlace::START)(OperatorPlace::MOVL+KeyCode-VK_LEFT));
  198. break;
  199. case 65:
  200. case 68:
  201. case 87:
  202. case 83:
  203. if(KeyCode==TwoKey||KeyCode==OneKey)return;
  204. if(OneKey && TwoKey)return;
  205. OneKey=TwoKey;
  206. TwoKey=KeyCode;
  207.                           /** 判断双键按下的操作 **/
  208. if((OneKey==65 && TwoKey==87)||(OneKey==87 && TwoKey==65))
  209. oPeratorPlace2->Start2(OperatorPlace::MOVLU);
  210. else if((OneKey==65 && TwoKey==83)||(OneKey==83 && TwoKey==65))
  211. oPeratorPlace2->Start2(OperatorPlace::MOVLD);
  212. else if((OneKey==68 && TwoKey==87)||(OneKey==87 && TwoKey==68))
  213.     oPeratorPlace2->Start2(OperatorPlace::MOVRU);
  214. else if((OneKey==68 && TwoKey==83)||(OneKey==83 && TwoKey==68))
  215.     oPeratorPlace2->Start2(OperatorPlace::MOVRD);
  216. else
  217. {
  218. oPeratorPlace2->Start2((OperatorPlace::START)(OperatorPlace::MOVL+KeyCode-65));
  219. oPeratorPlace2->Start2((OperatorPlace::START)(OperatorPlace::MOVR+KeyCode-68));
  220. oPeratorPlace2->Start2((OperatorPlace::START)(OperatorPlace::MOVD+KeyCode-83));
  221. oPeratorPlace2->Start2((OperatorPlace::START)(OperatorPlace::MOVU+KeyCode-87));
  222. }
  223. break;
  224. case 85:
  225. oPeratorPlace2->Start2(OperatorPlace::BEGINSHOOT);
  226. switch(i)
  227. {
  228. case 0:
  229. cs_play->ActionPlay();
  230. break;
  231. case 1:
  232. ms_play->ActionPlay();
  233. break;
  234. case 2:
  235. co_play->ActionPlay();
  236. break;
  237. }
  238. break;
  239. case 79:
  240. oPeratorPlace2->Start2(OperatorPlace::HOLDWEA);
  241. break;
  242. case VK_SPACE:
  243. oPeratorPlace->Start(OperatorPlace::BEGINSHOOT);
  244. /*射击时采用不用种类的弹,发出不用种类的声音*/
  245. switch(i)
  246. {
  247. case 0:
  248. cs_play->ActionPlay();
  249. break;
  250. case 1:
  251. ms_play->ActionPlay();
  252. break;
  253. case 2:
  254. co_play->ActionPlay();
  255. break;
  256. }
  257. /** if(!bmus)
  258. {
  259. co_play->ActionPlay();
  260.     cs_play->ActionStop();
  261. ms_play->ActionStop();
  262. }
  263. else if(bmus)
  264. {
  265. cs_play->ActionPlay();
  266.     co_play->ActionStop();
  267. ms_play->ActionStop();
  268. }else
  269. {
  270. cs_play->ActionStop();
  271.     co_play->ActionStop();
  272. ms_play->ActionPlay();
  273. }**/
  274. break;
  275. case VK_CONTROL:
  276. oPeratorPlace->Start(OperatorPlace::HOLDWEA);
  277. break;
  278. case VK_DELETE:
  279. case VK_ESCAPE:
  280. DestroyWindow(MgrFace);
  281. break;
  282. }
  283. }
  284. void Manager::KeyUp(WPARAM KeyCode)//释放
  285. {
  286. if(KeyCode==TwoKey)
  287. {
  288. TwoKey=0;
  289. if(OneKey)
  290. {
  291. TwoKey=OneKey;
  292. OneKey=0;
  293. oPeratorPlace->Start((OperatorPlace::START)(TwoKey-VK_LEFT));
  294. oPeratorPlace2->Start2((OperatorPlace::START)(TwoKey-65));
  295. }
  296. else
  297. {
  298. oPeratorPlace->Start(OperatorPlace::STOPMOV);
  299. oPeratorPlace2->Start2(OperatorPlace::STOPMOV);
  300. }
  301. }
  302. else if(KeyCode==OneKey)
  303. OneKey=0;
  304. else if(KeyCode==VK_SPACE)
  305. oPeratorPlace->Start(OperatorPlace::STOPSHOOT);
  306. else if(KeyCode==85)
  307. oPeratorPlace2->Start2(OperatorPlace::STOPSHOOT);
  308. }
  309. void Manager::RefreshScreen()//我方控制机重设回到客户区中间底部
  310. {
  311. delete oPeratorPlace;
  312. oPeratorPlace=new OperatorPlace(this,Imglong/2-50,Imgtall-50,0);
  313. flat=1;
  314. oEnermyPlace->RefreshStress();
  315. }
  316. void Manager::RefreshScreen2()
  317. {
  318. delete oPeratorPlace2;
  319. flat2=1;
  320. oPeratorPlace2=new OperatorPlace(this,Imglong/2+50,Imgtall-50,1);
  321. oEnermyPlace->RefreshStress();
  322. }
  323. BOOL Manager::InitializeDirectSound(HWND hwnd)
  324. {
  325. //创建背景音乐和音效DriectSound对象
  326. if(DS_OK!=DirectSoundCreate(NULL,&m_lpDS,NULL))
  327. {
  328. ::MessageBox(NULL,"创建DirectSound对象失败n找不到可利用的声音设备",NULL,MB_OK);
  329. return FALSE;
  330. }
  331. //设置协作级别
  332. if(DS_OK!=m_lpDS->SetCooperativeLevel(hwnd,DSSCL_NORMAL))
  333. {
  334. ::MessageBox(NULL,"设置协作级别失败",NULL,MB_OK);
  335. return FALSE;
  336. }
  337. return TRUE;
  338. }
  339. void Manager::ClearDSBuffer()
  340. {
  341.  m_lPlay->ActionStop();
  342.  ms_play->ActionStop();
  343.  cs_play->ActionStop();
  344.  co_play->ActionStop();
  345.      if(m_lPlay)
  346.  {
  347.  delete m_lPlay;
  348.  }
  349.  if(ms_play)
  350.  {
  351.  delete ms_play;
  352.  }
  353.  if(co_play)
  354.  {
  355.  delete co_play;
  356.  }
  357.  if(cs_play)
  358.  {
  359.  delete cs_play;
  360.  }
  361. }
  362. void Manager::playcs()
  363. {
  364. ms_play->ActionPlay();
  365. }