打开cmd,输入ping 10.1.230.74
,游戏服务器的校园内网IP地址为 10.1.230.74,需要连接校园网
接着输入telnet,如果输入telnet提示无效命令,去控制面板–程序–程序和功能–启动或关闭Windows功能,然后按提示勾选,重启电脑
输入telnet进入telnet命令界面
输入set localecho,打开本地回显,连接服务器,输入命令open 10.1.230.74 3900
客户端框架
打开框架大致有如下的组件,这些组件还没附上功能代码
双击进入游戏的按钮,代码如下
private void button10_Click(object sender, EventArgs e) { try { //向指定的IP地址的服务器发出连接请求 tcpClient.Connect("10.1.230.74", 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]); } } } catch { listBox1.Items.Add("连接失败!"); } }
点击发送的按钮
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(); }
添加背景音乐
音频文件必须是.wav后缀,需要用软件进行转换而不是直接改后缀名
播放函数
private void music_play() { SoundPlayer sp = new SoundPlayer(); sp.SoundLocation = @"jtbw.wav"; //你的音乐文件名称,且注意必须是wav文件 sp.PlayLooping(); }
背景图片切换
创建一个timer控件
点击该控件,修改其属性
准备好存放背景图的文件夹,将每张照片按顺序1~n存放
代码
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 == 4) picture = 0; }
在开头添加以下代码以定义对象变量
private int picture; private NetworkStream stream; private TcpClient tcpClient = new TcpClient();
if(picture==n)记得修改为你存放的照片数量
参考链接