实验要求
连接成功后,可以将服务器发来的消息不停地显示在 listbox 中;
-
客户端要发给服务器的数据,通过 textbox输入 或者点击button;
-
能够播放背景音乐;
-
每隔30秒,变换一次游戏背景图片。
实现代码
创建新项目,取名为game
界面布置:
定义的变量:
private NetworkStream stream;
private TcpClient tcpClient;
SoundPlayer player = new SoundPlayer("D:\下载\jtbw.wav");
//定义发送数据的套接字
Socket socket_send;
receiver_stream函数,功能是接收网络流并显示在listbox里显示
/*
* 将字节流用GBK格式编码在listbox里显示
*/
void receive_stream()
{
byte[] receive_data = new byte[1024];
//定义编码格式
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);//为使用GB2312做准备
System.Text.Encoding GBK = System.Text.Encoding.GetEncoding(“GBK”);
if (stream.CanRead)
{
int len = stream.Read(receive_data, 0, receive_data.Length);
string msg = GBK.GetString(receive_data, 0, receive_data.Length);
string str = "\r\n";
char[] str1 = str.ToCharArray();
//乱码集合
string[] messy_code = { "??[2J ", "[5m", "[44m", "[37;0m", "[1;33m", "[1;32m", "[1;31m" };
string[] msg1 = msg.Split(str1);//以换行符为分隔符
for (int j = 0; j < msg1.Length; j++)//逐行显示
{
//过滤乱码
msg1[j] = msg1[j].Replace(messy_code[0], " ");
msg1[j] = msg1[j].Replace(messy_code[1], " ");
msg1[j] = msg1[j].Replace(messy_code[2], " ");
msg1[j] = msg1[j].Replace(messy_code[3], " ");
msg1[j] = msg1[j].Replace(messy_code[4], " ");
msg1[j] = msg1[j].Replace(messy_code[5], " ");
msg1[j] = msg1[j].Replace(messy_code[6], " ");
listBox1.Items.Add(msg1[j]);
}
}
}
send_stream函数,发送网络流的函数
void send_stream(string str)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);//为使用GB2312做准备
System.Text.Encoding GBK = System.Text.Encoding.GetEncoding("GBK");
byte[] buffer = GBK.GetBytes(str+"\n");
stream.Write(buffer, 0, buffer.Length);
}
连接客户端
private void start_game_Click(object sender, EventArgs e)
{
tcpClient = new TcpClient();
//套接字建立连接
socket_send = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(IPAddress.Parse("10.1.230.74"), 3900);
socket_send.Connect(point);
try
{
//向指定的IP地址的服务器发送连接请求
tcpClient.Connect("10.1.230.74", 3900);
listBox1.Items.Add("连接成功");
stream = tcpClient.GetStream();
receive_stream();//接收字节流并显示在屏幕上
}
catch
{
listBox1.Items.Add("服务器未启动");
}
}
退出游戏
private void end_game_Click(object sender, EventArgs e)
{
if (stream != null)//关闭连接,关闭流
{
stream.Close();
tcpClient.Close();
socket_send.Close();
}
listBox1.Items.Add("已经退出游戏");
}
获取textbox里的值并发送
private void send_msg_Click(object sender, EventArgs e)
{
if (tcpClient.Connected) {
string action = textBox1.Text.ToString();
listBox1.Items.Add("输入的信息为:" + action);
send_stream(action);
receive_stream();
}
else
{
listBox1.Items.Add("连接已断开");
}
}
播放音乐,这里的play好像就是在子线程播放,所以不用写新线程
private void play_Click(object sender, EventArgs e)
{
player.Load();
player.Play();
}
停止播放
private void stop_Click(object sender, EventArgs e)
{
player.Stop();
}
定时切换图片:
private void timer1_Tick(object sender, EventArgs e)
{
Thread th = new Thread(play_pic);
th.IsBackground=true;
th.Start();
}
void play_pic()
{
flag++;
string picturePath = @"D:\testpic\" + flag + ".jpg";
pictureBox1.Image = Image.FromFile(picturePath);
if (flag == 5)
{
flag = 0;
}
}
效果:
参考文献:https://blog.csdn.net/junseven164/article/details/121481789?spm=1001.2014.3001.5501