Готовая лаба

This commit is contained in:
Данил Лопатин 2024-02-17 14:35:37 +03:00
parent 5a9b1fb6ae
commit d53c9971d2
2 changed files with 35 additions and 15 deletions

View File

@ -59,12 +59,14 @@ public class DrawningWarmlyLocomotive
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns> /// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height) public bool SetPictureSize(int width, int height)
{ {
// TODO проверка, что объект "влезает" в размеры поля if (width >= _drawningLocomotiveWidth && height >= _drawningLocomotiveHeight) {
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
return true; return true;
} }
return false;
}
/// <summary> /// <summary>
/// Установка позиции /// Установка позиции
/// </summary> /// </summary>
@ -76,11 +78,29 @@ public class DrawningWarmlyLocomotive
{ {
return; return;
} }
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
// то надо изменить координаты, чтобы он оставался в этих границах if((x + _drawningLocomotiveWidth > _pictureWidth ) && (y + _drawningLocomotiveHeight > _pictureHeight))
{
_startPosX = x - _drawningLocomotiveWidth;
_startPosY = y - _drawningLocomotiveHeight;
}
else if((x + _drawningLocomotiveWidth <= _pictureWidth) && (y + _drawningLocomotiveHeight > _pictureHeight))
{
_startPosX = x;
_startPosY = y - _drawningLocomotiveHeight;
}
else if ((x + _drawningLocomotiveWidth > _pictureWidth) && (y + _drawningLocomotiveHeight <= _pictureHeight))
{
_startPosX = x - _drawningLocomotiveWidth;
_startPosY = y;
}
else
{
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
} }
}
/// <summary> /// <summary>
/// Изменение направления перемещения /// Изменение направления перемещения
/// </summary> /// </summary>
@ -111,14 +131,14 @@ public class DrawningWarmlyLocomotive
return true; return true;
// вправо // вправо
case DirectionType.Right: case DirectionType.Right:
if (_startPosX.Value + EntityWarmlyLocomotive.Step < _pictureWidth) if (_startPosX.Value + EntityWarmlyLocomotive.Step + _drawningLocomotiveWidth < _pictureWidth)
{ {
_startPosX += (int)EntityWarmlyLocomotive.Step; _startPosX += (int)EntityWarmlyLocomotive.Step;
} }
return true; return true;
//вниз //вниз
case DirectionType.Down: case DirectionType.Down:
if (_startPosY.Value + EntityWarmlyLocomotive.Step < _pictureHeight) if (_startPosY.Value + EntityWarmlyLocomotive.Step + _drawningLocomotiveHeight < _pictureHeight)
{ {
_startPosY += (int)EntityWarmlyLocomotive.Step; _startPosY += (int)EntityWarmlyLocomotive.Step;
} }

View File

@ -43,10 +43,10 @@ namespace WarmlyLocomotive
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2))); Convert.ToBoolean(random.Next(0, 2)));
_drawningWarmlyLocomotive.SetPictureSize(pictureBoxWarmlyLocomotive.Width, _drawningWarmlyLocomotive.SetPictureSize(
pictureBoxWarmlyLocomotive.Width,
pictureBoxWarmlyLocomotive.Height); pictureBoxWarmlyLocomotive.Height);
_drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
100));
Draw(); Draw();
} }