【100分求解】图片移动起来闪呀闪呀闪。。。。
我把一张图片设置为背景图片,当角色移动是总是闪呀闪。。。。求解决!谢谢!
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
namespace 多线程带参数
{
class Form1 : Form
{
private PictureBox pic;
private static int Bit_X,Bit_Y;
private int X,Y;
Bitmap bit;
int[] Map1;
public Form1()
{
InitMap();
Initialize();
Thread thread1 = new Thread(new ThreadStart(ManMove));
thread1.IsBackground = true;
thread1.Start();
}
private void Initialize()
{
pic = new PictureBox();
pic.Size = new Size(32, 48);
pic.Location = new Point(0,0);
pic.BackColor = Color.Transparent;
this.SuspendLayout();
Panel panel1 = new Panel();
panel1.Dock = DockStyle.Fill;
panel1.Controls.Add(pic);
this.Controls.Add(panel1);
//20 9
this.Size = new Size(640, 432);
this.StartPosition = FormStartPosition.CenterScreen;
this.ResumeLayout();
panel1.BackgroundImage = Image.FromFile("MAP.png");
X=Y=Bit_X =Bit_Y= 0;
// this.BackColor = Color.Red;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
// CheckForIllegalCrossThreadCalls = false;
//panel1.BackColor = Color.Black;
this.pic.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pic)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
private void InitMap() //初始化地图数据
{
Map1 = new int[20];
int[] tree = { 6,10,20};
}
private void DrawMap()//画图
{
}
public void Form1_KeyDown(object sender, KeyEventArgs e) //按钮事件
{
if (e.KeyCode == Keys.Up)
{
Y -= 1;
Bit_Y=144;
if (Y < 0)
Y = 0;
}
else if (e.KeyCode == Keys.Down)
{
Y += 1;
Bit_Y=0;
if (Y > this.ClientSize.Height-pic.Size.Height)
Y = this.ClientSize.Height - pic.Size.Height;
}
else if (e.KeyCode == Keys.Left)
{
X -= 1;
Bit_Y = 48;
if (X < 0)
X = 0;
}
else if (e.KeyCode == Keys.Right)
{
X += 1;
Bit_Y = 96;
if (X > this.ClientSize.Width-pic.Size.Width)
X = this.ClientSize.Width - pic.Size.Width;
}
}
private void ManMove() //人物移动
{
while (true)
{
bit = new Bitmap(32, 48);
Graphics g = Graphics.FromImage(bit);
//bit.RotateFlip(RotateFlipType.Rotate90FlipY);
Rectangle rect = new Rectangle(Bit_X, Bit_Y, 32, 48);
Rectangle rectLoca = new Rectangle(0, 0, 32, 48);
Bitmap m2 = new Bitmap("MAN2.png");
//;
g.DrawImage(m2, rectLoca, rect, GraphicsUnit.Pixel);
pic.Image = bit;
Thread.Sleep(200);
Bit_X += 32;
if (Bit_X >= 128)
Bit_X = 0;
MethodInvoker m = new MethodInvoker(Move_Loca);
this.BeginInvoke(m); //异步执行委托
}
}
private void Move_Loca() //位置变换
{
pic.Location = new Point(X, Y);
}
}
}