界面设计
2.图片PictureBox 设置 (1)Imge 背景设置?
?2)SizeMode选择StrechImage,会把PictureBox图片框铺满,自适应图片框的大小。
二、连接服务器
1.在Form 中初始化进入游戏:
private NetworkStream stream;
private TcpClient tcpClient = new TcpClient();
public Form1()
{
InitializeComponent();
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);
//Encoding ToEncoding = Encoding.GetEncoding("UTF-8");
//Encoding FromEncoding = Encoding.GetEncoding("GB2312");
//data=Encoding.Convert(FromEncoding, ToEncoding, data);
//string msg = Encoding.UTF8.GetString(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("服务器未启动!");
}
}
?
三、客户端向服务器发送数据
1.双击button12
?
private void button12_Click(object sender, EventArgs e)
{
//判断连接是否断开
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("连接已断开");
}
}
四、播放背景音乐
1.设置音乐按钮如下,一个Lable和两个Button
2.添加WindowsMediaPlayer控件,工具箱 ->常规右键- >选择项 ?
?
3.勾选COM组件 中的Windows Media Player ?双击Button13(播放键) 设置代码.如下
private void button13_Click(object sender, EventArgs e)
{
string s = @"C:\Users\86199\Music\xz\Something just like this.mp3";
axWindowsMediaPlayer1.URL = s;
}
private void button14_Click(object sender, EventArgs e)
{
//用于暂停正在播放的背景音乐
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
?
切换游戏背景图片
1.添加Timer组件
Timer 的属性Enable 设置True ?
添加代码如下
int flag = 0;
private void timer1_Tick(object sender, EventArgs e)
{
flag++;
string picturePath = @"F:\game\pictures\" + flag + ".jpg";
pictureBox1.Image = Image.FromFile(picturePath);
if (flag ==3)
{
flag = 0;
}
}
?
|