From c1158d0aca30ef68eeede6236a8d8e86ea139394 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Tue, 27 Sep 2022 10:33:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=B2=D0=BE=D0=B7?= =?UTF-8?q?=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B4=D0=B2=D0=B8=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Airbus/Airbus/AbstractMap.cs | 90 +++++++++++++++++++-- Airbus/Airbus/DrawningAirbus.cs | 46 ++++++++++- Airbus/Airbus/DrawningSuperAirbus.cs | 2 + Airbus/Airbus/FormMap.cs | 38 ++------- Airbus/Airbus/SecondSimpleMap.cs | 11 +-- Airbus/Airbus/SecondVersionAbstractMap.cs | 95 ----------------------- Airbus/Airbus/SimpleMap.cs | 4 +- Airbus/Airbus/ThirdSimpleMap.cs | 6 +- Airbus/Airbus/ThirdVersionAbstractMap.cs | 95 ----------------------- 9 files changed, 143 insertions(+), 244 deletions(-) delete mode 100644 Airbus/Airbus/SecondVersionAbstractMap.cs delete mode 100644 Airbus/Airbus/ThirdVersionAbstractMap.cs diff --git a/Airbus/Airbus/AbstractMap.cs b/Airbus/Airbus/AbstractMap.cs index 14a9235..916a451 100644 --- a/Airbus/Airbus/AbstractMap.cs +++ b/Airbus/Airbus/AbstractMap.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.VisualBasic.Logging; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -33,17 +34,62 @@ namespace Airbus return DrawMapWithObject(); } + //проверка на "накладывание" + protected bool CheckPosition(float Left, float Right, float Top, float Bottom) + { + int x_start = (int)(Left / _size_x); + int y_start = (int)(Right / _size_y); + int x_end = (int)(Top / _size_x); + int y_end = (int)(Bottom / _size_y); + + for(int i = x_start; i <= x_end; i++) + { + for(int j = y_start; j <= y_end; j++) + { + if (_map[i, j] == _barrier) + { + return true; + } + } + } + + return false; + } + public Bitmap MoveObject(Direction direction) { - //Сделать проверку на то, что объект может поместиться в выбранном направлении - if (true) + _drawningObject.MoveObject(direction); + (float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition(); + + if (CheckPosition(Left, Right, Top, Bottom)) { - _drawningObject.MoveObject(direction); + _drawningObject.MoveObject(MoveObjectBack(direction)); } return DrawMapWithObject(); } + private Direction MoveObjectBack(Direction direction) + { + switch (direction) + { + case Direction.Up: + return Direction.Down; + break; + case Direction.Down: + return Direction.Up; + break; + case Direction.Left: + return Direction.Right; + break; + case Direction.Right: + return Direction.Left; + break; + } + + return Direction.None; + } + private bool SetObjectOnMap() { if(_drawningObject == null || _map == null) @@ -54,8 +100,40 @@ namespace Airbus int x = _random.Next(0, 10); int y = _random.Next(0, 10); _drawningObject.SetObject(x, y, _width, _height); - //Сделать проверку на то, то объект не "накладывается" на закрытые участки - return true; + (float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition(); + + if(!CheckPosition(Left, Right, Top, Bottom)) + { + return true; + } + + float x_start = Left; + float y_start = Right; + float x_length = Top - Left; + float y_length = Bottom - Right; + while(CheckPosition(x_start, y_start, x_start + x_length, y_start + y_length)) + { + bool check; + + do + { + check = CheckPosition(x_start, y_start, x_start + x_length, y_start + y_length); + + if (!check) + { + _drawningObject.SetObject((int)x_start, (int)y_start, _width, _height); + return true; + } + else + { + x_start += _size_x; + } + } while (check); + } + + x_start = x; + y_start += _size_y; + return false; } private Bitmap DrawMapWithObject() diff --git a/Airbus/Airbus/DrawningAirbus.cs b/Airbus/Airbus/DrawningAirbus.cs index de5b573..40b09ad 100644 --- a/Airbus/Airbus/DrawningAirbus.cs +++ b/Airbus/Airbus/DrawningAirbus.cs @@ -102,23 +102,57 @@ namespace Airbus } Pen pen = new(Color.Black); + Brush darkBrush = new SolidBrush(Color.DarkKhaki); + Brush darcBlue = new SolidBrush(Color.DarkSlateBlue); //корпус + g.FillRectangle(darkBrush, _startPosX, _startPosY + 10, 40, 10); g.DrawRectangle(pen, _startPosX, _startPosY + 10, 40, 10); //крыло - Brush darkBrush = new SolidBrush(Color.Black); g.FillRectangle(darkBrush, _startPosX + 10, _startPosY + 13, 20, 2); g.FillRectangle(darkBrush, _startPosX + 12, _startPosY + 15, 16, 2); + g.DrawRectangle(pen, _startPosX + 10, _startPosY + 13, 20, 2); + g.DrawRectangle(pen, _startPosX + 12, _startPosY + 15, 16, 2); //хвостовое крыло + Point[] point_tailWing = new Point[] + { + new Point((int)_startPosX, (int)_startPosY + 10), + new Point((int)_startPosX, (int)_startPosY), + new Point((int)_startPosX + 10, (int)_startPosY + 10), + new Point((int)_startPosX, (int)_startPosY + 10) + }; + + g.FillPolygon(darkBrush, point_tailWing); g.DrawLine(pen, _startPosX, _startPosY + 10, _startPosX, _startPosY); g.DrawLine(pen, _startPosX, _startPosY, _startPosX + 10, _startPosY + 10); //заднее поперечное крыло g.FillEllipse(darkBrush, _startPosX - 3, _startPosY + 7, 10, 5); + g.DrawEllipse(pen, _startPosX - 3, _startPosY + 7, 10, 5); //нос самолёта + //верхняя половина + Point[] point_upperBow = new Point[] + { + new Point((int)_startPosX + 40, (int)_startPosY + 15), + new Point((int)_startPosX + 50, (int)_startPosY + 16), + new Point((int)_startPosX + 40, (int)_startPosY + 10), + new Point((int)_startPosX + 40, (int)_startPosY + 15) + }; + + //нижняя половина + Point[] point_lowerBow = new Point[] + { + new Point((int)_startPosX + 40, (int)_startPosY + 15), + new Point((int)_startPosX + 40, (int)_startPosY + 20), + new Point((int)_startPosX + 50, (int)_startPosY + 16), + new Point((int)_startPosX + 40, (int)_startPosY + 15), + }; + + g.FillPolygon(darcBlue, point_upperBow); + g.FillPolygon(darkBrush, point_lowerBow); g.DrawLine(pen, _startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 16); g.DrawLine(pen, _startPosX + 40, _startPosY + 10, _startPosX + 47, _startPosY + 15); g.DrawLine(pen, _startPosX + 40, _startPosY + 20, _startPosX + 50, _startPosY + 15); @@ -127,10 +161,16 @@ namespace Airbus g.FillRectangle(darkBrush, _startPosX + 10, _startPosY + 21, 2, 2); g.FillRectangle(darkBrush, _startPosX + 12, _startPosY + 23, 4, 4); g.FillRectangle(darkBrush, _startPosX + 8, _startPosY + 23, 2, 4); + g.DrawRectangle(pen, _startPosX + 10, _startPosY + 21, 2, 2); + g.DrawRectangle(pen, _startPosX + 12, _startPosY + 23, 4, 4); + g.DrawRectangle(pen, _startPosX + 8, _startPosY + 23, 2, 4); //переднее шасси - g.FillRectangle(darkBrush, _startPosX + 35, _startPosY + 21, 2, 2); - g.FillRectangle(darkBrush, _startPosX + 35, _startPosY + 23, 4, 4); + g.FillRectangle(darkBrush, _startPosX + 36, _startPosY + 21, 2, 2); + g.FillRectangle(darkBrush, _startPosX + 36, _startPosY + 23, 4, 4); + g.DrawRectangle(pen, _startPosX + 35, _startPosY + 21, 2, 2); + g.DrawRectangle(pen, _startPosX + 35, _startPosY + 23, 4, 4); + } //смена границ формы отрисовки diff --git a/Airbus/Airbus/DrawningSuperAirbus.cs b/Airbus/Airbus/DrawningSuperAirbus.cs index 2658f09..4d3b1ba 100644 --- a/Airbus/Airbus/DrawningSuperAirbus.cs +++ b/Airbus/Airbus/DrawningSuperAirbus.cs @@ -45,5 +45,7 @@ namespace Airbus g.DrawEllipse(pen, _startPosX + 24, _startPosY + 22, 10, 5); } } + + } } diff --git a/Airbus/Airbus/FormMap.cs b/Airbus/Airbus/FormMap.cs index 71768bd..833a002 100644 --- a/Airbus/Airbus/FormMap.cs +++ b/Airbus/Airbus/FormMap.cs @@ -13,14 +13,11 @@ namespace Airbus public partial class FormMap : Form { private AbstractMap _abstractMap; - private SecondVersionAbstractMap _secondAbstractMap; - private ThirdVersionAbstractMap _thirdVersionAbstractMap; public FormMap() { InitializeComponent(); _abstractMap = new SimpleMap(); - _secondAbstractMap = new SecondSimpleMap(); } //заполнение информации по объекту @@ -30,21 +27,9 @@ namespace Airbus toolStripStatusLabelWeight.Text = $"Вес: {airbus.Airbus?.Weight}"; toolStripStatusLabelCorpusColor.Text = $"Цвет: {airbus.Airbus?.CorpusColor.Name}"; - if (comboBoxSelectorMap.Text == "Простая карта") - { - pictureBoxAirbus.Image = _abstractMap.CreateMap(pictureBoxAirbus.Width, pictureBoxAirbus.Height, - new DrawningObjectAirbus(airbus)); - } - if (comboBoxSelectorMap.Text == "Буря в пустыне") - { - pictureBoxAirbus.Image = _secondAbstractMap.CreateMap(pictureBoxAirbus.Width, pictureBoxAirbus.Height, - new DrawningObjectAirbus(airbus)); - } - if (comboBoxSelectorMap.Text == "Звёздные войны") - { - pictureBoxAirbus.Image = _thirdVersionAbstractMap.CreateMap(pictureBoxAirbus.Width, pictureBoxAirbus.Height, - new DrawningObjectAirbus(airbus)); - } + pictureBoxAirbus.Image = _abstractMap.CreateMap(pictureBoxAirbus.Width, pictureBoxAirbus.Height, + new DrawningObjectAirbus(airbus)); + } //обработка нажатия кнопки "Создать" @@ -77,18 +62,7 @@ namespace Airbus break; } - if (comboBoxSelectorMap.Text == "Простая карта") - { - pictureBoxAirbus.Image = _abstractMap?.MoveObject(dir); - } - if (comboBoxSelectorMap.Text == "Буря в пустыне") - { - pictureBoxAirbus.Image = _secondAbstractMap?.MoveObject(dir); - } - if (comboBoxSelectorMap.Text == "Звёздные войны") - { - pictureBoxAirbus.Image = _thirdVersionAbstractMap?.MoveObject(dir); - } + pictureBoxAirbus.Image = _abstractMap?.MoveObject(dir); } //обработка нажития кнопки "Модификация" @@ -110,10 +84,10 @@ namespace Airbus _abstractMap = new SimpleMap(); break; case "Буря в пустыне": - _secondAbstractMap = new SecondSimpleMap(); + _abstractMap = new SecondSimpleMap(); break; case "Звёздные войны": - _thirdVersionAbstractMap = new ThirdSimpleMap(); + _abstractMap = new ThirdSimpleMap(); break; } } diff --git a/Airbus/Airbus/SecondSimpleMap.cs b/Airbus/Airbus/SecondSimpleMap.cs index 64266be..87968ba 100644 --- a/Airbus/Airbus/SecondSimpleMap.cs +++ b/Airbus/Airbus/SecondSimpleMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Airbus { - internal class SecondSimpleMap : SecondVersionAbstractMap + internal class SecondSimpleMap : AbstractMap { //цвет закрытого участка Brush barriedColor = new SolidBrush(Color.DarkRed); @@ -16,17 +16,12 @@ namespace Airbus protected override void DrawBarrierPart(Graphics g, int i, int j) { - //float _weightLength = i * (_size_x + 1) - i * _size_x; - //float _heightLength = j * (_size_y + 1) - j * _size_y; - - g.FillEllipse(barriedColor, i * _size_x, j * _size_y, 10, 10); + g.FillEllipse(barriedColor, i * _size_x, j * _size_y, _size_x, _size_x); } protected override void DrawRoadPart(Graphics g, int i, int j) { - float _weightLength = i * (_size_x + 1); - float _heightLength = j * (_size_y + 1); - g.FillRectangle(roadColor, i * _size_x, j * _size_y, _weightLength, _heightLength); + g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x, _size_y); } protected override void GenerateMap() diff --git a/Airbus/Airbus/SecondVersionAbstractMap.cs b/Airbus/Airbus/SecondVersionAbstractMap.cs deleted file mode 100644 index 37449c5..0000000 --- a/Airbus/Airbus/SecondVersionAbstractMap.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Airbus -{ - internal abstract class SecondVersionAbstractMap - { - private IDrawningObject _drawningObject = null; - protected int[,] _map = null; - protected int _width; - protected int _height; - protected float _size_x; - protected float _size_y; - protected readonly Random _random = new(); - protected readonly int _freeRoad = 0; - protected readonly int _barrier = 1; - - public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject) - { - _width = width; - _height = height; - _drawningObject = drawningObject; - - GenerateMap(); - while (!SetObjectOnMap()) - { - GenerateMap(); - } - - return DrawMapWithObject(); - } - - public Bitmap MoveObject(Direction direction) - { - //Сделать проверку на то, что объект может поместиться в выбранном направлении - if (true) - { - _drawningObject.MoveObject(direction); - } - - return DrawMapWithObject(); - } - - private bool SetObjectOnMap() - { - if (_drawningObject == null || _map == null) - { - return false; - } - - int x = _random.Next(0, 10); - int y = _random.Next(0, 10); - _drawningObject.SetObject(x, y, _width, _height); - //Сделать проверку на то, то объект не "накладывается" на закрытые участки - return true; - } - - private Bitmap DrawMapWithObject() - { - Bitmap bmp = new(_width, _height); - - if (_drawningObject == null || _map == null) - { - return bmp; - } - - Graphics gr = Graphics.FromImage(bmp); - - for (int i = 0; i < _map.GetLength(0); ++i) - { - for (int j = 0; j < _map.GetLength(1); ++j) - { - if (_map[i, j] == _freeRoad) - { - DrawRoadPart(gr, i, j); - } - else if (_map[i, j] == _barrier) - { - DrawBarrierPart(gr, i, j); - } - } - } - - _drawningObject.DrawningObject(gr); - return bmp; - } - - protected abstract void GenerateMap(); - protected abstract void DrawRoadPart(Graphics g, int i, int j); - protected abstract void DrawBarrierPart(Graphics g, int i, int j); - } -} diff --git a/Airbus/Airbus/SimpleMap.cs b/Airbus/Airbus/SimpleMap.cs index f0a8f5e..d347c15 100644 --- a/Airbus/Airbus/SimpleMap.cs +++ b/Airbus/Airbus/SimpleMap.cs @@ -16,12 +16,12 @@ namespace Airbus protected override void DrawBarrierPart(Graphics g, int i, int j) { - g.FillRectangle(barriedColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + g.FillRectangle(barriedColor, i * _size_x, j * _size_y, _size_x, _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, _size_x, _size_y); } protected override void GenerateMap() diff --git a/Airbus/Airbus/ThirdSimpleMap.cs b/Airbus/Airbus/ThirdSimpleMap.cs index a38a182..0e24b10 100644 --- a/Airbus/Airbus/ThirdSimpleMap.cs +++ b/Airbus/Airbus/ThirdSimpleMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Airbus { - internal class ThirdSimpleMap : ThirdVersionAbstractMap + internal class ThirdSimpleMap : AbstractMap { Random rnd = new Random(); @@ -24,12 +24,12 @@ namespace Airbus { randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)); barriedColor = new SolidBrush(randomColor); - g.FillRectangle(barriedColor, i * _size_x, j * _size_y, i * (_size_x + 1) + 10, j * (_size_y + 1)); + g.FillRectangle(barriedColor, i * _size_x, j * _size_y, _size_x, _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, _size_x, _size_y); } protected override void GenerateMap() diff --git a/Airbus/Airbus/ThirdVersionAbstractMap.cs b/Airbus/Airbus/ThirdVersionAbstractMap.cs deleted file mode 100644 index c42499b..0000000 --- a/Airbus/Airbus/ThirdVersionAbstractMap.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Airbus -{ - internal abstract class ThirdVersionAbstractMap - { - private IDrawningObject _drawningObject = null; - protected int[,] _map = null; - protected int _width; - protected int _height; - protected float _size_x; - protected float _size_y; - protected readonly Random _random = new(); - protected readonly int _freeRoad = 0; - protected readonly int _barrier = 1; - - public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject) - { - _width = width; - _height = height; - _drawningObject = drawningObject; - - GenerateMap(); - while (!SetObjectOnMap()) - { - GenerateMap(); - } - - return DrawMapWithObject(); - } - - public Bitmap MoveObject(Direction direction) - { - //Сделать проверку на то, что объект может поместиться в выбранном направлении - if (true) - { - _drawningObject.MoveObject(direction); - } - - return DrawMapWithObject(); - } - - private bool SetObjectOnMap() - { - if (_drawningObject == null || _map == null) - { - return false; - } - - int x = _random.Next(0, 10); - int y = _random.Next(0, 10); - _drawningObject.SetObject(x, y, _width, _height); - //Сделать проверку на то, то объект не "накладывается" на закрытые участки - return true; - } - - private Bitmap DrawMapWithObject() - { - Bitmap bmp = new(_width, _height); - - if (_drawningObject == null || _map == null) - { - return bmp; - } - - Graphics gr = Graphics.FromImage(bmp); - - for (int i = 0; i < _map.GetLength(0); ++i) - { - for (int j = 0; j < _map.GetLength(1); ++j) - { - if (_map[i, j] == _freeRoad) - { - DrawRoadPart(gr, i, j); - } - else if (_map[i, j] == _barrier) - { - DrawBarrierPart(gr, i, j); - } - } - } - - _drawningObject.DrawningObject(gr); - return bmp; - } - - protected abstract void GenerateMap(); - protected abstract void DrawRoadPart(Graphics g, int i, int j); - protected abstract void DrawBarrierPart(Graphics g, int i, int j); - } -}