Нарисовала парусник

This commit is contained in:
Полина Чубыкина 2023-09-22 17:19:54 +04:00
parent 568549df04
commit e7832c9d05
5 changed files with 108 additions and 59 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Sailboat namespace Sailboat
{ {
internal enum DirectionType public enum DirectionType
{ {
/// <summary> /// <summary>
/// Вверх /// Вверх

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Sailboat namespace Sailboat
{ {
internal class DrawingSailboat public class DrawingSailboat
{ {
/// <summary> /// <summary>
/// Класс-сущность /// Класс-сущность
@ -31,11 +31,11 @@ namespace Sailboat
/// <summary> /// <summary>
/// Ширина прорисовки автомобиля /// Ширина прорисовки автомобиля
/// </summary> /// </summary>
private readonly int _carWidth = 110; private readonly int _boatWidth = 180;
/// <summary> /// <summary>
/// Высота прорисовки автомобиля /// Высота прорисовки автомобиля
/// </summary> /// </summary>
private readonly int _carHeight = 60; private readonly int _boatHeight = 180;
/// <summary> /// <summary>
/// Инициализация свойств /// Инициализация свойств
/// </summary> /// </summary>
@ -97,11 +97,17 @@ namespace Sailboat
break; break;
// вправо // вправо
case DirectionType.Right: case DirectionType.Right:
// TODO: Продумать логику if (_startPosX + EntitySailboat.Step + _boatWidth < _pictureWidth)
{
_startPosX += (int)EntitySailboat.Step;
}
break; break;
//вниз //вниз
case DirectionType.Down: case DirectionType.Down:
// TODO: Продумать логику if (_startPosY + EntitySailboat.Step + _boatHeight < _pictureHeight)
{
_startPosY += (int)EntitySailboat.Step;
}
break; break;
} }
} }
@ -119,7 +125,7 @@ namespace Sailboat
Brush additionalBrush = new Brush additionalBrush = new
SolidBrush(EntitySailboat.AdditionalColor); SolidBrush(EntitySailboat.AdditionalColor);
// обвесы // обвесы
if (EntitySailboat.BodyKit) /*if (EntitySailboat.BodyKit)
{ {
g.DrawEllipse(pen, _startPosX + 90, _startPosY, 20, 20); g.DrawEllipse(pen, _startPosX + 90, _startPosY, 20, 20);
g.DrawEllipse(pen, _startPosX + 90, _startPosY + 40, 20, g.DrawEllipse(pen, _startPosX + 90, _startPosY + 40, 20,
@ -167,6 +173,7 @@ namespace Sailboat
g.FillRectangle(additionalBrush, _startPosX + 35, g.FillRectangle(additionalBrush, _startPosX + 35,
_startPosY + 45, 40, 15); _startPosY + 45, 40, 15);
} }
//границы автомобиля //границы автомобиля
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 5, 20, 20); g.DrawEllipse(pen, _startPosX + 10, _startPosY + 5, 20, 20);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 35, 20, 20); g.DrawEllipse(pen, _startPosX + 10, _startPosY + 35, 20, 20);
@ -175,58 +182,42 @@ namespace Sailboat
g.DrawRectangle(pen, _startPosX + 9, _startPosY + 15, 10, 30); g.DrawRectangle(pen, _startPosX + 9, _startPosY + 15, 10, 30);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 15, 10, g.DrawRectangle(pen, _startPosX + 90, _startPosY + 15, 10,
30); 30);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 70, 52); g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 70, 52);*/
//задние фары
Brush brRed = new SolidBrush(Color.Red); //основной корпус парусника
g.FillEllipse(brRed, _startPosX + 10, _startPosY + 5, 20, 20); Point[] hull = new Point[]
g.FillEllipse(brRed, _startPosX + 10, _startPosY + 35, 20,
20);
//передние фары
Brush brYellow = new SolidBrush(Color.Yellow);
g.FillEllipse(brYellow, _startPosX + 80, _startPosY + 5, 20,
20);
g.FillEllipse(brYellow, _startPosX + 80, _startPosY + 35, 20,
20);
//кузов
Brush br = new SolidBrush(EntitySailboat.BodyColor);
g.FillRectangle(br, _startPosX + 10, _startPosY + 15, 10, 30);
g.FillRectangle(br, _startPosX + 90, _startPosY + 15, 10, 30);
g.FillRectangle(br, _startPosX + 20, _startPosY + 5, 70, 50);
//стекла
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX + 70, _startPosY + 10, 5,
40);
g.FillRectangle(brBlue, _startPosX + 30, _startPosY + 10, 5,
40);
g.FillRectangle(brBlue, _startPosX + 35, _startPosY + 8, 35,
2);
g.FillRectangle(brBlue, _startPosX + 35, _startPosY + 51, 35,
2);
//выделяем рамкой крышу
g.DrawRectangle(pen, _startPosX + 35, _startPosY + 10, 35,
40);
g.DrawRectangle(pen, _startPosX + 75, _startPosY + 15, 25,
30);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 15, 15,
30);
// спортивная линия
if (EntitySailboat.SportLine)
{ {
g.FillRectangle(additionalBrush, _startPosX + 75, new Point(_startPosX + 40, _startPosY + 110),
_startPosY + 23, 25, 15); new Point(_startPosX + 140, _startPosY + 110),
g.FillRectangle(additionalBrush, _startPosX + 35, new Point(_startPosX + 170, _startPosY + 140),
_startPosY + 23, 35, 15); new Point(_startPosX + 140, _startPosY + 170),
g.FillRectangle(additionalBrush, _startPosX + 10, new Point(_startPosX + 40, _startPosY + 170)
_startPosY + 23, 20, 15); };
} g.DrawPolygon(pen, hull);
// крыло
if (EntitySailboat.Wing) g.DrawEllipse(pen, _startPosX + 50, _startPosY + 120, 90, 40);
//усиленный корпус парусника
Point[] hullCooler = new Point[]
{ {
g.FillRectangle(additionalBrush, _startPosX, _startPosY new Point(_startPosX + 30, _startPosY + 100),
+ 5, 10, 50); new Point(_startPosX + 150, _startPosY + 100),
g.DrawRectangle(pen, _startPosX, _startPosY + 5, 10, new Point(_startPosX + 190, _startPosY + 140),
50); new Point(_startPosX + 150, _startPosY + 180),
} new Point(_startPosX + 30, _startPosY + 180)
};
g.DrawPolygon(pen, hullCooler);
//парус
g.DrawLine(pen, new Point(_startPosX + 95, _startPosY + 140),new Point (_startPosX + 95, _startPosY + 20));
Point[] sail = new Point[]
{
new Point(_startPosX + 95, _startPosY + 20),
new Point(_startPosX + 160, _startPosY + 140),
new Point(_startPosX + 45, _startPosY + 140)
};
g.DrawPolygon(pen, sail);
} }
} }
} }

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Sailboat namespace Sailboat
{ {
internal class EntitySailboat public class EntitySailboat
{ {
/// <summary> /// <summary>
/// Скорость /// Скорость

View File

@ -57,6 +57,7 @@
this.buttonCreate.TabIndex = 1; this.buttonCreate.TabIndex = 1;
this.buttonCreate.Text = "Создать"; this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
// //
// buttonLeft // buttonLeft
// //
@ -68,6 +69,7 @@
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;
this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click);
// //
// buttonUp // buttonUp
// //
@ -79,6 +81,7 @@
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;
this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click);
// //
// buttonRight // buttonRight
// //
@ -90,6 +93,7 @@
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;
this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
// //
// buttonDown // buttonDown
// //
@ -101,6 +105,7 @@
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;
this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
// //
// FormSailboat // FormSailboat
// //

View File

@ -2,9 +2,62 @@ namespace Sailboat
{ {
public partial class FormSailboat : Form public partial class FormSailboat : Form
{ {
private DrawingSailboat? _drawingSailboat;
public FormSailboat() public FormSailboat()
{ {
InitializeComponent(); InitializeComponent();
} }
private void Draw()
{
if (_drawingSailboat == null)
{
return;
}
Bitmap bmp = new(pictureBoxSailboat.Width,
pictureBoxSailboat.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingSailboat.DrawTransport(gr);
pictureBoxSailboat.Image = bmp;
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawingSailboat = new DrawingSailboat();
_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)),
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxSailboat.Width, pictureBoxSailboat.Height);
_drawingSailboat.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawingSailboat == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawingSailboat.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawingSailboat.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawingSailboat.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawingSailboat.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
} }
} }