PIbd-23_Dolgova_D.N._Warmly.../WarmlyShip/FormWarmlyShip.cs

91 lines
3.2 KiB
C#
Raw Normal View History

2023-11-24 22:12:36 +04:00
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 WarmlyShip
{
/// <summary>
/// Форма работы с объектом "Теплоход"
/// </summary>
public partial class FormWarmlyShip : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningWarmlyShip? _drawningWarmlyShip;
/// <summary>
/// Инициализация формы
/// </summary>
public FormWarmlyShip()
{
InitializeComponent();
}
/// <summary>
/// Метод прорисовки теплохода
/// </summary>
private void Draw()
{
if (_drawningWarmlyShip == null)
{
return;
}
Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningWarmlyShip.DrawTransport(gr);
pictureBoxWarmlyShip.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningWarmlyShip = new DrawningWarmlyShip();
_drawningWarmlyShip.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)),
pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
_drawningWarmlyShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Изменение размеров формы
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningWarmlyShip == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningWarmlyShip.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningWarmlyShip.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningWarmlyShip.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningWarmlyShip.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
}
}