Добавление проверок на размер окна и изменения координат

This commit is contained in:
Полина Чубыкина 2023-09-26 16:26:50 +04:00
parent d164bcbf1f
commit dbbef18263
4 changed files with 70 additions and 175 deletions

View File

@ -8,71 +8,38 @@ namespace Sailboat
{ {
public class DrawingSailboat public class DrawingSailboat
{ {
/// <summary>
/// Класс-сущность
/// </summary>
public EntitySailboat? EntitySailboat { get; private set; } public EntitySailboat? EntitySailboat { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth; private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight; private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки автомобиля
/// </summary>
private int _startPosX; private int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки автомобиля
/// </summary>
private int _startPosY; private int _startPosY;
/// <summary> private readonly int _boatWidth = 160;
/// Ширина прорисовки автомобиля private readonly int _boatHeight = 160;
/// </summary> public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail, int width, int height)
private readonly int _boatWidth = 180;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private readonly int _boatHeight = 180;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="wing">Признак наличия антикрыла</param>
/// <param name="sportLine">Признак наличия гоночной полосы</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height)
{ {
// TODO: Продумать проверки if (width < _boatWidth || height < _boatHeight)
{
return false;
}
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
EntitySailboat = new EntitySailboat(); EntitySailboat = new EntitySailboat();
EntitySailboat.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); EntitySailboat.Init(speed, weight, bodyColor, additionalColor, hull, sail);
return true; return true;
} }
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
// TODO: Изменение x, y if (x < 0 || x + _boatWidth > _pictureWidth)
{
x = 0;
}
if (y < 0 || y + _boatHeight > _pictureHeight)
{
y = 0;
}
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
} }
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction) public void MoveTransport(DirectionType direction)
{ {
if (EntitySailboat == null) if (EntitySailboat == null)
@ -81,28 +48,24 @@ namespace Sailboat
} }
switch (direction) switch (direction)
{ {
//влево
case DirectionType.Left: case DirectionType.Left:
if (_startPosX - EntitySailboat.Step > 0) if (_startPosX - EntitySailboat.Step > 0)
{ {
_startPosX -= (int)EntitySailboat.Step; _startPosX -= (int)EntitySailboat.Step;
} }
break; break;
//вверх
case DirectionType.Up: case DirectionType.Up:
if (_startPosY - EntitySailboat.Step > 0) if (_startPosY - EntitySailboat.Step > 0)
{ {
_startPosY -= (int)EntitySailboat.Step; _startPosY -= (int)EntitySailboat.Step;
} }
break; break;
// вправо
case DirectionType.Right: case DirectionType.Right:
if (_startPosX + EntitySailboat.Step + _boatWidth < _pictureWidth) if (_startPosX + EntitySailboat.Step + _boatWidth < _pictureWidth)
{ {
_startPosX += (int)EntitySailboat.Step; _startPosX += (int)EntitySailboat.Step;
} }
break; break;
//вниз
case DirectionType.Down: case DirectionType.Down:
if (_startPosY + EntitySailboat.Step + _boatHeight < _pictureHeight) if (_startPosY + EntitySailboat.Step + _boatHeight < _pictureHeight)
{ {
@ -111,103 +74,43 @@ namespace Sailboat
break; break;
} }
} }
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g) public void DrawTransport(Graphics g)
{ {
if (EntitySailboat == null) if (EntitySailboat == null)
{ {
return; return;
} }
Pen pen = new(Color.Black); Pen pen = new(Color.Black, 4);
Brush additionalBrush = new Brush additionalBrush = new
SolidBrush(EntitySailboat.AdditionalColor); SolidBrush(EntitySailboat.AdditionalColor);
// обвесы
/*if (EntitySailboat.BodyKit)
{
g.DrawEllipse(pen, _startPosX + 90, _startPosY, 20, 20);
g.DrawEllipse(pen, _startPosX + 90, _startPosY + 40, 20,
20);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 10,
20, 40);
g.DrawRectangle(pen, _startPosX + 90, _startPosY, 15,
15);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 45,
15, 15);
g.FillEllipse(additionalBrush, _startPosX + 90,
_startPosY, 20, 20);
g.FillEllipse(additionalBrush, _startPosX + 90,
_startPosY + 40, 20, 20);
g.FillRectangle(additionalBrush, _startPosX + 90,
_startPosY + 10, 20, 40);
g.FillRectangle(additionalBrush, _startPosX + 90,
_startPosY + 1, 15, 15);
g.FillRectangle(additionalBrush, _startPosX + 90,
_startPosY + 45, 15, 15);
g.DrawEllipse(pen, _startPosX, _startPosY, 20, 20);
g.DrawEllipse(pen, _startPosX, _startPosY + 40, 20, 20);
g.DrawRectangle(pen, _startPosX, _startPosY + 10, 20,
40);
g.DrawRectangle(pen, _startPosX + 5, _startPosY, 14,
15);
g.DrawRectangle(pen, _startPosX + 5, _startPosY + 45,
14, 15);
g.FillEllipse(additionalBrush, _startPosX, _startPosY,
20, 20);
g.FillEllipse(additionalBrush, _startPosX, _startPosY +
40, 20, 20);
g.FillRectangle(additionalBrush, _startPosX + 1,
_startPosY + 10, 25, 40);
g.FillRectangle(additionalBrush, _startPosX + 5,
_startPosY + 1, 15, 15);
g.FillRectangle(additionalBrush, _startPosX + 5,
_startPosY + 45, 15, 15);
g.DrawRectangle(pen, _startPosX + 35, _startPosY, 39,
15);
g.DrawRectangle(pen, _startPosX + 35, _startPosY + 45,
39, 15);
g.FillRectangle(additionalBrush, _startPosX + 35,
_startPosY + 1, 40, 15);
g.FillRectangle(additionalBrush, _startPosX + 35,
_startPosY + 45, 40, 15);
}
//границы автомобиля
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 5, 20, 20);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 35, 20, 20);
g.DrawEllipse(pen, _startPosX + 80, _startPosY + 5, 20, 20);
g.DrawEllipse(pen, _startPosX + 80, _startPosY + 35, 20, 20);
g.DrawRectangle(pen, _startPosX + 9, _startPosY + 15, 10, 30);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 15, 10,
30);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 70, 52);*/
//усиленный корпус парусника //усиленный корпус парусника
Point[] hullCooler = new Point[] if (EntitySailboat.Hull)
{ {
new Point(_startPosX + 30, _startPosY + 100), Point[] hullCooler = new Point[]
new Point(_startPosX + 150, _startPosY + 100), {
new Point(_startPosX + 190, _startPosY + 140), new Point(_startPosX, _startPosY + 80),
new Point(_startPosX + 150, _startPosY + 180), new Point(_startPosX + 120, _startPosY + 80),
new Point(_startPosX + 30, _startPosY + 180) new Point(_startPosX + 160, _startPosY + 120),
}; new Point(_startPosX + 120, _startPosY + 160),
g.FillPolygon(additionalBrush, hullCooler); new Point(_startPosX, _startPosY + 160)
g.DrawPolygon(pen, hullCooler); };
g.FillPolygon(additionalBrush, hullCooler);
g.DrawPolygon(pen, hullCooler);
}
//основной корпус парусника //основной корпус парусника
Brush Brush = new Brush Brush = new
SolidBrush(EntitySailboat.BodyColor); SolidBrush(EntitySailboat.BodyColor);
Point[] hull = new Point[] Point[] hull = new Point[]
{ {
new Point(_startPosX + 40, _startPosY + 110), new Point(_startPosX + 10, _startPosY + 90),
new Point(_startPosX + 140, _startPosY + 110), new Point(_startPosX + 110, _startPosY + 90),
new Point(_startPosX + 170, _startPosY + 140), new Point(_startPosX + 140, _startPosY + 120),
new Point(_startPosX + 140, _startPosY + 170), new Point(_startPosX + 110, _startPosY + 150),
new Point(_startPosX + 40, _startPosY + 170) new Point(_startPosX + 10, _startPosY + 150)
}; };
g.FillPolygon(Brush, hull); g.FillPolygon(Brush, hull);
g.DrawPolygon(pen, hull); g.DrawPolygon(pen, hull);
@ -215,22 +118,25 @@ namespace Sailboat
Brush addBrush = new Brush addBrush = new
SolidBrush(Color.Green); SolidBrush(Color.Green);
g.FillEllipse(addBrush, _startPosX + 50, _startPosY + 120, 90, 40); g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 90, 40);
g.DrawEllipse(pen, _startPosX + 50, _startPosY + 120, 90, 40); g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 90, 40);
//парус //парус
Brush sailBrush = new if (EntitySailboat.Sail)
SolidBrush(Color.Cyan);
Point[] sail = new Point[]
{ {
new Point(_startPosX + 95, _startPosY + 20), Brush sailBrush = new
new Point(_startPosX + 160, _startPosY + 140), SolidBrush(EntitySailboat.AdditionalColor);
new Point(_startPosX + 45, _startPosY + 140)
}; Point[] sail = new Point[]
g.FillPolygon(sailBrush, sail); {
g.DrawPolygon(pen, sail); new Point(_startPosX + 65, _startPosY),
g.DrawLine(pen, new Point(_startPosX + 95, _startPosY + 140), new Point(_startPosX + 95, _startPosY + 20)); new Point(_startPosX + 130, _startPosY + 120),
new Point(_startPosX + 15, _startPosY + 120)
};
g.FillPolygon(sailBrush, sail);
g.DrawPolygon(pen, sail);
g.DrawLine(pen, new Point(_startPosX + 65, _startPosY + 120), new Point(_startPosX + 65, _startPosY));
}
} }
} }
} }

View File

@ -25,17 +25,13 @@ namespace Sailboat
/// </summary> /// </summary>
public Color AdditionalColor { get; private set; } public Color AdditionalColor { get; private set; }
/// <summary> /// <summary>
/// Признак (опция) наличия обвеса /// Признак (опция) наличия усиленного корпуса
/// </summary> /// </summary>
public bool BodyKit { get; private set; } public bool Hull { get; private set; }
/// <summary> /// <summary>
/// Признак (опция) наличия антикрыла /// Признак (опция) наличия паруса
/// </summary> /// </summary>
public bool Wing { get; private set; } public bool Sail { get; private set; }
/// <summary>
/// Признак (опция) наличия гоночной полосы
/// </summary>
public bool SportLine { get; private set; }
/// <summary> /// <summary>
/// Шаг перемещения автомобиля /// Шаг перемещения автомобиля
/// </summary> /// </summary>
@ -47,19 +43,17 @@ namespace Sailboat
/// <param name="weight">Вес автомобиля</param> /// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param> /// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param> /// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param> /// <param name="hull">Признак наличия усиленного корпуса</param>
/// <param name="wing">Признак наличия антикрыла</param> /// <param name="sail">Признак наличия паруса</param>
/// <param name="sportLine">Признак наличия гоночной полосы</param>
public void Init(int speed, double weight, Color bodyColor, Color public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool bodyKit, bool wing, bool sportLine) additionalColor, bool hull, bool sail)
{ {
Speed = speed; Speed = speed;
Weight = weight; Weight = weight;
BodyColor = bodyColor; BodyColor = bodyColor;
AdditionalColor = additionalColor; AdditionalColor = additionalColor;
BodyKit = bodyKit; Hull = hull;
Wing = wing; Sail = sail;
SportLine = sportLine;
} }
} }
} }

View File

@ -43,7 +43,7 @@
this.pictureBoxSailboat.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxSailboat.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxSailboat.Location = new System.Drawing.Point(0, 0); this.pictureBoxSailboat.Location = new System.Drawing.Point(0, 0);
this.pictureBoxSailboat.Name = "pictureBoxSailboat"; this.pictureBoxSailboat.Name = "pictureBoxSailboat";
this.pictureBoxSailboat.Size = new System.Drawing.Size(1466, 741); this.pictureBoxSailboat.Size = new System.Drawing.Size(882, 453);
this.pictureBoxSailboat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxSailboat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxSailboat.TabIndex = 0; this.pictureBoxSailboat.TabIndex = 0;
this.pictureBoxSailboat.TabStop = false; this.pictureBoxSailboat.TabStop = false;
@ -51,7 +51,7 @@
// buttonCreate // buttonCreate
// //
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(12, 683); this.buttonCreate.Location = new System.Drawing.Point(12, 395);
this.buttonCreate.Name = "buttonCreate"; this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(127, 46); this.buttonCreate.Size = new System.Drawing.Size(127, 46);
this.buttonCreate.TabIndex = 1; this.buttonCreate.TabIndex = 1;
@ -64,7 +64,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonLeft.BackgroundImage"))); this.buttonLeft.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonLeft.BackgroundImage")));
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(1294, 691); this.buttonLeft.Location = new System.Drawing.Point(710, 403);
this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 2; this.buttonLeft.TabIndex = 2;
@ -76,7 +76,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonUp.BackgroundImage"))); this.buttonUp.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonUp.BackgroundImage")));
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(1343, 646); this.buttonUp.Location = new System.Drawing.Point(759, 358);
this.buttonUp.Name = "buttonUp"; this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 3; this.buttonUp.TabIndex = 3;
@ -88,7 +88,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonRight.BackgroundImage"))); this.buttonRight.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonRight.BackgroundImage")));
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(1393, 691); this.buttonRight.Location = new System.Drawing.Point(809, 403);
this.buttonRight.Name = "buttonRight"; this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 4; this.buttonRight.TabIndex = 4;
@ -100,7 +100,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonDown.BackgroundImage"))); this.buttonDown.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonDown.BackgroundImage")));
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(1343, 691); this.buttonDown.Location = new System.Drawing.Point(759, 403);
this.buttonDown.Name = "buttonDown"; this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 5; this.buttonDown.TabIndex = 5;
@ -111,7 +111,7 @@
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1466, 741); this.ClientSize = new System.Drawing.Size(882, 453);
this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonUp);

View File

@ -23,14 +23,9 @@ namespace Sailboat
{ {
Random random = new(); Random random = new();
_drawingSailboat = new DrawingSailboat(); _drawingSailboat = new DrawingSailboat();
_drawingSailboat.Init(random.Next(100, 300), _drawingSailboat.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)),
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)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxSailboat.Width, pictureBoxSailboat.Height); Convert.ToBoolean(random.Next(0, 2)), pictureBoxSailboat.Width, pictureBoxSailboat.Height);
_drawingSailboat.SetPosition(random.Next(10, 100), random.Next(10, 100)); _drawingSailboat.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw(); Draw();
} }