您好,登錄后才能下訂單哦!
本篇內容介紹了“C#基于TCP如何實現簡單游戲客戶端”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
左上方播放音頻按鈕和停止播放下面是圖片展示再下面是進入游戲以及退出游戲最后是命令輸入框右邊是消息框
需要用到WindowsMediaPlayer組件,在常規組件的選擇項里面可以找到。
實現代碼
private void start_Click(object sender, EventArgs e) { //新建線程打開音頻文件 new Thread(new ThreadStart(beginPlay)).Start(); } private void stop_Click(object sender, EventArgs e) { //暫定播放 axWindowsMediaPlayer1.Ctlcontrols.pause(); } public void beginPlay() { string s = @"G:\VisualStudioProject\GameWindows\resources\music.mp3"; axWindowsMediaPlayer1.URL = s; }
使用定時器,使能并且設置每1000ms觸發一次事件
事件函數
private String[] pictures={ "\\1.jpg", "\\2.jpg","\\3.jpg","\\4.jpg","5.jpg","6.jpg"}; private int postion = 0; private void timer1_Tick(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile("G:\\VisualStudioProject\\GameWindows\\resources\\"+pictures[(++postion)%pictures.Length]); }
采用tcp協議和服務器建立連接
public void connection() { try { //向指定的IP地址的服務器發出連接請求 tcpClient.Connect("10.1.230.74", 3900); messages.Items.Add("連接成功!"); stream = tcpClient.GetStream(); byte[] data = new byte[1024]; //判斷網絡流是否可讀 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++) { messages.Items.Add(msg1[j]); } } } catch (System.Exception ex) { messages.Items.Add(ex.Message); } }
登錄游戲建立tcp連接,并且禁用登錄按鈕使能退出按鈕退出游戲則端口tcp連接,并且禁用退出按鈕以及使能登錄按鈕
private void quitGame_Click(object sender, EventArgs e) { tcpClient.Close(); beginGame.Enabled = true; quitGame.Enabled = false; }
通過tcp向服務器發送數據
private void sendButton_Click(object sender, EventArgs e) { sendCommand(); } public void sendCommand() { if (tcpClient.Connected) { //向服務器發送數據 string msg = command.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++) { messages.Items.Add(msg2[j]); } } } else { messages.Items.Add("連接已斷開"); } }
通過listbox展示所有接收到的消息
using System; using System.Drawing; using System.Net; using System.Net.Sockets; using System.Windows.Forms; using System.Text; using System.Threading; namespace GameWindows { public partial class Form1 : Form { private NetworkStream stream; private TcpClient tcpClient = new TcpClient(); private String[] pictures={ "\\1.jpg", "\\2.jpg","\\3.jpg","\\4.jpg","5.jpg","6.jpg"}; private int postion = 0; public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; quitGame.Enabled = false; axWindowsMediaPlayer1.Hide(); pictureBox1.Image = Image.FromFile("G:\\VisualStudioProject\\GameWindows\\resources\\"+pictures[postion]); } private void start_Click(object sender, EventArgs e) { //新建線程打開音頻文件 new Thread(new ThreadStart(beginPlay)).Start(); } private void stop_Click(object sender, EventArgs e) { //暫定播放 axWindowsMediaPlayer1.Ctlcontrols.pause(); } private void beginGame_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(connection)); thread.Start(); beginGame.Enabled = false; quitGame.Enabled = true; } private void quitGame_Click(object sender, EventArgs e) { tcpClient.Close(); beginGame.Enabled = true; quitGame.Enabled = false; } private void timer1_Tick(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile("G:\\VisualStudioProject\\GameWindows\\resources\\"+pictures[(++postion)%pictures.Length]); } private void sendButton_Click(object sender, EventArgs e) { sendCommand(); } public void connection() { try { //向指定的IP地址的服務器發出連接請求 tcpClient.Connect("10.1.230.74", 3900); messages.Items.Add("連接成功!"); stream = tcpClient.GetStream(); byte[] data = new byte[1024]; //判斷網絡流是否可讀 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++) { messages.Items.Add(msg1[j]); } } } catch (System.Exception ex) { messages.Items.Add(ex.Message); } } public void beginPlay() { string s = @"G:\VisualStudioProject\GameWindows\resources\music.mp3"; axWindowsMediaPlayer1.URL = s; } public void sendCommand() { if (tcpClient.Connected) { //向服務器發送數據 string msg = command.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++) { messages.Items.Add(msg2[j]); } } } else { messages.Items.Add("連接已斷開"); } } } }
為了方便展示圖片,播放間隔設置的1000ms。
“C#基于TCP如何實現簡單游戲客戶端”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。