一、编写一个网游客户端
新建项目C#窗体程序,放置控件
创建了2个textbox,4个button,1个picturebox
1.实现连接
代码
TcpClient tcpClient;
NetworkStream stream;
private void button1_Click(object sender, EventArgs e)
{
try
{
//实例化
tcpClient = new TcpClient();
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect("10.160.52.106", 3900);
//获取网络传输流
stream = tcpClient.GetStream();
//接受数据并转化为字符串
byte[] data = new byte[1024];
int receive = stream.Read(data, 0, 1024);
string msg = Encoding.Default.GetString(data, 0, receive);
//去除字符串中的终端转义字符
//msg = strDelete(msg);
//显示出来
textBox1.AppendText(msg);
}
catch
{
textBox1.AppendText("服务器未启动" + Environment.NewLine);
}
}
运行效果:
2.客户端发给服务器的数据
private void button4_Click(object sender, EventArgs e)
{
//获取textBox1内的文本内容
string msg = textBox2.Text + "\n";
//将文本内容转化成比特流并发送给服务器
byte[] data = new byte[1024];
data = Encoding.Default.GetBytes(msg);
stream.Write(data, 0, data.Length);
//接收服务器端传来的数据流并转化为字符串
byte[] data1 = new byte[1024];
int receive = stream.Read(data1, 0, 1024);
msg = Encoding.Default.GetString(data1, 0, receive);
//清除显示框之前的内容
textBox1.Clear();
//显示数据
textBox1.AppendText(msg);
//刷新输入框
textBox2.Clear();
//将光标集中到输入框中
textBox2.Focus();
}
运行效果:
3.播放音乐
在项目引用里增加COM组件WindowsMediaPlayer
代码
private void button2_Click(object sender, EventArgs e)
{
WMPLib.WindowsMediaPlayer Player = new WMPLib.WindowsMediaPlayer();
Player.URL = @"D:\CloudMusic\阿保剛 - Karma -Piano version-.mp3";
Player.controls.play();
}
4.变换图片
放置timer控件
将enabled设置为true,interval设置为30000,
1000=1s
,间隔就为30s
enabled:使能属性,不能操作,对其他有数据源的控件,
interval:间隔
双击timer1,就有了timer 1_Tick的函数
代码
private void timer1_Tick(object sender, EventArgs e)
{
flag++;
string picturePath = @"D:\picture\" + flag + ".jpg";
//设置图片填充
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = Image.FromFile(picturePath);
if (flag == 4)
{
flag = 0;
}
运行效果
5.完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace wangyougame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TcpClient tcpClient;
NetworkStream stream;
int flag=0;
private void button3_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//实例化
tcpClient = new TcpClient();
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect("10.160.52.106", 3900);
//获取网络传输流
stream = tcpClient.GetStream();
//接受数据并转化为字符串
byte[] data = new byte[1024];
int receive = stream.Read(data, 0, 1024);
string msg = Encoding.Default.GetString(data, 0, receive);
//去除字符串中的终端转义字符
//msg = strDelete(msg);
//显示出来
textBox1.AppendText(msg);
}
catch
{
textBox1.AppendText("服务器未启动" + Environment.NewLine);
}
}
private void button4_Click(object sender, EventArgs e)
{
//获取textBox1内的文本内容
string msg = textBox2.Text + "\n";
//将文本内容转化成比特流并发送给服务器
byte[] data = new byte[1024];
data = Encoding.Default.GetBytes(msg);
stream.Write(data, 0, data.Length);
//接收服务器端传来的数据流并转化为字符串
byte[] data1 = new byte[1024];
int receive = stream.Read(data1, 0, 1024);
msg = Encoding.Default.GetString(data1, 0, receive);
//清除显示框之前的内容
textBox1.Clear();
//显示数据
textBox1.AppendText(msg);
//刷新输入框
textBox2.Clear();
//将光标集中到输入框中
textBox2.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
WMPLib.WindowsMediaPlayer Player = new WMPLib.WindowsMediaPlayer();
Player.URL = @"D:\CloudMusic\阿保剛 - Karma -Piano version-.mp3";
Player.controls.play();
}
private void timer1_Tick(object sender, EventArgs e)
{
flag++;
string picturePath = @"D:\picture\" + flag + ".jpg";
//设置图片填充
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = Image.FromFile(picturePath);
if (flag == 4)
{
flag = 0;
}
}
}
}
二、总结
学会了使用c#连接服务器,做一个客户端的网游
三、参考资料
C#播放音乐的5种方式
C#中如何用按钮播放mp3
C#winform怎样设计定时更换图片?(简单易行)
编写一个网游客户端
C#编写网游客户端连接游戏服务器