From 6db3ae138c3d0fd4c14f0202f4029108aafe176b Mon Sep 17 00:00:00 2001 From: Stranni15k Date: Sat, 19 Nov 2022 21:07:38 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ElectricLocomotive/AbstractMap.cs | 66 ++++++++++- .../ElectricLocomotive/BushesMap.cs | 56 ++++++++++ .../DrawningHardLocomotive.cs | 19 +--- .../ElectricLocomotive/DrawningLocomotive.cs | 103 ++++++++---------- .../DrawningObjectLocomotive.cs | 1 + .../ElectricLocomotive/FieldMap.cs | 53 +++++++++ .../ElectricLocomotive/FormMap.Designer.cs | 4 +- .../ElectricLocomotive/FormMap.cs | 7 ++ .../ElectricLocomotive/Program.cs | 2 +- .../ElectricLocomotive/SimpleMap.cs | 4 +- 10 files changed, 235 insertions(+), 80 deletions(-) create mode 100644 ElectricLocomotive/ElectricLocomotive/BushesMap.cs create mode 100644 ElectricLocomotive/ElectricLocomotive/FieldMap.cs diff --git a/ElectricLocomotive/ElectricLocomotive/AbstractMap.cs b/ElectricLocomotive/ElectricLocomotive/AbstractMap.cs index 5c71311..914017e 100644 --- a/ElectricLocomotive/ElectricLocomotive/AbstractMap.cs +++ b/ElectricLocomotive/ElectricLocomotive/AbstractMap.cs @@ -32,23 +32,85 @@ namespace ElectricLocomotive } public Bitmap MoveObject(Direction direction) { - // TODO проверка, что объект может переместится в требуемом направлении + (float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition(); + + float locomotiveWidth = rightX - leftX; + float locomotiveHeight = bottomY - topY; + + for (int i = 0; i < _map.GetLength(0); i++) + { + for (int j = 0; j < _map.GetLength(1); j++) + { + if (_map[i, j] == _barrier) + { + switch (direction) + { + case Direction.Up: + if (_size_y * (j + 1) >= topY - _drawningObject.Step && _size_y * (j + 1) < topY && _size_x * (i + 1) > leftX + && _size_x * (i + 1) <= rightX) + { + return DrawMapWithObject(); + } + break; + case Direction.Down: + if (_size_y * j <= bottomY + _drawningObject.Step && _size_y * j > bottomY && _size_x * (i + 1) > leftX + && _size_x * (i + 1) <= rightX) + { + return DrawMapWithObject(); + } + break; + case Direction.Left: + if (_size_x * (i + 1) >= leftX - _drawningObject.Step && _size_x * (i + 1) < leftX && _size_y * (j + 1) < bottomY + && _size_y * (j + 1) >= topY) + { + return DrawMapWithObject(); + } + break; + case Direction.Right: + if (_size_x * i <= rightX + _drawningObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY + && _size_y * (j + 1) >= topY) + { + return DrawMapWithObject(); + } + break; + } + } + } + } if (true) { _drawningObject.MoveObject(direction); } return DrawMapWithObject(); } + private bool SetObjectOnMap() { + (float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition(); if (_drawningObject == null || _map == null) { return false; } + + float locomotiveWidth = rightX - leftX; + float locomotiveHeight = bottomY - topY; + int x = _random.Next(0, 10); int y = _random.Next(0, 10); + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + if (_map[i, j] == _barrier) + { + if (x + locomotiveWidth >= _size_x * i && x <= _size_x * i && y + locomotiveHeight > _size_y * j && y <= _size_y * j) + { + return false; + } + } + } + } _drawningObject.SetObject(x, y, _width, _height); - // TODO првоерка, что объект не "накладывается" на закрытые участки return true; } private Bitmap DrawMapWithObject() diff --git a/ElectricLocomotive/ElectricLocomotive/BushesMap.cs b/ElectricLocomotive/ElectricLocomotive/BushesMap.cs new file mode 100644 index 0000000..376a534 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/BushesMap.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectricLocomotive +{ + internal class BushesMap : AbstractMap + { + /// + /// Цвет участка закрытого + /// + private readonly Pen barrierColor = new Pen(Color.DarkGreen, 3); + /// + /// Цвет участка открытого + /// + private readonly Brush roadColor = new SolidBrush(Color.Brown); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.DrawLine(barrierColor, new Point(Convert.ToInt32(i * (_size_x - 1)), Convert.ToInt32(j * (_size_y - 1))), new Point(Convert.ToInt32(i * (_size_x - 1)+7), Convert.ToInt32(j * (_size_y - 1))+7)); + g.DrawLine(barrierColor, new Point(Convert.ToInt32(i * (_size_x - 1)+7), Convert.ToInt32(j * (_size_y - 1))), new Point(Convert.ToInt32(i * (_size_x - 1) + 7), Convert.ToInt32(j * (_size_y - 1)) + 7)); + g.DrawLine(barrierColor, new Point(Convert.ToInt32(i * (_size_x - 1)+7), Convert.ToInt32(j * (_size_y - 1))+7), new Point(Convert.ToInt32(i * (_size_x - 1) + 14), Convert.ToInt32(j * (_size_y - 1)) )); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } + protected override void GenerateMap() + { + _map = new int[100, 100]; + _size_x = (float)_width / _map.GetLength(0); + _size_y = (float)_height / _map.GetLength(1); + int counter = 0; + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[i, j] = _freeRoad; + } + } + while (counter < 20) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + + } +} diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningHardLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningHardLocomotive.cs index 93a5dfa..0cf6419 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawningHardLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawningHardLocomotive.cs @@ -32,29 +32,20 @@ namespace ElectricLocomotive Pen pen = new(Color.Black); + Pen window = new(Color.Blue); Brush dopBrush = new SolidBrush(sportCar.DopColor); if (sportCar.BodyKit) { - g.FillRectangle(dopBrush, _startPosX, _startPosY + 20, 60, 10); - g.DrawRectangle(pen, _startPosX, _startPosY + 20, 60, 10); - - g.FillRectangle(dopBrush, _startPosX - 10, _startPosY + 10, 10, 30); - g.DrawRectangle(pen, _startPosX - 10, _startPosY + 10 , 10, 30); + Point[] pts = { new Point(Convert.ToInt32(_startPosX) + 15, Convert.ToInt32(_startPosY) - 20), new Point(Convert.ToInt32(_startPosX) + 45, Convert.ToInt32(_startPosY) - 30), new Point(Convert.ToInt32(_startPosX) + 75, Convert.ToInt32(_startPosY) - 20), new Point(Convert.ToInt32(_startPosX) + 45, Convert.ToInt32(_startPosY) + 10) }; + g.DrawPolygon(window, pts); } if (sportCar.SportLine) { - g.FillRectangle(dopBrush, _startPosX + 10, _startPosY - 20, 60, 10); - g.FillRectangle(dopBrush, _startPosX, _startPosY + 20, 60, 10); - g.DrawRectangle(pen, _startPosX + 10, _startPosY - 20, 60, 10); - g.DrawRectangle(pen, _startPosX, _startPosY + 20, 60, 10); - } - - if (sportCar.Wing) - { - + Point[] pts = { new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 30), new Point(Convert.ToInt32(_startPosX) + 150, Convert.ToInt32(_startPosY) + 60)}; + g.FillPolygon(Brushes.Black, pts); } base.DrawTransport(g); diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs index c03ca0e..18e2bb8 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs @@ -9,68 +9,33 @@ namespace ElectricLocomotive { internal class DrawningLocomotive { - /// - /// Класс-сущность - /// public EntityLocomotive Locomotive { get; protected set; } - /// - /// Левая координата отрисовки автомобиля - /// protected float _startPosX; - /// - /// Верхняя кооридната отрисовки автомобиля - /// protected float _startPosY; - /// - /// Ширина окна отрисовки - /// + protected int? _pictureWidth = null; - /// - /// Высота окна отрисовки - /// protected int? _pictureHeight = null; - /// - /// Ширина отрисовки автомобиля - /// + protected readonly int _locomotiveWidth = 160; - /// - /// Высота отрисовки автомобиля - /// protected readonly int _locomotiveHeight = 90; - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес автомобиля - /// Цвет кузова public DrawningLocomotive(int speed, float weight, Color bodyColor) { Locomotive = new EntityLocomotive(speed, weight, bodyColor); } - /// - /// Установка позиции автомобиля - /// - /// Координата X - /// Координата Y - /// Ширина картинки - /// Высота картинки public void SetPosition(int x, int y, int width, int height) { - // TODO checks + if (width <= _locomotiveWidth + x || height <= _locomotiveHeight + y || x < 0 || y < 0) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } - _startPosX = x; - _startPosY = y; + _startPosX = x + 20; + _startPosY = y + 30; _pictureWidth = width; _pictureHeight = height; - - if (_startPosX + _locomotiveWidth > _pictureWidth) { _startPosX = 20; } - if (_startPosY - _locomotiveHeight / 2 < 0) { _startPosY = _locomotiveHeight + 10; } - if (_startPosY + _locomotiveHeight > _pictureHeight) { _startPosY -= _locomotiveHeight; } } - /// - /// Изменение направления пермещения - /// - /// Направление public void MoveTransport(Direction direction) { if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) @@ -131,24 +96,42 @@ namespace ElectricLocomotive return; } Brush brBody = new SolidBrush(Locomotive?.BodyColor ?? Color.Black); + Brush blackturbo = new SolidBrush(Color.Black); Pen pen = new Pen(Color.Black); - //колёса - g.FillEllipse(brBody, _startPosX + 10, _startPosY + 50, 30, 30); - g.FillEllipse(brBody, _startPosX + 50, _startPosY + 50, 30, 30); - g.FillEllipse(brBody, _startPosX + 90, _startPosY + 50, 30, 30); - g.FillEllipse(brBody, _startPosX + 130, _startPosY + 50, 30, 30); + Pen window = new Pen(Color.Blue); - g.DrawEllipse(pen, _startPosX + 10, _startPosY + 50, 30, 30); - g.DrawEllipse(pen, _startPosX + 50, _startPosY + 50, 30, 30); - g.DrawEllipse(pen, _startPosX + 90, _startPosY + 50, 30, 30); - g.DrawEllipse(pen, _startPosX + 130, _startPosY + 50, 30, 30); + g.FillPolygon(brBody, new Point[] { new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 110, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 30), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60) }); + g.FillRectangle(blackturbo, _startPosX + 5, _startPosY + 15, 5, 40); + + g.FillEllipse(brBody, _startPosX + 15, _startPosY + 60, 20, 20); + g.FillEllipse(brBody, _startPosX + 45, _startPosY + 60, 20, 20); + + + g.FillEllipse(brBody, _startPosX + 85, _startPosY + 60, 20, 20); + g.FillEllipse(brBody, _startPosX + 115, _startPosY + 60, 20, 20); + + // Окна + + g.DrawRectangle(window, _startPosX + 20, _startPosY + 20, 20, 25); + g.DrawRectangle(window, _startPosX + 50, _startPosY + 20, 20, 25); + + // Колёса + + g.DrawEllipse(pen, _startPosX + 15, _startPosY + 60, 20, 20); + g.DrawEllipse(pen, _startPosX + 45, _startPosY + 60, 20, 20); + + + g.DrawEllipse(pen, _startPosX + 85, _startPosY + 60, 20, 20); + g.DrawEllipse(pen, _startPosX + 115, _startPosY + 60, 20, 20); + + // Дверь + + g.DrawRectangle(pen, _startPosX + 85, _startPosY + 15, 20, 40); + + // Локомотив + + g.DrawPolygon(pen, new Point[] { new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 110, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 30), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60) }); - g.FillRectangle(brBody, _startPosX + 10, _startPosY + 20, 150, 30); - g.FillRectangle(brBody, _startPosX + 10, _startPosY - 20, 40, 40); - g.FillRectangle(brBody, _startPosX + 100, _startPosY - 20, 10, 40); - g.DrawRectangle(pen, _startPosX + 10, _startPosY + 20, 150, 30); - g.DrawRectangle(pen, _startPosX + 10, _startPosY - 20, 40, 40); - g.DrawRectangle(pen, _startPosX + 100, _startPosY - 20, 10, 40); } /// /// Смена границ формы отрисовки diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs index 5239ec5..17b1063 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs @@ -35,6 +35,7 @@ namespace ElectricLocomotive void IDrawningObject.DrawningObject(Graphics g) { // TODO + _locomotive.DrawTransport(g); } } } diff --git a/ElectricLocomotive/ElectricLocomotive/FieldMap.cs b/ElectricLocomotive/ElectricLocomotive/FieldMap.cs new file mode 100644 index 0000000..adb5115 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/FieldMap.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectricLocomotive +{ + internal class FieldMap : AbstractMap + { + /// + /// Цвет участка закрытого + /// + private readonly Brush barrierColor = new SolidBrush(Color.Brown); + /// + /// Цвет участка открытого + /// + private readonly Brush roadColor = new SolidBrush(Color.Green); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillEllipse(barrierColor, i * (_size_x-1), j * (_size_y-1), 30, 15); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x), j * (_size_y)); + } + protected override void GenerateMap() + { + _map = new int[100, 100]; + _size_x = (float)_width / _map.GetLength(0); + _size_y = (float)_height / _map.GetLength(1); + int counter = 0; + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[i, j] = _freeRoad; + } + } + while (counter < 20) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +} diff --git a/ElectricLocomotive/ElectricLocomotive/FormMap.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormMap.Designer.cs index 137b438..67b4f68 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMap.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMap.Designer.cs @@ -164,7 +164,9 @@ this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта"}); + "Простая карта", + "Поле с грязью", + "Кусты на карте"}); this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(104, 23); diff --git a/ElectricLocomotive/ElectricLocomotive/FormMap.cs b/ElectricLocomotive/ElectricLocomotive/FormMap.cs index f68b1fb..11a83fb 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMap.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMap.cs @@ -91,6 +91,13 @@ namespace ElectricLocomotive case "Простая карта": _abstractMap = new SimpleMap(); break; + + case "Поле с грязью": + _abstractMap = new FieldMap(); + break; + case "Кусты на карте": + _abstractMap = new BushesMap(); + break; } } diff --git a/ElectricLocomotive/ElectricLocomotive/Program.cs b/ElectricLocomotive/ElectricLocomotive/Program.cs index 57d3b26..2cc6ef2 100644 --- a/ElectricLocomotive/ElectricLocomotive/Program.cs +++ b/ElectricLocomotive/ElectricLocomotive/Program.cs @@ -11,7 +11,7 @@ namespace ElectricLocomotive // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormLocomotive()); + Application.Run(new FormMap()); } } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/SimpleMap.cs b/ElectricLocomotive/ElectricLocomotive/SimpleMap.cs index 97be58c..6bce679 100644 --- a/ElectricLocomotive/ElectricLocomotive/SimpleMap.cs +++ b/ElectricLocomotive/ElectricLocomotive/SimpleMap.cs @@ -19,11 +19,11 @@ namespace ElectricLocomotive protected override void DrawBarrierPart(Graphics g, int i, int j) { - g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x), j * (_size_y)); } protected override void DrawRoadPart(Graphics g, int i, int j) { - g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x), j * (_size_y)); } protected override void GenerateMap() {