欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#基于TCP实现简单游戏客户端的完整实例

程序员文章站 2022-06-24 23:42:08
目录一、界面二、代码1.播放音频2.播放图片3.登录和退出游戏4.命令交互5.信息展示6.最终代码三、效果四、总结五、参考一、界面 左上方播放音频按钮和停止播放下面是图片展示再下面是进入游戏以及退出游...

一、界面

左上方播放音频按钮和停止播放下面是图片展示再下面是进入游戏以及退出游戏最后是命令输入框右边是消息框

C#基于TCP实现简单游戏客户端的完整实例

二、代码

1.播放音频

需要用到windowsmediaplayer组件,在常规组件的选择项里面可以找到。

C#基于TCP实现简单游戏客户端的完整实例

实现代码

		private void start_click(object sender, eventargs e)
        {
            //新建线程打开音频文件
            new thread(new threadstart(beginplay)).start();
        }

        private void stop_click(object sender, eventargs e)
        {
            //暂定播放
            axwindowsmediaplayer1.ctlcontrols.pause();
        }
 		public void beginplay()
        {
            string s = @"g:\visualstudioproject\gamewindows\resources\music.mp3";
            axwindowsmediaplayer1.url = s;
        }

2.播放图片

使用定时器,使能并且设置每1000ms触发一次事件

C#基于TCP实现简单游戏客户端的完整实例

事件函数

        private string[] pictures={ "\\1.jpg", "\\2.jpg","\\3.jpg","\\4.jpg","5.jpg","6.jpg"};
        private int postion = 0;
        private void timer1_tick(object sender, eventargs e)
        {
            picturebox1.image = image.fromfile("g:\\visualstudioproject\\gamewindows\\resources\\"+pictures[(++postion)%pictures.length]);
        }

3.登录和退出游戏

采用tcp协议和服务器建立连接

public void connection()
        {
            try
            {
                //向指定的ip地址的服务器发出连接请求

                tcpclient.connect("10.1.230.74", 3900);
                messages.items.add("连接成功!");
                stream = tcpclient.getstream();
                byte[] data = new byte[1024];
                //判断网络流是否可读            
                if (stream.canread)
                {
                    int len = stream.read(data, 0, data.length);
                    string msg = encoding.default.getstring(data, 0, data.length);
                    string str = "\r\n";
                    char[] str1 = str.tochararray();
                    string[] msg1 = msg.split(str1);
                    for (int j = 0; j < msg1.length; j++)
                    {
                        messages.items.add(msg1[j]);
                    }
                }

            }
            catch (system.exception ex)
            {
                messages.items.add(ex.message);
            }
        }

登录游戏建立tcp连接,并且禁用登录按钮使能退出按钮退出游戏则端口tcp连接,并且禁用退出按钮以及使能登录按钮

private void quitgame_click(object sender, eventargs e)
        {
            tcpclient.close();
            begingame.enabled = true;
            quitgame.enabled = false;
        }

4.命令交互

通过tcp向服务器发送数据

        private void sendbutton_click(object sender, eventargs e)
        {
            sendcommand();
        }
		public void sendcommand()
        {
            if (tcpclient.connected)
            {
                //向服务器发送数据
                string msg = command.text;
                byte[] outbytes = system.text.encoding.default.getbytes(msg + "\n");
                stream.write(outbytes, 0, outbytes.length);
                byte[] data = new byte[1024];
                //接收服务器回复数据
                if (stream.canread)
                {
                    int len = stream.read(data, 0, data.length);
                    string msg1 = encoding.default.getstring(data, 0, data.length);
                    string str = "\r\n";
                    char[] str1 = str.tochararray();
                    string[] msg2 = msg1.split(str1);
                    for (int j = 0; j < msg2.length; j++)
                    {
                        messages.items.add(msg2[j]);
                    }
                }
            }
            else
            {
                messages.items.add("连接已断开");
            }

        }

5.信息展示

通过listbox展示所有接收到的消息

6.最终代码

using system;
using system.drawing;
using system.net;
using system.net.sockets;
using system.windows.forms;
using system.text;
using system.threading;

namespace gamewindows
{
    public partial class form1 : form
    {
        private networkstream stream;
        private tcpclient tcpclient = new tcpclient();
        private string[] pictures={ "\\1.jpg", "\\2.jpg","\\3.jpg","\\4.jpg","5.jpg","6.jpg"};
        private int postion = 0;
        public form1()
        {
            initializecomponent();  
            checkforillegalcrossthreadcalls = false;
            quitgame.enabled = false;
            axwindowsmediaplayer1.hide();
            picturebox1.image = image.fromfile("g:\\visualstudioproject\\gamewindows\\resources\\"+pictures[postion]);
        }

        private void start_click(object sender, eventargs e)
        {
            //新建线程打开音频文件
            new thread(new threadstart(beginplay)).start();
        }

        private void stop_click(object sender, eventargs e)
        {
            //暂定播放
            axwindowsmediaplayer1.ctlcontrols.pause();
        }

        private void begingame_click(object sender, eventargs e)
        {

            thread thread = new thread(new threadstart(connection));
            thread.start();
            begingame.enabled = false;
            quitgame.enabled = true;

        }

 

        private void quitgame_click(object sender, eventargs e)
        {
            tcpclient.close();
            begingame.enabled = true;
            quitgame.enabled = false;
        }

        private void timer1_tick(object sender, eventargs e)
        {
            picturebox1.image = image.fromfile("g:\\visualstudioproject\\gamewindows\\resources\\"+pictures[(++postion)%pictures.length]);
        }



        private void sendbutton_click(object sender, eventargs e)
        {
            sendcommand();
        }


        public void connection()
        {
            try
            {
                //向指定的ip地址的服务器发出连接请求

                tcpclient.connect("10.1.230.74", 3900);
                messages.items.add("连接成功!");
                stream = tcpclient.getstream();
                byte[] data = new byte[1024];
                //判断网络流是否可读            
                if (stream.canread)
                {
                    int len = stream.read(data, 0, data.length);
                    string msg = encoding.default.getstring(data, 0, data.length);
                    string str = "\r\n";
                    char[] str1 = str.tochararray();
                    string[] msg1 = msg.split(str1);
                    for (int j = 0; j < msg1.length; j++)
                    {
                        messages.items.add(msg1[j]);
                    }
                }

            }
            catch (system.exception ex)
            {
                messages.items.add(ex.message);
            }
        }

        public void beginplay()
        {
            string s = @"g:\visualstudioproject\gamewindows\resources\music.mp3";
            axwindowsmediaplayer1.url = s;
        }

        public void sendcommand()
        {
            if (tcpclient.connected)
            {
                //向服务器发送数据
                string msg = command.text;
                byte[] outbytes = system.text.encoding.default.getbytes(msg + "\n");
                stream.write(outbytes, 0, outbytes.length);
                byte[] data = new byte[1024];
                //接收服务器回复数据
                if (stream.canread)
                {
                    int len = stream.read(data, 0, data.length);
                    string msg1 = encoding.default.getstring(data, 0, data.length);
                    string str = "\r\n";
                    char[] str1 = str.tochararray();
                    string[] msg2 = msg1.split(str1);
                    for (int j = 0; j < msg2.length; j++)
                    {
                        messages.items.add(msg2[j]);
                    }
                }
            }
            else
            {
                messages.items.add("连接已断开");
            }

        }
    }
}

三、效果

为了方便展示图片,播放间隔设置的1000ms。

C#基于TCP实现简单游戏客户端的完整实例

四、总结

c#在网络编程上特别的方便。

五、参考

六、源码

https://github.com/tangtangsix/gamewindows

到此这篇关于c#基于tcp实现简单游戏客户端的文章就介绍到这了,更多相关c#实现游戏客户端内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!