云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 网络台球游戏的网络编程(vc++)

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

  • 62 次阅读
  • 3 次下载
  • 2025/5/25 5:37:52

数,用于处理控制消息。 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++) 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

数,用于处理控制消息。 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[

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com