Первая часть лабы 2

This commit is contained in:
Petek1234 2024-03-05 21:35:15 +04:00
parent 2ce8892083
commit 70561aff1a
8 changed files with 252 additions and 130 deletions

View File

@ -1,4 +1,4 @@
namespace laba_0;
namespace laba_0.Drawning;
/// <summary>
/// направление перемещения

View File

@ -1,14 +1,18 @@
namespace laba_0;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using laba_0.Entities;
/// <summary>
/// класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningBoat
namespace laba_0.Drawning;
public class DrawningB
{
/// <summary>
/// класс-сущность
/// </summary>
public EntityBoat? EntityBoat { get; private set; }
public EntityB? EntityB{ get; protected set; }
/// <summary>
/// ширина окна
@ -23,45 +27,56 @@ public class DrawningBoat
/// <summary>
/// левая координата прорисовки катера
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// верхняя координата прорисовки катера
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// ширина прорисовки катера
/// </summary>
private readonly int _drawningBoatWidth = 115;
private readonly int _drawningBoatWidth = 90;
/// <summary>
/// высота прорисовки катера
/// </summary>
private readonly int _drawningBoatHeight = 40;
private readonly int _drawningBoatHeight = 50;
/// <summary>
/// Инициализация свойств
/// Пустой конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="kabina">Признак наличия кабины</param>
/// <param name="dvigatel">Признак наличия двигателя</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool kabina, bool dvigatel)
private DrawningB()
{
EntityBoat = new EntityBoat();
EntityBoat.Init(speed, weight, bodyColor, additionalColor,
kabina, dvigatel);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningB(int speed, double weight, Color bodyColor) : this()
{
EntityB = new EntityB(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="_drawningBoatWidth">ширина прорисовки катера</param>
/// <param name="_drawningBoatHeight">высота прорисовки катера</param>
protected DrawningB(int drawningBoatWidth, int drawningBoatHeight) : this()
{
_drawningBoatWidth = drawningBoatWidth;
_drawningBoatHeight = drawningBoatHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
@ -74,9 +89,9 @@ public class DrawningBoat
{
_pictureWidth = width;
_pictureHeight = height;
if(_startPosX.HasValue && _startPosY.HasValue)
{
if(_startPosX + _drawningBoatWidth > _pictureWidth)
if (_startPosX.HasValue && _startPosY.HasValue)
{
if (_startPosX + _drawningBoatWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningBoatWidth;
}
@ -103,20 +118,20 @@ public class DrawningBoat
return;
}
if(x < 0) //если новая позиция левее левой границы формы
if (x < 0) //если новая позиция левее левой границы формы
{
x = 0;//устанавливаем позицию по оси х равную 0
x = 0; //устанавливаем позицию по оси х равную 0
}
else if(x + _drawningBoatWidth > _pictureWidth) //если новая позиция выходит за правую границу формы
else if (x + _drawningBoatWidth > _pictureWidth) //если новая позиция выходит за правую границу формы
{
x = _pictureWidth.Value - _drawningBoatWidth;//корректируем по оси х
}
if(y < 0)//если новая позиция выше верхней границы формы
if (y < 0)//если новая позиция выше верхней границы формы
{
y = 0;
}
else if(y + _drawningBoatHeight > _pictureHeight)//если новая позиция выходит за границу формы
else if (y + _drawningBoatHeight > _pictureHeight) //если новая позиция выходит за границу формы
{
y = _pictureHeight.Value - _drawningBoatHeight;//корректируем по оси у
}
@ -132,7 +147,7 @@ public class DrawningBoat
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityBoat == null || !_startPosX.HasValue ||
if (EntityB == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
@ -141,33 +156,33 @@ public class DrawningBoat
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityBoat.Step > 0)
if (_startPosX.Value - EntityB.Step > 0)
{
_startPosX -= (int)EntityBoat.Step;
_startPosX -= (int)EntityB.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityBoat.Step > 0)
if (_startPosY.Value - EntityB.Step > 0)
{
_startPosY -= (int)EntityBoat.Step;
_startPosY -= (int)EntityB.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntityBoat.Step + _drawningBoatWidth < _pictureWidth)
if (_startPosX.Value + EntityB.Step + _drawningBoatWidth < _pictureWidth)
{
_startPosX += (int)EntityBoat.Step;
_startPosX += (int)EntityB.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityBoat.Step + _drawningBoatHeight < _pictureHeight)
if (_startPosY.Value + EntityB.Step + _drawningBoatHeight < _pictureHeight)
{
_startPosY += (int)EntityBoat.Step;
_startPosY += (int)EntityB.Step;
}
return true;
@ -180,52 +195,24 @@ public class DrawningBoat
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityBoat == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
if (EntityB == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityBoat.AdditionalColor);
Brush bodyBrush = new SolidBrush(EntityBoat.BodyColor);
//корпус
g.FillRectangle(bodyBrush, _startPosX.Value + 5, _startPosY.Value , 75, 40);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value, 75, 40);
//лестница
g.FillRectangle(additionalBrush, _startPosX.Value + 5, _startPosY.Value + 10, 20, 20);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 10, 20, 20);
//двигатель
if (EntityBoat.dvigatelBody)
{
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 15, 5, 10);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 15, 5, 10);
}
//корма
Point point1 = new Point(_startPosX.Value + 80, _startPosY.Value);
Point point2 = new Point(_startPosX.Value + 80, _startPosY.Value+40);
Point point2 = new Point(_startPosX.Value + 80, _startPosY.Value + 40);
Point point3 = new Point(_startPosX.Value + 120, _startPosY.Value + 20);
Point[] curvePointsKorma = { point1, point2, point3 };
g.DrawPolygon(pen, curvePointsKorma);
g.FillPolygon(bodyBrush, curvePointsKorma);
//кабина
if (EntityBoat.kabinaBody)
{
Point point4 = new Point(_startPosX.Value + 50, _startPosY.Value + 10);
Point point5 = new Point(_startPosX.Value + 55, _startPosY.Value + 15);
Point point6 = new Point(_startPosX.Value + 55, _startPosY.Value + 25);
Point point7 = new Point(_startPosX.Value + 50, _startPosY.Value + 30);
Point point8 = new Point(_startPosX.Value + 50, _startPosY.Value + 10);
Point[] curvePointsKabina = { point4, point5, point6, point7, point8 };
g.FillPolygon(additionalBrush, curvePointsKabina);
g.DrawPolygon(pen, curvePointsKabina);
}
}
}

View File

@ -0,0 +1,60 @@
using laba_0.Entities;
namespace laba_0.Drawning;
/// <summary>
/// класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningBoat : DrawningB
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="kabina">Признак наличия кабины</param>
/// <param name="dvigatel">Признак наличия двигателя</param>
public DrawningBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool kabina, bool dvigatel) : base(110, 60)
{
EntityB = new EntityBoat(speed, weight, bodyColor, additionalColor, kabina, dvigatel);
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public override void DrawTransport(Graphics g)
{
Pen pen = new(Color.Black);
if (EntityB == null || EntityB is not EntityBoat Boat || !_startPosX.HasValue || !_startPosY.HasValue)
return;
base.DrawTransport(g);
Brush additionalBrush = new SolidBrush(Boat.AdditionalColor);
//кабина
if (Boat.kabinaBody)
{
Point point4 = new Point(_startPosX.Value + 55, _startPosY.Value + 10);
Point point5 = new Point(_startPosX.Value + 60, _startPosY.Value + 15);
Point point6 = new Point(_startPosX.Value + 60, _startPosY.Value + 25);
Point point7 = new Point(_startPosX.Value + 55, _startPosY.Value + 30);
Point point8 = new Point(_startPosX.Value + 55, _startPosY.Value + 10);
Point[] curvePointsKabina = { point4, point5, point6, point7, point8 };
g.FillPolygon(additionalBrush, curvePointsKabina);
g.DrawPolygon(pen, curvePointsKabina);
}
//двигатель
if (Boat.dvigatelBody)
{
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 15, 5, 10);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 15, 5, 10);
}
base.DrawTransport(g);
}
}

View File

@ -0,0 +1,41 @@
namespace laba_0.Entities;
/// <summary>
/// Класс-сущность "Катер"
/// </summary>
public class EntityB
{
/// <summary>
/// скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения катера
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed"> Скорость </param>
/// <param name="weight"> Вес </param>
/// <param name="bodyColor"> Основной цвет </param>
public EntityB(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -1,22 +1,10 @@
namespace laba_0;
namespace laba_0.Entities;
public class EntityBoat
/// <summary>
/// Класс-сущность "Катер"
/// </summary>
public class EntityBoat : EntityB
{
/// <summary>
/// скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
@ -33,12 +21,7 @@ public class EntityBoat
public bool dvigatelBody { get; private set; }
/// <summary>
/// Шаг перемещения катера
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
///
/// Инициализация полей объекта-класса катера
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
@ -47,11 +30,8 @@ public class EntityBoat
/// <param name="kabinaBody">Признак наличия кабины</param>
/// <param name="dvigatelBody">Признак наличия двигателя</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool kabina, bool dvigatel)
public EntityBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool kabina, bool dvigatel) : base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
kabinaBody = kabina;
dvigatelBody = dvigatel;

View File

@ -30,11 +30,12 @@ partial class FormBoat
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormBoat));
pictureBoxBoat = new PictureBox();
buttonCreate = new Button();
buttonCreateBoat = new Button();
buttonRight = new Button();
buttonDown = new Button();
buttonUp = new Button();
buttonLeft = new Button();
ButtonCreateB = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxBoat).BeginInit();
SuspendLayout();
//
@ -50,16 +51,16 @@ partial class FormBoat
pictureBoxBoat.TabStop = false;
pictureBoxBoat.Click += ButtonMove_Click;
//
// buttonCreate
// buttonCreateBoat
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 327);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать ";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreateBoat_Click;
buttonCreateBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateBoat.Location = new Point(12, 327);
buttonCreateBoat.Name = "buttonCreateBoat";
buttonCreateBoat.Size = new Size(153, 23);
buttonCreateBoat.TabIndex = 1;
buttonCreateBoat.Text = "Создать катер";
buttonCreateBoat.UseVisualStyleBackColor = true;
buttonCreateBoat.Click += ButtonCreateBoat_Click;
//
// buttonRight
//
@ -109,16 +110,28 @@ partial class FormBoat
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// ButtonCreateB
//
ButtonCreateB.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateB.Location = new Point(171, 327);
ButtonCreateB.Name = "ButtonCreateB";
ButtonCreateB.Size = new Size(147, 23);
ButtonCreateB.TabIndex = 6;
ButtonCreateB.Text = "Создать простой катер";
ButtonCreateB.UseVisualStyleBackColor = true;
ButtonCreateB.Click += ButtonCreateB_Click;
//
// FormBoat
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(640, 359);
Controls.Add(ButtonCreateB);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonLeft);
Controls.Add(buttonUp);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateBoat);
Controls.Add(pictureBoxBoat);
Name = "FormBoat";
Text = "FormBoat";
@ -130,9 +143,10 @@ partial class FormBoat
#endregion
private PictureBox pictureBoxBoat;
private Button buttonCreate;
private Button buttonCreateBoat;
private Button buttonRight;
private Button buttonDown;
private Button buttonUp;
private Button buttonLeft;
private Button ButtonCreateB;
}

View File

@ -7,12 +7,13 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using laba_0.Drawning;
namespace laba_0;
public partial class FormBoat : Form
{
private DrawningBoat? _drawningBoat;
private DrawningB? _drawningB;
public FormBoat()
{
@ -21,34 +22,73 @@ public partial class FormBoat : Form
private void Draw()
{
if (_drawningBoat == null)
if (_drawningB == null)
{
return;
}
Bitmap bmp = new(pictureBoxBoat.Width,
pictureBoxBoat.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningBoat.DrawTransport(gr);
_drawningB.DrawTransport(gr);
pictureBoxBoat.Image = bmp;
}
private void ButtonCreateBoat_Click(object sender, EventArgs e)
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
{
Random random = new();
_drawningBoat = new DrawningBoat();
_drawningBoat.Init(random.Next(650, 700), random.Next(15760, 16130),
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)));
_drawningBoat.SetPictureSize(pictureBoxBoat.Width, pictureBoxBoat.Height);
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
switch (type)
{
case nameof(DrawningB):
_drawningB = new DrawningB(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawningBoat):
_drawningB = new DrawningBoat(random.Next(100, 300), random.Next(1000, 3000),
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)));
break;
default:
return;
}
_drawningB.SetPictureSize(pictureBoxBoat.Width, pictureBoxBoat.Height);
_drawningB.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка создания кнопки "создать катер"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateBoat_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningBoat));
}
/// <summary>
/// Обработка создания кнопки "создать простой катер"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateB_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningB));
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningBoat == null)
if (_drawningB == null)
{
return;
}
@ -57,16 +97,16 @@ public partial class FormBoat : Form
switch (name)
{
case "buttonUp":
result = _drawningBoat.MoveTransport(DirectionType.Up);
result = _drawningB.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningBoat.MoveTransport(DirectionType.Down);
result = _drawningB.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningBoat.MoveTransport(DirectionType.Left);
result = _drawningB.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningBoat.MoveTransport(DirectionType.Right);
result = _drawningB.MoveTransport(DirectionType.Right);
break;
}
if (result)

View File

@ -121,7 +121,7 @@
<data name="buttonRight.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAYAAAA+s9J6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vwAADr8BOAVTJAAACHlJREFUeF7tnTFuGzEQRdWnSiUDAVKksIG0aXKAHCBdLujKJ/B5XKUxkBsoM7CN
vAAADrwBlbxySQAACHlJREFUeF7tnTFuGzEQRdWnSiUDAVKksIG0aXKAHCBdLujKJ/B5XKUxkBsoM7CN
yJuxrF2RHJL/PeAhsS2RQ0AfWopccXcAgFQIIUAyhBAgGUIIkAwhBEiGEAIkQwgBkiGEAMkQQoBkCCFA
MoQQIBlCCJAMIQRIhhACJEMIAZIhhADJEEJx7u/vDw8PD88/QQaEUJzb29vDbrc7/Pnz5/k30BpCKM5L
CD98+HB4fHx8/i20hBCK8xJC9/r6+vD79+/nv0ArCKE4xyF0v379ShAbQwjFWYbQ3e/3zBEbQgjFiUL4
@ -163,7 +163,7 @@
<data name="buttonUp.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAYAAAA+s9J6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vwAADr8BOAVTJAAACBBJREFUeF7tnTtOI0sARcmJiIyENAEBSKQkLIAFkLFBIlbAeohIkNiB37vMWBqY
vAAADrwBlbxySQAACBBJREFUeF7tnTtOI0sARcmJiIyENAEBSKQkLIAFkLFBIlbAeohIkNiB37vMWBqY
BmO7PrfqniMdjeZv7D7uqq4yfbSGYXl9fV3f3t6+/wjjQoSD8vLysr68vFwfHR29/6ifw5gQ4YDozLcJ
cKN+zhlxTIhwMN7e3tYnJycfAtyoX9fvw1gQ4UBoyHl2drYY4Eb9PkPTsSDCQVBYV1dXi+F9Vn+OEMeB
CAdAc73z8/PF4L5Sf5454hgQoTkK6fj4eDG0bervEaI/RGiMhpS7ngE/q7/P0NQbIjRllzngNpkjekOE