C#Simple Game Client Programming (VS)

Preface

Write a web visitor client, the game server's intranet IP address is 10.1.230.74, port is 3900, using TCP connection.
1) After successful connection, messages from the server can be displayed in the listbox continuously;
2) The data the client wants to send to the server, enter through textbox or click button;
3) Ability to play background music;
4) Change the background picture of the game every 30 seconds.

The IP address here must be connected to the WiFi network of the pen master school in order to connect successfully.

1. Create a project

  • The pen owner here uses VS2017 version due to computer storage limitation. Other versions may be slightly different, but the content is consistent.

2. Interface Design

  • A listBox, button s, textboxes, and a pictureBox

3. Code

3.1 Connect to the server and receive data

  • Double-click the button to enter the game
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Make a connection request to the server with the specified IP address
                tcpClient.Connect("10.1.230.74", 3900);
                listBox1.Items.Add("Connection successful!");
                stream = tcpClient.GetStream();
                byte[] data = new byte[1024];
                //Determine whether network streams are 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; j++)
                    {
                        listBox1.Items.Add(msg1[j]);
                         music_play();
                    }
                }
            }
            catch
            {
                listBox1.Items.Add("Connection failed!");
            }
        }

3.2 Client sends data to server

  • Double-click OK button
private void button12_Click(object sender, EventArgs e)
        {
            music_play();
            if (tcpClient.Connected)
            {
                //Send data to 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; j++)
                    {
                        listBox1.Items.Add(msg2[j]);
                    }
                }
            }
            else
            {
                listBox1.Items.Add("connection dropped");
            }
            textBox1.Clear();
        }

3.3 Play background music

  • Double-click to open

  • Select a.wav audio file

Note that the file type is.wav. You cannot simply change the suffix name here. You need to use a format conversion tool to convert it.
A website that can download audio in.wav format: Download wav audio

  • Add a play function to your code
private void music_play()
        {
            SoundPlayer sp = new SoundPlayer();
            sp.SoundLocation = @"D:\VSdaima\playgame\playgame\Fei Yuqing - Summer trip.wav";       //Your music file name, and note that it must be a wav file
            sp.PlayLooping();
        }

3.4 Automatically change game background pictures on a regular basis

  • Back to Form1.cs [Design] window
  • Find the Timer control in the toolbox and drag it to the empty area of the window

  • Click timer1 to find the Interval in Settings, set to 1000, and set Enable to true

  • Prepare background pictures, using jpg pictures here, and name the pictures as numbers starting with 1

  • Double-click timer to add code:
 private void timer1_Tick(object sender, EventArgs e)
        {
            Thread th = new Thread(pic_play);
            th.IsBackground = true;
            th.Start();
        }
       
  • Add Picture Switching Function
    Set the path before adding (the path to store the previous picture):

  • SizeMode chooses StrechImage, which will fill the PictureBox frame with an adaptive size
  • Code
void pic_play()
       {
            picture++;   //Remember to define the variable picture before
            string picturePath = @"D:\VSdaima\playgame\playgame\tu\" + picture + ".jpeg";
            //Set Picture Fill
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.Image = Image.FromFile(picturePath);
            if (picture == 5)
                picture = 0;
        }

IV. EXPERIMENTAL RESULTS

  • The final results are as follows:

A small summary

C# is a convenient visual interface, so beginners can start with it directly. Just drag the control and double-click the control to write the corresponding function.

Setting up a network connection is also very convenient, but this IP can only ping successfully under the condition of WiFi network of pen master school, other networks can not!!! The pit that the pen master stepped in.

When connecting to a network IP, you can try ping first in win+R->cmd.

Reference

  1. telnet is not an internal or external command
  2. [C#] VS Writes a simple web visitor client

Tags: C# Programming network

Posted on Sat, 27 Nov 2021 13:11:35 -0500 by barrylee