C # how to write the web tourist client
This article mainly shows you "how to write an online game client in C#". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to write an online game client in C#".
I. preliminary setup of the project
1. New project
Create a new WIndows forms application (.Net Framework):
Set the project name and location:
two。 Interface design
Right-click General in the toolbox and click the selection:
Check Windows Media Player under the COM component:
The interface is as follows:
Connect to the server
Initialize the entry into the game in Form:
Private NetworkStream stream;private TcpClient tcpClient = new TcpClient (); public Form1 () {InitializeComponent (); try {/ / issue a connection request tcpClient.Connect ("10.1.230.74", 3900) to the server at the specified IP address; listBox1.Items.Add ("connection succeeded!") ; stream = tcpClient.GetStream (); byte [] data = new byte [1024]; / / determine whether the network flow is readable 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("服务器未启动!"); }} 运行结果: 三、发送数据 设置确定按钮的name为btnSend,双击该按钮: //判断连接是否断开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("连接已断开");} 运行结果: 四、播放背景音乐 设置播放、停止按钮的name分别为btnPlay、btnStop:
If you can't see the effect of playing the music, the running result will not be played here.
Fifth, realize the background picture transformation of the game.
Drag a timer and set its Enable to True:
Double-click timer to write the code:
Int flag = 0private void timer1_Tick (object sender, EventArgs e) {flag++; string picturePath = @ "C:\ Users\ 16438\ Desktop\ game1\ bin\ Debug\ img\" + flag+ ".jpg"; pictureBox1.Image = Image.FromFile (picturePath); if (flag = = 3) {flag = 0;}}
Running result:
The above is all the content of this article "how to write online Game client in C#". Thank you for your reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!