From 4ec1797948e962ad790462c129cc647fc8d7cebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Tue, 18 Oct 2022 20:56:42 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=D1=8E=20=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D1=83=202.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/AbstractMap.cs | 89 ++++++++++--------------- AirBomber/AirBomber/DrawningBomber.cs | 4 +- AirBomber/AirBomber/FormMap.Designer.cs | 6 +- AirBomber/AirBomber/FormMap.cs | 7 ++ AirBomber/AirBomber/SecondMap.cs | 49 ++++++++++++++ AirBomber/AirBomber/SimpleMap.cs | 2 +- AirBomber/AirBomber/ThirdMap.cs | 49 ++++++++++++++ 7 files changed, 148 insertions(+), 58 deletions(-) create mode 100644 AirBomber/AirBomber/SecondMap.cs create mode 100644 AirBomber/AirBomber/ThirdMap.cs diff --git a/AirBomber/AirBomber/AbstractMap.cs b/AirBomber/AirBomber/AbstractMap.cs index 2754461..09a11d9 100644 --- a/AirBomber/AirBomber/AbstractMap.cs +++ b/AirBomber/AirBomber/AbstractMap.cs @@ -24,66 +24,21 @@ namespace AirBomber _width = width; _height = heigth; _drawningObject = drawningObject; - GenerateMap(); + GenerateMap(); while (!SetObjectOnMap()) { - GenerateMap(); + GenerateMap(); } return DrawMapWithObject(); } private bool CanMove(Direction direction, (float Left, float Right, float Top, float Bottom) position) - { - if (direction == Direction.Left) + { + for (int i = (int)(position.Top / _size_y); i <= (int)(position.Bottom / _size_y); ++i) { - for (int i = (int)((position.Left - _drawningObject.Step) / _size_y); i <= (int)(position.Left / _size_y); ++i) + for (int j = (int)(position.Left / _size_x); j <= (int)(position.Right / _size_x); ++j) { - for (int j = (int)(position.Top / _size_x); j <= (int)(position.Bottom / _size_x); ++j) - { - if (i >= 0 && _map[i,j] == _barrier) - { - return false; - } - } - } - } - else if (direction == Direction.Right) - { - for (int i = (int)(position.Right / _size_y); i <= (int)((position.Right + _drawningObject.Step) / _size_y); ++i) - { - for (int j = (int)(position.Top / _size_x); j <= (int)(position.Bottom / _size_x); ++j) - { - if (i <= _map.GetLength(0) && _map[i, j] == _barrier) - { - return false; - } - } - } - } - else if (direction == Direction.Up) - { - for (int i = (int)(position.Left / _size_y); i <= (int)(position.Right / _size_y); ++i) - { - for (int j = (int)((position.Top - _drawningObject.Step) / _size_x); j <= (int)(position.Top / _size_x); ++j) - { - if (j >= 0 && _map[i, j] == _barrier) - { - return false; - } - } - } - } - else if (direction == Direction.Down) - { - for (int i = (int)(position.Left / _size_y); i <= (int)(position.Right / _size_y); ++i) - { - for (int j = (int)(position.Bottom / _size_x); j <= (int)((position.Bottom + _drawningObject.Step) / _size_x); ++j) - { - if (j <= _map.GetLength(1) && _map[i, j] == _barrier) - { - return false; - } - } + if (i >= 0 && j >= 0 && i < _map.GetLength(0) && j < _map.GetLength(1) && _map[i, j] == _barrier) return false; } } return true; @@ -91,10 +46,27 @@ namespace AirBomber public Bitmap MoveObject(Direction direction) { - if (CanMove(direction, _drawningObject.GetCurrentPosition())) + (float Left, float Right, float Top, float Bottom) position = _drawningObject.GetCurrentPosition(); + if (direction == Direction.Left) + { + position.Left -= _drawningObject.Step; + } + else if (direction == Direction.Right) + { + position.Right += _drawningObject.Step; + } + else if (direction == Direction.Up) + { + position.Top -= _drawningObject.Step; + } + else if (direction == Direction.Down) + { + position.Bottom += _drawningObject.Step; + } + + if (CanMove(direction, position)) { _drawningObject.MoveObject(direction); - } return DrawMapWithObject(); } @@ -107,8 +79,17 @@ namespace AirBomber } int x = _random.Next(0, 10); int y = _random.Next(0, 10); + _drawningObject.SetObject(x, y, _width, _height); - //TODO + (float Left, float Right, float Top, float Bottom) position = _drawningObject.GetCurrentPosition(); + for (int i = (int)(position.Top / _size_y); i <= (int)(position.Bottom / _size_y); ++i) + { + for (int j = (int)(position.Left / _size_x); j <= (int)(position.Right / _size_x); ++j) + { + _map[i, j] = _freeRoad; + } + } + return true; } diff --git a/AirBomber/AirBomber/DrawningBomber.cs b/AirBomber/AirBomber/DrawningBomber.cs index 6b0e810..dcb4edf 100644 --- a/AirBomber/AirBomber/DrawningBomber.cs +++ b/AirBomber/AirBomber/DrawningBomber.cs @@ -80,6 +80,8 @@ namespace AirBomber return; } + Pen pen = new Pen(Color.Black); + SolidBrush brush = new SolidBrush(AirBomber?.BodyColor ?? Color.Cyan); g.FillPolygon(brush, new[] { @@ -108,7 +110,7 @@ namespace AirBomber new Point((int)(_startPosX), (int)(_startPosY + _airBomberHeight / 2)), new Point((int)(_startPosX + 20), (int)(_startPosY + _airBomberHeight / 2 - 5)), new Point((int)(_startPosX + 20), (int)(_startPosY + _airBomberHeight / 2 + 5)), - }); + }); } public void ChangeBorders(int width, int height) diff --git a/AirBomber/AirBomber/FormMap.Designer.cs b/AirBomber/AirBomber/FormMap.Designer.cs index 1741191..99527c8 100644 --- a/AirBomber/AirBomber/FormMap.Designer.cs +++ b/AirBomber/AirBomber/FormMap.Designer.cs @@ -158,8 +158,10 @@ 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(21, 12); + "Простая карта", + "Вторая карта", + "Третья карта"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(20, 12); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); this.comboBoxSelectorMap.TabIndex = 8; diff --git a/AirBomber/AirBomber/FormMap.cs b/AirBomber/AirBomber/FormMap.cs index f1e39bf..8bbcf54 100644 --- a/AirBomber/AirBomber/FormMap.cs +++ b/AirBomber/AirBomber/FormMap.cs @@ -17,6 +17,7 @@ namespace AirBomber public FormMap() { InitializeComponent(); + comboBoxSelectorMap.SelectedIndex = 0; _abstractMap = new SimpleMap(); } @@ -75,6 +76,12 @@ namespace AirBomber case "Простая карта": _abstractMap = new SimpleMap(); break; + case "Вторая карта": + _abstractMap = new SecondMap(); + break; + case "Третья карта": + _abstractMap = new ThirdMap(); + break; } } } diff --git a/AirBomber/AirBomber/SecondMap.cs b/AirBomber/AirBomber/SecondMap.cs new file mode 100644 index 0000000..a2316df --- /dev/null +++ b/AirBomber/AirBomber/SecondMap.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class SecondMap : AbstractMap + { + Brush barrierColor = new SolidBrush(Color.Yellow); + Brush roadColor = new SolidBrush(Color.Blue); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierColor, j * _size_x, i * _size_y, _size_x, _size_y); + } + + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, j * _size_x, i * _size_y, _size_x, _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 < 35) + { + 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/AirBomber/AirBomber/SimpleMap.cs b/AirBomber/AirBomber/SimpleMap.cs index 347fcdb..fb904d0 100644 --- a/AirBomber/AirBomber/SimpleMap.cs +++ b/AirBomber/AirBomber/SimpleMap.cs @@ -34,7 +34,7 @@ namespace AirBomber _map[i, j] = _freeRoad; } } - while (counter < 50) + while (counter < 250) { int x = _random.Next(0, 100); int y = _random.Next(0, 100); diff --git a/AirBomber/AirBomber/ThirdMap.cs b/AirBomber/AirBomber/ThirdMap.cs new file mode 100644 index 0000000..6ae0bb5 --- /dev/null +++ b/AirBomber/AirBomber/ThirdMap.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class ThirdMap : AbstractMap + { + Brush barrierColor = new SolidBrush(Color.Red); + Brush roadColor = new SolidBrush(Color.DarkGreen); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierColor, j * _size_x, i * _size_y, _size_x, _size_y); + } + + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, j * _size_x, i * _size_y, _size_x, _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 < 40) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +}