一、项目设置
- 新建项目
- 创建Windows窗体应用(.Net Framework)
- 后续操作中要添加音乐,因此勾选设置,添加组件
工具箱-> 选择项 -> COM->Windows Media Player - 绘制界面,并添加必要插件的属性
二、搭建客户端服务器
- 连接服务器可以进行登录操纵
private NetworkStream stream;
private TcpClient tcpClient = new TcpClient();
public Form1()
{
InitializeComponent();
try
{
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("服务器未启动!");
}
}
三、数据交互
- 发送数据
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("连接已断开");
}
- 运行结果
四、插入背景音乐
private void button13_Click(object sender, EventArgs e)
{
string s = @"C:\Users\24768\Desktop\game1\城里的月光.mp3";
axWindowsMediaPlayer1.URL = s;
}
private void button14_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
五、切换背景图片
private String[] pictures = { "\\1.jpg", "\\2.jpg", "\\3.jpg" };
private int postion = 0;
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("E:\\md\\wangluo\\g\\game1\\bin\\Debug\\img\\" + pictures[(++postion) % pictures.Length]);
}
- 进行图片切换,添加Timer控件
- 运行结果
参考
C#编写网游客户端 https://blog.csdn.net/qq_47281915/article/details/121561464?spm=1001.2014.3001.5501
|