1,´úÂë
ÏÈн¨ÏîÄ¿ Éè¼Æ½çÃæ ¼Çס,»¹ÓÐÍ϶¯Ò»¸ötimerµ½pictureboxÉÏ
Ë«»÷¿ªÊ¼ÓÎÏ·,½øÈë´úÂë±àд½çÃæ
private void button4_Click(object sender, EventArgs e)
{
tcpClient = new TcpClient();
//Ì×½Ó×Ö½¨Á¢Á¬½Ó
socket_send = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(IPAddress.Parse("10.1.230.74"), 3900);
socket_send.Connect(point);
try
{
//ÏòÖ¸¶¨µÄIPµØÖ·µÄ·þÎñÆ÷·¢ËÍÁ¬½ÓÇëÇó
tcpClient.Connect("10.1.230.74", 3900);
listBox1.Items.Add("Á¬½Ó³É¹¦");
stream = tcpClient.GetStream();
receive_stream();//½ÓÊÕ×Ö½ÚÁ÷²¢ÏÔʾÔÚÆÁÄ»ÉÏ
}
catch
{
listBox1.Items.Add("·þÎñÆ÷δÆô¶¯");
}
}
Ë«»÷·¢ËÍ,½ñ½øÈë´úÂë±àд
private void button1_Click(object sender, EventArgs e)
{
if (tcpClient.Connected)
{
string action = textBox1.Text.ToString();
listBox1.Items.Add("ÊäÈëµÄÐÅϢΪ:" + action);
send_stream(action);
receive_stream();
}
else
{
listBox1.Items.Add("Á¬½ÓÒѶϿª");
}
}
Ë«»÷²¥·Å,½øÈë´úÂë±àд
private void button2_Click(object sender, EventArgs e)
{
player.Load();
player.Play();
}
private void music_play()
{
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"ÐíáÔ.wav"; //ÄãµÄÒôÀÖÎļþÃû³Æ,ÇÒ×¢Òâ±ØÐëÊÇwavÎļþ
sp.PlayLooping();
}
½øÈëresxÎļþ,°´ÕÕÒÔÏÂÎļþÌí¼ÓÒª²¥·ÅµÄÒôÀÖ
½«×Ö½ÚÁ÷ÓÃGBK¸ñʽ±àÂëÔÚlistboxÀïÏÔʾ
void receive_stream()
{
byte[] receive_data = new byte[1024];
//¶¨Òå±àÂë¸ñʽ
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);//ΪʹÓÃGB2312×ö×¼±¸
System.Text.Encoding GBK = System.Text.Encoding.GetEncoding("GBK");
if (stream.CanRead)
{
int len = stream.Read(receive_data, 0, receive_data.Length);
string msg = GBK.GetString(receive_data, 0, receive_data.Length);
string str = "\r\n";
char[] str1 = str.ToCharArray();
//ÂÒÂ뼯ºÏ
string[] messy_code = { "ÿ?ÿ?[2J ", "[5m", "[44m", "[37;0m", "[1;33m", "[1;32m", "[1;31m" };
string[] msg1 = msg.Split(str1);//ÒÔ»»ÐзûΪ·Ö¸ô·û
for (int j = 0; j < msg1.Length; j++)//ÖðÐÐÏÔʾ
{
//¹ýÂËÂÒÂë
msg1[j] = msg1[j].Replace(messy_code[0], " ");
msg1[j] = msg1[j].Replace(messy_code[1], " ");
msg1[j] = msg1[j].Replace(messy_code[2], " ");
msg1[j] = msg1[j].Replace(messy_code[3], " ");
msg1[j] = msg1[j].Replace(messy_code[4], " ");
msg1[j] = msg1[j].Replace(messy_code[5], " ");
msg1[j] = msg1[j].Replace(messy_code[6], " ");
listBox1.Items.Add(msg1[j]);
}
}
}
Ë«»÷Í˳ö,½øÐбàд
void send_stream(string str)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);//ΪʹÓÃGB2312×ö×¼±¸
System.Text.Encoding GBK = System.Text.Encoding.GetEncoding("GBK");
byte[] buffer = GBK.GetBytes(str + "\n");
stream.Write(buffer, 0, buffer.Length);
}
private void button5_Click(object sender, EventArgs e)
{
{
if (stream != null)//¹Ø±ÕÁ¬½Ó,¹Ø±ÕÁ÷
{
stream.Close();
tcpClient.Close();
socket_send.Close();
}
listBox1.Items.Add("ÒѾÍ˳öÓÎÏ·");
}
}
Ë«»÷Í£Ö¹,½øÐбà¼
private void button3_Click(object sender, EventArgs e)
{
player.Stop();
}
½øÐÐͼÏñÌî³ä,ÒÀ¾ÉË«»÷
private void timer1_Tick(object sender, EventArgs e)
{
picture++;
string picturePath = @"C:\\Users\\hp\source\\repos\\game\\game\\game\" + picture + ".jpg";
//ÉèÖÃͼƬÌî³ä
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = Image.FromFile(picturePath);
if (picture == 5)
picture = 0;
}
2,½á¹ûÈçÏÂ
ÓÉÓÚ30Ì«³¤,²»Ì«ÈÝÒ׿´µ½Ê±¼ä±ä»¯,ÎÒ¼ÆÁÊÇÉèÖõÄ1000,¼´1ÃëÖÓ»»Ò»ÕÅͼƬ
3,ÍêÕû´úÂë
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Media; using System.Threading;
namespace game { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private NetworkStream stream; private TcpClient tcpClient; SoundPlayer player = new SoundPlayer(¡°C:/Users/hp/Desktop/ÐíáÔ.wav¡±); Socket socket_send; private int picture = 0;
private void button4_Click(object sender, EventArgs e)
{
tcpClient = new TcpClient();
//Ì×½Ó×Ö½¨Á¢Á¬½Ó
socket_send = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(IPAddress.Parse("10.1.230.74"), 3900);
socket_send.Connect(point);
try
{
//ÏòÖ¸¶¨µÄIPµØÖ·µÄ·þÎñÆ÷·¢ËÍÁ¬½ÓÇëÇó
tcpClient.Connect("10.1.230.74", 3900);
listBox1.Items.Add("Á¬½Ó³É¹¦");
stream = tcpClient.GetStream();
receive_stream();//½ÓÊÕ×Ö½ÚÁ÷²¢ÏÔʾÔÚÆÁÄ»ÉÏ
}
catch
{
listBox1.Items.Add("·þÎñÆ÷δÆô¶¯");
}
}
private void button1_Click(object sender, EventArgs e)
{
if (tcpClient.Connected)
{
string action = textBox1.Text.ToString();
listBox1.Items.Add("ÊäÈëµÄÐÅϢΪ:" + action);
send_stream(action);
receive_stream();
}
else
{
listBox1.Items.Add("Á¬½ÓÒѶϿª");
}
}
private void music_play()
{
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"ÐíáÔ.wav"; //ÄãµÄÒôÀÖÎļþÃû³Æ,ÇÒ×¢Òâ±ØÐëÊÇwavÎļþ
sp.PlayLooping();
}
/*
* ½«×Ö½ÚÁ÷ÓÃGBK¸ñʽ±àÂëÔÚlistboxÀïÏÔʾ
*/
void receive_stream()
{
byte[] receive_data = new byte[1024];
//¶¨Òå±àÂë¸ñʽ
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);//ΪʹÓÃGB2312×ö×¼±¸
System.Text.Encoding GBK = System.Text.Encoding.GetEncoding("GBK");
if (stream.CanRead)
{
int len = stream.Read(receive_data, 0, receive_data.Length);
string msg = GBK.GetString(receive_data, 0, receive_data.Length);
string str = "\r\n";
char[] str1 = str.ToCharArray();
//ÂÒÂ뼯ºÏ
string[] messy_code = { "ÿ?ÿ?[2J ", "[5m", "[44m", "[37;0m", "[1;33m", "[1;32m", "[1;31m" };
string[] msg1 = msg.Split(str1);//ÒÔ»»ÐзûΪ·Ö¸ô·û
for (int j = 0; j < msg1.Length; j++)//ÖðÐÐÏÔʾ
{
//¹ýÂËÂÒÂë
msg1[j] = msg1[j].Replace(messy_code[0], " ");
msg1[j] = msg1[j].Replace(messy_code[1], " ");
msg1[j] = msg1[j].Replace(messy_code[2], " ");
msg1[j] = msg1[j].Replace(messy_code[3], " ");
msg1[j] = msg1[j].Replace(messy_code[4], " ");
msg1[j] = msg1[j].Replace(messy_code[5], " ");
msg1[j] = msg1[j].Replace(messy_code[6], " ");
listBox1.Items.Add(msg1[j]);
}
}
}
void send_stream(string str)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);//ΪʹÓÃGB2312×ö×¼±¸
System.Text.Encoding GBK = System.Text.Encoding.GetEncoding("GBK");
byte[] buffer = GBK.GetBytes(str + "\n");
stream.Write(buffer, 0, buffer.Length);
}
private void button5_Click(object sender, EventArgs e)
{
{
if (stream != null)//¹Ø±ÕÁ¬½Ó,¹Ø±ÕÁ÷
{
stream.Close();
tcpClient.Close();
socket_send.Close();
}
listBox1.Items.Add("ÒѾÍ˳öÓÎÏ·");
}
}
private void button2_Click(object sender, EventArgs e)
{
player.Load();
player.Play();
}
private void button3_Click(object sender, EventArgs e)
{
player.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
picture++;
string picturePath = @"C:\\Users\\hp\source\\repos\\game\\game\\game\" + picture + ".jpg";
//ÉèÖÃͼƬÌî³ä
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = Image.FromFile(picturePath);
if (picture == 5)
picture = 0;
}
}
}
4,×ܽá
ÀûÓÃc#д¿ÉÊÓ»¯½çÃæºÍQT²î²»¶à,¶¼ÊÇÍ϶¯×é¼þ,È»ºóд´úÂë,²»¹ý±ÈQTҪдµÄ¼òµ¥Ò»Ð©
5,²Î¿¼
C#±àдÓÎÏ·¿Í»§¶Ë
|