c#游戏客户端编程

一、测试服务器连通性

  • 开始菜单输入cmd打开控制台,输入ping 10.1.230.74(该ip是校园的内网ip,不在内网还是ping不通的)
    在这里插入图片描述
  • 可以ping通,输入telnet,进入telnet界面
    在这里插入图片描述

无法进入telnet,显示“telnet不是本地命令”,可能是因为没有安装telnet,可以参考Telnet 详解 及命令使用来安装telnet

  • 输入set localecho,打开本地回显:
    在这里插入图片描述
  • 连接服务器,输入命令open 10.1.230.74 3900,进入游戏
    在这里插入图片描述

二、编写设计客户端

  • 新建项目,打开VS2019选择新建Windows窗体应用
    在这里插入图片描述
  • 设计窗口界面
    在这里插入图片描述
  • 实现连接到服务器并接受数据,双击Button“开始游戏”,添加代码
private NetworkStream stream;
private TcpClient tcpClient = new TcpClient();
private int picture = 0;
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //向指定的IP地址的服务器发出连接请求
                tcpClient.Connect("10.1.230.41", 3900);
                listBox1.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++)
                    {
                        listBox1.Items.Add(msg1[j]);
                         music_play();
                    }
                }
            }
            catch
            {
                listBox1.Items.Add("连接失败!");
            }
        }
  • 实现客户端发送数据到服务器,双击发送的Button,添加代码
private void button12_Click(object sender, EventArgs e)
        {
            music_play();
            if (tcpClient.Connected)
            {
                //向服务器发送数据
                string msg = textBox1.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++)
                    {
                        listBox1.Items.Add(msg2[j]);
                    }
                }
            }
            else
            {
                listBox1.Items.Add("连接已断开");
            }
            textBox1.Clear();
        }
  • 播放背景音乐,在项目引用里增加COM组件WindowsMediaPlayer
    在这里插入图片描述
    在这里插入图片描述
    代码
 private void music_play()
        {
			WMPLib.WindowsMediaPlayer Player = new WMPLib.WindowsMediaPlayer();
            Player.URL = @"D:\CloudMusic\阿保剛 - Karma -Piano version-.mp3";
            Player.controls.play();
        }
  • 定时自动更换游戏背景图片,在工具箱中找到Timer控件,将它拖动到窗口空白区域在这里插入图片描述

  • 点击timer1,找到设置中的Interval,设置为30000,将Enable设为true。在这里插入图片描述

  • 准备好背景图片,将图片分别命名为从1开始的数字。双击timer1,添加代码:

 private void timer1_Tick(object sender, EventArgs e)
        {
            Thread th = new Thread(pic_play);
            th.IsBackground = true;
            th.Start();
        }
void pic_play()
        {
            picture++;   //记得在前面定义变量picture
            string picturePath = @"C:\Users\28205\Pictures\game\" + picture + ".jpg";
            //设置图片填充
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.Image = Image.FromFile(picturePath);
            if (picture == 6)
                picture = 0;
        }
  • 设置tick事件(这很重要!!)
    在这里插入图片描述
  • 运行
    在这里插入图片描述

三、总结

自己完成一个游戏客户端,与服务器实现交互还是很有意思的,可惜还是有一些bug没有解决,服务端返回的代码显示时也还有很多乱码,就等以后有时间再解决吧


参考链接

https://blog.csdn.net/u011889811/article/details/43356075
https://blog.csdn.net/weixin_45888898/article/details/109788760

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Course Technology PTR, 2010 Even experienced game developers sometimes have a hard time making their vision for a great game a reality. The number of available programming languages, libraries, and production methods can make the development process overwhelming and result in complicated, unreliable game code. C# Game Programming: For Serious Game Creation shows programmers how to write simple, clean, and reliable code step-by-step through the creation of a basic game. The game is built using C#, a high-level programming langua ge, and OpenGL, an industry favorite for graphics display. You'll get an overview of the methods and libraries used to build good games, learn how to use those libraries and create your own, and finally build your own scrolling shooter game. You'll even find tips and information on how to develop your own game ideas and you'll have an excellent code base to work with. C# Game Programming: For Serious Game Creation provides you with all the information you need to take your game ideas from concept to completion. Aboit author Daniel Schuller is a British-born computer game developer who has worked and lived in the United States, Singapore, Japan, and is currently working in the United Kingdom. He has released games on the PC as well as the Xbox 360 and PlayStation 3. Schuller has developed games for Sony, Ubisoft, Naughty Dog, RedBull, and Wizards of the Coast, and maintains a game development website at http://www.godpatterns.com. In addition to developing computer games, Schuller also studies Japanese and is interested in Artificial Intelligence, cognition, and the use of games in education. amazon link:http://www.amazon.com/exec/obidos/ASIN/1435455568/buythisbooks-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值