我试图通过来自客户端计算机的数据在主机(一个简单的服务器)屏幕上移动图片框。这是一个简单的多人乒乓游戏。只要按下按键,客户端就会发送数据。它主要发送PictureBox的Y轴位置,但是尝试更改图片框的位置时出现错误。 我的客户端代码将player1 pictureBox的Y轴位置发送到服务器播放器
private void keyisdown(对象发送者,KeyEventArgs e) {
if (e.KeyCode == Keys.Up)
{
goup = true;
String s = Convert.ToString(player.Location.Y);
byte[] byteTime = Encoding.ASCII.GetBytes(s);
ns.Write(byteTime, 0, byteTime.Length);
}
if (e.KeyCode == Keys.Down)
{
godown = true;
String s = Convert.ToString(player.Location.Y);
byte[] byteTime = Encoding.ASCII.GetBytes(s);
ns.Write(byteTime, 0, byteTime.Length);
}
}
我正在接收数据的服务器程序
公共局部类Form1:表单 {
delegate void SetTextCallback(string text);
TcpListener listener;
TcpClient client;
NetworkStream ns;
Thread t = null;
public Form1()
{
InitializeComponent();
listener = new TcpListener(IPAddress.Any, 4545);
listener.Start();
client = listener.AcceptTcpClient();
ns = client.GetStream();
t = new Thread(DoWork);
t.Start();
}
private void button1_Click(object sender, EventArgs e)
{
String s = textBox2.Text;
byte[] byteTime = Encoding.ASCII.GetBytes(s);
ns.Write(byteTime, 0, byteTime.Length);
}
public void DoWork()
{
byte[] bytes = new byte[1024];
while (true)
{
int bytesRead = ns.Read(bytes, 0, bytes.Length);
string data;
this.SetText(Encoding.ASCII.GetString(bytes, 0, bytesRead));
// data = (Encoding.ASCII.GetString(bytes, 0, bytesRead));
// int Y = Convert.ToInt32(data);
// pictureBox1.Location = new Point(769, Y);
}
}
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.pictureBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.pictureBox1.Location = new Point(769, Convert.ToInt32( text));
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
im收到客户端播放器的Y位置图片框,并且收到了它,但是以某种方式无法正确移动,只是在获得Y位置时消失了,服务器n客户端程序的大小相同并且位置也相同,因此应该移动像镜子
This is the game I'm trying to make left is player1 and right is player2
我还需要有关如何更新双方球员的球位以及如何正确实现多人游戏的帮助,现在我正试图缓慢更改简单的聊天客户端代码并获取所需的数据。但是我如何才能每20或50ms不断更新播放器,我感到非常困惑。预先感谢。