From 9805789853236eae56eb899e88f876af75448634 Mon Sep 17 00:00:00 2001 From: devil_1nc Date: Tue, 27 Sep 2022 10:37:49 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20FormMap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectPlane/ProjectPlane/AbstractMap.cs | 84 ++++++++ ProjectPlane/ProjectPlane/FormMap.Designer.cs | 197 ++++++++++++++++++ ProjectPlane/ProjectPlane/FormMap.cs | 88 ++++++++ ProjectPlane/ProjectPlane/FormMap.resx | 63 ++++++ ProjectPlane/ProjectPlane/SimpleMap.cs | 53 +++++ 5 files changed, 485 insertions(+) create mode 100644 ProjectPlane/ProjectPlane/AbstractMap.cs create mode 100644 ProjectPlane/ProjectPlane/FormMap.Designer.cs create mode 100644 ProjectPlane/ProjectPlane/FormMap.cs create mode 100644 ProjectPlane/ProjectPlane/FormMap.resx create mode 100644 ProjectPlane/ProjectPlane/SimpleMap.cs diff --git a/ProjectPlane/ProjectPlane/AbstractMap.cs b/ProjectPlane/ProjectPlane/AbstractMap.cs new file mode 100644 index 0000000..9d4b84e --- /dev/null +++ b/ProjectPlane/ProjectPlane/AbstractMap.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectPlane +{ + internal abstract class AbstractMap + { + private IDrawingObject _drawingObject = 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, IDrawingObject drawingObject) + { + _width = width; + _height = height; + _drawingObject = drawingObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + public Bitmap MoveObject(Direction direction) + { + // TODO проверка, что объект может переместится в требуемом направлении + if (true) + { + _drawingObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + private bool SetObjectOnMap() + { + if (_drawingObject == null || _map == null) + { + return false; + } + int x = _random.Next(0, 10); + int y = _random.Next(0, 10); + _drawingObject.SetObject(x, y, _width, _height); + // TODO првоерка, что объект не "накладывается" на закрытые участки + return true; + } + private Bitmap DrawMapWithObject() + { + Bitmap bmp = new(_width, _height); + if (_drawingObject == 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); + } + } + } + _drawingObject.DrawingObject(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/ProjectPlane/ProjectPlane/FormMap.Designer.cs b/ProjectPlane/ProjectPlane/FormMap.Designer.cs new file mode 100644 index 0000000..77e026f --- /dev/null +++ b/ProjectPlane/ProjectPlane/FormMap.Designer.cs @@ -0,0 +1,197 @@ +namespace ProjectPlane +{ + 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.buttonUp = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.pictureBoxPlane = 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.buttonCreateModif = new System.Windows.Forms.Button(); + this.comboMapSelector = new System.Windows.Forms.ComboBox(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxPlane)).BeginInit(); + this.statusStrip1.SuspendLayout(); + this.SuspendLayout(); + // + // buttonUp + // + this.buttonUp.BackgroundImage = global::ProjectPlane.Properties.Resources.up; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(686, 323); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(48, 47); + this.buttonUp.TabIndex = 8; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.BackgroundImage = global::ProjectPlane.Properties.Resources.right; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(740, 372); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(48, 47); + this.buttonRight.TabIndex = 7; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonDown + // + this.buttonDown.BackgroundImage = global::ProjectPlane.Properties.Resources.down; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(686, 372); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(48, 47); + this.buttonDown.TabIndex = 6; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonLeft + // + this.buttonLeft.BackgroundImage = global::ProjectPlane.Properties.Resources.left; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(632, 372); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(48, 47); + this.buttonLeft.TabIndex = 5; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonCreate + // + this.buttonCreate.BackColor = System.Drawing.SystemColors.ControlLightLight; + this.buttonCreate.Location = new System.Drawing.Point(12, 376); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(120, 47); + this.buttonCreate.TabIndex = 4; + this.buttonCreate.Text = "New Plane"; + this.buttonCreate.UseVisualStyleBackColor = false; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // pictureBoxPlane + // + this.pictureBoxPlane.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxPlane.Location = new System.Drawing.Point(0, 0); + this.pictureBoxPlane.Name = "pictureBoxPlane"; + this.pictureBoxPlane.Size = new System.Drawing.Size(800, 450); + this.pictureBoxPlane.TabIndex = 5; + this.pictureBoxPlane.TabStop = false; + // + // 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 = 9; + this.statusStrip1.Text = "statusStrip1"; + this.statusStrip1.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.statusStrip1_ItemClicked); + // + // toolStripStatusLabelSpeed + // + this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; + this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(39, 17); + this.toolStripStatusLabelSpeed.Text = "Speed"; + // + // toolStripStatusLabelWeight + // + this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; + this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(45, 17); + this.toolStripStatusLabelWeight.Text = "Weight"; + // + // toolStripStatusLabelBodyColor + // + this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; + this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(36, 17); + this.toolStripStatusLabelBodyColor.Text = "Color"; + // + // buttonCreateModif + // + this.buttonCreateModif.BackColor = System.Drawing.SystemColors.ControlLight; + this.buttonCreateModif.Location = new System.Drawing.Point(138, 376); + this.buttonCreateModif.Name = "buttonCreateModif"; + this.buttonCreateModif.Size = new System.Drawing.Size(115, 47); + this.buttonCreateModif.TabIndex = 10; + this.buttonCreateModif.Text = "Modificate"; + this.buttonCreateModif.UseVisualStyleBackColor = false; + this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click); + // + // comboMapSelector + // + this.comboMapSelector.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboMapSelector.FormattingEnabled = true; + this.comboMapSelector.Location = new System.Drawing.Point(12, 12); + this.comboMapSelector.Name = "comboMapSelector"; + this.comboMapSelector.Size = new System.Drawing.Size(121, 23); + this.comboMapSelector.TabIndex = 11; + // + // 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.comboMapSelector); + this.Controls.Add(this.buttonCreateModif); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.buttonLeft); + this.Controls.Add(this.buttonDown); + this.Controls.Add(this.buttonRight); + this.Controls.Add(this.buttonUp); + this.Controls.Add(this.statusStrip1); + this.Controls.Add(this.pictureBoxPlane); + this.Name = "FormMap"; + this.Text = "FormMap"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxPlane)).EndInit(); + this.statusStrip1.ResumeLayout(false); + this.statusStrip1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + private Button buttonUp; + private Button buttonRight; + private Button buttonDown; + private Button buttonLeft; + private Button buttonCreate; + private PictureBox pictureBoxPlane; + private StatusStrip statusStrip1; + private ToolStripStatusLabel toolStripStatusLabelSpeed; + private ToolStripStatusLabel toolStripStatusLabelWeight; + private ToolStripStatusLabel toolStripStatusLabelBodyColor; + private Button buttonCreateModif; + private ComboBox comboMapSelector; + } +} \ No newline at end of file diff --git a/ProjectPlane/ProjectPlane/FormMap.cs b/ProjectPlane/ProjectPlane/FormMap.cs new file mode 100644 index 0000000..680c02b --- /dev/null +++ b/ProjectPlane/ProjectPlane/FormMap.cs @@ -0,0 +1,88 @@ +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 ProjectPlane +{ + public partial class FormMap : Form + { + private AbstractMap _abstractMap; + public FormMap() + { + InitializeComponent(); + } + + private void SetData() + { + toolStripStatusLabelSpeed.Text = $"Скорость: {plane.Plane.Speed}"; + toolStripStatusLabelWeight.Text = $"Вес: {plane.Plane.Weight}"; + toolStripStatusLabelBodyColor.Text = $"Цвет: {plane.Plane.BodyColor.Name}"; + pictureBoxPlane.Image = _abstractMap.CreateMap(pictureBoxPlane.Width, pictureBoxPlane.Height, + new DrawingObject(plane)); + } + + /// + /// "Обработка нажатия на кнопки движения" + /// + /// + /// + 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; + } + pictureBoxPlane.Image = _abstractMap?.MoveObject(dir); + } + private void buttonCreate_Click(object sender, EventArgs e) + { + Random rnd = new(); + var plane = new DrawingPlane(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + SetData(plane); + + } + private void PictureBoxPlane_Resize(object sender, EventArgs e) + { + _plane?.ChangeBorders(pictureBoxPlane.Width, pictureBoxPlane.Height); + Draw(); + } + /// + /// Обработка нажатия кнопки "Модификация" + /// + /// + /// + private void buttonCreateModif_Click(object sender, EventArgs e) + { + Random rnd = new(); + _plane = new DrawingWarPlane(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)), Convert.ToBoolean(rnd.Next(0, 2))); + SetData(); + Draw(); + } + + + } +} +} diff --git a/ProjectPlane/ProjectPlane/FormMap.resx b/ProjectPlane/ProjectPlane/FormMap.resx new file mode 100644 index 0000000..5cb320f --- /dev/null +++ b/ProjectPlane/ProjectPlane/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/ProjectPlane/ProjectPlane/SimpleMap.cs b/ProjectPlane/ProjectPlane/SimpleMap.cs new file mode 100644 index 0000000..8752b0b --- /dev/null +++ b/ProjectPlane/ProjectPlane/SimpleMap.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectPlane +{ + 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++; + } + } + } + } +}