diff --git a/ArmoredCar/ArmoredCar/AbstractMap.cs b/ArmoredCar/ArmoredCar/AbstractMap.cs new file mode 100644 index 0000000..8b2f5c0 --- /dev/null +++ b/ArmoredCar/ArmoredCar/AbstractMap.cs @@ -0,0 +1,197 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal abstract class AbstractMap + { + 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) + { + bool flag = true; + float step = _drawningObject.Step; + (float left, float right, float top, float bottom) = _drawningObject.GetCurrentPosition(); + + int x1_obj_next = (int)((left - step) / _size_x); + int y1_obj_next = (int)((top - step) / _size_y); + int x2_obj_next = (int)((right + step) / _size_x); + int y2_obj_next = (int)((bottom + step) / _size_y); + + int x1_obj_current = (int)(left / _size_x); + int y1_obj_current = (int)(top / _size_y); + int x2_obj_current = (int)(right / _size_x); + int y2_obj_current = (int)(bottom / _size_y); + + // Проверка возможности перемещения + switch (direction) + { + case Direction.Left: + { + if (x1_obj_next < 0) + { + flag = false; + } + else + { + for (int i = x1_obj_next; i <= x1_obj_current; i++) + { + for (int j = y1_obj_current; j <= y2_obj_current; j++) + { + if (_map[i, j] == _barrier) + { + flag = false; + } + } + } + } + break; + } + case Direction.Right: + { + if (x2_obj_next >= _map.GetLength(0)) + { + flag = false; + } + else + { + for (int i = x2_obj_current; i <= x2_obj_next; i++) + { + for (int j = y1_obj_current; j <= y2_obj_current; j++) + { + if (_map[i, j] == _barrier) + { + flag = false; + } + } + } + } + break; + } + case Direction.Up: + { + if (y1_obj_next < 0) + { + flag = false; + } + else + { + for (int i = x1_obj_current; i <= x2_obj_current; i++) + { + for (int j = y1_obj_next; j <= y1_obj_current; j++) + { + if (_map[i, j] == _barrier) + { + flag = false; + } + } + } + } + break; + } + case Direction.Down: + { + if (y2_obj_next >= _map.GetLength(0)) + { + flag = false; + } + else + { + for (int i = x1_obj_current; i <= x2_obj_current; i++) + { + for (int j = y2_obj_current; j <= y2_obj_next; j++) + { + if (_map[i, j] == _barrier) + { + flag = false; + } + } + } + } + break; + } + } + + if (flag) + { + _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); + (float left, float right, float top, float bottom) = _drawningObject.GetCurrentPosition(); + for (int i = (int)(x / _size_x); i <= (int) (right / _size_x); i++) + { + for (int j = (int)(y / _size_y); j <= (int) (bottom / _size_y); j++) + { + if (_map[i, j] == _barrier) + { + return false; + } + } + } + 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/ArmoredCar/ArmoredCar/Direction.cs b/ArmoredCar/ArmoredCar/Direction.cs index 4bcfc42..9e28806 100644 --- a/ArmoredCar/ArmoredCar/Direction.cs +++ b/ArmoredCar/ArmoredCar/Direction.cs @@ -8,6 +8,7 @@ namespace ArmoredCar { enum Direction { + None = 0, Up = 1, Down = 2, Left = 3, diff --git a/ArmoredCar/ArmoredCar/DrawningArmoredCar.cs b/ArmoredCar/ArmoredCar/DrawningArmoredCar.cs index a0ae58b..8277aa6 100644 --- a/ArmoredCar/ArmoredCar/DrawningArmoredCar.cs +++ b/ArmoredCar/ArmoredCar/DrawningArmoredCar.cs @@ -12,15 +12,15 @@ namespace ArmoredCar /// /// Класс-сущность /// - public EntityArmoredCar ArmoredCar { private set; get; } + public EntityArmoredCar ArmoredCar { protected set; get; } /// /// Левая координата отрисовки бронированной машины /// - private float _startPosX; + protected float _startPosX; /// /// Верхняя кооридната отрисовки бронированной машины /// - private float _startPosY; + protected float _startPosY; /// /// Ширина окна отрисовки /// @@ -43,11 +43,18 @@ namespace ArmoredCar /// Скорость /// Вес бронированной машины /// Цвет кузова - public void Init(int speed, float weight, Color bodyColor) + public DrawningArmoredCar(int speed, float weight, Color bodyColor) { - ArmoredCar = new EntityArmoredCar(); - ArmoredCar.Init(speed, weight, bodyColor); + ArmoredCar = new EntityArmoredCar(speed, weight, bodyColor); } + + protected DrawningArmoredCar(int speed, float weight, Color bodyColor, int carWidth, int carHeight) : + this(speed, weight, bodyColor) + { + _carWidth = carWidth; + _carHeight = carHeight; + } + /// /// Установка позиции бронированной машины /// @@ -114,24 +121,22 @@ namespace ArmoredCar /// Отрисовка бронированной машины /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue) { return; } - Pen pen = new(Color.Black); - - Brush br = new SolidBrush(ArmoredCar?.BodyColor ?? Color.Black); + g.FillRectangle(br, _startPosX + 20, _startPosY, 40, 20); g.FillRectangle(br, _startPosX, _startPosY + 20, 80, 20); g.FillEllipse(br, _startPosX, _startPosY + 30, 20, 20); - + g.FillEllipse(br, _startPosX + 80 - 20, _startPosY + 30, 20, 20); - + g.FillRectangle(br, _startPosX + 15, _startPosY + 20, 60, 30); Brush brGray = new SolidBrush(Color.LightGray); @@ -163,6 +168,14 @@ namespace ArmoredCar _startPosY = _pictureHeight.Value - _carHeight; } } - + /// + /// Получение текущей позиции объекта + /// + /// + public (float Left, float Right, float Top, float Bottom) + GetCurrentPosition() + { + return (_startPosX, _startPosX + _carWidth, _startPosY, _startPosY + _carHeight); + } } } diff --git a/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs new file mode 100644 index 0000000..09aebde --- /dev/null +++ b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class DrawningObjectArmCar : IDrawningObject + { + private DrawningArmoredCar _armCar = null; + public DrawningObjectArmCar(DrawningArmoredCar car) + { + _armCar = car; + } + public float Step => _armCar?.ArmoredCar?.Step ?? 0; + public (float Left, float Right, float Top, float Bottom) + GetCurrentPosition() + { + return _armCar?.GetCurrentPosition() ?? default; + } + public void MoveObject(Direction direction) + { + _armCar?.MoveTransport(direction); + } + public void SetObject(int x, int y, int width, int height) + { + _armCar.SetPosition(x, y, width, height); + } + void IDrawningObject.DrawningObject(Graphics g) + { + if (_armCar != null) + _armCar.DrawTransport(g); + } + } +} diff --git a/ArmoredCar/ArmoredCar/DrawningTank.cs b/ArmoredCar/ArmoredCar/DrawningTank.cs new file mode 100644 index 0000000..b874d52 --- /dev/null +++ b/ArmoredCar/ArmoredCar/DrawningTank.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class DrawningTank : DrawningArmoredCar + { + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес бронированной машины + /// Цвет кузова + /// Дополнительный цвет + /// Признак наличия башни с орудием + /// Признак наличия зенитного пулемета + public DrawningTank(int speed, float weight, Color bodyColor, Color + dopColor, bool towerWeapon, bool aMachineGun) : + base(speed, weight, bodyColor, 110, 60) + { + ArmoredCar = new EntityTank(speed, weight, bodyColor, dopColor, towerWeapon, aMachineGun); + } + public override void DrawTransport(Graphics g) + { + if (ArmoredCar is not EntityTank Tank) + { + return; + } + Brush dopBrush = new SolidBrush(Tank.DopColor); + + if (Tank.AMachineGun) + { + g.FillRectangle(dopBrush, _startPosX + 40, _startPosY, 5, 20); + g.FillRectangle(dopBrush, _startPosX + 40, _startPosY, 40, 5); + g.FillEllipse(dopBrush, _startPosX + 27, _startPosY + 7, 30, 10); + } + if (Tank.TowerWeapon) + { + g.FillRectangle(dopBrush, _startPosX + 60, _startPosY + 15, 7, 10); + g.FillRectangle(dopBrush, _startPosX + 60, _startPosY + 17, 20, 5); + } + _startPosY += 10; + base.DrawTransport(g); + _startPosY -= 10; + } + + } +} diff --git a/ArmoredCar/ArmoredCar/EntityArmoredCar.cs b/ArmoredCar/ArmoredCar/EntityArmoredCar.cs index a63e8c2..02396ba 100644 --- a/ArmoredCar/ArmoredCar/EntityArmoredCar.cs +++ b/ArmoredCar/ArmoredCar/EntityArmoredCar.cs @@ -30,7 +30,7 @@ namespace ArmoredCar /// /// /// - public void Init(int speed, float weight, Color bodyColor) + public EntityArmoredCar(int speed, float weight, Color bodyColor) { Random rnd = new(); Speed = speed <= 0 ? rnd.Next(50, 150) : speed; diff --git a/ArmoredCar/ArmoredCar/EntityTank.cs b/ArmoredCar/ArmoredCar/EntityTank.cs new file mode 100644 index 0000000..e540189 --- /dev/null +++ b/ArmoredCar/ArmoredCar/EntityTank.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class EntityTank : EntityArmoredCar + { + /// + /// Дополнительный цвет + /// + public Color DopColor { get; private set; } + /// + /// Признак наличия башни с орудием + /// + public bool TowerWeapon { get; private set; } + /// + /// Признак наличия зенитного пулемета + /// + public bool AMachineGun { get; private set; } + + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес бронированной машины + /// Цвет кузова + /// Дополнительный цвет + /// Признак наличия башни с орудием + /// Признак наличия зенитного пулемета + public EntityTank(int speed, float weight, Color bodyColor, Color + dopColor, bool towerWeapon, bool aMachineGun) : + base(speed, weight, bodyColor) + { + DopColor = dopColor; + TowerWeapon = towerWeapon; + AMachineGun = aMachineGun; + } + } +} diff --git a/ArmoredCar/ArmoredCar/FormCar.cs b/ArmoredCar/ArmoredCar/FormCar.cs index 9f5049c..e48d650 100644 --- a/ArmoredCar/ArmoredCar/FormCar.cs +++ b/ArmoredCar/ArmoredCar/FormCar.cs @@ -36,8 +36,7 @@ namespace ArmoredCar private void ButtonCreate_Click(object sender, EventArgs e) { Random rnd = new(); - _car = new DrawningArmoredCar(); - _car.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), + _car = new DrawningArmoredCar(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); _car.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxCar.Width, pictureBoxCar.Height); diff --git a/ArmoredCar/ArmoredCar/FormMap.Designer.cs b/ArmoredCar/ArmoredCar/FormMap.Designer.cs new file mode 100644 index 0000000..574b60c --- /dev/null +++ b/ArmoredCar/ArmoredCar/FormMap.Designer.cs @@ -0,0 +1,212 @@ + +namespace ArmoredCar +{ + partial class FormMap + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.statusStrip1 = new System.Windows.Forms.StatusStrip(); + this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel(); + this.pictureBoxCar = new System.Windows.Forms.PictureBox(); + this.button1 = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.button2 = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.statusStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCar)).BeginInit(); + this.SuspendLayout(); + // + // statusStrip1 + // + this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripStatusLabelSpeed, + this.toolStripStatusLabelWeight, + this.toolStripStatusLabelBodyColor}); + this.statusStrip1.Location = new System.Drawing.Point(0, 428); + this.statusStrip1.Name = "statusStrip1"; + this.statusStrip1.Size = new System.Drawing.Size(800, 22); + this.statusStrip1.TabIndex = 0; + this.statusStrip1.Text = "statusStrip1"; + // + // toolStripStatusLabelSpeed + // + this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; + this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(62, 17); + this.toolStripStatusLabelSpeed.Text = "Скорость:"; + // + // toolStripStatusLabelWeight + // + this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; + this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(32, 17); + this.toolStripStatusLabelWeight.Text = "Вес: "; + // + // toolStripStatusLabelBodyColor + // + this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; + this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(36, 17); + this.toolStripStatusLabelBodyColor.Text = "Цвет:"; + // + // pictureBoxCar + // + this.pictureBoxCar.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxCar.Location = new System.Drawing.Point(0, 0); + this.pictureBoxCar.Name = "pictureBoxCar"; + this.pictureBoxCar.Size = new System.Drawing.Size(800, 428); + this.pictureBoxCar.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; + this.pictureBoxCar.TabIndex = 6; + this.pictureBoxCar.TabStop = false; + // + // button1 + // + this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.button1.Location = new System.Drawing.Point(42, 382); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 7; + this.button1.Text = "Создать"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.ButtonCreate_Click); + // + // buttonUp + // + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.BackgroundImage = global::ArmoredCar.Properties.Resources.Up; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonUp.Location = new System.Drawing.Point(692, 324); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 8; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonLeft + // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::ArmoredCar.Properties.Resources.Left; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonLeft.Location = new System.Drawing.Point(656, 360); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 9; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::ArmoredCar.Properties.Resources.Down; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonDown.Location = new System.Drawing.Point(692, 360); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 10; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::ArmoredCar.Properties.Resources.Right; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonRight.Location = new System.Drawing.Point(728, 360); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 11; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // button2 + // + this.button2.Location = new System.Drawing.Point(154, 382); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(95, 23); + this.button2.TabIndex = 12; + this.button2.Text = "Модификация"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.ButtonCreateModif_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Карта 1", + "Карта 2"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(42, 26); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(107, 23); + this.comboBoxSelectorMap.TabIndex = 13; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // + // FormMap + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.comboBoxSelectorMap); + this.Controls.Add(this.button2); + this.Controls.Add(this.buttonRight); + this.Controls.Add(this.buttonDown); + this.Controls.Add(this.buttonLeft); + this.Controls.Add(this.buttonUp); + this.Controls.Add(this.button1); + this.Controls.Add(this.pictureBoxCar); + this.Controls.Add(this.statusStrip1); + this.Name = "FormMap"; + this.Text = "Карта"; + this.statusStrip1.ResumeLayout(false); + this.statusStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCar)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.StatusStrip statusStrip1; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelSpeed; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelWeight; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelBodyColor; + private System.Windows.Forms.PictureBox pictureBoxCar; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button buttonUp; + private System.Windows.Forms.Button buttonLeft; + private System.Windows.Forms.Button buttonDown; + private System.Windows.Forms.Button buttonRight; + private System.Windows.Forms.Button button2; + private System.Windows.Forms.ComboBox comboBoxSelectorMap; + } +} + diff --git a/ArmoredCar/ArmoredCar/FormMap.cs b/ArmoredCar/ArmoredCar/FormMap.cs new file mode 100644 index 0000000..996cdd7 --- /dev/null +++ b/ArmoredCar/ArmoredCar/FormMap.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ArmoredCar +{ + public partial class FormMap : Form + { + + private AbstractMap _abstractMap; + public FormMap() + { + InitializeComponent(); + _abstractMap = new SimpleMap(); + } + private void SetData(DrawningArmoredCar car) + { + toolStripStatusLabelSpeed.Text = $"Скорость: {car.ArmoredCar.Speed}"; + toolStripStatusLabelWeight.Text = $"Вес: {car.ArmoredCar.Weight}"; + toolStripStatusLabelBodyColor.Text = $"Цвет:{ car.ArmoredCar.BodyColor.Name}"; + pictureBoxCar.Image = _abstractMap.CreateMap(pictureBoxCar.Width, + pictureBoxCar.Height, new DrawningObjectArmCar(car)); + } + + /// + /// Обработка нажатия кнопки "Создать" + /// + /// + /// + private void ButtonCreate_Click(object sender, EventArgs e) + { + Random rnd = new(); + var car = new DrawningArmoredCar(rnd.Next(100, 300), rnd.Next(1000, 2000), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + SetData(car); + } + + /// + /// Изменение размеров формы + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + Direction dir = Direction.None; + switch (name) + { + case "buttonUp": + dir = Direction.Up; + break; + case "buttonDown": + dir = Direction.Down; + break; + case "buttonLeft": + dir = Direction.Left; + break; + case "buttonRight": + dir = Direction.Right; + break; + } + pictureBoxCar.Image = _abstractMap?.MoveObject(dir); + } + /// + /// Обработка нажатия кнопки "Модификация" + /// + /// + /// + private void ButtonCreateModif_Click(object sender, EventArgs e) + { + Random rnd = new(); + var tank = new DrawningTank(rnd.Next(100, 300), rnd.Next(1000, + 2000), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, + 256)), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, + 256)), + Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); + SetData(tank); + } + /// + /// Смена карты + /// + /// + /// + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, + EventArgs e) + { + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + + case "Карта 1": + _abstractMap = new MyMapWooden(); + break; + case "Карта 2": + _abstractMap = new MyMapLabirinth(); + break; + + } + } + } +} diff --git a/ArmoredCar/ArmoredCar/FormMap.resx b/ArmoredCar/ArmoredCar/FormMap.resx new file mode 100644 index 0000000..5cb320f --- /dev/null +++ b/ArmoredCar/ArmoredCar/FormMap.resx @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/ArmoredCar/ArmoredCar/IDrawningObject.cs b/ArmoredCar/ArmoredCar/IDrawningObject.cs new file mode 100644 index 0000000..64307a1 --- /dev/null +++ b/ArmoredCar/ArmoredCar/IDrawningObject.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal interface IDrawningObject + { + /// + /// Шаг перемещения объекта + /// + public float Step { get; } + /// + /// Установка позиции объекта + /// + /// Координата X + /// Координата Y + /// Ширина полотна + /// Высота полотна + void SetObject(int x, int y, int width, int height); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(Direction direction); + /// + /// Отрисовка объекта + /// + /// + void DrawningObject(Graphics g); + /// + /// Получение текущей позиции объекта + /// + /// + (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + } +} diff --git a/ArmoredCar/ArmoredCar/MyMapLabirinth.cs b/ArmoredCar/ArmoredCar/MyMapLabirinth.cs new file mode 100644 index 0000000..fc30914 --- /dev/null +++ b/ArmoredCar/ArmoredCar/MyMapLabirinth.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class MyMapLabirinth : AbstractMap + { + /// + /// Цвет участка закрытого + /// + private readonly Brush barrierTreeColor = new SolidBrush(Color.Lime); + /// + /// Цвет участка открытого + /// + private readonly Brush roadColor = new SolidBrush(Color.SandyBrown); + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierTreeColor, i * _size_x, j * _size_y, _size_x * 2, _size_y * 2); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x * 2, _size_y * 2); + } + protected override void GenerateMap() + { + _map = new int[50, 50]; + _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, 50); + int y = _random.Next(0, 50); + int number = _random.Next(1, 15); + if (counter < 5) { + if (number + x > _map.GetLength(0)) + { + number = _map.GetLength(0) - x - 1; + } + for (int i = x; i < x + number; ++i) + { + _map[i, y] = _barrier; + } + } else + { + if (number + y > _map.GetLength(1)) + { + number = _map.GetLength(1) - y - 1; + } + for (int i = y; i < y + number; ++i) + { + _map[x, i] = _barrier; + } + } + counter++; + + } + } + } +} diff --git a/ArmoredCar/ArmoredCar/MyMapWooden.cs b/ArmoredCar/ArmoredCar/MyMapWooden.cs new file mode 100644 index 0000000..6951040 --- /dev/null +++ b/ArmoredCar/ArmoredCar/MyMapWooden.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class MyMapWooden : AbstractMap + { + /// + /// Цвет участка закрытого + /// + private readonly Brush barrierTreeColor = new SolidBrush(Color.LightGreen); + /// + /// Цвет участка открытого + /// + private readonly Brush roadColor = new SolidBrush(Color.Brown); + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierTreeColor, i * _size_x, j * _size_y, _size_x * 2, _size_y * 2); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x * 2, _size_y * 2 ); + } + protected override void GenerateMap() + { + _map = new int[50, 50]; + _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 < 25) + { + int x = _random.Next(0, 50); + int y = _random.Next(0, 50); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +} diff --git a/ArmoredCar/ArmoredCar/Program.cs b/ArmoredCar/ArmoredCar/Program.cs index d8b6012..d282ea3 100644 --- a/ArmoredCar/ArmoredCar/Program.cs +++ b/ArmoredCar/ArmoredCar/Program.cs @@ -17,7 +17,7 @@ namespace ArmoredCar Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new FormCar()); + Application.Run(new FormMap()); } } } diff --git a/ArmoredCar/ArmoredCar/SimpleMap.cs b/ArmoredCar/ArmoredCar/SimpleMap.cs new file mode 100644 index 0000000..8029b92 --- /dev/null +++ b/ArmoredCar/ArmoredCar/SimpleMap.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class SimpleMap : AbstractMap + { + /// + /// Цвет участка закрытого + /// + private readonly Brush barrierColor = new SolidBrush(Color.Black); + /// + /// Цвет участка открытого + /// + private readonly Brush roadColor = new SolidBrush(Color.Gray); + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierColor, 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, _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 < 50) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +}