C#问题,高手进....

来源:百度知道 编辑:UC知道 时间:2024/06/10 18:23:49
1.我在窗口放了张图片,我想让它随机乱动,但为什么只能上下动?
代码如下:
int x = pictureBox1.Location.X + Stochastic();//给x随即赋值为窗口上的横坐标
int y = pictureBox1.Location.Y + Stochastic();//给y随即赋值为窗口上的横坐标
if (x > 0 && y > 0 && x < this.Size.Width -pictureBox1.Size.Width
&& y < this.Size.Height - pictureBox1.Size.Height)//不能让图片超出窗口范围
{
pictureBox1.Location = new Point(x, y);//获得图片以后显示在窗口上面
pictureBox3.Location = new Point(x, y);
pictureBox1.Image = global::小游戏.Properties.Resources.y1;//获得图片内容
}
Stochastic为随机函数;
int Stochastic()
{
Random r = new Random(DateTime.Now.Millisecond);
return r.Next(-300, 300);
}
2:我想当鼠标点击这张图片的时候变成一把剑的形状,我已经在项目资源里面添加了剑的图片,但不知道怎么用

急急急...拜托各位高手了,我刚学,说的清楚点

随机函数调用时间间隔太短了,所以得到的是同一个值,把你程序改了一下.

int StochasticX()
{
System.Threading.Thread.Sleep(1);
Random rand = new Random();
return rand.Next(0, this.Size.Width - pictureBox1.Width);
}

int StochasticY()
{
System.Threading.Thread.Sleep(1);
Random rand = new Random();
return rand.Next(0, this.Size.Height - pictureBox1.Height);
}

private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 100;
t.Tick += new EventHandler(t_Tick);
t.Enabled = true;
}

void t_Tick(object sender, EventArgs e)
{
pictureBox1.Location = new Point(StochasticX(), StochasticY());
}

要做