Добавление родителей и ввод конструкторов

This commit is contained in:
Никита Шипилов 2024-03-17 23:05:49 +04:00
parent 9d7d666aa8
commit cb9068a06d
8 changed files with 231 additions and 147 deletions

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane;
namespace ProjectSeaplane.Drawings;
public enum DirectionType
{

View File

@ -3,34 +3,44 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectSeaplane.Entities;
namespace ProjectSeaplane;
public class DrawingSeaplane
namespace ProjectSeaplane.Drawings;
public class DrawingPlane
{
public EntitySeaplane? EntitySeaplane {get;private set;}
public EntityPlane? EntityPlane { get; protected set; }
private int? _pictureWidth;
private int? _pictureHeight;
private int? _startPosX;
protected int? _startPosX;
private int? _startPosY;
protected int? _startPosY;
private readonly int _drawingSeaplaneWidth = 190;
private readonly int _drawingSeaplaneHeight = 85;
private readonly int _drawingSeaplaneHeight = 70;
public void Init (int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool inflatableBoat)
private DrawingPlane()
{
EntitySeaplane = new EntitySeaplane();
EntitySeaplane.Init(speed, weight, bodyColor, additionalColor, floats, inflatableBoat);
_pictureHeight = null;
_pictureWidth = null;
_startPosX = null;
_startPosY = null;
}
public DrawingPlane(int speed, double weight, Color bodyColor) : this()
{
EntityPlane = new EntityPlane(speed, weight, bodyColor);
}
protected DrawingPlane(int drawingSeaplaneWidth, int drawingSeaplaneHeight) : this()
{
_drawingSeaplaneWidth = drawingSeaplaneWidth;
_drawingSeaplaneHeight = drawingSeaplaneHeight;
}
public bool SetPictureSize(int width, int height)
{
if (_drawingSeaplaneWidth > width || _drawingSeaplaneHeight > height)
@ -88,7 +98,7 @@ public class DrawingSeaplane
public bool MoveTransport(DirectionType direction)
{
if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue || !_pictureWidth.HasValue || !_pictureHeight.HasValue)
if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue || !_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return false;
}
@ -97,30 +107,30 @@ public class DrawingSeaplane
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntitySeaplane.Step >= 0)
if (_startPosX.Value - EntityPlane.Step >= 0)
{
_startPosX -= (int)EntitySeaplane.Step;
_startPosX -= (int)EntityPlane.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntitySeaplane.Step >= 0)
if (_startPosY.Value - EntityPlane.Step >= 0)
{
_startPosY -= (int)EntitySeaplane.Step;
_startPosY -= (int)EntityPlane.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntitySeaplane.Step <= _pictureWidth.Value - _drawingSeaplaneWidth)
if (_startPosX.Value + EntityPlane.Step <= _pictureWidth.Value - _drawingSeaplaneWidth)
{
_startPosX += (int)EntitySeaplane.Step;
_startPosX += (int)EntityPlane.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntitySeaplane.Step <= _pictureHeight.Value - _drawingSeaplaneHeight)
if (_startPosY.Value + EntityPlane.Step <= _pictureHeight.Value - _drawingSeaplaneHeight)
{
_startPosY += (int)EntitySeaplane.Step;
_startPosY += (int)EntityPlane.Step;
}
return true;
default:
@ -128,18 +138,16 @@ public class DrawingSeaplane
}
}
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntitySeaplane.AdditionalColor);
Brush bodyBrush = new SolidBrush(EntitySeaplane.BodyColor);
Brush bodyBrush = new SolidBrush(EntityPlane.BodyColor);
Brush brBlack = new SolidBrush(Color.Black);
Brush brRed = new SolidBrush(Color.Red);
//границы самолета
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 34, 20, 20);
@ -149,7 +157,7 @@ public class DrawingSeaplane
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 34, 160, 30);
Point[] pointsFaceTop = {
new Point(_startPosX.Value + 170, _startPosY.Value+30),
new Point(_startPosX.Value + 170, _startPosY.Value + 30),
new Point(_startPosX.Value + 170, _startPosY.Value + 50),
new Point(_startPosX.Value + 190, _startPosY.Value + 50)
};
@ -164,31 +172,15 @@ public class DrawingSeaplane
Point[] pointsKeel = {
new Point(_startPosX.Value + 10, _startPosY.Value),
new Point(_startPosX.Value + 10, _startPosY.Value+35),
new Point(_startPosX.Value + 50, _startPosY.Value+35)
new Point(_startPosX.Value + 10, _startPosY.Value + 35),
new Point(_startPosX.Value + 50, _startPosY.Value + 35)
};
g.DrawPolygon(pen, pointsKeel);
if (!EntitySeaplane.Floats)
{
// Рисуем переднее шасси с одним колесом
g.DrawEllipse(pen, _startPosX.Value + 140, _startPosY.Value + 75, 10, 10);
// Рисуем задние шасси с двумя колесами
g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 75, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 75, 10, 10);
// Рисуем ногу переднего шасси
g.DrawLine(pen, _startPosX.Value + 145, _startPosY.Value + 65, _startPosX.Value + 145, _startPosY.Value + 80);
// Рисуем ноги заднего шасси
g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 65, _startPosX.Value + 30, _startPosY.Value + 80);
}
// Рисуем триммера
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value+30, 10, 10);
g.DrawEllipse(pen, _startPosX.Value+30, _startPosY.Value+30, 10, 10);
g.DrawRectangle(pen, _startPosX.Value+5, _startPosY.Value+30, 30, 10);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 30, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 30, 10, 10);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 30, 30, 10);
// Рисуем крыло
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value + 45, 6, 6);
@ -196,9 +188,9 @@ public class DrawingSeaplane
g.DrawRectangle(pen, _startPosX.Value + 75, _startPosY.Value + 45, 60, 6);
// Закрашиваем носовую часть и киль
g.FillPolygon(additionalBrush, pointsFaceBottom);
g.FillPolygon(additionalBrush, pointsFaceTop);
g.FillPolygon(additionalBrush, pointsKeel);
g.FillPolygon(bodyBrush, pointsFaceBottom);
g.FillPolygon(bodyBrush, pointsFaceTop);
g.FillPolygon(bodyBrush, pointsKeel);
// Закрашиваем корпус
g.FillEllipse(bodyBrush, _startPosX.Value, _startPosY.Value + 34, 20, 20);
@ -207,34 +199,14 @@ public class DrawingSeaplane
g.FillRectangle(bodyBrush, _startPosX.Value + 10, _startPosY.Value + 34, 160, 30);
// Закрашиваем триммера
g.FillEllipse(brBlack, _startPosX.Value, _startPosY.Value+30, 10, 10);
g.FillEllipse(brBlack, _startPosX.Value + 30, _startPosY.Value+30, 10, 10);
g.FillRectangle(brBlack, _startPosX.Value + 5, _startPosY.Value+30, 30, 10);
g.FillEllipse(brBlack, _startPosX.Value, _startPosY.Value + 30, 10, 10);
g.FillEllipse(brBlack, _startPosX.Value + 30, _startPosY.Value + 30, 10, 10);
g.FillRectangle(brBlack, _startPosX.Value + 5, _startPosY.Value + 30, 30, 10);
// Закрашиваем крыло
g.FillEllipse(brBlack, _startPosX.Value + 70, _startPosY.Value + 44, 6, 6);
g.FillEllipse(brBlack, _startPosX.Value + 130, _startPosY.Value + 44, 6, 6);
g.FillRectangle(brBlack, _startPosX.Value + 75, _startPosY.Value + 45, 60, 5);
// Поплавки
if (EntitySeaplane.Floats)
{
g.DrawLine(pen, _startPosX.Value + 125, _startPosY.Value + 65, _startPosX.Value + 125, _startPosY.Value + 80);
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 65, _startPosX.Value + 50, _startPosY.Value + 80);
g.FillEllipse(brBlack, _startPosX.Value + 40, _startPosY.Value + 75, 10, 10);
g.FillEllipse(brBlack, _startPosX.Value + 125, _startPosY.Value + 75, 10, 10);
g.FillRectangle(brBlack, _startPosX.Value + 45, _startPosY.Value + 75, 85, 10);
}
// Надувная лодка
if (EntitySeaplane.InflatableBoat)
{
g.FillEllipse(brRed, _startPosX.Value, _startPosY.Value + 60, 8, 8);
g.FillEllipse(brRed, _startPosX.Value + 165, _startPosY.Value + 60, 8, 8);
g.FillRectangle(brRed, _startPosX.Value + 4, _startPosY.Value + 60, 165, 8);
}
}
}

View File

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectSeaplane.Entities;
namespace ProjectSeaplane.Drawings;
public class DrawingSeaplane : DrawingPlane
{
public DrawingSeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool inflatableBoat) : base(190, 85)
{
EntityPlane = new EntitySeaplane(speed, weight, bodyColor, additionalColor, floats, inflatableBoat);
}
public override void DrawTransport(Graphics g)
{
if (EntityPlane == null || EntityPlane is not EntitySeaplane seaplane || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(seaplane.AdditionalColor);
Brush brBlack = new SolidBrush(Color.Black);
Brush brRed = new SolidBrush(Color.Red);
if (!seaplane.Floats)
{
// Рисуем переднее шасси с одним колесом
g.DrawEllipse(pen, _startPosX.Value + 140, _startPosY.Value + 75, 10, 10);
// Рисуем задние шасси с двумя колесами
g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 75, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 75, 10, 10);
// Рисуем ногу переднего шасси
g.DrawLine(pen, _startPosX.Value + 145, _startPosY.Value + 65, _startPosX.Value + 145, _startPosY.Value + 80);
// Рисуем ноги заднего шасси
g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 65, _startPosX.Value + 30, _startPosY.Value + 80);
}
base.DrawTransport(g);
// Поплавки
if (seaplane.Floats)
{
g.DrawLine(pen, _startPosX.Value + 125, _startPosY.Value + 65, _startPosX.Value + 125, _startPosY.Value + 80);
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 65, _startPosX.Value + 50, _startPosY.Value + 80);
g.FillEllipse(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 75, 10, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 125, _startPosY.Value + 75, 10, 10);
g.FillRectangle(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 75, 85, 10);
}
// Надувная лодка
if (seaplane.InflatableBoat)
{
g.FillEllipse(brRed, _startPosX.Value, _startPosY.Value + 60, 8, 8);
g.FillEllipse(brRed, _startPosX.Value + 165, _startPosY.Value + 60, 8, 8);
g.FillRectangle(brRed, _startPosX.Value + 4, _startPosY.Value + 60, 165, 8);
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.Entities;
public class EntityPlane
{
public int Speed { get; set; }
public double Weight { get; set; }
public Color BodyColor { get; private set; }
public double Step => Speed * 100 / Weight;
public EntityPlane(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.Entities;
public class EntitySeaplane : EntityPlane
{
public Color AdditionalColor { get; private set; }
public bool Floats { get; private set; }
public bool InflatableBoat { get; private set; }
public EntitySeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool inflatableBoat) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Floats = floats;
InflatableBoat = inflatableBoat;
}
}

View File

@ -1,34 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane;
public class EntitySeaplane
{
public int Speed { get; set; }
public double Weight { get; set; }
public Color BodyColor { get; private set; }
public Color AdditionalColor { get; private set; }
public double Step => Speed * 100 / Weight;
public bool Floats { get; private set; }
public bool InflatableBoat { get; private set; }
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool inflatableBoat)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Floats = floats;
InflatableBoat = inflatableBoat;
}
}

View File

@ -29,11 +29,12 @@
private void InitializeComponent()
{
pictureBoxSeaplane = new PictureBox();
buttonCreate = new Button();
buttonCreateSeaplane = new Button();
buttonLeft = new Button();
buttonRight = new Button();
buttonDown = new Button();
buttonUp = new Button();
buttonCreatePlane = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).BeginInit();
SuspendLayout();
//
@ -48,14 +49,14 @@
//
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 511);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
buttonCreateSeaplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateSeaplane.Location = new Point(12, 511);
buttonCreateSeaplane.Name = "buttonCreate";
buttonCreateSeaplane.Size = new Size(205, 23);
buttonCreateSeaplane.TabIndex = 1;
buttonCreateSeaplane.Text = "Создать Seaplane";
buttonCreateSeaplane.UseVisualStyleBackColor = true;
buttonCreateSeaplane.Click += buttonCreateSeaplane_Click;
//
// buttonLeft
//
@ -105,16 +106,28 @@
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// buttonCreatePlane
//
buttonCreatePlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreatePlane.Location = new Point(223, 511);
buttonCreatePlane.Name = "buttonCreatePlane";
buttonCreatePlane.Size = new Size(205, 23);
buttonCreatePlane.TabIndex = 6;
buttonCreatePlane.Text = "Создать Plane";
buttonCreatePlane.UseVisualStyleBackColor = true;
buttonCreatePlane.Click += buttonCreatePlane_Click;
//
// FormSeaplane
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(817, 546);
Controls.Add(buttonCreatePlane);
Controls.Add(buttonUp);
Controls.Add(buttonDown);
Controls.Add(buttonRight);
Controls.Add(buttonLeft);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateSeaplane);
Controls.Add(pictureBoxSeaplane);
Name = "FormSeaplane";
Text = "FormSeaplane";
@ -125,10 +138,11 @@
#endregion
private PictureBox pictureBoxSeaplane;
private Button buttonCreate;
private Button buttonCreateSeaplane;
private Button buttonLeft;
private Button buttonRight;
private Button buttonDown;
private Button buttonUp;
private Button buttonCreatePlane;
}
}

View File

@ -7,12 +7,13 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectSeaplane.Drawings;
namespace ProjectSeaplane
{
public partial class FormSeaplane : Form
{
private DrawingSeaplane? _drawingSeaplane;
private DrawingPlane? _drawingPlane;
public FormSeaplane()
{
@ -21,35 +22,48 @@ namespace ProjectSeaplane
private void Draw()
{
if (_drawingSeaplane == null)
if (_drawingPlane == null)
{
return;
}
Bitmap bmp = new(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingSeaplane.DrawTransport(gr);
_drawingPlane.DrawTransport(gr);
pictureBoxSeaplane.Image = bmp;
}
private void buttonCreate_Click(object sender, EventArgs e)
private void CreateObject(string type)
{
Random random = new();
_drawingSeaplane = new DrawingSeaplane();
_drawingSeaplane.Init(random.Next(100, 300), random.Next(1000, 3000),
switch (type)
{
case nameof(DrawingPlane):
_drawingPlane = new DrawingPlane(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawingSeaplane):
_drawingPlane = new DrawingSeaplane(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))
);
_drawingSeaplane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
_drawingSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
break;
default:
return;
}
_drawingPlane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
_drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateSeaplane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingSeaplane));
private void buttonCreatePlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingPlane));
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingSeaplane == null)
if (_drawingPlane == null)
{
return;
}
@ -59,16 +73,16 @@ namespace ProjectSeaplane
switch (name)
{
case "buttonUp":
result = _drawingSeaplane.MoveTransport(DirectionType.Up);
result = _drawingPlane.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawingSeaplane.MoveTransport(DirectionType.Down);
result = _drawingPlane.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawingSeaplane.MoveTransport(DirectionType.Left);
result = _drawingPlane.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawingSeaplane.MoveTransport(DirectionType.Right);
result = _drawingPlane.MoveTransport(DirectionType.Right);
break;
}