Сделанная вторая лабороторная
This commit is contained in:
parent
e5d7602c05
commit
81268266be
@ -1,113 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat
|
||||
{
|
||||
internal class DrawningSailBoat
|
||||
{
|
||||
public EntitySailBoat? EntitySailBoat { get; private set; }
|
||||
|
||||
private int _pictureWidth;
|
||||
|
||||
private int _pictureHeight;
|
||||
|
||||
private int _startPosX;
|
||||
|
||||
private int _startPosY;
|
||||
|
||||
private readonly int _boatWidth = 180;
|
||||
|
||||
private readonly int _boatHeight = 125;
|
||||
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool rainforcedBody, int width, int height)
|
||||
{
|
||||
if (width < _boatWidth) { return false; }
|
||||
if (height < _boatHeight) { return false; }
|
||||
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntitySailBoat = new EntitySailBoat();
|
||||
EntitySailBoat.Init(speed, weight, bodyColor, additionalColor, sail, rainforcedBody);
|
||||
return true;
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x < 0) { x = 0; }
|
||||
else if (x > _pictureWidth) { x = _pictureWidth; }
|
||||
if (y < 0) { y = 0; }
|
||||
else if (y > _pictureHeight) { y = _pictureHeight; }
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntitySailBoat == null) return;
|
||||
switch (direction)
|
||||
{
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - EntitySailBoat.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntitySailBoat.Step;
|
||||
}
|
||||
break;
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + _boatWidth + EntitySailBoat.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntitySailBoat.Step;
|
||||
}
|
||||
break;
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - EntitySailBoat.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntitySailBoat.Step;
|
||||
}
|
||||
break;
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _boatHeight + EntitySailBoat.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntitySailBoat.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntitySailBoat == null) return;
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(EntitySailBoat.AdditionalColor);
|
||||
|
||||
//границы корабля
|
||||
g.DrawLine(pen, _startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60);
|
||||
g.DrawLine(pen, _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90);
|
||||
g.DrawLine(pen, _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120);
|
||||
g.DrawLine(pen, _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120);
|
||||
g.DrawLine(pen, _startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60);
|
||||
g.FillEllipse(additionalBrush, _startPosX + 15, _startPosY + 65, 95, 50);
|
||||
g.DrawEllipse(pen, _startPosX + 15, _startPosY + 65, 95, 50);
|
||||
|
||||
if (EntitySailBoat.RainforcedBody)
|
||||
{
|
||||
Brush rainforcedBody = new SolidBrush(Color.DarkGray);
|
||||
g.FillRectangle(rainforcedBody, _startPosX + 2, _startPosY + 65, 10, 50);
|
||||
g.DrawRectangle(pen, _startPosX + 2, _startPosY + 65, 10, 50);
|
||||
g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 53, 90, 10);
|
||||
g.DrawRectangle(pen, _startPosX + 15, _startPosY + 53, 90, 10);
|
||||
g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 118, 90, 10);
|
||||
g.DrawRectangle(pen, _startPosX + 15, _startPosY + 118, 90, 10);
|
||||
}
|
||||
|
||||
if (EntitySailBoat.Sail)
|
||||
{
|
||||
Brush mast = new SolidBrush(Color.Brown);
|
||||
g.FillRectangle(mast, _startPosX + 60, _startPosY , 4, 90);
|
||||
g.DrawRectangle(pen, _startPosX + 60, _startPosY , 4, 90);
|
||||
g.DrawLine(pen, _startPosX + 25, _startPosY + 20, _startPosX + 100, _startPosY);
|
||||
g.DrawLine(pen, _startPosX + 100, _startPosY, _startPosX + 100, _startPosY + 50);
|
||||
g.DrawLine(pen, _startPosX + 25, _startPosY + 70, _startPosX + 100, _startPosY + 50);
|
||||
g.DrawLine(pen, _startPosX + 25, _startPosY + 70, _startPosX + 25, _startPosY + 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat
|
||||
{
|
||||
internal class EntitySailBoat
|
||||
{
|
||||
public int Speed { get; private set; }
|
||||
|
||||
public double Weight { get; private set; }
|
||||
|
||||
public Color BodyColor { get; private set; }
|
||||
|
||||
public Color AdditionalColor { get; private set; }
|
||||
|
||||
public bool Sail { get; private set; }
|
||||
|
||||
public bool RainforcedBody { get; private set; }
|
||||
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool rainforcedBody)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Sail = sail;
|
||||
RainforcedBody = rainforcedBody;
|
||||
}
|
||||
}
|
||||
}
|
45
SailBoat/SailBoat/FormSailBoat.Designer.cs
generated
45
SailBoat/SailBoat/FormSailBoat.Designer.cs
generated
@ -34,6 +34,9 @@
|
||||
buttonLeft = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonRight = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStep = new Button();
|
||||
buttonCreateBoat = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxSailBoat).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -50,11 +53,11 @@
|
||||
// buttonCreateSailBoat
|
||||
//
|
||||
buttonCreateSailBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateSailBoat.Location = new Point(12, 419);
|
||||
buttonCreateSailBoat.Location = new Point(12, 407);
|
||||
buttonCreateSailBoat.Name = "buttonCreateSailBoat";
|
||||
buttonCreateSailBoat.Size = new Size(96, 30);
|
||||
buttonCreateSailBoat.Size = new Size(96, 42);
|
||||
buttonCreateSailBoat.TabIndex = 3;
|
||||
buttonCreateSailBoat.Text = "Создать";
|
||||
buttonCreateSailBoat.Text = "Создать Парусник";
|
||||
buttonCreateSailBoat.UseVisualStyleBackColor = true;
|
||||
buttonCreateSailBoat.Click += buttonCreateSailBoat_Click;
|
||||
//
|
||||
@ -106,11 +109,44 @@
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += buttonMove_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "Движение в центр", "Движение в правый нижний угол" });
|
||||
comboBoxStrategy.Location = new Point(644, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(228, 23);
|
||||
comboBoxStrategy.TabIndex = 11;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Location = new Point(797, 41);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(75, 23);
|
||||
buttonStep.TabIndex = 12;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += buttonStep_Click;
|
||||
//
|
||||
// buttonCreateBoat
|
||||
//
|
||||
buttonCreateBoat.Location = new Point(114, 407);
|
||||
buttonCreateBoat.Name = "buttonCreateBoat";
|
||||
buttonCreateBoat.Size = new Size(75, 42);
|
||||
buttonCreateBoat.TabIndex = 13;
|
||||
buttonCreateBoat.Text = "Создать лодку";
|
||||
buttonCreateBoat.UseVisualStyleBackColor = true;
|
||||
buttonCreateBoat.Click += buttonCreateBoat_Click;
|
||||
//
|
||||
// FormSailBoat
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(884, 461);
|
||||
Controls.Add(buttonCreateBoat);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonUp);
|
||||
@ -133,5 +169,8 @@
|
||||
private Button buttonLeft;
|
||||
private Button buttonUp;
|
||||
private Button buttonRight;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStep;
|
||||
private Button buttonCreateBoat;
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
using SailBoat.DrawningObjects;
|
||||
using SailBoat.MovementStrategy;
|
||||
|
||||
namespace SailBoat
|
||||
{
|
||||
public partial class FormSailBoat : Form
|
||||
@ -9,10 +12,10 @@ namespace SailBoat
|
||||
}
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningSailBoat == null) { return; }
|
||||
if (_drawningBoat == null) { return; }
|
||||
Bitmap bmp = new(pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningSailBoat.DrawTransport(gr);
|
||||
_drawningBoat.DrawTransport(gr);
|
||||
pictureBoxSailBoat.Image = bmp;
|
||||
}
|
||||
|
||||
@ -24,26 +27,61 @@ namespace SailBoat
|
||||
_drawningSailBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonCreateBoat_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningBoat = new DrawningBoat(random.Next(300, 700), random.Next(2000, 4000), Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
|
||||
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningSailBoat == null) { return; }
|
||||
if (_drawningBoat == null) { return; }
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
_drawningSailBoat.MoveTransport(DirectionType.Up);
|
||||
_drawningBoat.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
_drawningSailBoat.MoveTransport(DirectionType.Down);
|
||||
_drawningBoat.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonRight":
|
||||
_drawningSailBoat.MoveTransport(DirectionType.Right);
|
||||
_drawningBoat.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
_drawningSailBoat.MoveTransport(DirectionType.Left);
|
||||
_drawningBoat.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningBoat == null) return;
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
|
||||
};
|
||||
if (_abstractStrategy == null) return;
|
||||
_abstractStrategy.SetData(new DrawningObjectBoat(_drawningBoat), pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null) return;
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user