关于C#多线程程序设计
想写个双人坦克小游戏,想要双人玩需要用到多线程,以下是我写的代码,没能实现效果,想请教以下大家哪里错了,先谢谢了。
程序代码: Thread thread1, thread2;
Message m;
Keys k;
int x1 = 0, y1 = 0, x2, y2;
Bitmap pic1 = new Bitmap("C:\\Users\\llq664301573\\Desktop\\res\\己方1上.gif");
Bitmap pic2 = new Bitmap("C:\\Users\\llq664301573\\Desktop\\res\\己方1下.gif");
Bitmap pic3 = new Bitmap("C:\\Users\\llq664301573\\Desktop\\res\\己方1左.gif");
Bitmap pic4 = new Bitmap("C:\\Users\\llq664301573\\Desktop\\res\\己方1右.gif");
public void func1()
{
switch (k)
{
case Keys.Up:
y1 -= 8;
pictureBox1.Top = y1;
pictureBox1.Image = pic1;
pictureBox1.Width = pic1.Width;
pictureBox1.Height = pic1.Height;
break;
case Keys.Down:
y1 += 8;
pictureBox1.Top = y1;
pictureBox1.Image = pic2;
pictureBox1.Width = pic2.Width;
pictureBox1.Height = pic2.Height;
break;
case Keys.Left:
x1 -= 8;
pictureBox1.Left = x1;
pictureBox1.Image = pic3;
pictureBox1.Width = pic3.Width;
pictureBox1.Height = pic3.Height;
break;
case Keys.Right:
x1 += 8;
pictureBox1.Left = x1;
pictureBox1.Image = pic4;
pictureBox1.Width = pic4.Width;
pictureBox1.Height = pic4.Height;
break;
default:
break;
}
Thread.Sleep(10);
}
public void func2()
{
switch (k)
{
case Keys.W:
y2 -= 8;
pictureBox2.Top = y2;
pictureBox2.Image = pic1;
pictureBox2.Width = pic1.Width;
pictureBox2.Height = pic1.Height;
break;
case Keys.S:
y2 += 8;
pictureBox2.Top = y2;
pictureBox2.Image = pic2;
pictureBox2.Width = pic2.Width;
pictureBox2.Height = pic2.Height;
break;
case Keys.A:
x2 -= 8;
pictureBox2.Left = x2;
pictureBox2.Image = pic3;
pictureBox2.Width = pic3.Width;
pictureBox2.Height = pic3.Height;
break;
case Keys.D:
x2 += 8;
pictureBox2.Left = x2;
pictureBox2.Image = pic4;
pictureBox2.Width = pic4.Width;
pictureBox2.Height = pic4.Height;
break;
default:
break;
}
Thread.Sleep(10);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
m = msg;
k = keyData;
thread1 = new Thread(new ThreadStart(func1));//生成线程类对象
thread1.Start();
thread2 = new Thread(new ThreadStart(func2));//生成线程类对象
thread2.Start();
return base.ProcessCmdKey(ref msg, keyData);
}








