diff --git a/WinFormsApp1.rar1 b/WinFormsApp1.rar1 new file mode 100644 index 0000000..25038de Binary files /dev/null and b/WinFormsApp1.rar1 differ diff --git a/WinFormsApp1/AbstractMap.cs b/WinFormsApp1/AbstractMap.cs new file mode 100644 index 0000000..1ecea2a --- /dev/null +++ b/WinFormsApp1/AbstractMap.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsApp1 +{ + 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 bool CheckAround(float Left, float Right, float Top, float Bottom) + { + int startX = (int)(Left / _size_x); + int startY = (int)(Right / _size_y); + int endX = (int)(Top / _size_x); + if (endX > 100) + { + endX = 100; + } + int endY = (int)(Bottom / _size_y); + if (endY > 100) + { + endY = 100; + } + + for (int i = startX; i < endX; i++) + { + for (int j = startY; j < endY; j++) + { + if (_map[i, j] == _barrier) + { + return true; + } + } + } + return false; + } + + public Bitmap MoveObject(Direction direction) + { + _drawningObject.MoveObject(direction); + (float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition(); + + if (CheckAround(Left, Right, Top, Bottom)) + { + _drawningObject.MoveObject(MoveObjectBack(direction)); + } + return DrawMapWithObject(); + } + + private Direction MoveObjectBack(Direction direction) + { + switch (direction) + { + case Direction.Up: + return Direction.Down; + case Direction.Down: + return Direction.Up; + case Direction.Left: + return Direction.Right; + case Direction.Right: + return Direction.Left; + } + return Direction.None; + } + + 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(); + if (!CheckAround(Left, Right, Top, Bottom)) return true; + float startX = Left; + float startY = Right; + float lengthX = Top - Left; + float lengthY = Bottom - Right; + while (CheckAround(startX, startY, startX + lengthX, startY + lengthY)) + { + bool result; + do + { + result = CheckAround(startX, startY, startX + lengthX, startY + lengthY); + if (!result) + { + _drawningObject.SetObject((int)startX, (int)startY, _width, _height); + return true; + } + else + { + startX += _size_x; + } + } while (result); + startX = x; + startY += _size_y; + } + return false; + } + + 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/WinFormsApp1/Direction.cs b/WinFormsApp1/Direction.cs index 5275f08..371a417 100644 --- a/WinFormsApp1/Direction.cs +++ b/WinFormsApp1/Direction.cs @@ -6,6 +6,7 @@ namespace WinFormsApp1 { internal enum Direction { + None = 0, Up = 1, Down = 2, Left = 3, diff --git a/WinFormsApp1/DrawningObjectTraktor.cs b/WinFormsApp1/DrawningObjectTraktor.cs new file mode 100644 index 0000000..a7973ec --- /dev/null +++ b/WinFormsApp1/DrawningObjectTraktor.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsApp1 +{ + class DrawningObjectTractor : IDrawningObject + { + private TractorDraw _tractor = null; + + public DrawningObjectTractor(TractorDraw tractor) + { + _tractor = tractor; + } + + public float Step => _tractor?.Tractor?.Step ?? 0; + + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() + { + return _tractor?.GetCurrentPosition() ?? default; + } + + public void MoveObject(Direction direction) + { + _tractor?.MoveTransport(direction); + } + + public void SetObject(int x, int y, int width, int height) + { + _tractor.SetPosition(x, y, width, height); + } + + void IDrawningObject.DrawningObject(Graphics g) + { + _tractor.DrawEntity(g); + } + } +} diff --git a/WinFormsApp1/EntityTractor.cs b/WinFormsApp1/EntityTractor.cs index 088ce6a..f401a35 100644 --- a/WinFormsApp1/EntityTractor.cs +++ b/WinFormsApp1/EntityTractor.cs @@ -26,12 +26,7 @@ namespace WinFormsApp1 /// public float Step => Speed * 100 / Weight; /// - /// Инициализация полей объекта-класса автомобиля - /// - /// - /// - /// - /// + public EntityTractor(int speed, float weight, Color bodyColor) { Random rnd = new Random(); diff --git a/WinFormsApp1/FieldMap.cs b/WinFormsApp1/FieldMap.cs new file mode 100644 index 0000000..11cfef4 --- /dev/null +++ b/WinFormsApp1/FieldMap.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 WinFormsApp1 +{ + class FieldMap : AbstractMap + { + /// Цвет участка закрытого + private readonly Brush barrierColor = new SolidBrush(Color.Yellow); + /// Цвет участка открытого + private readonly Brush roadColor = new SolidBrush(Color.Green); + + 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)); + } + 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 < 25) + { + int x = _random.Next(0, 97); + int y = _random.Next(0, 97); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + _map[x + 2, y] = _barrier; + _map[x, y + 2] = _barrier; + counter++; + } + } + } + } +} diff --git a/WinFormsApp1/FormMap.Designer.cs b/WinFormsApp1/FormMap.Designer.cs new file mode 100644 index 0000000..0da36fd --- /dev/null +++ b/WinFormsApp1/FormMap.Designer.cs @@ -0,0 +1,210 @@ + +namespace WinFormsApp1 +{ + 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.pictureBoxTractor = new System.Windows.Forms.PictureBox(); + 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.buttonCreate = 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.buttonCreateModif = new System.Windows.Forms.Button(); + this.comboBoxSelectionMap = new System.Windows.Forms.ComboBox(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTractor)).BeginInit(); + this.statusStrip1.SuspendLayout(); + this.SuspendLayout(); + // + // pictureBoxTractor + // + this.pictureBoxTractor.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxTractor.Location = new System.Drawing.Point(0, 0); + this.pictureBoxTractor.Name = "pictureBoxTractor"; + this.pictureBoxTractor.Size = new System.Drawing.Size(800, 424); + this.pictureBoxTractor.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; + this.pictureBoxTractor.TabIndex = 0; + this.pictureBoxTractor.TabStop = false; + // + // statusStrip1 + // + this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripStatusLabelSpeed, + this.toolStripStatusLabelWeight, + this.toolStripStatusLabelBodyColor}); + this.statusStrip1.Location = new System.Drawing.Point(0, 424); + this.statusStrip1.Name = "statusStrip1"; + this.statusStrip1.Size = new System.Drawing.Size(800, 26); + this.statusStrip1.TabIndex = 1; + this.statusStrip1.Text = "statusStrip1"; + // + // toolStripStatusLabelSpeed + // + this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; + this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(73, 20); + this.toolStripStatusLabelSpeed.Text = "Скорость"; + // + // toolStripStatusLabelWeight + // + this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; + this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(32, 20); + this.toolStripStatusLabelWeight.Text = "вес"; + // + // toolStripStatusLabelBodyColor + // + this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; + this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(42, 20); + this.toolStripStatusLabelBodyColor.Text = "Цвет"; + this.toolStripStatusLabelBodyColor.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonCreate + // + this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreate.Location = new System.Drawing.Point(0, 378); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(94, 29); + this.buttonCreate.TabIndex = 2; + this.buttonCreate.Text = "создать"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.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::Tractors.Properties.Resources._2EdzyM4iEKw; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(682, 296); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 3; + 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::Tractors.Properties.Resources.Hhxt4dLqV5g; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(649, 332); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 4; + 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::Tractors.Properties.Resources.MbV2DYU_nPM; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(682, 332); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 5; + 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::Tractors.Properties.Resources.RkYIe2_6DuQ; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(718, 332); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 6; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonCreateModif + // + this.buttonCreateModif.Location = new System.Drawing.Point(111, 378); + this.buttonCreateModif.Name = "buttonCreateModif"; + this.buttonCreateModif.Size = new System.Drawing.Size(129, 27); + this.buttonCreateModif.TabIndex = 7; + this.buttonCreateModif.Text = "Модификация"; + this.buttonCreateModif.UseVisualStyleBackColor = true; + this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click); + // + // comboBoxSelectionMap + // + this.comboBoxSelectionMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelectionMap.FormattingEnabled = true; + this.comboBoxSelectionMap.Items.AddRange(new object[] { + "Простая карта", + "Поле"}); + this.comboBoxSelectionMap.Location = new System.Drawing.Point(0, 0); + this.comboBoxSelectionMap.Name = "comboBoxSelectionMap"; + this.comboBoxSelectionMap.Size = new System.Drawing.Size(194, 28); + this.comboBoxSelectionMap.TabIndex = 8; + this.comboBoxSelectionMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectionMap_SelectedIndexChanged); + // + // FormMap + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.comboBoxSelectionMap); + this.Controls.Add(this.buttonCreateModif); + this.Controls.Add(this.buttonRight); + this.Controls.Add(this.buttonDown); + this.Controls.Add(this.buttonLeft); + this.Controls.Add(this.buttonUp); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.pictureBoxTractor); + this.Controls.Add(this.statusStrip1); + this.Name = "FormMap"; + this.Text = "FormMap"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTractor)).EndInit(); + this.statusStrip1.ResumeLayout(false); + this.statusStrip1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + private System.Windows.Forms.PictureBox pictureBoxTractor; + 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.Button buttonCreate; + 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 buttonCreateModif; + private System.Windows.Forms.ComboBox comboBoxSelectionMap; + } +} \ No newline at end of file diff --git a/WinFormsApp1/FormMap.cs b/WinFormsApp1/FormMap.cs new file mode 100644 index 0000000..7f03745 --- /dev/null +++ b/WinFormsApp1/FormMap.cs @@ -0,0 +1,90 @@ +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 WinFormsApp1 +{ + public partial class FormMap : Form + { + private AbstractMap _abstractMap; + + public FormMap() + { + InitializeComponent(); + _abstractMap = new SimpleMap(); + } + + //Заполнение информации по объекту + private void SetData(TractorDraw tractor) + { + toolStripStatusLabelSpeed.Text = $"Скорость: {tractor.Tractor.Speed}"; + toolStripStatusLabelWeight.Text = $"Вес: {tractor.Tractor.Weight}"; + toolStripStatusLabelBodyColor.Text = $"Цвет: {tractor.Tractor.BodyColor.Name}"; + pictureBoxTractor.Image = _abstractMap.CreateMap(pictureBoxTractor.Width, pictureBoxTractor.Height, + new DrawningObjectTractor(tractor)); + } + + //Логика кнопки Создать + private void ButtonCreate_Click(object sender, EventArgs e) + { + Random random = new(); + var tractor = new TractorDraw(random.Next(100, 200), random.Next(2500, 5000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); + SetData(tractor); + } + + 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; + } + pictureBoxTractor.Image = _abstractMap?.MoveObject(dir); + } + + // Обработка нажатия кнопки "Модификация" + private void buttonCreateModif_Click(object sender, EventArgs e) + { + Random random = new Random(); + var _Tractor = new MultiTraktorDraw(random.Next(100, 200), random.Next(2500, 5000), + 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))); + SetData(_Tractor); + } + + private void comboBoxSelectionMap_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectionMap.Text) + { + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + case "Поле": + _abstractMap = new FieldMap(); + break; + } + + } + + + } +} diff --git a/WinFormsApp1/FormMap.resx b/WinFormsApp1/FormMap.resx new file mode 100644 index 0000000..5cb320f --- /dev/null +++ b/WinFormsApp1/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/WinFormsApp1/FormTractor.Designer.cs b/WinFormsApp1/FormTractor.Designer.cs index d891929..1615d46 100644 --- a/WinFormsApp1/FormTractor.Designer.cs +++ b/WinFormsApp1/FormTractor.Designer.cs @@ -39,6 +39,7 @@ namespace WinFormsApp1 this.buttonLeft = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); + this.buttonCreateModif = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTractor)).BeginInit(); this.statusStrip1.SuspendLayout(); this.SuspendLayout(); @@ -145,11 +146,22 @@ namespace WinFormsApp1 this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); // + // buttonCreateModif + // + this.buttonCreateModif.Location = new System.Drawing.Point(111, 378); + this.buttonCreateModif.Name = "buttonCreateModif"; + this.buttonCreateModif.Size = new System.Drawing.Size(129, 27); + this.buttonCreateModif.TabIndex = 7; + this.buttonCreateModif.Text = "Модификация"; + this.buttonCreateModif.UseVisualStyleBackColor = true; + this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click); + // // FormTractor // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonLeft); @@ -179,6 +191,7 @@ namespace WinFormsApp1 private System.Windows.Forms.Button buttonLeft; private System.Windows.Forms.Button buttonDown; private System.Windows.Forms.Button buttonRight; + private System.Windows.Forms.Button buttonCreateModif; } } diff --git a/WinFormsApp1/FormTractor.cs b/WinFormsApp1/FormTractor.cs index 16e9570..cd79669 100644 --- a/WinFormsApp1/FormTractor.cs +++ b/WinFormsApp1/FormTractor.cs @@ -27,15 +27,20 @@ namespace WinFormsApp1 pictureBoxTractor.Image = bmp; } - + private void SetData() + { + Random random = new(); + _Tractor.SetPosition(random.Next(10, 50), random.Next(10, 50), pictureBoxTractor.Width, pictureBoxTractor.Height); + toolStripStatusLabelSpeed.Text = $"Скорость: {_Tractor.Tractor.Speed}"; + toolStripStatusLabelWeight.Text = $"Вес: {_Tractor.Tractor.Weight}"; + toolStripStatusLabelBodyColor.Text = $"Цвет кузова: {_Tractor.Tractor.BodyColor.Name}"; + } + private void ButtonCreate_Click(object sender, EventArgs e) { Random random = new Random(); _Tractor = new TractorDraw(random.Next(100, 200), random.Next(2500, 5000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); - _Tractor.SetPosition(random.Next(10, 50), random.Next(10, 50), pictureBoxTractor.Width, pictureBoxTractor.Height); - toolStripStatusLabelSpeed.Text = $"Скорость: {_Tractor.Tractor.Speed}"; - toolStripStatusLabelWeight.Text = $"Вес: {_Tractor.Tractor.Weight}"; - toolStripStatusLabelBodyColor.Text = $"Цвет: {_Tractor.Tractor.BodyColor.Name}"; + SetData(); Draw(); } @@ -70,5 +75,19 @@ namespace WinFormsApp1 _Tractor?.ChangeBorders(pictureBoxTractor.Width, pictureBoxTractor.Height); Draw(); } + + + + + + private void buttonCreateModif_Click(object sender, EventArgs e) + { + Random random = new Random(); + _Tractor = new MultiTraktorDraw(random.Next(100, 200), random.Next(2500, 5000), + 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))); + SetData(); + Draw(); + } } } diff --git a/WinFormsApp1/IDrawningObject.cs b/WinFormsApp1/IDrawningObject.cs new file mode 100644 index 0000000..e257f3a --- /dev/null +++ b/WinFormsApp1/IDrawningObject.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsApp1 +{ + 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/WinFormsApp1/MapWithSetTraktorGeneric.cs b/WinFormsApp1/MapWithSetTraktorGeneric.cs new file mode 100644 index 0000000..9ebc163 --- /dev/null +++ b/WinFormsApp1/MapWithSetTraktorGeneric.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsApp1 +{ + internal class MapWithSetTraktorGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 180; + private readonly int _placeSizeHeight = 150; + private readonly SetTraktorGeneric _setTraktors; + private readonly U _map; + + public MapWithSetTraktorGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setTraktors = new SetTraktorGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static int operator +(MapWithSetTraktorGeneric map, T bus) + { + return map._setTraktors.Insert(bus); + } + + public static T operator -(MapWithSetTraktorGeneric map, int position) + { + return map._setTraktors.Remove(position); + } + + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawTraktors(gr); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setTraktors.Count; i++) + { + var bus = _setTraktors.Get(i); + if (bus != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, bus); + } + } + return new(_pictureWidth, _pictureHeight); + } + + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + + private void Shaking() + { + int j = _setTraktors.Count - 1; + for (int i = 0; i < _setTraktors.Count; i++) + { + if (_setTraktors.Get(i) == null) + { + for (; j > i; j--) + { + var bus = _setTraktors.Get(j); + if (bus != null) + { + _setTraktors.Insert(bus, i); + _setTraktors.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + Brush brush = new SolidBrush(Color.LightSlateGray); + g.FillRectangle(brush, 0, 0, _pictureWidth, _pictureHeight); + for (int i = 0; i <= _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j <= _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 10, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + 10); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + + private void DrawTraktors(Graphics g) + { + int widthEl = _pictureWidth / _placeSizeWidth; + int heightEl = _pictureHeight / _placeSizeHeight; + + int curWidth = 0; + int curHeight = 0; + + for (int i = _setTraktors.Count; i >= 0; i--) + { + _setTraktors.Get(i)?.SetObject( + _pictureWidth - _placeSizeWidth * curWidth - 20, + curHeight * _placeSizeHeight + 30, _pictureWidth, _pictureHeight); + _setTraktors.Get(i)?.DrawningObject(g); + + if (curWidth < widthEl) + curWidth++; + else + { + curWidth = 1; + curHeight++; + } + if (curHeight > heightEl) + { + return; + } + + } + } + } +} diff --git a/WinFormsApp1/MultiTraktor.cs b/WinFormsApp1/MultiTraktor.cs new file mode 100644 index 0000000..12f1caa --- /dev/null +++ b/WinFormsApp1/MultiTraktor.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Text; + +namespace WinFormsApp1 +{ + internal class MultiTraktor : EntityTractor + { + public Color DopColor { get; private set; } + /// Инициализация свойств + /// Скорость + /// Вес трактора + /// Цвет кузова + /// Дополнительный цвет + public MultiTraktor(int speed, float weight, Color bodyColor, Color dopColor) : base(speed, weight, bodyColor) + { + DopColor = dopColor; + } + } +} diff --git a/WinFormsApp1/MultiTraktorDraw.cs b/WinFormsApp1/MultiTraktorDraw.cs new file mode 100644 index 0000000..7e7c06d --- /dev/null +++ b/WinFormsApp1/MultiTraktorDraw.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Text; + +namespace WinFormsApp1 +{ + class MultiTraktorDraw : TractorDraw + { + /// Инициализация свойств + /// Скорость + /// Вес трактора + /// Цвет кузова + /// Дополнительный цвет + /// Ширина отрисовки автомобиля + /// Высота отрисовки автомобиля + public MultiTraktorDraw(int speed, float weight, Color bodyColor, Color dopColor) : base(speed, weight, bodyColor, 188, 100) + { + Tractor = new MultiTraktor(speed, weight, bodyColor, dopColor); + } + + public override void DrawEntity(Graphics g) + { + if (Tractor is not MultiTraktor multiTraktor) + { + return; + } + Pen pen_Black_1pxl = new Pen(Color.Black, 1); + Pen pen_Black_2pxl = new Pen(Color.Black, 2); + Brush brBlack = new SolidBrush(Color.Black); + Brush dopBrush = new SolidBrush(multiTraktor.DopColor); + + PointF point1; + PointF point2; + PointF point3; + PointF point4; + + g.DrawRectangle(pen_Black_1pxl, startPosX, startPosY + 8, 4, 35); + g.DrawRectangle(pen_Black_1pxl, startPosX + 33, startPosY + 34, 10, 15); + + point1 = new PointF(startPosX, startPosY + 8); + point2 = new PointF(startPosX + 33, startPosY + 41); + point3 = new PointF(startPosX + 33, startPosY + 34); + point4 = new PointF(startPosX + 7, startPosY + 8); + PointF[] curvePoints = + { + point1, + point2, + point3, + point4 + }; + g.FillPolygon(dopBrush, curvePoints); + g.DrawPolygon(pen_Black_1pxl, curvePoints); + + point1 = new PointF(startPosX + 6, startPosY + 8 + 15); + point2 = new PointF(startPosX + 6, startPosY + 8 + 35); + point3 = new PointF(startPosX + 26, startPosY + 8 + 35); + PointF[] curvePoints2 = +{ + point1, + point2, + point3 + }; + g.FillPolygon(dopBrush, curvePoints2); + g.DrawPolygon(pen_Black_1pxl, curvePoints2); + + startPosX += 43; + base.DrawEntity(g); + startPosX -= 43; + + point1 = new PointF(startPosX + 43 + 102, startPosY + 30); + point2 = new PointF(startPosX + 43 + 102, startPosY + 65); + point3 = new PointF(startPosX + 43 + 137, startPosY + 65); + + PointF[] curvePoints3 = + { + point1, + point2, + point3 + }; + g.FillPolygon(dopBrush, curvePoints3); + g.DrawPolygon(pen_Black_1pxl, curvePoints3); + } + } +} diff --git a/WinFormsApp1/Program.cs b/WinFormsApp1/Program.cs index ab6b2a6..d6c746a 100644 --- a/WinFormsApp1/Program.cs +++ b/WinFormsApp1/Program.cs @@ -17,7 +17,7 @@ namespace WinFormsApp1 Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new FormTractor()); + Application.Run(new FormMap()); } } } diff --git a/WinFormsApp1/SetTraktorGeneric.cs b/WinFormsApp1/SetTraktorGeneric.cs new file mode 100644 index 0000000..7b5093e --- /dev/null +++ b/WinFormsApp1/SetTraktorGeneric.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsApp1 +{ + internal class SetTraktorGeneric + where T : class + { + private readonly T[] _places; + public int Count => _places.Length; + private int TractorPlaces = 0; + + public SetTraktorGeneric(int count) + { + _places = new T[count]; + } + + public int Insert(T tractor) + { + return Insert(tractor, 0); + } + + public int Insert(T tractor, int position) + { + if (position < 0 || position >= _places.Length || TractorPlaces == _places.Length) + { + return -1; + } + TractorPlaces++; + while (_places[position] != null) + { + for (int i = _places.Length - 1; i > 0; --i) + { + if (_places[i] == null && _places[i - 1] != null) + { + _places[i] = _places[i - 1]; + _places[i - 1] = null; + } + } + } + _places[position] = tractor; + return position; + } + + public T Remove(int position) + { + if (position < 0 || position >= _places.Length) return null; + T savedTractor = _places[position]; + _places[position] = null; + return savedTractor; + } + + public T Get(int position) + { + if (position < 0 || position >= _places.Length) return null; + return _places[position]; + } + } +} diff --git a/WinFormsApp1/SimpleMap.cs b/WinFormsApp1/SimpleMap.cs new file mode 100644 index 0000000..c68c721 --- /dev/null +++ b/WinFormsApp1/SimpleMap.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsApp1 +{ + 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, i * (_size_x + 1), 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 < 33) + { + 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/WinFormsApp1/TractorDraw.cs b/WinFormsApp1/TractorDraw.cs index f94101e..635410f 100644 --- a/WinFormsApp1/TractorDraw.cs +++ b/WinFormsApp1/TractorDraw.cs @@ -11,17 +11,17 @@ namespace WinFormsApp1 { //Сущность - public EntityTractor Tractor { get; private set; } + public EntityTractor Tractor { get; protected set; } /// Левая координата отрисовки сущности - private float startPosX; + protected float startPosX; /// Верхняя кооридната отрисовки сущности - private float startPosY; + protected float startPosY; /// Ширина окна отрисовки private int? pictureWidth = null; /// Высота окна отрисовки private int? pictureHeight = null; /// Ширина отрисовки сущности - private readonly int entWidth = 130; + private readonly int entWidth = 115; /// Высота отрисовки сущности private readonly int entHeight = 100; @@ -30,6 +30,18 @@ namespace WinFormsApp1 Tractor = new EntityTractor(speed, weight, bodycolor); } + /// Инициализация свойств + /// Скорость + /// Вес автомобиля + /// Цвет кузова + /// Ширина отрисовки автомобиля + /// Высота отрисовки автомобиля + /// + protected TractorDraw(int speed, float weight, Color bodyColor, int trktrWidth, int trktrHeight) : this(speed, weight, bodyColor) + { + entWidth = trktrWidth; + entHeight = trktrHeight; + } //Установка позиции сущности public void SetPosition(int x, int y, int width, int height) { @@ -111,7 +123,7 @@ namespace WinFormsApp1 } //Отрисовка сущности - public void DrawEntity(Graphics g) + public virtual void DrawEntity(Graphics g) { if (startPosX < 0 || startPosY < 0 || !pictureHeight.HasValue || !pictureWidth.HasValue) { @@ -170,5 +182,9 @@ namespace WinFormsApp1 startPosY = pictureHeight.Value - entHeight; } } + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() + { + return (startPosX, startPosY, startPosX + entWidth, startPosY + entHeight); + } } } diff --git a/WinFormsApp1/Tractors.csproj b/WinFormsApp1/Tractors.csproj index ef04a4f..c0aed77 100644 --- a/WinFormsApp1/Tractors.csproj +++ b/WinFormsApp1/Tractors.csproj @@ -2,7 +2,7 @@ WinExe - netcoreapp3.1 + net5.0-windows true