【发布时间】:2021-04-07 09:50:00
【问题描述】:
我正在制作一个游戏,您需要单击某些对象(使用PictureBoxes),这些对象以类似于 DVD 屏幕保护程序的方式移动(边缘弹跳)。问题是当我在屏幕上有超过 1 个PictureBox 时。当一个人接触到边缘时,所有其他人PictureBoxes 将他们的 X 或 Y 速度值更改为负值并开始移动,就好像他们接触到了边缘一样。我怎样才能让它为每个PictureBox单独工作?
运动引擎:
private void MovementTimer_Tick(object sender, EventArgs e) //movement engine
{
foreach (Control z in this.Controls)
{
if (z is PictureBox)
{
if (z.Location.X < 0 || z.Location.X + z.Width > Size.Width) //bouncing effect in horizontal
{
x = -x;
}
if (z.Location.Y < 0 || z.Location.Y + z.Height > Size.Height) //bouncing effect in vertical
{
y = -y;
}
z.Location = new Point(z.Location.X + x, z.Location.Y + y);
}
}
}
生成图片框:
private void InitializePictureBox()
{
PictureBox[] pb = new PictureBox[12];
for (int i = 0; i < 12; i++)
{
x = rng.Next(10, 16) * (rng.Next(0, 2) * 2 - 1); //velocity in horz dim
y = rng.Next(10, 16) * (rng.Next(0, 2) * 2 - 1); //velocity in vert dim
locx = rng.Next(100, 500); //random locations
locy = rng.Next(100, 500);
pb[i] = new PictureBox();
pb[i].Image = Image.FromFile("../red/rbr.png");
pb[i].Location = new Point(locx, locy);
pb[i].SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pb[i]);
}
}
【问题讨论】:
-
对我来说,了解变量(例如 x 和 y)的初始化位置会有所帮助。
标签: c# winforms methods picturebox