Лабораторная работа №1

This commit is contained in:
Egor_Shtyrkin 2024-04-11 18:47:33 +04:00
parent f504666346
commit 9e82cbdd7f
5 changed files with 182 additions and 102 deletions

View File

@ -1,18 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAircraftCarrier;
namespace ProjectAircraftCarrier;
/// <summary>
/// Направление перемещения
/// </summary>
public enum DirectionType
{
/// <summary>
/// Вверх
/// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}

View File

@ -1,27 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAircraftCarrier;
namespace ProjectAircraftCarrier;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningAircraftCarrier
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityAircraftCarrier? EntityAircraftCarrier { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int? _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int? _pictureHeight;
/// <summary>
/// Левая координата прорисовки авианосца
/// </summary>
private int? _startPosX;
/// <summary>
/// Верхняя координата отрисовки авианосца
/// </summary>
private int? _startPosY;
/// <summary>
/// Ширина прорисовки авианосца
/// </summary>
private readonly int _drawningAircraftCarrierWidth = 150;
/// <summary>
/// Высота прорисовки авианосца
/// </summary>
private readonly int _drawningAircraftCarrierHeight = 65;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="aircraftDeck">Признак наличия взлётно-посадочной полосы</param>
/// <param name="controlRoom">Признак наличия рубки управления</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool aircraftDeck, bool controlRoom)
{
EntityAircraftCarrier = new EntityAircraftCarrier();
@ -32,6 +59,12 @@ public class DrawningAircraftCarrier
_startPosY = null;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
if (width < _drawningAircraftCarrierWidth || height < _drawningAircraftCarrierHeight)
@ -64,6 +97,11 @@ public class DrawningAircraftCarrier
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if(!_pictureHeight.HasValue || !_pictureWidth.HasValue)
@ -85,6 +123,11 @@ public class DrawningAircraftCarrier
_startPosY = y;
}
/// <summary>
/// Измнение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if(EntityAircraftCarrier == null || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
@ -94,24 +137,28 @@ public class DrawningAircraftCarrier
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityAircraftCarrier.Step > 0)
{
_startPosX -= (int)EntityAircraftCarrier.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityAircraftCarrier.Step > 0)
{
_startPosY -= (int)EntityAircraftCarrier.Step;
}
return true;
//вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningAircraftCarrierWidth + EntityAircraftCarrier.Step < _pictureWidth)
{
_startPosX += (int)EntityAircraftCarrier.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningAircraftCarrierHeight + EntityAircraftCarrier.Step < _pictureHeight)
{
@ -123,6 +170,10 @@ public class DrawningAircraftCarrier
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityAircraftCarrier == null || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
@ -198,7 +249,5 @@ public class DrawningAircraftCarrier
g.DrawPolygon(pen, DeckPoint);
g.DrawLine(pen, _startPosX.Value + 10, _startPosY.Value + 45, _startPosX.Value + 55, _startPosY.Value + 20);
}
}
}

View File

@ -1,60 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAircraftCarrier;
namespace ProjectAircraftCarrier;
/// <summary>
///
/// Класс-сущность "Авианосец"
/// </summary>
public class EntityAircraftCarrier
{
/// <summary>
///
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
///
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
///
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
///
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
///
/// Признак (опция) наличие взлётно-посадочной полосы
/// </summary>
public bool AircraftDeck { get; private set; }
/// <summary>
///
/// Признак (опция) наличие рубки управления
/// </summary>
public bool ControlRoom { get; private set; }
/// <summary>
///
/// Шаг перемещения авианосца
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
///
/// Инициализация полей объекта-класса авианосца
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <param name="additionalColor"></param>
/// <param name="aircraftDeck"></param>
/// <param name="controlRoom"></param>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес авианосца</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="aircraftDeck">Признак наличия взлётно-посадочной полосы</param>
/// <param name="controlRoom">Признак наличия рубки управления</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool aircraftDeck, bool controlRoom)
{
Speed = speed;

View File

@ -41,8 +41,9 @@
//
pictureBoxAircraftCarrier.Dock = DockStyle.Fill;
pictureBoxAircraftCarrier.Location = new Point(0, 0);
pictureBoxAircraftCarrier.Margin = new Padding(3, 4, 3, 4);
pictureBoxAircraftCarrier.Name = "pictureBoxAircraftCarrier";
pictureBoxAircraftCarrier.Size = new Size(803, 471);
pictureBoxAircraftCarrier.Size = new Size(918, 628);
pictureBoxAircraftCarrier.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxAircraftCarrier.TabIndex = 0;
pictureBoxAircraftCarrier.TabStop = false;
@ -50,9 +51,10 @@
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 436);
buttonCreate.Location = new Point(14, 581);
buttonCreate.Margin = new Padding(3, 4, 3, 4);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23);
buttonCreate.Size = new Size(86, 31);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "создать";
buttonCreate.UseVisualStyleBackColor = true;
@ -63,7 +65,8 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(761, 429);
buttonRight.Location = new Point(876, 585);
buttonRight.Margin = new Padding(3, 4, 3, 4);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 2;
@ -75,7 +78,8 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(725, 393);
buttonUp.Location = new Point(840, 547);
buttonUp.Margin = new Padding(3, 4, 3, 4);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 3;
@ -87,7 +91,8 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(725, 429);
buttonDown.Location = new Point(840, 585);
buttonDown.Margin = new Padding(3, 4, 3, 4);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 4;
@ -99,7 +104,8 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(689, 429);
buttonLeft.Location = new Point(804, 585);
buttonLeft.Margin = new Padding(3, 4, 3, 4);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 5;
@ -108,15 +114,16 @@
//
// FormAircraftCarrier
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(803, 471);
ClientSize = new Size(918, 628);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonCreate);
Controls.Add(pictureBoxAircraftCarrier);
Margin = new Padding(3, 4, 3, 4);
Name = "FormAircraftCarrier";
StartPosition = FormStartPosition.CenterScreen;
Text = "Авианосец";

View File

@ -8,72 +8,93 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectAircraftCarrier
namespace ProjectAircraftCarrier;
/// <summary>
/// Форма работы с объектом "Авианосец"
/// </summary>
public partial class FormAircraftCarrier : Form
{
public partial class FormAircraftCarrier : Form
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningAircraftCarrier? _drawningAircraftCarrier;
/// <summary>
/// Конструктор формы
/// </summary>
public FormAircraftCarrier()
{
private DrawningAircraftCarrier? _drawningAircraftCarrier;
InitializeComponent();
}
public FormAircraftCarrier()
/// <summary>
/// Метод прорисовки авианосца
/// </summary>
private void Draw()
{
if (_drawningAircraftCarrier == null)
{
InitializeComponent();
return;
}
private void Draw()
{
if (_drawningAircraftCarrier == null)
{
return;
}
Bitmap bmp = new(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAircraftCarrier.DrawTransport(gr);
pictureBoxAircraftCarrier.Image = bmp;
}
Bitmap bmp = new(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAircraftCarrier.DrawTransport(gr);
pictureBoxAircraftCarrier.Image = bmp;
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAircraftCarrier = new DrawningAircraftCarrier();
_drawningAircraftCarrier.Init(random.Next(100, 300), random.Next(1000, 3000),
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)));
_drawningAircraftCarrier.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
_drawningAircraftCarrier.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningAircraftCarrier == null)
{
return;
}
private void buttonCreate_Click(object sender, EventArgs e)
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Random random = new();
_drawningAircraftCarrier = new DrawningAircraftCarrier();
_drawningAircraftCarrier.Init(random.Next(100, 300), random.Next(1000, 3000),
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)));
_drawningAircraftCarrier.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
_drawningAircraftCarrier.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningAircraftCarrier == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}
}