您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“C#基于TCP如何實現(xiàn)簡單游戲客戶端”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
左上方播放音頻按鈕和停止播放下面是圖片展示再下面是進(jìn)入游戲以及退出游戲最后是命令輸入框右邊是消息框
需要用到WindowsMediaPlayer組件,在常規(guī)組件的選擇項里面可以找到。
實現(xiàn)代碼
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; }
使用定時器,使能并且設(shè)置每1000ms觸發(fā)一次事件
事件函數(shù)
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協(xié)議和服務(wù)器建立連接
public void connection() { try { //向指定的IP地址的服務(wù)器發(fā)出連接請求 tcpClient.Connect("10.1.230.74", 3900); messages.Items.Add("連接成功!"); stream = tcpClient.GetStream(); byte[] data = new byte[1024]; //判斷網(wǎng)絡(luò)流是否可讀 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向服務(wù)器發(fā)送數(shù)據(jù)
private void sendButton_Click(object sender, EventArgs e) { sendCommand(); } public void sendCommand() { if (tcpClient.Connected) { //向服務(wù)器發(fā)送數(shù)據(jù) string msg = command.Text; Byte[] outbytes = System.Text.Encoding.Default.GetBytes(msg + "\n"); stream.Write(outbytes, 0, outbytes.Length); byte[] data = new byte[1024]; //接收服務(wù)器回復(fù)數(shù)據(jù) 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地址的服務(wù)器發(fā)出連接請求 tcpClient.Connect("10.1.230.74", 3900); messages.Items.Add("連接成功!"); stream = tcpClient.GetStream(); byte[] data = new byte[1024]; //判斷網(wǎng)絡(luò)流是否可讀 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) { //向服務(wù)器發(fā)送數(shù)據(jù) string msg = command.Text; Byte[] outbytes = System.Text.Encoding.Default.GetBytes(msg + "\n"); stream.Write(outbytes, 0, outbytes.Length); byte[] data = new byte[1024]; //接收服務(wù)器回復(fù)數(shù)據(jù) 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("連接已斷開"); } } } }
為了方便展示圖片,播放間隔設(shè)置的1000ms。
“C#基于TCP如何實現(xiàn)簡單游戲客戶端”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。