In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to write a simple online game client using VS". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
one。 Test connection to the server
1. Open cmd and enter ping 10.1.230.74
two。 Enter telnet to enter the telnet interface
3. Enter set localecho to turn on the local echo:
4. To connect to the server, enter the command open 10.1.230.74 3900
two。 Design client
1. New project
Open VS2022 and select New Windows forms Application
If it is not found, the corresponding configuration is not installed. You can add tools.
Choice
You can find it if the installation is successful.
two。 Design interface
After the new project is completed, you will go directly to the Form1.cs [Design] window, where you can click on the toolbox and drag the control to the design window.
The design window is as follows:
3. Control event
Implement to connect to the server and accept data
Double-click to enter the game's Button
Add the following code:
Private void button1_Click (object sender, EventArgs e) {try {/ / issue a connection request tcpClient.Connect ("10.1.230.41", 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) 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) ) {listBox1.Items.Add (msg1 [j]); music_play ();} catch {listBox1.Items.Add ("connection failed!") ;}}
Realize that the client sends data to the server
Double-click the sent Button
Add the following code:
Private void button12_Click (object sender, EventArgs e) {music_play (); if (tcpClient.Connected) {/ / send data to the server string msg = textBox1.Text; Byte [] outbytes = System.Text.Encoding.Default.GetBytes (msg + "\ n") Stream.Write (outbytes, 0, outbytes.Length); byte [] data = new byte [1024]; / / receive server reply data 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) ) {listBox1.Items.Add (msg2 [j]);} else {listBox1.Items.Add ("disconnected");} textBox1.Clear ();}
Play background music
Find the Form1.resx file in the project structure and open it
Select audio and add resources
Note that the file type is .wav, you can't simply change the suffix here, you need to convert it with a format conversion tool.
Add a playback function to the code
Private void music_play () {SoundPlayer sp = new SoundPlayer (); sp.SoundLocation = @ "Liu Junyu, Liu Yiqiao, Jin Haitian-Iron Blood Danxin .wav"; / / the name of your music file, and note that it must be a wav file sp.PlayLooping ();}
This function is called when the client sends data.
Change the background picture of the game regularly and automatically.
Go back to the window of Form1.cs [Design]
Find the Timer control in the toolbox and drag it to a blank area of the window
Click timer1, find the Interval in the settings, set it to 30000, and set Enable to true.
Get the background picture ready. Here you use the jpg picture, and name the picture a number starting with 1.
Double-click timer1
Add code:
Private void timer1_Tick (object sender, EventArgs e) {Thread th = new Thread (pic_play); th.IsBackground = true; th.Start ();}
Add picture switch function
Void pic_play () {picture++; / / remember to define the variable picture string picturePath = @ "C:\ Users\ 28205\ Pictures\ game\" + picture+ ".jpg"; / / set the image fill pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; pictureBox1.Image = Image.FromFile (picturePath); if (picture = = 6) picture = 0 } three. Running effect
This is the end of the content of "how to write a simple online game client using VS". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.