C#编写游戏客户端
时间:2023-04-08 19:37:00
少年
- 一、连接客户端原理流程图
- 二、功能要求
- 三、实现代码
- 四、总结
- 五、参考
一、连接客户端原理流程图
二、功能要求
1)连接成功后,服务器发送的信息可以不断显示 listbox 中;
2) 通过 textbox输入 或者点击button;
3) 可播放背景音乐;
4) 每30秒换一次游戏背景图片。
三、实现代码
- 新建一个Windows窗体应用
- 以下是界面布局。记得还有一个Timer,并且timer要使能,interval也要设置,这里3000为3秒
- 定义的变量
private NetworkStream stream; private TcpClient tcpClient; SoundPlayer player = new SoundPlayer("D:/新文件夹 (2)/p3r68-cdx67/59tp0-mg2nx.wav"); ///定义发送数据的套接字 Socket socket_send;
- receiver_stream功能是接收网络流并显示函数listbox里面显示,关于GBK请参考这里的编码
C# 中使用GB2312或GBK编码报错
/* * 将字节流用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;
}
}
- 效果
四、总结
- C#做可视化界面很方便,比QT还简单,代码格式有点像Java,基本上可以直接上手了,搭建网络连接也很方便,不过有时候界面的设置不小心就看过了。这次还是遇到麻烦的,本来想用Windows media player来播放音乐,但因为电脑一直更新走的,版本不适配用不了,最后用的是自带的播放器,只能播放wav文件,直接改后缀还不行,需要转化一下。
五、参考
编写一个网游客户端
C# 中使用GB2312或GBK编码报错