第一范文网 - 专业文章范例文档资料分享平台

网络台球游戏的网络编程(vc++)

来源:用户分享 时间:2026/1/1 10:06:10 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

数,用于处理控制消息。 ReceiveVelocity 函数定义如下。

void CBilliardsDoc::ReceiveVelocity(char *pBuf) {

Velocity *v=new Velocity; v=(Velocity *)pBuf;

CBilliardsPlayView* pPlayView;

pPlayView=(CBilliardsPlayView *)((CMainFrame *)AfxGetMainWnd())->m_wndSplitter. GetPane(0,0);

pPlayView->m_fVelocity[0][0]=v->fvalue0; pPlayView->m_fVelocity[0][1]=v->fvalue1; pPlayView->m_fVelocity[0][2]=v->fvalue2; while (pPlayView->m_bAnimate);

pPlayView->HitBall(); //调用视图类的碰撞算法 248 Visual C++游戏开发技术与实例 }

ReceiveMSG 函数定义如下。

BOOL CBilliardsDoc::ReceiveMSG(CString strTemp) {

CBilliardsFormView* pFormView;

CMainFrame *pView=(CMainFrame *)(AfxGetMainWnd()); CPropertyItem *pItem=NULL; POSITION pos;

for(pos=GetFirstViewPosition();pos!=NULL;) {

CView* pView = GetNextView(pos);

pFormView= DYNAMIC_DOWNCAST(CBilliardsFormView, pView);

if (pFormView != NULL) break; }

switch(strTemp.GetAt(0)){ case ’0’: //Login 消息 strTemp.Delete(0);

if(m_bPlayer==SERVER){

//如果是服务器,则表示有客户上站消息

pFormView->GetDlgItem(IDC_STATIC_CLIENT)->SetWindowText(strTemp);

pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);

m_strClient=strTemp;

sndPlaySound(\}

else{//如果是客户机,则表示成功连接服务器

pFormView->GetDlgItem(IDC_STATIC_SERVER)->SetWindowText(strTemp);

pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);

m_strServer=strTemp; }

break;

case ’1’: //聊天消息

strTemp.Delete(0);

if(m_bPlayer==SERVER){ pItem=new

CPropertyItem(m_strClient,strTemp,PIT_EDIT,\pFormView->m_ctlListMSG.AddPropItem(pItem); if(pView->m_bSoundMsg)

sndPlaySound(\}

else{

pItem=new

CPropertyItem(m_strServer,strTemp,PIT_EDIT,\pFormView->m_ctlListMSG.AddPropItem(pItem); if(pView->m_bSoundMsg)

sndPlaySound(\}

pItem=NULL; break;

第4 章网络游戏开发249 case ’3’: //Logout 消息 if(m_bPlayer==SERVER){

AfxMessageBox(\客户端已离开游戏\this->m_pClientSocket->ShutDown(); this->m_pClientSocket->Close();

pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE); }

else{

AfxMessageBox(\服务器端已离开游戏\

this->m_pClientSocket->ShutDown(); this->m_pClientSocket->Close(); ((CMainFrame

*)AfxGetMainWnd())->m_wndToolBar.GetToolBarCtrl() .HideButton(ID_NEWGAME,FALSE); ((CMainFrame

*)AfxGetMainWnd())->m_wndToolBar.GetToolBarCtrl() .HideButton(ID_JOINGAME,FALSE); ((CMainFrame

*)AfxGetMainWnd())->m_wndToolBar.GetToolBarCtrl() .HideButton(ID_STOPGAME); ((CMainFrame

*)AfxGetMainWnd())->GetMenu()->GetSubMenu(0) ->EnableMenuItem(ID_NEWGAME,MF_ENABLED); ((CMainFrame

*)AfxGetMainWnd())->GetMenu()->GetSubMenu(0) ->EnableMenuItem(ID_JOINGAME,MF_ENABLED); ((CMainFrame

*)AfxGetMainWnd())->GetMenu()->GetSubMenu(0) ->EnableMenuItem(ID_STOPGAME,MF_GRAYED);

pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE); }

break; default: break; }

return 1; }

CListenSocket 也继承自CSocket,它是服务器端的连接监听Socket,所以需要重载OnAccept 以获

得网络连接消息。这也和前面介绍的CSocket 使用方法差别不大。 CListenSocket::CListenSocket(CBilliardsDoc* pDoc) {

m_pListenDoc=pDoc;

}

CListenSocket::~CListenSocket() { }

//接收连接的消息响应函数

void CListenSocket::OnAccept(int nErrorCode) {

//当有连接到来的时候,调用文档类的AcceptConnection 函数处理连接

if(nErrorCode==0)

m_pListenDoc->AcceptConnection(); }

250 Visual C++游戏开发技术与实例

OnAccept 函数使用了文档类函数AcceptConnection 处理连接。AcceptConnection 定义如下:

BOOL CBilliardsDoc::AcceptConnection() {

if(!m_pListenSocket->Accept(*m_pClientSocket)){ AfxMessageBox(\接受连接失败!\return 0; }

//向客户端发送成功登陆消息

CString strTemp;

strTemp=\

m_pClientSocket->Send(strTemp,strTemp.GetLength());

return 1; }

在CBilliardsDoc 中,函数SendVelocity 是用来发送击球位置的,击球消息的控制码是2。

void CBilliardsDoc::SendVelocity(float m_fVelocity[3]) {

Velocity v;

v.type=’2’;

v.fvalue0=m_fVelocity[0]; v.fvalue1=m_fVelocity[1]; v.fvalue2=m_fVelocity[2];

m_pClientSocket->Send(&v,sizeof(v)); }

搜索更多关于: 网络台球游戏的网络编程(vc++) 的文档
网络台球游戏的网络编程(vc++).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c9nuww13fy10weks4q3ev_4.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top