using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectCruiser
{
public partial class FormCruiser : Form
{
///
/// Поле-объект для прорисовки объекта
///
private DrawningCruiser? _drawningCruiser;
public FormCruiser()
{
InitializeComponent();
}
///
/// Метод прорисовки машины
///
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
Bitmap bmp = new(pictureBox1.Width,
pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
pictureBox1.Image = bmp;
}
///
/// Обработка нажатия кнопки "Создать"
///
///
///
private void ButtonCreateCruiser_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCruiser = new DrawningCruiser();
_drawningCruiser.Init(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)), Convert.ToBoolean(random.Next(0, 2)));
_drawningCruiser.SetPictureSize(pictureBox1.Width,
pictureBox1.Height);
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
///
/// Перемещение объекта по форме (нажатие кнопок навигации)
///
///
///
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawningCruiser.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawningCruiser.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawningCruiser.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningCruiser.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}
}