Лабораторная работа №1
This commit is contained in:
parent
906f781d5f
commit
33f58a2310
@ -1,4 +1,7 @@
|
||||
namespace ProjectMotorBoat;
|
||||
using System;
|
||||
using System.DirectoryServices.ActiveDirectory;
|
||||
|
||||
namespace ProjectMotorBoat;
|
||||
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
@ -34,12 +37,12 @@ public class DrawingMotorBoat
|
||||
/// <summary>
|
||||
/// Ширина прорисовки катера
|
||||
/// </summary>
|
||||
private readonly int _drawningBoatWidth = 110;
|
||||
private readonly int _drawningBoatWidth = 100;
|
||||
|
||||
/// <summary>
|
||||
/// Высота прорисовки катера
|
||||
/// </summary>
|
||||
private readonly int _drawningBoatHeight = 60;
|
||||
private readonly int _drawningBoatHeight = 50;
|
||||
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
@ -70,9 +73,25 @@ public class DrawingMotorBoat
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
// TODO проверка, что объект "влезает" в размеры поля
|
||||
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена ✔
|
||||
if (_drawningBoatWidth < width)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
if (_startPosX + _drawningBoatWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningBoatWidth;
|
||||
}
|
||||
}
|
||||
|
||||
if (_drawningBoatHeight < height)
|
||||
{
|
||||
_pictureHeight = height;
|
||||
if (_startPosY + _drawningBoatHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningBoatHeight;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -88,7 +107,17 @@ public class DrawingMotorBoat
|
||||
return;
|
||||
}
|
||||
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
|
||||
// то надо изменить координаты, чтобы он оставался в этих границах
|
||||
// то надо изменить координаты, чтобы он оставался в этих границах ✔
|
||||
if (x + _drawningBoatWidth > _pictureWidth)
|
||||
{
|
||||
x = (int)_pictureWidth - _drawningBoatWidth;
|
||||
}
|
||||
|
||||
if (y + _drawningBoatHeight > _pictureHeight)
|
||||
{
|
||||
y = (int)_pictureHeight - _drawningBoatHeight;
|
||||
}
|
||||
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
@ -123,21 +152,21 @@ public class DrawingMotorBoat
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
//TODO прописать логику сдвига в право
|
||||
//TODO прописать логику сдвига в право ✔
|
||||
|
||||
//if (_startPosX.Value + EntityMotorBoat.Step < _pictureWidth)
|
||||
//{
|
||||
// _startPosX += (int)EntityMotorBoat.Step;
|
||||
//}
|
||||
if (_startPosX.Value + EntityMotorBoat.Step < _pictureWidth - _drawningBoatWidth)
|
||||
{
|
||||
_startPosX += (int)EntityMotorBoat.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
//TODO прописать логику сдвига в низ
|
||||
//TODO прописать логику сдвига в низ ✔
|
||||
|
||||
//if (_startPosY.Value + EntityMotorBoat.Step < _pictureHeight)
|
||||
//{
|
||||
// _startPosY += (int)EntityMotorBoat.Step;
|
||||
//}
|
||||
if (_startPosY.Value + EntityMotorBoat.Step < _pictureHeight - _drawningBoatHeight)
|
||||
{
|
||||
_startPosY += (int)EntityMotorBoat.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
@ -154,5 +183,90 @@ public class DrawingMotorBoat
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(EntityMotorBoat.AdditionalColor);
|
||||
Brush br = new SolidBrush(EntityMotorBoat.BodyColor);
|
||||
|
||||
// Границы катера
|
||||
Point[] points =
|
||||
{
|
||||
new Point(_startPosX.Value, _startPosY.Value + 10),
|
||||
new Point(_startPosX.Value + 70, _startPosY.Value + 10),
|
||||
new Point(_startPosX.Value + 100, _startPosY.Value + 25),
|
||||
new Point(_startPosX.Value + 70, _startPosY.Value + 40),
|
||||
new Point(_startPosX.Value, _startPosY.Value + 40),
|
||||
new Point(_startPosX.Value, _startPosY.Value + 10),
|
||||
};
|
||||
g.DrawLines(pen, points);
|
||||
|
||||
points = new Point[]
|
||||
{
|
||||
new Point(_startPosX.Value + 1, _startPosY.Value + 11),
|
||||
new Point(_startPosX.Value + 72, _startPosY.Value + 11),
|
||||
new Point(_startPosX.Value + 99, _startPosY.Value + 25),
|
||||
new Point(_startPosX.Value + 70, _startPosY.Value + 40),
|
||||
new Point(_startPosX.Value + 1, _startPosY.Value + 40),
|
||||
new Point(_startPosX.Value + 1, _startPosY.Value + 11),
|
||||
};
|
||||
g.FillPolygon(br, points);
|
||||
|
||||
// Палуба
|
||||
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 20, 65, 10);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 15, 55, 20);
|
||||
|
||||
g.FillRectangle(br, _startPosX.Value + 6, _startPosY.Value + 20, 64, 11);
|
||||
g.FillRectangle(br, _startPosX.Value + 11, _startPosY.Value + 16, 56, 19);
|
||||
|
||||
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 15, 10, 10);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 15, 10, 10);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 25, 10, 10);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 25, 10, 10);
|
||||
|
||||
g.FillEllipse(br, _startPosX.Value + 5, _startPosY.Value + 15, 11, 11);
|
||||
g.FillEllipse(br, _startPosX.Value + 59, _startPosY.Value + 15, 11, 11);
|
||||
g.FillEllipse(br, _startPosX.Value + 5, _startPosY.Value + 24, 11, 11);
|
||||
g.FillEllipse(br, _startPosX.Value + 59, _startPosY.Value + 24, 11, 11);
|
||||
|
||||
// Мотор в корме
|
||||
if (EntityMotorBoat.InboardEngine)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX.Value + 3, _startPosY.Value + 13, 20, 24);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 4, _startPosY.Value + 14, 19, 23);
|
||||
|
||||
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 15, 6, 6);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 15, 6, 6);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 28, 6, 6);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 28, 6, 6);
|
||||
g.FillEllipse(additionalBrush, _startPosX.Value + 6, _startPosY.Value + 16, 4, 4);
|
||||
g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 16, 4, 4);
|
||||
g.FillEllipse(additionalBrush, _startPosX.Value + 6, _startPosY.Value + 29, 4, 4);
|
||||
g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 29, 4, 4);
|
||||
}
|
||||
|
||||
// Вёсла
|
||||
if (EntityMotorBoat.Paddle)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 3, 2, 17);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 30, 2, 17);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 33, _startPosY.Value + 3, 1, 17);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 33, _startPosY.Value + 31, 1, 16);
|
||||
|
||||
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value, 6, 8);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 42, 6, 8);
|
||||
g.FillEllipse(additionalBrush, _startPosX.Value + 30, _startPosY.Value, 6, 8);
|
||||
g.FillEllipse(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 42, 6, 8);
|
||||
}
|
||||
|
||||
// Защитное стекло
|
||||
if (EntityMotorBoat.ProtectiveGlass)
|
||||
{
|
||||
Brush brBlue = new SolidBrush(Color.LightBlue);
|
||||
g.FillRectangle(brBlue, _startPosX.Value + 45, _startPosY.Value + 20, 25, 11);
|
||||
g.FillRectangle(brBlue, _startPosX.Value + 45, _startPosY.Value + 16, 17, 19);
|
||||
|
||||
g.FillEllipse(brBlue, _startPosX.Value + 59, _startPosY.Value + 15, 11, 11);
|
||||
g.FillEllipse(brBlue, _startPosX.Value + 59, _startPosY.Value + 24, 11, 11);
|
||||
}
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
pictureBoxMotorBoat = new PictureBox();
|
||||
buttonCreate = new Button();
|
||||
buttonCreateMotorBoat = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonRight = new Button();
|
||||
@ -47,16 +47,16 @@
|
||||
pictureBoxMotorBoat.TabIndex = 0;
|
||||
pictureBoxMotorBoat.TabStop = false;
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateMotorBoat
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(12, 448);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(75, 23);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += ButtonCreate_Click;
|
||||
buttonCreateMotorBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateMotorBoat.Location = new Point(12, 448);
|
||||
buttonCreateMotorBoat.Name = "buttonCreateMotorBoat";
|
||||
buttonCreateMotorBoat.Size = new Size(75, 23);
|
||||
buttonCreateMotorBoat.TabIndex = 1;
|
||||
buttonCreateMotorBoat.Text = "Создать";
|
||||
buttonCreateMotorBoat.UseVisualStyleBackColor = true;
|
||||
buttonCreateMotorBoat.Click += ButtonCreateMotorBoat_Click;
|
||||
//
|
||||
// buttonLeft
|
||||
//
|
||||
@ -68,6 +68,7 @@
|
||||
buttonLeft.Size = new Size(35, 35);
|
||||
buttonLeft.TabIndex = 2;
|
||||
buttonLeft.UseVisualStyleBackColor = true;
|
||||
buttonLeft.Click += ButtonMoveMotorBoat_click;
|
||||
//
|
||||
// buttonUp
|
||||
//
|
||||
@ -79,6 +80,7 @@
|
||||
buttonUp.Size = new Size(35, 35);
|
||||
buttonUp.TabIndex = 3;
|
||||
buttonUp.UseVisualStyleBackColor = true;
|
||||
buttonUp.Click += ButtonMoveMotorBoat_click;
|
||||
//
|
||||
// buttonRight
|
||||
//
|
||||
@ -90,6 +92,7 @@
|
||||
buttonRight.Size = new Size(35, 35);
|
||||
buttonRight.TabIndex = 4;
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += ButtonMoveMotorBoat_click;
|
||||
//
|
||||
// buttonDown
|
||||
//
|
||||
@ -101,6 +104,7 @@
|
||||
buttonDown.Size = new Size(35, 35);
|
||||
buttonDown.TabIndex = 5;
|
||||
buttonDown.UseVisualStyleBackColor = true;
|
||||
buttonDown.Click += ButtonMoveMotorBoat_click;
|
||||
//
|
||||
// FormMotorBoat
|
||||
//
|
||||
@ -111,7 +115,7 @@
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateMotorBoat);
|
||||
Controls.Add(pictureBoxMotorBoat);
|
||||
Name = "FormMotorBoat";
|
||||
Text = "Катер";
|
||||
@ -123,7 +127,7 @@
|
||||
#endregion
|
||||
|
||||
private PictureBox pictureBoxMotorBoat;
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateMotorBoat;
|
||||
private Button buttonLeft;
|
||||
private Button buttonUp;
|
||||
private Button buttonRight;
|
||||
|
@ -1,41 +1,92 @@
|
||||
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 ProjectMotorBoat;
|
||||
|
||||
namespace ProjectMotorBoat
|
||||
/// <summary>
|
||||
/// Форма работы с объектом "Катер"
|
||||
/// </summary>
|
||||
public partial class FormMotorBoat : Form
|
||||
{
|
||||
public partial class FormMotorBoat : Form
|
||||
{
|
||||
private DrawingMotorBoat? _drawingMotorBoat;
|
||||
/// <summary>
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawingMotorBoat? _drawingMotorBoat;
|
||||
|
||||
public FormMotorBoat()
|
||||
/// <summary>
|
||||
/// Конструктор формы
|
||||
/// </summary>
|
||||
public FormMotorBoat()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Метод прорисовки катера
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawingMotorBoat == null)
|
||||
{
|
||||
InitializeComponent();
|
||||
return;
|
||||
}
|
||||
|
||||
private void ButtonCreate_Click(object sender, EventArgs e)
|
||||
Bitmap bmp = new(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawingMotorBoat.DrawTransport(gr);
|
||||
pictureBoxMotorBoat.Image = bmp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateMotorBoat_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingMotorBoat = new DrawingMotorBoat();
|
||||
_drawingMotorBoat.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)));
|
||||
_drawingMotorBoat.SetPictureSize(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
||||
_drawingMotorBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
|
||||
Draw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMoveMotorBoat_click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingMotorBoat == null)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingMotorBoat = new DrawingMotorBoat();
|
||||
_drawingMotorBoat.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)));
|
||||
_drawingMotorBoat.SetPictureSize(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
||||
_drawingMotorBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap bmp = new(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawingMotorBoat.DrawTransport(gr);
|
||||
pictureBoxMotorBoat.Image = bmp;
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
bool result = false;
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawingMotorBoat.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawingMotorBoat.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawingMotorBoat.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawingMotorBoat.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
|
||||
if (result)
|
||||
{
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net7.0-windows</TargetFramework>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
|
Loading…
x
Reference in New Issue
Block a user