PIbd-21_Krasnikov_Lab1.base/WinFormsApp1/FormTractor.cs

76 lines
2.3 KiB
C#
Raw Permalink Normal View History

2022-11-13 20:39:23 +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 WinFormsApp1
{
public partial class FormTractor : Form
{
private TractorDraw _Tractor;
public FormTractor()
{
InitializeComponent();
}
private void Draw()
{
Bitmap bmp = new Bitmap(pictureBoxTractor.Width, pictureBoxTractor.Height);
Graphics gr = Graphics.FromImage(bmp);
_Tractor?.DrawEntity(gr);
pictureBoxTractor.Image = bmp;
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random random = new Random();
_Tractor = new TractorDraw();
_Tractor.Init(random.Next(100, 200), random.Next(2500, 5000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
_Tractor.SetPosition(random.Next(10, 50), random.Next(10, 50), pictureBoxTractor.Width, pictureBoxTractor.Height);
toolStripStatusLabelSpeed.Text = $"Скорость: {_Tractor.Tractor.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {_Tractor.Tractor.Weight}";
toolStripStatusLabelBodyColor.Text = $"Цвет: {_Tractor.Tractor.BodyColor.Name}";
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_Tractor?.MoveTransport(Direction.Up);
break;
case "buttonDown":
_Tractor?.MoveTransport(Direction.Down);
break;
case "buttonLeft":
_Tractor?.MoveTransport(Direction.Left);
break;
case "buttonRight":
_Tractor?.MoveTransport(Direction.Right);
break;
}
Draw();
}
private void pictureBoxTractor_Resize(object sender, EventArgs e)
{
_Tractor?.ChangeBorders(pictureBoxTractor.Width, pictureBoxTractor.Height);
Draw();
}
}
}