Приведено к виду с практического занятия

This commit is contained in:
Oleg 2022-10-09 13:19:51 +04:00
parent 98281c3db4
commit c4de4279c9
10 changed files with 153 additions and 32 deletions

View File

@ -33,28 +33,83 @@ namespace Trolleybus
} }
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
{ {
// TODO проверка, что объект может переместится в требуемом направлении (float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
if (true)
for (int i = 0; i < _map.GetLength(0); i++)
{ {
_drawningObject.MoveObject(direction); for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier)
{
switch (direction)
{
case Direction.Up:
if (_size_y * (j + 1) >= topY - _drawningObject.Step && _size_y * (j + 1) < topY && _size_x * (i + 1) > leftX
&& _size_x * (i + 1) <= rightX)
{
return DrawMapWithObject();
} }
break;
case Direction.Down:
if (_size_y * j <= bottomY + _drawningObject.Step && _size_y * j > bottomY && _size_x * (i + 1) > leftX
&& _size_x * (i + 1) <= rightX)
{
return DrawMapWithObject();
}
break;
case Direction.Left:
if (_size_x * (i + 1) >= leftX - _drawningObject.Step && _size_x * (i + 1) < leftX && _size_y * (j + 1) < bottomY
&& _size_y * (j + 1) >= topY)
{
return DrawMapWithObject();
}
break;
case Direction.Right:
if (_size_x * i <= rightX + _drawningObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY
&& _size_y * (j + 1) >= topY)
{
return DrawMapWithObject();
}
break;
}
}
}
}
_drawningObject.MoveObject(direction);
return DrawMapWithObject(); return DrawMapWithObject();
} }
private bool SetObjectOnMap() private bool SetObjectOnMap()
{ {
(float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
if (_drawningObject == null || _map == null) if (_drawningObject == null || _map == null)
{ {
return false; return false;
} }
float airplaneWidth = rightX - leftX;
float airplaneHeight = bottomY - topY;
int x = _random.Next(0, 10); int x = _random.Next(0, 10);
int y = _random.Next(0, 10); int y = _random.Next(0, 10);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i, j] == _barrier)
{
if (x + airplaneWidth >= _size_x * i && x <= _size_x * i && y + airplaneHeight > _size_y * j && y <= _size_y * j)
{
return false;
}
}
}
}
_drawningObject.SetObject(x, y, _width, _height); _drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
return true; return true;
} }
private Bitmap DrawMapWithObject() private Bitmap DrawMapWithObject()
{ {
Bitmap bmp = new Bitmap(_width, _height); Bitmap bmp = new(_width, _height);
if (_drawningObject == null || _map == null) if (_drawningObject == null || _map == null)
{ {
return bmp; return bmp;

View File

@ -9,10 +9,10 @@ namespace Trolleybus
{ {
internal class DrawningSmallTrolleybus : DrawingTrolleybus internal class DrawningSmallTrolleybus : DrawingTrolleybus
{ {
public DrawningSmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool sportLine) : public DrawningSmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool horns, bool battary) :
base(speed, weight, bodyColor, 110, 60) base(speed, weight, bodyColor, 110, 60)
{ {
Trolleybus = new EntitySmallTrolleybus(speed, weight, bodyColor, dopColor, bodyKit, wing, sportLine); Trolleybus = new EntitySmallTrolleybus(speed, weight, bodyColor, dopColor, bodyKit, wing, horns, battary);
} }
public override void DrawTransport(Graphics g) public override void DrawTransport(Graphics g)
@ -29,8 +29,8 @@ namespace Trolleybus
Brush bWhite = new SolidBrush(Color.White); Brush bWhite = new SolidBrush(Color.White);
Pen WinBlue = new Pen(Color.Blue); Pen WinBlue = new Pen(Color.Blue);
_startPosY = _startPosY + 100; //_startPosY = _startPosY + 100;
base.DrawTransport(g);
if (smallTrolleybus.BodyKit) if (smallTrolleybus.BodyKit)
{ {
g.DrawRectangle(pen, _startPosX, _startPosY, 200, 50); g.DrawRectangle(pen, _startPosX, _startPosY, 200, 50);
@ -55,11 +55,11 @@ namespace Trolleybus
g.DrawEllipse(pen, _startPosX + 170, _startPosY + 40, 30, 30); g.DrawEllipse(pen, _startPosX + 170, _startPosY + 40, 30, 30);
} }
_startPosX += 10; //_startPosX += 10;
_startPosY += 5; //_startPosY += 5;
base.DrawTransport(g); //base.DrawTransport(g);
_startPosX -= 10; //_startPosX -= 10;
_startPosY -= 5; //_startPosY -= 5;
if (smallTrolleybus.Horns) if (smallTrolleybus.Horns)
{ {
@ -69,6 +69,11 @@ namespace Trolleybus
g.DrawRectangle(pen, _startPosX + 50, _startPosY - 10, 100, 10); g.DrawRectangle(pen, _startPosX + 50, _startPosY - 10, 100, 10);
} }
if (smallTrolleybus.Battary)
{
g.DrawRectangle(pen, _startPosX + 100, _startPosY, 10, 10);
}
if (smallTrolleybus.Wing) if (smallTrolleybus.Wing)
{ {
//g.FillRectangle(dopBrush, _startPosX, _startPosY + 5, 10, 50); //g.FillRectangle(dopBrush, _startPosX, _startPosY + 5, 10, 50);

View File

@ -29,6 +29,11 @@ namespace Trolleybus
_trolleybus?.MoveTransport(direction); _trolleybus?.MoveTransport(direction);
} }
public void nothing()
{
throw new NotImplementedException();
}
public void SetObject(int x, int y, int width, int height) public void SetObject(int x, int y, int width, int height)
{ {
// _trolleybus.SetPosition(x, y, startX, startY, width, height); // _trolleybus.SetPosition(x, y, startX, startY, width, height);

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Trolleybus
{
internal class DrawningObjectTrolleybus : IDrawningObject
{
private DrawingTrolleybus _trolleybus = null;
public DrawningObjectTrolleybus(DrawingTrolleybus trolleybus)
{
_trolleybus = trolleybus;
}
public float Step => _trolleybus?.Trolleybus?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _trolleybus?.GetCurrentPosition() ?? default;
}
public void MoveObject(Direction direction)
{
_trolleybus?.MoveTransport(direction);
}
public void nothing()
{
throw new NotImplementedException();
}
public void SetObject(int x, int y, int width, int height)
{
_trolleybus.SetPosition(x, y, width, height);
}
void IDrawningObject.DrawningObject(Graphics g)
{
_trolleybus.DrawTransport(g);
}
}
}

View File

@ -25,6 +25,7 @@ namespace Trolleybus
/// Признак наличия гоночной полосы /// Признак наличия гоночной полосы
/// </summary> /// </summary>
public bool Horns { get; private set; } public bool Horns { get; private set; }
public bool Battary { get; private set; }
/// <summary> /// <summary>
/// Инициализация свойств /// Инициализация свойств
/// </summary> /// </summary>
@ -35,13 +36,15 @@ namespace Trolleybus
/// <param name="bodyKit">Признак наличия обвеса</param> /// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="wing">Признак наличия антикрыла</param> /// <param name="wing">Признак наличия антикрыла</param>
/// <param name="horns">Признак наличия гоночной полосы</param> /// <param name="horns">Признак наличия гоночной полосы</param>
public EntitySmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool horns) : /// /// <param name="battary">Признак наличия гоночной полосы</param>
public EntitySmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool horns, bool battary) :
base(speed, weight, bodyColor) base(speed, weight, bodyColor)
{ {
DopColor = dopColor; DopColor = dopColor;
BodyKit = bodyKit; BodyKit = bodyKit;
Wing = wing; Wing = wing;
Horns = horns; Horns = horns;
Battary = battary;
} }
} }
} }

View File

@ -31,10 +31,15 @@ namespace Trolleybus
private void SetData() private void SetData()
{ {
Bitmap bmp = new Bitmap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); Random rnd = new Random();
Graphics gr = Graphics.FromImage(bmp); //Bitmap bmp = new Bitmap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
_trolleybus?.DrawTransport(gr); //Graphics gr = Graphics.FromImage(bmp);
pictureBoxTrolleybus.Image = bmp; //_trolleybus?.DrawTransport(gr);
//pictureBoxTrolleybus.Image = bmp;
_trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}";
toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}";
} }
/// <summary> /// <summary>
@ -48,10 +53,11 @@ namespace Trolleybus
Random rnd = new Random(); Random rnd = new Random();
_trolleybus = new DrawingTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); _trolleybus = new DrawingTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
// _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), Location.X, Location.Y, pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); // _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), Location.X, Location.Y, pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
_trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); //_trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}"; //toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}"; //toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}";
toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}"; //toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}";
SetData();
Draw(); Draw();
} }
@ -88,7 +94,7 @@ namespace Trolleybus
_trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), _trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), 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)), Convert.ToBoolean(rnd.Next(0, 2))); Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData(); SetData();
Draw(); Draw();
} }

View File

@ -4,17 +4,16 @@ using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Numerics;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace Trolleybus namespace Trolleybus
{ {
public partial class FormMap : Form public partial class FormMap : Form
{ {
private AbstractMap _abstractMap; private AbstractMap _abstractMap;
public FormMap() public FormMap()
{ {
InitializeComponent(); InitializeComponent();
@ -30,7 +29,7 @@ namespace Trolleybus
toolStripStatusLabelWeight.Text = $"Вес: {trolleybus.Trolleybus.Weight}"; toolStripStatusLabelWeight.Text = $"Вес: {trolleybus.Trolleybus.Weight}";
toolStripStatusLabelBodyColor.Text = $"Цвет: {trolleybus.Trolleybus.BodyColor.Name}"; toolStripStatusLabelBodyColor.Text = $"Цвет: {trolleybus.Trolleybus.BodyColor.Name}";
pictureBoxTrolleybus.Image = _abstractMap.CreateMap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height, pictureBoxTrolleybus.Image = _abstractMap.CreateMap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height,
new DrawningObject(trolleybus)); new DrawningObjectTrolleybus(trolleybus));
} }
/// <summary> /// <summary>
/// Обработка нажатия кнопки "Создать" /// Обработка нажатия кнопки "Создать"
@ -81,7 +80,7 @@ namespace Trolleybus
var trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), var trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), 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)), Convert.ToBoolean(rnd.Next(0, 2))); Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData(trolleybus); SetData(trolleybus);
} }
/// <summary> /// <summary>

View File

@ -12,7 +12,7 @@ namespace Trolleybus
/// <summary> /// <summary>
/// Шаг перемещения объекта /// Шаг перемещения объекта
/// </summary> /// </summary>
float Step { get; } public float Step { get; }
/// <summary> /// <summary>
/// Установка позиции объекта /// Установка позиции объекта
/// </summary> /// </summary>
@ -36,6 +36,7 @@ namespace Trolleybus
/// Получение текущей позиции объекта /// Получение текущей позиции объекта
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
void nothing();
(float Left, float Right, float Top, float Bottom) GetCurrentPosition(); (float Left, float Right, float Top, float Bottom) GetCurrentPosition();
} }
} }

View File

@ -16,7 +16,7 @@ namespace Trolleybus
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); Application.Run(new FormMap());
} }
} }
} }

View File

@ -52,6 +52,7 @@
<Compile Include="DrawingSmallTrolleybus.cs" /> <Compile Include="DrawingSmallTrolleybus.cs" />
<Compile Include="DrawingTrolleybus.cs" /> <Compile Include="DrawingTrolleybus.cs" />
<Compile Include="DrawningObject.cs" /> <Compile Include="DrawningObject.cs" />
<Compile Include="DrawningObjectTrolleybus.cs" />
<Compile Include="EntitySmallTrolleybus.cs" /> <Compile Include="EntitySmallTrolleybus.cs" />
<Compile Include="EntityTrolleybus.cs" /> <Compile Include="EntityTrolleybus.cs" />
<Compile Include="Form1.cs"> <Compile Include="Form1.cs">