изменения_1.1
This commit is contained in:
parent
703b99f565
commit
a4c26fed31
@ -3,50 +3,57 @@ using System.Drawing;
|
||||
|
||||
namespace Teplohod;
|
||||
|
||||
public class DrawingTeplohod
|
||||
public class DrawningTeplohod
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityTeplohod? EntityTeplohod { get; set; }
|
||||
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? _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)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Проверяем, что объект не выходит за пределы формы
|
||||
if (width >= _drawningTeplohodWidth && height >= _drawningTeplohodHeight)
|
||||
if (_drawningTeplohodWidth <= width && _drawningTeplohodHeight <= 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 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);
|
||||
}
|
||||
}
|
@ -2,28 +2,65 @@
|
||||
|
||||
public class EntityTeplohod
|
||||
{
|
||||
|
||||
/// <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 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;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Pipes = pipes;
|
||||
FuelTank = fueltank;
|
||||
Tank = tank;
|
||||
FirstDack = firstDack;
|
||||
}
|
||||
}
|
||||
|
1
lab0/lab0/FormTeplohod.Designer.cs
generated
1
lab0/lab0/FormTeplohod.Designer.cs
generated
@ -55,7 +55,6 @@
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
//
|
||||
// buttonLeft
|
||||
//
|
||||
|
@ -1,24 +1,7 @@
|
||||
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;
|
||||
|
||||
namespace Teplohod
|
||||
{
|
||||
public partial class FormTeplohod : Form
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawingTeplohod? _drawingTeplohod;
|
||||
|
||||
|
||||
public FormTeplohod()
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -26,39 +9,46 @@ public partial class FormTeplohod : Form
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Метод прорисовки теплохода
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawningTeplohod? _drawningTeplohod;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор формы
|
||||
/// </summary>
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Метод прорисовки машины
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawingTeplohod == null)
|
||||
if (_drawningTeplohod == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap bmp = new(pictureBoxTeplohod.Width, pictureBoxTeplohod.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawingTeplohod.DrawTransport(gr);
|
||||
_drawningTeplohod.DrawTransport(gr);
|
||||
pictureBoxTeplohod.Image = bmp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
private void ButtonCreateSportCar_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingTeplohod = new DrawingTeplohod();
|
||||
_drawingTeplohod.Init(random.Next(100, 300), random.Next(1000, 3000),
|
||||
_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)));
|
||||
_drawingTeplohod.SetPictureSize(pictureBoxTeplohod.Width, pictureBoxTeplohod.Height);
|
||||
_drawingTeplohod.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
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();
|
||||
}
|
||||
|
||||
@ -69,34 +59,33 @@ public partial class FormTeplohod : Form
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingTeplohod == null)
|
||||
if (_drawningTeplohod == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
bool result = false;
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result =
|
||||
_drawingTeplohod.MoveTransport(DirectionType.Up);
|
||||
result = _drawningTeplohod.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result =
|
||||
_drawingTeplohod.MoveTransport(DirectionType.Down);
|
||||
result = _drawningTeplohod.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result =
|
||||
_drawingTeplohod.MoveTransport(DirectionType.Left);
|
||||
result = _drawningTeplohod.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result =
|
||||
_drawingTeplohod.MoveTransport(DirectionType.Right);
|
||||
result = _drawningTeplohod.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
|
||||
if (result)
|
||||
{
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user