Compare commits

...

2 Commits

Author SHA1 Message Date
Артём Ерастов
ade3f04903 исправления 2024-10-01 21:49:49 +04:00
Артём Ерастов
a4c26fed31 изменения_1.1 2024-10-01 21:16:10 +04:00
5 changed files with 246 additions and 225 deletions

View File

@ -4,12 +4,9 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lab0;
namespace Teplohod;
public enum DirectionType
{
Up=1,
Down=2,
Left=3,
Right=4,
Up = 1, Down = 2, Left = 3, Right = 4,
}

View File

@ -3,50 +3,57 @@ using System.Drawing;
namespace Teplohod;
public class DrawingTeplohod
public class DrawningTeplohod
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTeplohod? EntityTeplohod { get; set; }
/// Класс-сущность
/// </summary>
public EntityTeplohod? EntityTeplohod { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int? _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int? _pictureHeight;
/// <summary>
/// Левая координата прорисовки теплохода
/// </summary>
private int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки теплохода
/// Левая координата прорисовки автомобиля
/// </summary>
private int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки автомобиля
/// </summary>
private int? _startPosY;
/// <summary>
/// Ширина прорисовки теплохода
/// Ширина прорисовки автомобиля
/// </summary>
private readonly int _drawningTeplohodWidth = 150;
private readonly int _drawningTeplohodWidth = 110;
/// <summary>
/// Высота прорисовки теплохода
/// Высота прорисовки автомобиля
/// </summary>
private readonly int _drawningTeplohodHeight = 88;
private readonly int _drawningTeplohodHeight = 60;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodycolor">Основной цвет</param>
/// <param name="additionalcolor">Дополнительный цвет</param>
/// <param name="pipes">Признак наличия труб</param>
/// <param name="fueltank">Признак наличия топливных баков</param>
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, bool pipes, bool fueltank)
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="pipes">Признак наличия обвеса</param>
/// <param name="tank">Признак наличия антикрыла</param>
/// <param name="firstDack">Признак наличия гоночной полосы</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pipes, bool tank, bool firstDack)
{
EntityTeplohod = new EntityTeplohod();
EntityTeplohod.Init(speed, weight, bodycolor, additionalcolor, pipes, fueltank);
EntityTeplohod.Init(speed, weight, bodyColor, additionalColor, pipes, tank, firstDack);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
@ -61,33 +68,24 @@ public class DrawingTeplohod
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
if (EntityTeplohod == null)
if (_drawningTeplohodWidth <= width && _drawningTeplohodHeight <= height)
{
return false;
}
// Проверяем, что объект не выходит за пределы формы
if (width >= _drawningTeplohodWidth && height >= _drawningTeplohodHeight)
{
_pictureWidth = width;
_pictureHeight = height;
// Если уже заданы координаты прорисовки, то корректируем их, чтобы объект оставался видимым
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosY.HasValue)
{
if (_startPosX + _drawningTeplohodWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningTeplohodWidth;
}
if (_startPosY + _drawningTeplohodHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningTeplohodHeight;
}
}
return true;
}
return true;
}
return false;
}
@ -102,26 +100,11 @@ public class DrawingTeplohod
{
return;
}
if (x < 0) x = 0;
else if (x + _drawningTeplohodWidth > _pictureWidth) x = _pictureWidth.Value - _drawningTeplohodWidth;
// Проверяем, выходит ли объект за границы формы
if (x < 0)
{
x = 0;
}
else if (x + _drawningTeplohodWidth > _pictureWidth)
{
x = _pictureWidth.Value - _drawningTeplohodWidth;
}
if (y < 0)
{
y = 0;
}
else if (y + _drawningTeplohodHeight > _pictureHeight)
{
y = _pictureHeight.Value - _drawningTeplohodHeight;
}
if (y < 0) y = 0;
else if (y + _drawningTeplohodHeight > _pictureHeight) y = _pictureHeight.Value - _drawningTeplohodHeight;
_startPosX = x;
_startPosY = y;
}
@ -137,6 +120,7 @@ public class DrawingTeplohod
{
return false;
}
switch (direction)
{
//влево
@ -155,75 +139,73 @@ public class DrawingTeplohod
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntityTeplohod.Step + _drawningTeplohodWidth < _pictureWidth)
{
if (_startPosX.Value + _drawningTeplohodWidth + EntityTeplohod.Step < _pictureWidth)
_startPosX += (int)EntityTeplohod.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityTeplohod.Step + _drawningTeplohodHeight < _pictureHeight)
{
if (_startPosY.Value + _drawningTeplohodHeight + EntityTeplohod.Step < _pictureHeight)
_startPosY += (int)EntityTeplohod.Step;
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityTeplohod == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
if (EntityTeplohod == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityTeplohod.AdditionalColor);
Brush bodybrush = new SolidBrush(EntityTeplohod.BodyColor);
Brush bodybrush2 = new SolidBrush(EntityTeplohod.AdditionalColor);
//корпус
g.DrawRectangle(pen, new Rectangle(_startPosX.Value + 30, _startPosY.Value + 40, 90, 10));
g.FillRectangle(bodybrush, _startPosX.Value + 30, _startPosY.Value + 40, 90, 10);
g.DrawPolygon(pen, new[]{
new Point(_startPosX.Value, _startPosY.Value + 50),
new Point(_startPosX.Value + 150, _startPosY.Value + 50),
new Point(_startPosX.Value + 130, _startPosY.Value + 80),
new Point(_startPosX.Value + 20, _startPosY.Value + 80)
});
g.FillPolygon(bodybrush, new[]{
new Point(_startPosX.Value, _startPosY.Value + 50),
new Point(_startPosX.Value + 150, _startPosY.Value + 50),
new Point(_startPosX.Value + 130, _startPosY.Value + 80),
new Point(_startPosX.Value + 20, _startPosY.Value + 80)
});
g.DrawLine(pen, _startPosX.Value, _startPosY.Value + 50, _startPosX.Value + 150, _startPosY.Value + 50);
//трубы
// Трубы
if (EntityTeplohod.Pipes)
{
g.DrawRectangle(pen, new Rectangle(_startPosX.Value + 5, _startPosY.Value, 15, 50));
g.FillRectangle(additionalBrush, _startPosX.Value + 5, _startPosY.Value, 15, 50);
g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value, 10, 20);
g.FillRectangle(bodybrush2, _startPosX.Value + 30, _startPosY.Value, 10, 20);
}
//топливный бак
if (EntityTeplohod.FuelTank)
//границы Теплохода
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value, 30, 20);
Point point1 = new Point(_startPosX.Value, _startPosY.Value + 20);
Point point2 = new Point(_startPosX.Value + 150, _startPosY.Value + 20);
Point point3 = new Point(_startPosX.Value + 130, _startPosY.Value + 45);
Point point4 = new Point(_startPosX.Value + 40, _startPosY.Value + 45);
Point point5 = new Point(_startPosX.Value, _startPosY.Value + 20);
Point[] pointsTeplohod = { point1, point2, point3, point4, point5 };
g.DrawPolygon(pen, pointsTeplohod);
g.FillRectangle(bodybrush, _startPosX.Value + 100, _startPosY.Value, 30, 20);
g.FillPolygon(bodybrush2, pointsTeplohod);
if (EntityTeplohod.FirstDack)
{
Brush brRed = new SolidBrush(Color.Red);
g.DrawRectangle(pen, new Rectangle(_startPosX.Value + 35, _startPosY.Value + 5, 80, 30));
g.FillRectangle(brRed, _startPosX.Value + 35, _startPosY.Value + 5, 80, 30);
Brush bodybrush3 = new SolidBrush(Color.Yellow);
g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 5, 48, 15);
g.FillRectangle(bodybrush3, _startPosX.Value + 51, _startPosY.Value + 5, 47, 15);
}
// топливный бак
if (EntityTeplohod.Tank)
{
Brush bodybrush4 = new SolidBrush(Color.Red);
g.DrawRectangle(pen, _startPosX.Value + 131, _startPosY.Value + 5, 15, 15);
g.FillRectangle(bodybrush4, _startPosX.Value + 131, _startPosY.Value + 5, 15, 15);
}
g.DrawLine(pen, _startPosX.Value + 35, _startPosY.Value + 35, _startPosX.Value + 30, _startPosY.Value + 40);
g.DrawLine(pen, _startPosX.Value + 115, _startPosY.Value + 35, _startPosX.Value + 120, _startPosY.Value + 40);
//якорь
g.DrawLine(pen, _startPosX.Value + 25, _startPosY.Value + 55, _startPosX.Value + 25, _startPosY.Value + 75);
g.DrawLine(pen, _startPosX.Value + 17, _startPosY.Value + 65, _startPosX.Value + 33, _startPosY.Value + 65);
g.DrawLine(pen, _startPosX.Value + 23, _startPosY.Value + 75, _startPosX.Value + 27, _startPosY.Value + 75);
}
}
}

View File

@ -2,28 +2,65 @@
public class EntityTeplohod
{
public int Speed { get; private set; }
public double Weight { get; private set; }
/// <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; }
public bool Pipes { get; private set; }
/// <summary>
/// Признак (опция) наличия труб
/// </summary>
public bool Pipes { get; private set; }
public bool FuelTank { get; private set; }
/// <summary>
/// Признак (опция) наличия топливного бака
/// </summary>
public bool Tank { get; private set; }
/// <summary>
/// Признак (опция) наличия гоночной полосы
/// </summary>
public bool FirstDack { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, bool pipes, bool fueltank)
/// <summary>
/// Инициализация полей объекта-класса спортивного автомобиля
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="pipes">Признак наличия обвеса</param>
/// <param name="tank">Признак наличия антикрыла</param>
/// <param name="firstDack">Признак наличия гоночной полосы</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pipes, bool tank, bool firstDack)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
AdditionalColor = additionalcolor;
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Pipes = pipes;
FuelTank = fueltank;
Tank = tank;
FirstDack = firstDack;
}
}

View File

@ -3,12 +3,12 @@
partial class FormTeplohod
{
/// <summary>
/// Required designer variable.
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
@ -23,8 +23,8 @@
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
@ -41,55 +41,67 @@
//
pictureBoxTeplohod.Dock = DockStyle.Fill;
pictureBoxTeplohod.Location = new Point(0, 0);
pictureBoxTeplohod.Margin = new Padding(3, 4, 3, 4);
pictureBoxTeplohod.Name = "pictureBoxTeplohod";
pictureBoxTeplohod.Size = new Size(882, 553);
pictureBoxTeplohod.Size = new Size(775, 487);
pictureBoxTeplohod.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxTeplohod.TabIndex = 0;
pictureBoxTeplohod.TabStop = false;
//
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 512);
buttonCreate.Location = new Point(12, 415);
buttonCreate.Margin = new Padding(3, 4, 3, 4);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.Size = new Size(93, 49);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "создать";
buttonCreate.Text = "СОЗДАТЬ";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
buttonCreate.Click += ButtonCreateSportCar_Click;
//
// buttonLeft
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackColor = SystemColors.ButtonFace;
buttonLeft.BackgroundImage = Properties.Resources.влево;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.Location = new Point(738, 500);
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(557, 415);
buttonLeft.Margin = new Padding(3, 4, 3, 4);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(40, 40);
buttonLeft.Size = new Size(50, 50);
buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Text = " ";
buttonLeft.UseVisualStyleBackColor = false;
buttonLeft.Click += ButtonMove_Click;
//
// buttonUp
//
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackColor = SystemColors.Menu;
buttonUp.BackgroundImage = Properties.Resources.вверх;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.Location = new Point(784, 454);
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.ImageAlign = ContentAlignment.MiddleLeft;
buttonUp.Location = new Point(613, 357);
buttonUp.Margin = new Padding(3, 4, 3, 4);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(40, 40);
buttonUp.Size = new Size(50, 50);
buttonUp.TabIndex = 3;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Text = " ";
buttonUp.UseVisualStyleBackColor = false;
buttonUp.Click += ButtonMove_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.вправо;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.Location = new Point(830, 501);
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(669, 415);
buttonRight.Margin = new Padding(3, 4, 3, 4);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(40, 40);
buttonRight.TabIndex = 4;
buttonRight.Size = new Size(50, 50);
buttonRight.TabIndex = 5;
buttonRight.Text = " ";
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
@ -97,11 +109,13 @@
//
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.вниз;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.Location = new Point(784, 500);
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(613, 415);
buttonDown.Margin = new Padding(3, 4, 3, 4);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(40, 40);
buttonDown.TabIndex = 5;
buttonDown.Size = new Size(50, 50);
buttonDown.TabIndex = 6;
buttonDown.Text = " ";
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
@ -109,17 +123,19 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(882, 553);
ClientSize = new Size(775, 487);
Controls.Add(buttonDown);
Controls.Add(buttonRight);
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
Controls.Add(buttonCreate);
Controls.Add(pictureBoxTeplohod);
Margin = new Padding(3, 4, 3, 4);
Name = "FormTeplohod";
Text = "Теплоход";
Text = "Form1";
((System.ComponentModel.ISupportInitialize)pictureBoxTeplohod).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion

View File

@ -1,102 +1,91 @@
using lab0;
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 Teplohod;
public partial class FormTeplohod : Form
namespace Teplohod
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawingTeplohod? _drawingTeplohod;
public FormTeplohod()
public partial class FormTeplohod : Form
{
InitializeComponent();
}
/// <summary>
/// Метод прорисовки теплохода
/// </summary>
private void Draw()
{
if (_drawingTeplohod == null)
public FormTeplohod()
{
return;
InitializeComponent();
}
Bitmap bmp = new(pictureBoxTeplohod.Width, pictureBoxTeplohod.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingTeplohod.DrawTransport(gr);
pictureBoxTeplohod.Image = bmp;
}
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningTeplohod? _drawningTeplohod;
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawingTeplohod = new DrawingTeplohod();
_drawingTeplohod.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)));
_drawingTeplohod.SetPictureSize(pictureBoxTeplohod.Width, pictureBoxTeplohod.Height);
_drawingTeplohod.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Конструктор формы
/// </summary>
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingTeplohod == null)
/// <summary>
/// Метод прорисовки машины
/// </summary>
private void Draw()
{
return;
if (_drawningTeplohod == null)
{
return;
}
Bitmap bmp = new(pictureBoxTeplohod.Width, pictureBoxTeplohod.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTeplohod.DrawTransport(gr);
pictureBoxTeplohod.Image = bmp;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawingTeplohod.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawingTeplohod.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawingTeplohod.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawingTeplohod.MoveTransport(DirectionType.Right);
break;
}
if (result)
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateSportCar_Click(object sender, EventArgs e)
{
Random random = new();
_drawningTeplohod = new DrawningTeplohod();
_drawningTeplohod.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)), Convert.ToBoolean(random.Next(0, 2)));
_drawningTeplohod.SetPictureSize(pictureBoxTeplohod.Width, pictureBoxTeplohod.Height);
_drawningTeplohod.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 (_drawningTeplohod == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningTeplohod.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningTeplohod.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningTeplohod.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningTeplohod.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}
}
}