Лабораторная работа №1

This commit is contained in:
Almaz 2024-04-09 10:09:18 +04:00
parent 9dd23a6995
commit 32dc88b8a9
3 changed files with 74 additions and 86 deletions

View File

@ -78,7 +78,7 @@ public class DrawingCruiser
public bool MoveTransport(DirectionType direction)
{
if (EntityCruiser == null || _startPosX.HasValue || _startPosY.HasValue)
if (EntityCruiser == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
@ -99,8 +99,16 @@ public class DrawingCruiser
}
return true;
case DirectionType.Right:
if (_startPosX.Value - EntityCruiser.Step > 0)
{
_startPosX += (int)EntityCruiser.Step;
}
return true;
case DirectionType.Down:
if (_startPosY.Value - EntityCruiser.Step > 0)
{
_startPosY += (int)EntityCruiser.Step;
}
return true;
default:
return false;
@ -109,7 +117,7 @@ public class DrawingCruiser
public void DrawTransport(Graphics g)
{
if (EntityCruiser == null || !_startPosX.HasValue || _startPosY.HasValue)
if (EntityCruiser == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
@ -118,41 +126,7 @@ public class DrawingCruiser
Brush additionalBrush = new SolidBrush(EntityCruiser.AdditionalColor);
if (EntityCruiser.BodyKit)
{
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 40, 20, 20);
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 10, 20, 40);
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value, 15, 15);
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 45, 15, 15);
g.FillEllipse(additionalBrush, _startPosX.Value + 90, _startPosY.Value, 20, 20);
g.FillEllipse(additionalBrush, _startPosX.Value + 90, _startPosY.Value + 40, 20, 20);
g.FillRectangle(additionalBrush, _startPosX.Value + 90, _startPosY.Value + 10, 20, 40);
g.FillRectangle(additionalBrush, _startPosX.Value + 90, _startPosY.Value + 1, 15, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 90, _startPosY.Value + 45, 15, 15);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value, 20, 20);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 40, 20, 20);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 10, 20, 40);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value, 14, 15);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 45, 14, 15);
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value, 20, 20);
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 40, 20, 20);
g.FillRectangle(additionalBrush, _startPosX.Value + 1, _startPosY.Value + 10, 25, 40);
g.FillRectangle(additionalBrush, _startPosX.Value + 5, _startPosY.Value + 1, 15, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 5, _startPosY.Value + 45, 15, 15);
g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value, 39, 15);
g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value + 45, 39, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 1, 40, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 45, 40, 15);
}
//Границы крейсера
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 5, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 35, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 5, 20, 20);
@ -161,36 +135,14 @@ public class DrawingCruiser
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 4, 70, 52);
// задние фары
Brush brRed = new SolidBrush(Color.Red);
g.FillEllipse(brRed, _startPosX.Value + 10, _startPosY.Value + 5, 20, 20);
g.FillEllipse(brRed, _startPosX.Value + 10, _startPosY.Value + 35, 20, 20);
//передние фары
Brush brYellow = new SolidBrush(Color.Yellow);
g.FillEllipse(brYellow, _startPosX.Value + 80, _startPosY.Value + 5, 20, 20);
g.FillEllipse(brYellow, _startPosX.Value + 80, _startPosY.Value + 35, 20, 20);
//кузов
//кузов крейсера
Brush br = new SolidBrush(EntityCruiser.BodyColor);
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 20, _startPosY.Value + 5, 70, 50);
//стекла
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX.Value + 70, _startPosY.Value + 10, 5, 40);
g.FillRectangle(brBlue, _startPosX.Value + 30, _startPosY.Value + 10, 5, 40);
g.FillRectangle(brBlue, _startPosX.Value + 35, _startPosY.Value + 8, 35, 2);
g.FillRectangle(brBlue, _startPosX.Value + 35, _startPosY.Value + 51, 35, 2);
//выделяем рамкой крышу
g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value + 10, 35, 40);
g.DrawRectangle(pen, _startPosX.Value + 75, _startPosY.Value + 15, 25, 30);
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 15, 15, 30);
// спортивная линия
if (EntityCruiser.Armor)
// оружие крейсера
if (EntityCruiser.Weapon)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 23, 25, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 23, 35, 15);
@ -198,12 +150,5 @@ public class DrawingCruiser
}
// крыло
if (EntityCruiser.Weapon)
{
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 5, 10, 50);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 5, 10, 50);
}
}
}

View File

@ -29,7 +29,7 @@
private void InitializeComponent()
{
pictureBoxCruiser = new PictureBox();
buttonCreate = new Button();
buttonCreateCruiser = new Button();
buttonDown = new Button();
buttonUp = new Button();
buttonRight = new Button();
@ -46,16 +46,16 @@
pictureBoxCruiser.TabIndex = 0;
pictureBoxCruiser.TabStop = false;
//
// buttonCreate
// buttonCreateCruiser
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 409);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click;
buttonCreateCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateCruiser.Location = new Point(12, 409);
buttonCreateCruiser.Name = "buttonCreateCruiser";
buttonCreateCruiser.Size = new Size(94, 29);
buttonCreateCruiser.TabIndex = 1;
buttonCreateCruiser.Text = "Создать";
buttonCreateCruiser.UseVisualStyleBackColor = true;
buttonCreateCruiser.Click += ButtonCreateCruiser_Click;
//
// buttonDown
//
@ -67,6 +67,7 @@
buttonDown.Size = new Size(35, 35);
buttonDown.TabIndex = 2;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
// buttonUp
//
@ -78,6 +79,7 @@
buttonUp.Size = new Size(35, 35);
buttonUp.TabIndex = 3;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// buttonRight
//
@ -89,6 +91,7 @@
buttonRight.Size = new Size(35, 35);
buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// buttonLeft
//
@ -100,6 +103,7 @@
buttonLeft.Size = new Size(35, 35);
buttonLeft.TabIndex = 5;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// FormCruiser
//
@ -110,7 +114,7 @@
Controls.Add(buttonRight);
Controls.Add(buttonUp);
Controls.Add(buttonDown);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateCruiser);
Controls.Add(pictureBoxCruiser);
Name = "FormCruiser";
Text = "Крейсер";
@ -121,7 +125,7 @@
#endregion
private PictureBox pictureBoxCruiser;
private Button buttonCreate;
private Button buttonCreateCruiser;
private Button buttonDown;
private Button buttonUp;
private Button buttonRight;

View File

@ -9,7 +9,20 @@
InitializeComponent();
}
private void ButtonCreate_Click(object sender, EventArgs e)
private void Draw()
{
if (_drawingCruiser == null)
{
return;
}
Bitmap bmp = new(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingCruiser.DrawTransport(gr);
pictureBoxCruiser.Image = bmp;
}
private void ButtonCreateCruiser_Click(object sender, EventArgs e)
{
Random random = new();
_drawingCruiser = new DrawingCruiser();
@ -25,10 +38,36 @@
_drawingCruiser.SetPictireSize(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
_drawingCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
Bitmap bmp = new(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingCruiser.DrawTransport(gr);
pictureBoxCruiser.Image = bmp;
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if(_drawingCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch(name) {
case "buttonUp":
result = _drawingCruiser.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawingCruiser.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawingCruiser.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawingCruiser.MoveTransport(DirectionType.Right);
break;
}
if (result) {
Draw();
}
}
}
}