1、界面设计
1、首先创建一个窗体项目,创建完成之后会直接打开界面设计页
2、将需要的控件放到窗体中,并进行布局设计
2、完善代码
双击进入游戏的button,然后进入代码页面,并在进入游戏的button函数中添加代码
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("连接失败!");
}
2、再双击确定的button,编入代码是发生的数据连接到服务器上
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();
3、在项目结构中找到Form1.resx文件并打开,然后选择音频,添加资源
4、找到要使用的背景音乐,添加进去
5、添加播放音乐的代码,使其在客户端发送数据时被调用。
private void music_play()
{
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"刘俊宇,刘绮翘,金海天 - 铁血丹心.wav"; //你的音乐文件名称,且注意必须是wav文件
sp.PlayLooping();
}
6、找到添加的Timer控件,点击属性,然后点击timer1,找到设置中的Interval,设置为30000(即30秒),将Enable设为true。并双击timer,添加函数
Thread th = new Thread(pic_play);
th.IsBackground = true;
th.Start();
7、找到准备好背景图片,复制文件路径,然后编写图片切换的代码
void pic_play() //实现图片切换的函数
{
picture++; //记得在前面定义变量picture
string picturePath = @"D:\picture\jinyong\" + picture + ".jfif";
//设置图片填充
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = Image.FromFile(picturePath);
if (picture == 4)
picture = 0;
}
8、运行结果,转换成动图有点长
3、总结
使用vs来完成界面设计是一个比较轻松的的选择,它看起来更加的直观,界面的设计也非常的方便,只需要拖动需要的控件即可。而完成布局之后点击相应的控件即可进入代码的编写,我个人认为还是非常方便的。
参考:
https://blog.csdn.net/qq_43279579/article/details/109693257
https://blog.csdn.net/qq_43279579/article/details/109693257