C#实现游戏客户端的编程

本文详细介绍了使用C#在VS2019环境下创建网游客户端的过程,包括连接校园内网服务器、接收并显示消息、用户输入数据发送、背景音乐播放以及每30秒自动更换游戏背景图片的功能实现。通过WindowsMediaPlayer控件控制音乐播放,使用TcpClient进行网络通信,并利用Timer控件实现实时图片轮播。
摘要由CSDN通过智能技术生成

一、环境准备

编译软件:VS2019

操作系统:Windows10

客户端要求:

编写一个网游客户端,游戏服务器的校园内网IP地址为 10.1.230.74,端口为3900,采用TCP连接。

1)连接成功后,可以将服务器发来的消息不停地显示在 listbox 中;

  1. 客户端要发给服务器的数据,通过 textbox输入 或者点击button;

  2. 能够播放背景音乐;

  3. 每隔30秒,变换一次游戏背景图片。

二、项目制作

①创建工程与界面设计

打开VS2019创建一个新的工程
请添加图片描述
创建好工程后,首先要添加一个WindowsMediaPlayer 的控件,用于播放音乐

点击工具箱,右键常规,选择项
请添加图片描述
选择COM项中的WindowsMediaPlayer,然后确定
请添加图片描述
添加好Media Player控件之后,设计如下的界面:
请添加图片描述其中MainView需要特殊设置多行显示,滚动条垂直滚动的属性

在这里插入图片描述
VolumnBar最大值为100,用于音量调节
在这里插入图片描述

②连接服务器

双击开始游戏按键,自动生成click函数,在函数体内编写如下代码

 TcpClient tcpClient;
        NetworkStream stream;
        private void Btn_start_Click(object sender, EventArgs e)
        {
        /*
       * 连接服务器
       */
            try
            {
                //实例化
                tcpClient = new TcpClient();
                //向指定的IP地址的服务器发出连接请求
                tcpClient.Connect("10.1.230.74", 3900);
                //获取网络传输流
                stream = tcpClient.GetStream();
                //接受数据并转化为字符串
                byte[] data = new byte[1024];
                int receive = stream.Read(data, 0, 1024);
                string msg = Encoding.Default.GetString(data, 0, receive);
                //显示出来
                MainView.AppendText(msg);
            }
            catch
            {
                MainView.AppendText("服务器未启动" + Environment.NewLine);
            }
        }

运行效果
请添加图片描述

③数据传输

双击发送按钮,生成onclick函数,编写代码发送请求

   private void Btn_send_Click(object sender, EventArgs e)
        {
            //获取InputView内的文本内容
            string msg = inputView.Text + "\n";
            //将文本内容转化成比特流并发送给服务器
            byte[] data = new byte[1024];
            data = Encoding.Default.GetBytes(msg);
            stream.Write(data, 0, data.Length);
            //接收服务器端传来的数据流并转化为字符串
            byte[] data1 = new byte[1024];
            int receive = stream.Read(data1, 0, 1024);
            msg = Encoding.Default.GetString(data1, 0, receive);
            //清除显示框之前的内容
            MainView.Clear();
            //显示数据
            MainView.AppendText(msg);
            //刷新输入框
            inputView.Clear();
            //将光标集中到输入框中
            inputView.Focus();
        }

运行效果
请添加图片描述

④背景音乐播放

在构造函数中添加以下代码

  public Form1()
        {
            InitializeComponent();
 
                //设置音量调节器的初始位置
                VolumnBar.Value = 50;
                lbl_volumn.Text = "50";
                MediaPlayer.Hide();
 
        }

然后在Btn_start的点击函数中加入这段代码

* 播放背景音乐
    ******************/
            //设置音量键值
            VolumnBar.Value = 50;
            lbl_volumn.Text = "50";
            //字符串存储音乐路径
            string s = @"D:\music\music.mp3";
            //设置为循环播放
            MediaPlayer.settings.setMode("loop", true);
            //设置初始音乐的音量大小(范围:0——100)
            MediaPlayer.settings.volume = 50;
            //设置播放歌曲的路径
            MediaPlayer.URL = s;

其中s为储存mp3文件的路径

双击音量调节滑动条,写入代码

  private void VolumnBar_Scroll(object sender, EventArgs e)
        {
                //控制音量大小
                MediaPlayer.settings.volume = VolumnBar.Value;
                //控制音量显示
                lbl_volumn.Text = VolumnBar.Value.ToString();
 
        }

这样就可以通过拖动滑动条来调节音量大小了。

⑤图片轮播

在工具箱中找到Timer控件,将它拖入窗体中

Timer的设置
在这里插入图片描述
双击Timer,进入函数,编写以下代码

 private int flag;
        private void timer1_Tick(object sender, EventArgs e)
        {
            flag++;
            string picturePath = @"E:\GamePic\" + flag + ".jpg";
            //设置图片填充
            PictureBar.SizeMode = PictureBoxSizeMode.Zoom;
            PictureBar.Image = Image.FromFile(picturePath);
            if (flag == 5)
            {
                flag = 0;
            }
        }

其中PicturePath是放图片的路径

准备五张图片,命名为1.2.3.4.5
在这里插入图片描述
效果如下
请添加图片描述

三、参考文章

C#编写网游客户端连接游戏服务器

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、付费专栏及课程。

余额充值