From 1333c85ae3db2321f4e7fe4ad62eba4ca8a6c51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D1=81=D0=BB=D0=B0=D0=B2=20?= =?UTF-8?q?=D0=97=D0=B0=D1=85=D0=B0=D1=80=D0=BE=D0=B2?= Date: Tue, 27 Sep 2022 00:07:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=81=D1=82=D0=B0=D0=BB=D0=BE=D1=81?= =?UTF-8?q?=D1=8C=20=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D1=83(TODO)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Traktor/Traktor/AbstractMap.cs | 84 ++++++++++ Traktor/Traktor/Direction.cs | 1 + Traktor/Traktor/DrawField.Designer.cs | 2 +- Traktor/Traktor/FormMap.Designer.cs | 216 ++++++++++++++++++++++++++ Traktor/Traktor/FormMap.cs | 85 ++++++++++ Traktor/Traktor/FormMap.resx | 63 ++++++++ Traktor/Traktor/Program.cs | 2 +- Traktor/Traktor/SimpleMap.cs | 49 ++++++ 8 files changed, 500 insertions(+), 2 deletions(-) create mode 100644 Traktor/Traktor/AbstractMap.cs create mode 100644 Traktor/Traktor/FormMap.Designer.cs create mode 100644 Traktor/Traktor/FormMap.cs create mode 100644 Traktor/Traktor/FormMap.resx create mode 100644 Traktor/Traktor/SimpleMap.cs diff --git a/Traktor/Traktor/AbstractMap.cs b/Traktor/Traktor/AbstractMap.cs new file mode 100644 index 0000000..d84ab26 --- /dev/null +++ b/Traktor/Traktor/AbstractMap.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Traktor +{ + 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) + { + // TODO проверка, что объект может переместится в требуемом направлении + 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); + // TODO првоерка, что объект не "накладывается" на закрытые участки + 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/Traktor/Traktor/Direction.cs b/Traktor/Traktor/Direction.cs index a764490..75984bf 100644 --- a/Traktor/Traktor/Direction.cs +++ b/Traktor/Traktor/Direction.cs @@ -3,6 +3,7 @@ //Направление перемещения internal enum Direction { + None = 0, Up = 1, Down = 2, Left = 3, diff --git a/Traktor/Traktor/DrawField.Designer.cs b/Traktor/Traktor/DrawField.Designer.cs index 94499b5..e38b790 100644 --- a/Traktor/Traktor/DrawField.Designer.cs +++ b/Traktor/Traktor/DrawField.Designer.cs @@ -152,6 +152,7 @@ // // buttonCreateModif // + this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.buttonCreateModif.Location = new System.Drawing.Point(113, 272); this.buttonCreateModif.Name = "buttonCreateModif"; this.buttonCreateModif.Size = new System.Drawing.Size(110, 22); @@ -182,7 +183,6 @@ this.statusStrip.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); - } #endregion diff --git a/Traktor/Traktor/FormMap.Designer.cs b/Traktor/Traktor/FormMap.Designer.cs new file mode 100644 index 0000000..ba83c46 --- /dev/null +++ b/Traktor/Traktor/FormMap.Designer.cs @@ -0,0 +1,216 @@ +namespace Traktor +{ + 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.pictureBoxTraktor = new System.Windows.Forms.PictureBox(); + this.statusStrip = 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.buttonLEFT = new System.Windows.Forms.Button(); + this.buttonRIGHT = new System.Windows.Forms.Button(); + this.buttonDOWN = new System.Windows.Forms.Button(); + this.buttonUP = new System.Windows.Forms.Button(); + this.buttonCreateModif = new System.Windows.Forms.Button(); + this.comboBoxSelector = new System.Windows.Forms.ComboBox(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTraktor)).BeginInit(); + this.statusStrip.SuspendLayout(); + this.SuspendLayout(); + // + // pictureBoxTraktor + // + this.pictureBoxTraktor.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxTraktor.Location = new System.Drawing.Point(0, 0); + this.pictureBoxTraktor.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.pictureBoxTraktor.Name = "pictureBoxTraktor"; + this.pictureBoxTraktor.Size = new System.Drawing.Size(700, 316); + this.pictureBoxTraktor.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; + this.pictureBoxTraktor.TabIndex = 0; + this.pictureBoxTraktor.TabStop = false; + // + // statusStrip + // + this.statusStrip.ImageScalingSize = new System.Drawing.Size(20, 20); + this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripStatusLabelSpeed, + this.toolStripStatusLabelWeight, + this.toolStripStatusLabelBodyColor}); + this.statusStrip.Location = new System.Drawing.Point(0, 316); + this.statusStrip.Name = "statusStrip"; + this.statusStrip.Padding = new System.Windows.Forms.Padding(1, 0, 12, 0); + this.statusStrip.Size = new System.Drawing.Size(700, 22); + this.statusStrip.TabIndex = 1; + // + // toolStripStatusLabelSpeed + // + this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; + this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(65, 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(75, 17); + this.toolStripStatusLabelBodyColor.Text = "Цвет кузова:"; + // + // 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(25, 272); + this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(82, 22); + this.buttonCreate.TabIndex = 2; + this.buttonCreate.Text = "Создать"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); + // + // buttonLEFT + // + this.buttonLEFT.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLEFT.BackgroundImage = global::Traktor.Properties.Resources.arrowLeft; + this.buttonLEFT.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLEFT.Location = new System.Drawing.Point(589, 271); + this.buttonLEFT.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonLEFT.Name = "buttonLEFT"; + this.buttonLEFT.Size = new System.Drawing.Size(26, 22); + this.buttonLEFT.TabIndex = 3; + this.buttonLEFT.UseVisualStyleBackColor = true; + this.buttonLEFT.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::Traktor.Properties.Resources.arrowRight; + this.buttonRIGHT.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRIGHT.Location = new System.Drawing.Point(652, 271); + this.buttonRIGHT.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonRIGHT.Name = "buttonRIGHT"; + this.buttonRIGHT.Size = new System.Drawing.Size(26, 22); + this.buttonRIGHT.TabIndex = 4; + this.buttonRIGHT.UseVisualStyleBackColor = true; + this.buttonRIGHT.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::Traktor.Properties.Resources.arrowDown; + this.buttonDOWN.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDOWN.Location = new System.Drawing.Point(620, 284); + this.buttonDOWN.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonDOWN.Name = "buttonDOWN"; + this.buttonDOWN.Size = new System.Drawing.Size(26, 22); + this.buttonDOWN.TabIndex = 5; + this.buttonDOWN.UseVisualStyleBackColor = true; + this.buttonDOWN.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonUP + // + this.buttonUP.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUP.BackgroundImage = global::Traktor.Properties.Resources.arrowUp; + this.buttonUP.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUP.Location = new System.Drawing.Point(620, 257); + this.buttonUP.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonUP.Name = "buttonUP"; + this.buttonUP.Size = new System.Drawing.Size(26, 22); + this.buttonUP.TabIndex = 6; + this.buttonUP.UseVisualStyleBackColor = true; + this.buttonUP.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonCreateModif + // + this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreateModif.Location = new System.Drawing.Point(113, 272); + this.buttonCreateModif.Name = "buttonCreateModif"; + this.buttonCreateModif.Size = new System.Drawing.Size(110, 22); + this.buttonCreateModif.TabIndex = 7; + this.buttonCreateModif.Text = "Модификация"; + this.buttonCreateModif.UseVisualStyleBackColor = true; + this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click); + // + // comboBoxSelector + // + this.comboBoxSelector.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelector.FormattingEnabled = true; + this.comboBoxSelector.Items.AddRange(new object[] { + "Простая карта"}); + this.comboBoxSelector.Location = new System.Drawing.Point(12, 12); + this.comboBoxSelector.Name = "comboBoxSelector"; + this.comboBoxSelector.Size = new System.Drawing.Size(121, 23); + this.comboBoxSelector.TabIndex = 8; + this.comboBoxSelector.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelector_SelectedIndexChanged); + // + // FormMap + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(700, 338); + this.Controls.Add(this.comboBoxSelector); + this.Controls.Add(this.buttonCreateModif); + this.Controls.Add(this.buttonUP); + this.Controls.Add(this.buttonDOWN); + this.Controls.Add(this.buttonRIGHT); + this.Controls.Add(this.buttonLEFT); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.pictureBoxTraktor); + this.Controls.Add(this.statusStrip); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormMap"; + this.Text = "Трактор"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTraktor)).EndInit(); + this.statusStrip.ResumeLayout(false); + this.statusStrip.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private PictureBox pictureBoxTraktor; + private StatusStrip statusStrip; + private ToolStripStatusLabel toolStripStatusLabelSpeed; + private ToolStripStatusLabel toolStripStatusLabelWeight; + private ToolStripStatusLabel toolStripStatusLabelBodyColor; + private Button buttonCreate; + private Button buttonLEFT; + private Button buttonRIGHT; + private Button buttonDOWN; + private Button buttonUP; + private Button buttonCreateModif; + private ComboBox comboBoxSelector; + } +} \ No newline at end of file diff --git a/Traktor/Traktor/FormMap.cs b/Traktor/Traktor/FormMap.cs new file mode 100644 index 0000000..86ed0c1 --- /dev/null +++ b/Traktor/Traktor/FormMap.cs @@ -0,0 +1,85 @@ +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 Traktor +{ + public partial class FormMap : Form + { + private AbstractMap _abstractMap; + + public FormMap() + { + InitializeComponent(); + _abstractMap = new SimpleMap(); + } + + //Заполнение информации по объекту + private void SetData(TraktorDraw traktor) + { + toolStripStatusLabelSpeed.Text = $"Скорость: {traktor.Traktor.Speed}"; + toolStripStatusLabelWeight.Text = $"Вес: {traktor.Traktor.Weight}"; + toolStripStatusLabelBodyColor.Text = $"Цвет: {traktor.Traktor.BodyColor.Name}"; + pictureBoxTraktor.Image = _abstractMap.CreateMap(pictureBoxTraktor.Width, pictureBoxTraktor.Height, + new DrawningObjectTraktor(traktor)); + } + + //Логика кнопки Создать + private void ButtonCreate_Click(object sender, EventArgs e) + { + Random random = new(); + var car = new TraktorDraw(random.Next(100, 200), random.Next(2500, 5000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.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; + } + pictureBoxTraktor.Image = _abstractMap?.MoveObject(dir); + } + + // Обработка нажатия кнопки "Модификация" + private void buttonCreateModif_Click(object sender, EventArgs e) + { + Random random = new Random(); + var _Traktor = 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)), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + SetData(_Traktor); + } + + private void ComboBoxSelector_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelector.Text) + { + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + } + } + } +} diff --git a/Traktor/Traktor/FormMap.resx b/Traktor/Traktor/FormMap.resx new file mode 100644 index 0000000..2c0949d --- /dev/null +++ b/Traktor/Traktor/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/Traktor/Traktor/Program.cs b/Traktor/Traktor/Program.cs index c7b1b20..712b393 100644 --- a/Traktor/Traktor/Program.cs +++ b/Traktor/Traktor/Program.cs @@ -11,7 +11,7 @@ namespace Traktor // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new DrawField()); + Application.Run(new FormMap()); } } } \ No newline at end of file diff --git a/Traktor/Traktor/SimpleMap.cs b/Traktor/Traktor/SimpleMap.cs new file mode 100644 index 0000000..9936339 --- /dev/null +++ b/Traktor/Traktor/SimpleMap.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Traktor +{ + 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, 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 < 50) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +}