ELSView.cpp
Upload User: zhengxing
Upload Date: 2016-05-26
Package Size: 53k
Code Size: 20k
Development Platform:

Visual C++

  1. // ELSView.cpp : implementation of the CELSView class
  2. //
  3. #include "stdafx.h"
  4. #include "ELS.h"
  5. #include "ELSDoc.h"
  6. #include "ELSView.h"
  7. #include "OKDlg.h"
  8. #include "MemDC.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CELSView
  16. IMPLEMENT_DYNCREATE(CELSView, CView)
  17. BEGIN_MESSAGE_MAP(CELSView, CView)
  18. //{{AFX_MSG_MAP(CELSView)
  19. ON_COMMAND(ID_GAME_START, OnGameStart)
  20. ON_COMMAND(ID_GAME_PAUSE, OnGamePause)
  21. ON_COMMAND(ID_GAME_END, OnGameEnd)
  22. //ON_WM_PAINT()
  23. ON_WM_TIMER()
  24. ON_WM_KEYDOWN()
  25. ON_COMMAND(ID_PREPARE, OnPrepare)
  26. ON_UPDATE_COMMAND_UI(ID_GAME_START, OnUpdateGameStart)
  27. ON_UPDATE_COMMAND_UI(ID_GAME_PAUSE, OnUpdateGamePause)
  28. ON_UPDATE_COMMAND_UI(ID_GAME_END, OnUpdateGameEnd)
  29. //}}AFX_MSG_MAP
  30. // Standard printing commands
  31. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  32. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  33. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CELSView construction/destruction
  37. CELSView::CELSView()
  38. {
  39. // TODO: add construction code here
  40.     InitVariable();
  41. }
  42. CELSView::~CELSView()
  43. {
  44. }
  45. BOOL CELSView::PreCreateWindow(CREATESTRUCT& cs)
  46. {
  47. // TODO: Modify the Window class or styles here by modifying
  48. //  the CREATESTRUCT cs
  49. return CView::PreCreateWindow(cs);
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CELSView drawing
  53. void CELSView::OnDraw(CDC* pDC)
  54. {
  55. CELSDoc* pDoc = GetDocument();
  56. if (m_nFlag==false)
  57. {
  58. //CDC *cDC=GetDC();
  59. CMemDC m_DC(pDC);
  60. CBrush   *brush=new CBrush(RGB(22,22,22)); //改变背景的颜色,这里主要是黑色
  61. pDC->SelectObject(brush);
  62. CRect  rect(0,0,500,500); //设置的背景的矩形区域
  63. GetClientRect(&rect);
  64. pDC->Rectangle(rect);
  65. InitGame();                                         //当窗口生成时就要初始化操作
  66. COLORREF clrRim = RGB(68, 71, 255); //每一个方块的色彩边框
  67. COLORREF clrMid = RGB(255,255,255); //每一个方块的色中间色彩,白色
  68. const UINT wide = 20; //方块的宽
  69. const UINT hight = 20; //方块的高
  70. const UINT StartX = 120; //起始绘表格处的位置
  71. //在离窗口(0,120)处 开始绘20*10个方格,作为游戏区域
  72. for (int x=0; x<20; x++)
  73. {
  74. for (int j=0; j<10; j++)
  75. {
  76. m_DC.FillSolidRect(StartX+j*20, x*20, wide, hight, clrMid); //填充图形
  77. m_DC.Draw3dRect(StartX+j*20, x*20, wide, hight, clrRim, clrMid); //画矩形
  78. }
  79. }
  80. //在(20,150)处,开始绘提示图的背景
  81. for (x=0; x<4; x++)
  82. {
  83. for (int j=0; j<4; j++)
  84. {
  85. m_DC.FillSolidRect(20+j*20, 150+x*20, wide, hight, clrMid);
  86. m_DC.Draw3dRect(20+j*20, 150+x*20, wide, hight, clrRim, clrMid);
  87. }
  88. }
  89. }
  90. else
  91. {
  92. DrawGame();
  93. }
  94.     m_nFlag=true;
  95. ASSERT_VALID(pDoc);
  96. // TODO: add draw code for native data here
  97. }
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CELSView printing
  100. BOOL CELSView::OnPreparePrinting(CPrintInfo* pInfo)
  101. {
  102. // default preparation
  103. return DoPreparePrinting(pInfo);
  104. }
  105. void CELSView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  106. {
  107. // TODO: add extra initialization before printing
  108. }
  109. void CELSView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  110. {
  111. // TODO: add cleanup after printing
  112. }
  113. /////////////////////////////////////////////////////////////////////////////
  114. // CELSView diagnostics
  115. #ifdef _DEBUG
  116. void CELSView::AssertValid() const
  117. {
  118. CView::AssertValid();
  119. }
  120. void CELSView::Dump(CDumpContext& dc) const
  121. {
  122. CView::Dump(dc);
  123. }
  124. CELSDoc* CELSView::GetDocument() // non-debug version is inline
  125. {
  126. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CELSDoc)));
  127. return (CELSDoc*)m_pDocument;
  128. }
  129. #endif //_DEBUG
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CELSView message handlers
  132. void CELSView::OnRandomRect()
  133. {
  134.     m_nRectIndex = rand()%7; //生成7个图形中的一个
  135. //m_RectIndex = 1; //调试图形时用
  136. //每次新生成图形前,Init it
  137. for (int i= 0 ; i<4; i++)
  138. {
  139. RandomRect.Sqr[i].x=-1;
  140. RandomRect.Sqr[i].y=-1;
  141. RandomRect.m_nColor=0;
  142. RandomRect.Type=-1;
  143. }
  144.     
  145. switch (m_nRectIndex)
  146. {
  147. case 0:
  148. RandomRect.Sqr[0].x = 4;
  149. RandomRect.Sqr[0].y = 0;
  150. RandomRect.Sqr[1].x = 5;
  151. RandomRect.Sqr[1].y = 0;
  152. RandomRect.Sqr[2].x = 4;
  153. RandomRect.Sqr[2].y = 1;
  154. RandomRect.Sqr[3].x = 5;
  155. RandomRect.Sqr[3].y = 1;
  156. RandomRect.m_nColor = RGB(204, 153, 255);
  157. RandomRect.Type = 0;
  158. break;
  159. /*图形为{  色彩:淡紫
  160. }
  161. */
  162. case 1:
  163. RandomRect.Sqr[0].x = 6;
  164. RandomRect.Sqr[0].y = 0;
  165. RandomRect.Sqr[1].x = 5;
  166. RandomRect.Sqr[1].y = 0;
  167. RandomRect.Sqr[2].x = 4;
  168. RandomRect.Sqr[2].y = 0;
  169. RandomRect.Sqr[3].x = 3;
  170. RandomRect.Sqr[3].y = 0;
  171. RandomRect.m_nColor = RGB(0, 255, 0);
  172. RandomRect.Type = 1;
  173. //RandomRect.Flag=1;
  174. break;
  175. /*图形为{ 鲜绿
  176. ----
  177. }
  178. */
  179. case 2:
  180. RandomRect.Sqr[0].x = 5;
  181. RandomRect.Sqr[0].y = 0;
  182. RandomRect.Sqr[1].x = 4;
  183. RandomRect.Sqr[1].y = 1;
  184. RandomRect.Sqr[2].x = 5;
  185. RandomRect.Sqr[2].y = 1;
  186. RandomRect.Sqr[3].x = 4;
  187. RandomRect.Sqr[3].y = 2;
  188. RandomRect.m_nColor = RGB(255, 0, 255);
  189. RandomRect.Type = 2;
  190. //RandomRect.Flag=1;
  191. break;
  192. /*图形为{     |
  193. _ _
  194.     |
  195.   粉红
  196.   }
  197. */
  198. case 3:
  199. RandomRect.Sqr[0].x = 5;
  200. RandomRect.Sqr[0].y = 0;
  201. RandomRect.Sqr[1].x = 5;
  202. RandomRect.Sqr[1].y = 1;
  203. RandomRect.Sqr[2].x = 4;
  204. RandomRect.Sqr[2].y = 1;
  205. RandomRect.Sqr[3].x = 6;
  206. RandomRect.Sqr[3].y = 1;
  207. RandomRect.m_nColor = RGB(0,0 ,255);
  208. RandomRect.Type = 3;
  209. //RandomRect.Flag=1;
  210. break;
  211. /*图形为{     -
  212. _ _ _
  213.    
  214.  }蓝色
  215. */
  216. case 4:
  217. RandomRect.Sqr[0].x = 4;
  218. RandomRect.Sqr[0].y = 0;
  219. RandomRect.Sqr[1].x = 5;
  220. RandomRect.Sqr[1].y = 1;
  221. RandomRect.Sqr[2].x = 4;
  222. RandomRect.Sqr[2].y = 1;
  223. RandomRect.Sqr[3].x = 6;
  224. RandomRect.Sqr[3].y = 1;
  225. RandomRect.m_nColor = RGB(255, 0, 0);
  226. RandomRect.Type = 4;
  227. break;
  228. /*图形为{   |
  229. _ _ _
  230.    
  231.  }红色
  232. */
  233. case 5:
  234. RandomRect.Sqr[0].x = 5;
  235. RandomRect.Sqr[0].y = 0;
  236. RandomRect.Sqr[1].x = 4;
  237. RandomRect.Sqr[1].y = 1;
  238. RandomRect.Sqr[2].x = 3;
  239. RandomRect.Sqr[2].y = 1;
  240. RandomRect.Sqr[3].x = 5;
  241. RandomRect.Sqr[3].y = 1;
  242. RandomRect.m_nColor = RGB(0, 204, 255);
  243. RandomRect.Type = 5;
  244. break;
  245. /*图形为{    |
  246. _ _ _
  247.   }天蓝
  248. */
  249. case 6:
  250. RandomRect.Sqr[0].x = 5;
  251. RandomRect.Sqr[0].y = 0;
  252. RandomRect.Sqr[1].x = 5;
  253. RandomRect.Sqr[1].y = 1;
  254. RandomRect.Sqr[2].x = 6;
  255. RandomRect.Sqr[2].y = 1;
  256. RandomRect.Sqr[3].x = 6;
  257. RandomRect.Sqr[3].y = 2;
  258. RandomRect.m_nColor = RGB(255, 204, 0);
  259. RandomRect.Type = 6;
  260. break;
  261. /*图形为{  |
  262. - -
  263. |
  264. }金色
  265. */
  266. }
  267. }
  268. /*void CELSView::OnPaint() 
  269. {
  270. CPaintDC cdc(this); // device context for painting
  271. }*/
  272. void CELSView::InitGame()
  273. {
  274. //表格和相对应色彩表
  275. for (int i=0; i<10; i++)
  276. {
  277. for(int j=0; j<20; j++)
  278. {
  279. m_nCor[j][i] = -1; //表格数不可能为-1
  280. m_nCorcolor[j][i] = RGB(255,255,255);
  281. }
  282. }
  283. //图形初始化
  284. for (int k=0; k<4; k++)
  285. {
  286. RandomRect.Sqr[k].x = -1;
  287. RandomRect.Sqr[k].y = -1;
  288. NextRect.Sqr[k].x = -1;
  289. NextRect.Sqr[k].y = -1;
  290. OldRect.Sqr[k].x = -1;
  291. OldRect.Sqr[k].y = -1;
  292. CurrentRect.Sqr[k].x = -1;
  293. CurrentRect.Sqr[k].y = -1;
  294. }
  295. }
  296. void CELSView::DrawGame()
  297. {
  298. CDC *cDC=GetDC();
  299. CMemDC pDC(cDC);
  300. COLORREF clrRim = RGB(68, 71, 140); //每一个方块的色彩边框
  301. COLORREF clrMid = RGB(255,255,255); //每一个方块的色中间色彩,白色
  302. const UINT wide = 20; //方块的宽
  303. const UINT hight = 20; //方块的高
  304. const UINT StartX = 120; //起始绘表格处的位置
  305. CString lsStr1,lsStr2,lsStr3;
  306. lsStr1.Format("总分为:%d ",m_nCount);
  307. pDC.SetBkColor(RGB(22,22,22));
  308. pDC.SetTextColor(RGB(255,0,0));
  309. pDC .TextOut(0, 20,lsStr1);
  310.     lsStr2.Format("  Level:%d",m_nLevel);
  311. pDC.TextOut(0, 50,lsStr2);
  312.     
  313. lsStr2.Format("Time(ms):%d",m_nTime-m_nLevel*150);
  314. pDC.TextOut(0, 350,lsStr2);
  315. pDC.TextOut(0,120,"下一个方块:");
  316. int i,j;
  317. //把整个表格都绘一次,主游戏区域,包括已经在 底部的 和 空区域 两部分,所以改变以后不能画已经落下的方块了
  318. for (i=0; i<20; i++)
  319. {
  320. for (j=0; j<10; j++)
  321. {
  322. if (m_nCor[i][j] != -1)
  323. {
  324. pDC.FillSolidRect(StartX+j*20, i*20, wide, hight, m_nCorcolor[i][j]);
  325. pDC.Draw3dRect(StartX+j*20, i*20, wide, hight, clrRim, m_nCorcolor[i][j]);
  326. }
  327. else
  328. {
  329. pDC.FillSolidRect(StartX+j*20, i*20, wide, hight, clrMid);
  330. pDC.Draw3dRect(StartX+j*20, i*20, wide, hight, clrRim, clrMid);
  331. //continue;
  332. //return;
  333. }
  334. }
  335. }
  336. //绘制当前的正在操作的方块
  337. for (i=0; i<4; i++)
  338. {
  339. pDC.FillSolidRect(StartX+CurrentRect.Sqr[i].x*20, CurrentRect.Sqr[i].y*20, wide, hight, m_nCurrentColor);
  340. pDC.Draw3dRect(StartX+CurrentRect.Sqr[i].x*20, CurrentRect.Sqr[i].y*20, wide, hight, clrRim, m_nCurrentColor);
  341. }
  342. if ( true == m_current_next_exchange)
  343. {
  344. //提示下一方块  4*4方格的绘制,颜色会和初始化的时候冲突,
  345. for (int x=0; x<4; x++)
  346. {
  347. for (int j=0; j<4; j++)
  348. {
  349. pDC.FillSolidRect(20+j*20, 150+x*20, wide, hight, WHITE);
  350. pDC.Draw3dRect(20+j*20, 150+x*20, wide, hight, clrRim, WHITE);
  351. }
  352. }
  353. //绘制下一个提示方块
  354. for (i = 0; i<4; i++)
  355. {
  356. pDC.FillSolidRect(20+(NextRect.Sqr[i].x-3)*20, 150+NextRect.Sqr[i].y*20, wide, hight, m_nNextColor);
  357. pDC.Draw3dRect(20+(NextRect.Sqr[i].x-3)*20, 150+NextRect.Sqr[i].y*20, wide, hight, clrRim, m_nNextColor);
  358. }
  359. }
  360. }
  361. void CELSView::OnGameStart() 
  362. {
  363. if (m_nFirstStart==true && m_nGamePause==false) 
  364. {
  365. MakeRect();
  366. m_nFirstStart = false;
  367. }
  368. m_nGamePause = false;
  369. m_nGameOver = false;
  370. DrawGame();
  371. SetTimer(1,m_nTime-m_nLevel*150,NULL); //设置下落时间间隔
  372. }
  373. void CELSView::OnGamePause() 
  374. {
  375. m_nGamePause =true;
  376. m_nGameOver =true;
  377. KillTimer(1);
  378. }
  379. void CELSView::OnGameEnd() 
  380. {
  381. //m_nGameOver=true;
  382. //m_nGamePause = false;  //清除游戏暂停状态
  383. KillTimer(1);
  384. InitGame(); //清除所有数据
  385. InitVariable();
  386. Invalidate();
  387. //PostQuitMessage(0);
  388. //AfxGetApp()->Exit();
  389. //CEApp::ExitInstance();
  390. //PostQuitMessage(0);
  391. //exit(0);
  392. /*if (MessageBox("确定要退出吗?","提示",MB_YESNO|MB_DEFBUTTON2)==IDYES)
  393. {
  394. PostQuitMessage(0);
  395. return;
  396. //SendMessage(WM_CLOSE); //各个菜单键没有用了
  397. }*/
  398. }
  399. void CELSView::OnTimer(UINT nIDEvent) 
  400. {
  401.     if(false==IsDropBottom())
  402. DropOneRow();
  403. else
  404. BottomOperation();
  405. //Invalidate();
  406. DrawGame();
  407. CView::OnTimer(nIDEvent);
  408. }
  409. void CELSView::MakeRect()
  410. {
  411.     OnRandomRect(); //生成一个图形
  412. int i;
  413. //把生成图形拷贝给现在表格上要绘的图
  414. for (i= 0 ; i<4; i++)
  415. {
  416. CurrentRect.Sqr[i].x = RandomRect.Sqr[i].x;
  417. CurrentRect.Sqr[i].y = RandomRect.Sqr[i].y;
  418.         
  419. }
  420. m_nRectType  = RandomRect.Type;
  421. m_nCurrentColor = RandomRect.m_nColor;
  422. OnRandomRect(); //再生成一个图形
  423. //把生成图形拷贝给提示表格上要绘的图
  424. for (i= 0 ; i<4; i++)
  425. {
  426. NextRect.Sqr[i].x = RandomRect.Sqr[i].x;
  427. NextRect.Sqr[i].y = RandomRect.Sqr[i].y;
  428. }
  429. //m_nRectType  = RandomRect.Type;
  430. m_nNextColor = RandomRect.m_nColor;
  431. }
  432. void CELSView::DropOneRow()
  433. {
  434.     //图形改变
  435. SaveRect();
  436. //CDC *pDC=GetDC();
  437. //图形的四个方块都下降一行
  438. for (int i=0; i<4; i++)
  439. CurrentRect.Sqr[i].y++;
  440. //CRect  rect(CurrentRect.Sqr.x+CurrentRect.Sqr.x*)
  441. }
  442. void CELSView::BottomOperation()
  443. {
  444. //到了底后要将图形拷贝到表格上去
  445. m_nDrawBeginRow = -1;
  446. for (int i=0; i<4; i++)
  447. {
  448. m_nCor[CurrentRect.Sqr[i].y][CurrentRect.Sqr[i].x] = 1; //将图形所在位置贴到表格上去
  449. m_nCorcolor[CurrentRect.Sqr[i].y][CurrentRect.Sqr[i].x] = m_nCurrentColor; //色彩拷贝上去
  450. if (m_nDrawBeginRow < CurrentRect.Sqr[i].y)
  451. {
  452. m_nDrawBeginRow = CurrentRect.Sqr[i].y; //找落下后的最下面一行位置
  453. }
  454. }
  455. //测试图形落到底后,可以消除几行(最多4行)
  456. for (i=m_nDrawBeginRow; i>m_nDrawBeginRow-4 && i>0; i--)
  457. {
  458. if (true == IsRowFull(i))
  459. {
  460. CleanOneRow(i);
  461. CleanTheRow++;          
  462. i++;
  463. }
  464. }
  465. m_nCount+=CleanTheRow*CleanTheRow*100;
  466. CleanTheRow=0;
  467. if (m_nCount>(m_nLevel+1)*1000 && m_nLevel>=0 && m_nLevel<7)
  468. {
  469. m_nLevel++;
  470. KillTimer(1);
  471. SetTimer(1,m_nTime-m_nLevel*150,NULL);
  472. }
  473. if ( IsTableFull() == true )
  474. {
  475. //如果表格满,则退出游戏
  476. KillTimer(1); //结束游戏前停止计时
  477. //MessageBox("游戏结束"); //发送出游戏结束信息
  478. COKDlg  dlg;
  479. dlg.DoModal();
  480. InitGame(); //清除所有数据
  481. InitVariable();
  482. Invalidate();
  483. }
  484. else
  485. {
  486. m_nRectType = RandomRect.Type; //记录当前图形的类型
  487. //提示图形给当前图形,随机生成的图形给提示图形
  488. OnRandomRect();
  489. for (int i=0; i<4; i++)
  490. {
  491. //提示图形给当前图形
  492. CurrentRect.Sqr[i].x = NextRect.Sqr[i].x;
  493. CurrentRect.Sqr[i].y = NextRect.Sqr[i].y;
  494. //记下提示图,因下次绘图时,要清除原有图形
  495. OldRect.Sqr[i].x = NextRect.Sqr[i].x;
  496. OldRect.Sqr[i].x = NextRect.Sqr[i].y;
  497. //随机生成的图形给提示图形
  498. NextRect.Sqr[i].x = RandomRect.Sqr[i].x;
  499. NextRect.Sqr[i].y = RandomRect.Sqr[i].y;
  500. }
  501. //现在色为提示色
  502. m_nCurrentColor = m_nNextColor;
  503. //提示色为随机色
  504. m_nNextColor     = RandomRect.m_nColor;
  505. m_current_next_exchange = true; //因为交换了图形
  506. }
  507. }
  508. void CELSView::SaveRect()
  509. {
  510. //记原图形,在绘图时,如果不符合要求以便清除
  511. for (int k=0; k<4; k++)
  512. {
  513. OldRect.Sqr[k].x = CurrentRect.Sqr[k].x;
  514. OldRect.Sqr[k].y = CurrentRect.Sqr[k].y;
  515. }
  516. }
  517. bool CELSView::IsDropBottom()
  518. {
  519. for (int i=0; i<4; i++)
  520. {
  521. //表示图形下一行有数据(主要是方格的标志为1)或下一行为HIHG-1,表示到了底
  522. if (-1 != m_nCor[ CurrentRect.Sqr[i].y+1][ CurrentRect.Sqr[i].x] || (CurrentRect.Sqr[i].y >= 19) )
  523. {
  524. return true;
  525. }
  526. }
  527. return false; //测试完没到HIGH-1或是下一行没有数据表示没有到底
  528. }
  529. bool CELSView::IsRowFull(int Row)
  530. {
  531. for (int i=0; i<WIDTH; i++)
  532. {
  533. if (m_nCor[Row][i] == -1)
  534. {
  535. return false; //如果一行中有一个格子没有数据,表示行没满
  536. }
  537. }
  538. return true; //检测完一行那么表示满
  539. }
  540. bool CELSView::IsTableFull()
  541. {
  542. for (int i=0; i<WIDTH; i++)
  543. {
  544. if (m_nCor[0][i] != -1) //只要检测第一行就可以判断
  545. return true;
  546. }
  547. return false;
  548. }
  549. void CELSView::CleanOneRow(int Place)
  550. {
  551. bool moveData; //标志数移完没有
  552. m_nTableChangeFlag = true;
  553. //清除此行
  554. for (int k=0; k<10; k++ )
  555. {
  556. m_nCor[Place][k] = -1;
  557. m_nCorcolor[Place][k] = 0;
  558. }
  559. //向下移动数据
  560. for ( int i = Place; i>0; i--)
  561. {
  562. moveData = false;
  563. for (int j=0; j<10; j++)
  564. {
  565. if (m_nCor[i-1][j] != -1) //只有不是空的才会向下传递
  566. {
  567. moveData = true;
  568. //将上行数据拷贝下来
  569. m_nCor[i][j] = 1;
  570. m_nCorcolor[i][j] = m_nCorcolor[i-1][j];
  571. //将上一行数据清除
  572. m_nCor[i-1][j] = -1;
  573. m_nCorcolor[i-1][j] = 0;
  574. }
  575. }
  576. //如果有一行没有移动数据,表示上面的数据都不变;
  577. if (moveData == false)
  578. {
  579. break;
  580. }
  581. }
  582. }
  583. void CELSView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  584. {
  585. if (m_nGamePause==false) 
  586. {
  587. switch(nChar)
  588. {
  589. case VK_LEFT:
  590. {
  591. if (IsOutSide(LEFT) == false )//如果没有溢则图形左移一格
  592. {
  593. SaveRect();
  594. for (int i=0; i<4; i++)
  595. CurrentRect.Sqr[i].x--;
  596. }
  597. }
  598. break;
  599. case VK_RIGHT:
  600. {
  601. if (IsOutSide(RIGHT) == false )//如果没有溢则图形右移一格
  602. {
  603. SaveRect();
  604. for (int i=0; i<4; i++)
  605. CurrentRect.Sqr[i].x++; 
  606. }
  607. }
  608. break;
  609. case VK_UP:
  610. {
  611. RoundRect();
  612. }
  613. break;
  614. case VK_DOWN:
  615. {
  616. DirectDropBottom();
  617. }
  618. break;
  619. }
  620. DrawGame();
  621. }
  622. CView::OnKeyDown(nChar, nRepCnt, nFlags);
  623. }
  624. bool CELSView::IsOutSide(int dir)
  625. {
  626. //如果是右边,测试有没有出界
  627. if (dir == RIGHT)
  628. {
  629. for (int i=0; i<4; i++)
  630. {
  631. if ( CurrentRect.Sqr[i].x >= 9 
  632. || (m_nCor[CurrentRect.Sqr[i].y][CurrentRect.Sqr[i].x+1] == 1))//右边方格以已经填充
  633. {
  634. return true; //如果下一次会超出,刚返回true
  635. }
  636. }
  637. }
  638. //如果是左边,测试有没有出界
  639. if (dir == LEFT)
  640. {
  641. for (int i=0; i<4; i++)
  642. {
  643. if (CurrentRect.Sqr[i].x <= 0 
  644. || (m_nCor[CurrentRect.Sqr[i].y][CurrentRect.Sqr[i].x-1] == 1) )//左边方格已经填充
  645. {
  646. return true; //如果下一次会超出,刚返回true
  647. }
  648. }
  649. }
  650. return false;
  651. }
  652. void CELSView::RoundRect()
  653. {
  654. int tempx = 0, tempy = 0, index = 0;
  655. int rect_y = 0, rect_x = 0;
  656. //记忆原图形
  657. SaveRect();
  658. //判断图形的索引号
  659. if (m_nRectType != 0)
  660. {
  661. rect_x = CurrentRect.Sqr[1].x;
  662. rect_y = CurrentRect.Sqr[1].y;
  663. index = 1;
  664. }
  665. //根据index来判断哪些方块要移动,达到变形效果
  666. for (int i=0; i<4; i++)
  667. {
  668. if (i != index && m_nRectType != 0)
  669. {
  670. tempx = CurrentRect.Sqr[i].x - rect_x; //得到相对坐标x
  671. tempy = CurrentRect.Sqr[i].y - rect_y; //得到相对坐标y
  672. //逆时针旋转90°
  673. CurrentRect.Sqr[i].x = tempy + rect_x;
  674. CurrentRect.Sqr[i].y = -tempx + rect_y;
  675. if ( m_nCor[CurrentRect.Sqr[i].y][CurrentRect.Sqr[i].x] != -1
  676. || (CurrentRect.Sqr[i].x>9)  || (CurrentRect.Sqr[i].x<0) 
  677. || (CurrentRect.Sqr[i].y>19) || (CurrentRect.Sqr[i].y<0)
  678. )
  679. {
  680. //如果变形后图超出表格范围,则恢复原图形
  681. for (int j=0; j<4; j++)
  682. {
  683. CurrentRect.Sqr[j].y = OldRect.Sqr[j].y;
  684. CurrentRect.Sqr[j].x = OldRect.Sqr[j].x;
  685. }
  686. return;
  687. }
  688. }
  689. }
  690. return;
  691. }
  692. void CELSView::DirectDropBottom()
  693. {
  694. while ((m_nCor[CurrentRect.Sqr[0].y+1][CurrentRect.Sqr[0].x] == -1) && (CurrentRect.Sqr[0].y+1 < 20)
  695. && (m_nCor[CurrentRect.Sqr[1].y+1][CurrentRect.Sqr[1].x] == -1) && (CurrentRect.Sqr[1].y+1 < 20)
  696. && (m_nCor[CurrentRect.Sqr[2].y+1][CurrentRect.Sqr[2].x] == -1) && (CurrentRect.Sqr[2].y+1 < 20)
  697. && (m_nCor[CurrentRect.Sqr[3].y+1][CurrentRect.Sqr[3].x] == -1) && (CurrentRect.Sqr[3].y+1 < 20) )
  698. {
  699. for (int i=0; i<4; i++)
  700. {
  701. CurrentRect.Sqr[i].y++;
  702. }
  703. }
  704.     DrawGame();
  705. //Invalidate();
  706. }
  707. void CELSView::OnPrepare() 
  708. {
  709. /*CDC *cDC=GetDC();
  710. CMemDC pDC(cDC);
  711. CBrush   *brush=new CBrush(RGB(22,22,22)); //改变背景的颜色,这里主要是黑色
  712. pDC->SelectObject(brush);
  713. CRect  rect(0,0,500,500); //设置的背景的矩形区域
  714. GetClientRect(&rect);
  715. pDC->Rectangle(rect);
  716. InitGame();                                         //当窗口生成时就要初始化操作
  717. COLORREF clrRim = RGB(68, 71, 255); //每一个方块的色彩边框
  718. COLORREF clrMid = RGB(255,255,255); //每一个方块的色中间色彩,白色
  719. const UINT wide = 20; //方块的宽
  720. const UINT hight = 20; //方块的高
  721. const UINT StartX = 120; //起始绘表格处的位置
  722. //在离窗口(0,120)处 开始绘20*10个方格,作为游戏区域
  723. for (int x=0; x<20; x++)
  724. {
  725. for (int j=0; j<10; j++)
  726. {
  727. pDC->FillSolidRect(StartX+j*20, x*20, wide, hight, clrMid); //填充图形
  728. pDC->Draw3dRect(StartX+j*20, x*20, wide, hight, clrRim, clrMid); //画矩形
  729. }
  730. }
  731. //在(20,150)处,开始绘提示图的背景
  732. for (x=0; x<4; x++)
  733. {
  734. for (int j=0; j<4; j++)
  735. {
  736. pDC->FillSolidRect(20+j*20, 150+x*20, wide, hight, clrMid);
  737. pDC->Draw3dRect(20+j*20, 150+x*20, wide, hight, clrRim, clrMid);
  738. }
  739. }*/
  740. }
  741. void CELSView::OnUpdateGameStart(CCmdUI* pCmdUI) 
  742. {
  743. if(m_nGamePause || m_nGameOver)
  744. pCmdUI->Enable(true);
  745. else
  746. pCmdUI->Enable(false);
  747. }
  748. void CELSView::OnUpdateGamePause(CCmdUI* pCmdUI) 
  749. {
  750. if(!m_nGameOver)
  751. pCmdUI->Enable(true);
  752. else
  753. pCmdUI->Enable(false);
  754. }
  755. void CELSView::OnUpdateGameEnd(CCmdUI* pCmdUI) 
  756. {
  757. if(m_nGameOver)
  758. pCmdUI->Enable(true);
  759. else
  760. pCmdUI->Enable(false);
  761. }
  762. void CELSView::InitVariable()
  763. {
  764. m_nCount = 0;
  765. m_current_next_exchange = true; //交换图形
  766. m_nGameOver = true; //缺省状态是结束的
  767. m_nGamePause = false; //缺省不是暂停
  768. m_nTableChangeFlag = false; //表格改变标志
  769. m_nFlag = false;
  770. m_nFirstStart = true; //第一次ture
  771. m_nTime = 1100; //时间间隔计数
  772. m_nDrawBeginRow = -1; //绘图开始位置
  773. m_nDrawEndRow = -1; //绘图结束位置
  774. m_nCurrentColor = 0;
  775. CleanTheRow = 0;  
  776. m_nLevel = 0;
  777.     m_nRectType = -1;
  778. }