本课内容 接下来的几节课,我们要利用C语言开发一些游戏 本节课开发一个俄罗斯方块游戏 游戏规则
游戏区域的绘制 int x,y,nW,nH; //小方块的绘制 for (i=0;i<m_iRow;i++) for (j=0;j<m_iCol;j++) { if (GameStatus[i][j]==MAP_STATE_NOT_EMPTY) //在游戏区域中状态为被占用状态的区域绘制小方块 x = m_iStartY+j*m_iLarge +2; y = m_iStartX+i*m_iLarge +2; nW = m_iLarge-4; nH = m_iLarge-4; pDC- >BitBlt(x,y,nW,nH,&m_memRectDC,m_iBlockSytle*30,0,SRCCOPY); }
游戏区域右侧的文本显示 //显示游戏区域及游戏级别的汉字描述 if (!m_bGameEnd) { pDC -> SetBkColor(BLACK); pDC -> SetTextColor(WHITE); pDC -> TextOut(m_iStartY+320, m_iStartX+220, "游戏区域大小:"); pDC -> TextOut(m_iStartY+320, m_iStartX+240,m_strArea); pDC -> TextOut(m_iStartY+320, m_iStartX+280, "游戏级别:"); pDC -> TextOut(m_iStartY+320, m_iStartX+300,m_strLevel); }
游戏区域右侧的文本显示 //显示总分 if (!m_bGameEnd) { CString lsStr; lsStr.Format("总分为:%d 分",m_iPerformance); pDC -> SetBkColor(BLACK); pDC -> SetTextColor(WHITE); pDC -> TextOut(m_iStartY+320, m_iStartX+180,lsStr); }
开发第二步(进行游戏实战) //画下一次将要出现的方块,用于提示用户 if (!m_bGameEnd) { pDC -> SetBkColor(BLACK); pDC -> SetTextColor(WHITE); pDC -> TextOut(m_iStartY+320, m_iStartX,"下一个方块:"); int x,y,nW,nH; for (UINT k=0;k<4;k++) i = NextStatus[k][0]; j = NextStatus[k][1]; x = m_iStartY+j*30 +2+320; y = m_iStartX+i*30 +2+30; nW = m_iLarge-4; nH = m_iLarge-4; pDC->BitBlt(x,y,nW,nH,&m_memRectDC,m_iBlockSytle*30,0,SRCCOPY); }
进行事件处理(游戏开始) if (!m_bGamePaush) //如果不是游戏暂停状态,则必须作些初始工作 { m_bGameEnd = FALSE; //总分值清零, 并显示总分记分牌 m_iPerformance = 0; //显示当前的区域及游戏级别的汉字描述 CurrentAreaAndLevel(); CRect rect(m_iStartY, m_iStartX, m_iStartY+440, m_iStartX+370); InvalidateRect(&rect); } m_bGamePaush = FALSE;
显示当前的区域及游戏级别的汉字描述 void CSkyblue_RectView::CurrentAreaAndLevel() { switch(m_iRow) case 12: m_strArea = "12行列"; break; case 18: m_strArea = "18行列"; break; case 24: m_strArea = "24行列"; break; case 30: m_strArea = "30行列"; break; } switch(m_iLevel) case 0: m_strLevel = "第一级: 入门级"; break; case 1: m_strLevel = "第二级: 初级级"; break; case 2: m_strLevel = "第三级: 中级"; break; case 3: m_strLevel = "第四级: 中高级"; break; case 4: m_strLevel = "第五级: 高级"; break; case 5: m_strLevel = "第六级: 顶级"; break;
设置对话框 修改构造函数: CSettingDlg(int m_iArea, int m_iLevel,int m_iBlockStyle, BOOL m_bMusic,BOOL m_bDrawGrid, CWnd* pParent = NULL) OnInitDialog()中设置默认的选项 //参传来的参数赋初值 CComboBox *m_comboArea; CComboBox *m_comboLevel; CComboBox *m_comboBlockStyle; CButton *m_btnDrawGrid; CButton *m_btnMusic; m_comboArea = (CComboBox *)GetDlgItem(IDC_COMBO_AREA); m_comboArea -> SetCurSel( m_oldArea ); m_comboLevel = (CComboBox *)GetDlgItem(IDC_COMBO_LEVEL); m_comboLevel -> SetCurSel( m_oldLevel ); m_comboBlockStyle = (CComboBox *)GetDlgItem(IDC_COMBO_BLOCK_SYTLE); m_comboBlockStyle -> SetCurSel( m_oldBlockSytle ); m_btnDrawGrid = (CButton *)GetDlgItem(IDC_CHECK_GRID); m_btnDrawGrid -> SetCheck(m_oldDrawGrid); m_btnMusic = (CButton *)GetDlgItem(IDC_CHECK_MUSIC); m_btnMusic -> SetCheck(m_oldMusic);
设置对话框 点击OK,设置选中的项目: CComboBox *m_comboArea; CComboBox *m_comboLevel; CComboBox *m_comboBlockStyle; m_comboArea = (CComboBox *)GetDlgItem(IDC_COMBO_AREA); m_comboLevel = (CComboBox *)GetDlgItem(IDC_COMBO_LEVEL); m_comboBlockStyle = (CComboBox *)GetDlgItem(IDC_COMBO_BLOCK_SYTLE); m_iArea = m_comboArea -> GetCurSel(); if (m_iArea<0 || m_iArea>3) m_iArea = 0; m_iLevel = m_comboLevel -> GetCurSel(); if (m_iLevel<0 || m_iLevel>5) m_iLevel = 2; m_iBlockStyle = m_comboBlockStyle -> GetCurSel(); if (m_iBlockStyle<0 || m_iBlockStyle>5) m_iBlockStyle = 0;
调用对话框 游戏选择: int m_lsArea; switch(m_iRow) { case 12: m_lsArea = 0; break; case 18: m_lsArea = 1; case 24: m_lsArea = 2; case 30: m_lsArea = 3; }
调用对话框 CSettingDlg dlg(m_lsArea,m_iLevel,m_iBlockSytle,m_bMusic,m_bDrawGrid); if (dlg.DoModal()==IDOK) { //确定区域的大小 switch(dlg.m_iArea) case 0: m_iRow = 12; m_iCol = 10; m_iLarge = 30; break; case 1: m_iRow = 18; m_iCol = 15; m_iLarge = 20; case 2: m_iRow = 24; m_iCol = 20; m_iLarge = 15; case 3: m_iRow = 30; m_iCol = 25; m_iLarge = 12; }
调用对话框 //确定级别 m_iLevel = dlg.m_iLevel; //选择的样式 m_iBlockSytle = dlg.m_iBlockStyle; //确定是否绘网格背景 m_bDrawGrid = dlg.m_bDrawGrid; //检查是否插放音乐 m_bMusic = dlg.m_bMusic; Invalidate();
TOOLBAR选择设置 添加一个UPDATA_COMMAND_UI事件 if (m_bGameEnd || m_bGamePaush) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE);
课程讲授完毕 感谢聆听! www.dut-px.com/