PIBD-11_Basalov_A.D_Simple/ProjectElectricLocomotive/FormlectricLocomotive.cs
2024-02-19 08:25:06 +03:00

85 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectElectricLocomotive
{
public partial class FormlectricLocomotive : Form
{
private void Draw()
{
if (_drawnningElectricLocomotive == null)
{
return;
}
Bitmap bmp = new(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawnningElectricLocomotive.DrawTransport(gr);
pictureBoxElectricLocomotive.Image = bmp;
}
private DrawningElectricLocomotive? _drawnningElectricLocomotive;
public FormlectricLocomotive()
{
InitializeComponent();
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawnningElectricLocomotive = new DrawningElectricLocomotive();
_drawnningElectricLocomotive.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)));
_drawnningElectricLocomotive.SetPictureSize(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
_drawnningElectricLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawnningElectricLocomotive == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawnningElectricLocomotive.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawnningElectricLocomotive.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawnningElectricLocomotive.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawnningElectricLocomotive.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}
}