constructors+extended object

This commit is contained in:
Valitov.Damir 2023-03-18 02:05:18 +04:00
parent 27b3072270
commit 7f4ef450a3
6 changed files with 214 additions and 16 deletions

View File

@ -29,7 +29,7 @@ namespace Sailboat
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <returns></returns>
public void Init(int speed, float weight, Color bodyColor)
public Boat(int speed, float weight, Color bodyColor)
{
Random rnd = new();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;

View File

@ -39,6 +39,7 @@ namespace Sailboat
this.btn_down = new System.Windows.Forms.Button();
this.btn_up = new System.Windows.Forms.Button();
this.btn_left = new System.Windows.Forms.Button();
this.btn_create_sailboat = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).BeginInit();
this.SuspendLayout();
@ -144,11 +145,22 @@ namespace Sailboat
this.btn_left.UseVisualStyleBackColor = true;
this.btn_left.Click += new System.EventHandler(this.btn_move_Click);
//
// btn_create_sailboat
//
this.btn_create_sailboat.Location = new System.Drawing.Point(113, 13);
this.btn_create_sailboat.Name = "btn_create_sailboat";
this.btn_create_sailboat.Size = new System.Drawing.Size(150, 29);
this.btn_create_sailboat.TabIndex = 8;
this.btn_create_sailboat.Text = "Создать парусник";
this.btn_create_sailboat.UseVisualStyleBackColor = true;
this.btn_create_sailboat.Click += new System.EventHandler(this.btn_create_sailboat_Click);
//
// BoatForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.btn_create_sailboat);
this.Controls.Add(this.btn_left);
this.Controls.Add(this.btn_up);
this.Controls.Add(this.btn_down);
@ -180,6 +192,7 @@ namespace Sailboat
private System.Windows.Forms.Button btn_down;
private System.Windows.Forms.Button btn_up;
private System.Windows.Forms.Button btn_left;
private System.Windows.Forms.Button btn_create_sailboat;
}
}

View File

@ -12,7 +12,7 @@ namespace Sailboat
{
public partial class BoatForm : Form
{
private DrawningBoat _boat;
private DrawingBoat _boat;
public BoatForm()
{
InitializeComponent();
@ -25,18 +25,31 @@ namespace Sailboat
private void btn_create_Click(object sender, EventArgs e)
{
Random rnd = new();
_boat = new DrawningBoat();
_boat.Init(rnd.Next(100, 300), rnd.Next(1000, 2000),
_boat = new DrawingBoat(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_boat.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100),
pictureBoxBoat.Width, pictureBoxBoat.Height);
toolStripLabel_color.Text = $"Скорость: {_boat.Boat.Speed}";
toolStripLabel_weight.Text = $"Вес: {_boat.Boat.Weight}";
toolStripLabel_color.Text = $"Цвет: { _boat.Boat.BodyColor.Name}";
SetData();
Draw();
}
/// <summary>
/// Изменение размеров формы
/// Обработка нажатия кнопки "Создать парусник"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_create_sailboat_Click(object sender, EventArgs e)
{
Random rnd = new();
_boat = new DrawingSailboat(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
_boat.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100),
pictureBoxBoat.Width, pictureBoxBoat.Height);
SetData();
Draw();
}
/// <summary>
/// Обработка нажатия стрелок
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
@ -83,5 +96,16 @@ namespace Sailboat
_boat?.ChangeBorders(pictureBoxBoat.Width, pictureBoxBoat.Height);
Draw();
}
/// <summary>
/// Метод прорисовки машины
/// </summary>
private void SetData()
{
Random rnd = new();
toolStripLabel_color.Text = $"Скорость: {_boat.Boat.Speed}";
toolStripLabel_weight.Text = $"Вес: {_boat.Boat.Weight}";
toolStripLabel_color.Text = $"Цвет: { _boat.Boat.BodyColor.Name}";
}
}
}

View File

@ -10,20 +10,20 @@ namespace Sailboat
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
internal class DrawningBoat
internal class DrawingBoat
{
/// <summary>
/// Класс-сущность
/// </summary>
public Boat Boat{ private set; get; }
public Boat Boat{ protected set; get; }
/// <summary>`
/// Левая координата отрисовки автомобиля
/// </summary>
private float _startPosX;
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки автомобиля
/// </summary>
private float _startPosY;
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -46,10 +46,16 @@ namespace Sailboat
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
public void Init(int speed, float weight, Color bodyColor)
public DrawingBoat(int speed, float weight, Color bodyColor)
{
Boat = new Boat();
Boat.Init(speed, weight, bodyColor);
Boat = new Boat(speed, weight, bodyColor);
}
public DrawingBoat(int speed, float weight, Color bodyColor, int boatHeight, int boatWith):
this(speed, weight, bodyColor)
{
_boatHeight = boatHeight;
_boatWidth = boatWith;
}
/// <summary>
/// Установка позиции автомобиля
@ -115,7 +121,7 @@ namespace Sailboat
/// Отрисовка автомобиля
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)

120
Sailboat/DrawingSailboat.cs Normal file
View File

@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
class DrawingSailboat : DrawingBoat
{
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="sail">Признак наличия паруса</param>
/// <param name="extendedBody">Признак наличия усиленного корпуса</param>
public DrawingSailboat(int speed, float weight, Color bodyColor, bool sail, bool extendedBody) :
base(speed, weight, bodyColor, 50, 120)
{
Boat = new Sailboat(speed, weight, bodyColor, sail, extendedBody);
}
public override void DrawTransport(Graphics g)
{
if (Boat is not Sailboat sailboat)
{
return;
}
Pen pen = new(Color.Black);
Brush brush = new SolidBrush(Color.Black);
_startPosY += 40;
_startPosX += 20;
base.DrawTransport(g);
_startPosY -= 40;
_startPosX -= 20;
if (sailboat.Sail)
{
g.DrawLine(pen, _startPosX + 40, _startPosY, _startPosX + 60, _startPosY + 60);
g.DrawLine(pen, _startPosX + 40, _startPosY, _startPosX + 70, _startPosY + 30);
g.DrawLine(pen, _startPosX + 70, _startPosY + 30, _startPosX + 60, _startPosY + 55);
}
if (sailboat.ExtendedBody)
{
//корма
g.FillRectangle(brush, _startPosX + 15, _startPosY + 38, 70, 5);
g.FillRectangle(brush, _startPosX + 15, _startPosY + 78, 70, 5);
g.FillRectangle(brush, _startPosX + 15, _startPosY + 38, 5, 40);
//усиленный нос
g.FillRectangle(brush, _startPosX + 133, _startPosY + 55, 10, 10);
}
/*
Brush dopBrush = new SolidBrush(sportCar.DopColor);
if (sportCar.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(dopBrush, _startPosX + 90, _startPosY, 20, 20);
g.FillEllipse(dopBrush, _startPosX + 90, _startPosY + 40, 20, 20);
g.FillRectangle(dopBrush, _startPosX + 90, _startPosY + 10, 20, 40);
g.FillRectangle(dopBrush, _startPosX + 90, _startPosY + 1, 15, 15);
g.FillRectangle(dopBrush, _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(dopBrush, _startPosX, _startPosY, 20, 20);
g.FillEllipse(dopBrush, _startPosX, _startPosY + 40, 20, 20);
g.FillRectangle(dopBrush, _startPosX + 1, _startPosY + 10, 25, 40);
g.FillRectangle(dopBrush, _startPosX + 5, _startPosY + 1, 15, 15);
g.FillRectangle(dopBrush, _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(dopBrush, _startPosX + 35, _startPosY + 1, 40, 15);
g.FillRectangle(dopBrush, _startPosX + 35, _startPosY + 45, 40, 15);
}
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
if (sportCar.SportLine)
{
g.FillRectangle(dopBrush, _startPosX + 76, _startPosY + 23, 24, 15);
g.FillRectangle(dopBrush, _startPosX + 36, _startPosY + 23, 34, 15);
g.FillRectangle(dopBrush, _startPosX + 11, _startPosY + 23, 14, 15);
}
if (sportCar.Wing)
{
g.FillRectangle(dopBrush, _startPosX, _startPosY + 5, 10, 50);
g.DrawRectangle(pen, _startPosX, _startPosY + 5, 10, 50);
}
*/
}
}
}

35
Sailboat/Sailboat.cs Normal file
View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat
{
internal class Sailboat : Boat
{
/// <summary>
/// Признак наличия усиленного корпуса
/// </summary>
public bool ExtendedBody { get; private set; }
/// <summary>
/// Признак наличия паруса
/// </summary>
public bool Sail { get; private set; }
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="sail">Признак наличия паруса</param>
/// <param name="extendedBody">Признак наличия усиленного корпуса</param>
public Sailboat(int speed, float weight, Color bodyColor, bool sail, bool extendedBody) :
base(speed, weight, bodyColor)
{
Sail = sail;
ExtendedBody = extendedBody;
}
}
}