diff --git a/Locomative/Locomative/DrawningLocomative.cs b/Locomative/Locomative/DrawningLocomative.cs index a8321a0..f16adca 100644 --- a/Locomative/Locomative/DrawningLocomative.cs +++ b/Locomative/Locomative/DrawningLocomative.cs @@ -22,6 +22,16 @@ namespace Locomative } public void SetPosition(int x, int y, int width, int height) { + Random rnd1 = new(); + if (width < 120 || height < 50) return; + if (x > width - _locomativeWidth) + { + x = rnd1.Next(10, width - _locomativeWidth - 1); + } + if( y > height- _locomativeHeight) + { + y = rnd1.Next(10, height - _locomativeHeight - 1); + } _startPosX = x; _startPosY = y; _pictureHeight = height; diff --git a/Locomative/Locomative/LocomativeForm.cs b/Locomative/Locomative/LocomativeForm.cs index 9541727..5333d7c 100644 --- a/Locomative/Locomative/LocomativeForm.cs +++ b/Locomative/Locomative/LocomativeForm.cs @@ -21,17 +21,14 @@ namespace Locomative private void buttonÑreate_Click(object sender, EventArgs e) { - if (pictureBoxLocomative.Width >= 120 && pictureBoxLocomative.Height >= 50) - { - Random rnd = new(); - _loco = new DrawningLocomative(); - _loco.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - _loco.SetPosition(rnd.Next(10, pictureBoxLocomative.Width - 100), rnd.Next(10, pictureBoxLocomative.Height - 40), pictureBoxLocomative.Width, pictureBoxLocomative.Height); - toolStripStatusLabelColor.Text = $"Öâåò:{_loco.Locomative?.BodyColor.Name}"; - toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü:{_loco.Locomative?.Speed}"; - toolStripStatusLabelWeight.Text = $"Âåñ:{_loco.Locomative?.Weight}"; - Draw(); - } + Random rnd = new(); + _loco = new DrawningLocomative(); + _loco.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + _loco.SetPosition(rnd.Next(10, 300), rnd.Next(10, 400), pictureBoxLocomative.Width, pictureBoxLocomative.Height); + toolStripStatusLabelColor.Text = $"Öâåò:{_loco.Locomative?.BodyColor.Name}"; + toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü:{_loco.Locomative?.Speed}"; + toolStripStatusLabelWeight.Text = $"Âåñ:{_loco.Locomative?.Weight}"; + Draw(); } private void buttonMove_Click(object sender, EventArgs e)