让图片走动起来 C#实现

来源:百度知道 编辑:UC知道 时间:2024/09/28 13:15:49
我已经写出了一部分代码,代码的功能只可以从左走到右,我现在想实现的功能是从右走到左.可以参考一下我已经写的部分程序.
http://hi.baidu.com/285ru/blog/item/de2dec1e25aa74174034174c.html

改改你的timer1_Tick事件:

/// <summary>
/// 用Count来确定pictureBox1的运动方向.0为向左,1为向右
/// </summary>
int Count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (Count == 0)
{
this.pictureBox1.Location = new Point(this.pictureBox1.Location.X - 1, this.pictureBox1.Location.Y);
if (this.pictureBox1.Location.X < 0)//当pictureBox1到达元宝的最左端时
{ Count = 1; }//改变pictureBox1的运动方向为右
}
else
{
this.pictureBox1.Location = new Point(this.pictureBox1.Location.X+1,this.pictureBox1.Location.Y);
if (this.pictureBox1.Location.X > 313)//当pictureBox1到达元宝的最右端时
{ Count = 0; }//改变pictureBox1的运动方向为左
}
}

从右走到左就是反一反啊,你加X轴坐标的话,那就变成减X轴坐标即可。X的原点是0,就设置移动的对象X坐标等于零就停止而已。