Абстрактный класс

This commit is contained in:
Yuee Shiness 2022-09-26 23:17:44 +04:00
parent 88725c4703
commit ee062b561c
7 changed files with 510 additions and 1 deletions

93
AirFighter/AbstractMap.cs Normal file
View File

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal abstract class AbstractMap
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected readonly Random _random = new();
protected readonly int _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawingObject drawingObject)
{
_width = width;
_height = height;
_drawingObject = drawingObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
//ПРОВЕРКА НА СТОЛКНОВЕНИЕ
if (true)
{
_drawingObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private bool SetObjectOnMap()
{
if (_drawingObject == null || _map == null)
{
return false;
}
int x = _random.Next(0, 10);
int y = _random.Next(0,10);
_drawingObject.SetObject(x, y, _width, _height);
//ПРОВЕРКА ЕСТЬ ЛИ УЖЕ НА ЭТОМ МЕСТЕ ОБЪЕКТ
return true;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawingObject == null || _map == null)
{
return bmp;
}
Graphics gr = Graphics.FromImage(bmp);
for (int i = 0; i < _map.GetLength(0); i++)
{
for(int j = 0; j < _map.GetLength(1);j++)
{
if (_map[i, j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i, j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawingObject.DrawingObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}
}

View File

@ -8,6 +8,7 @@ namespace AirFighter
{
internal enum Direction
{
None = 0,
Up =1,
Down = 2,
Left = 3,

206
AirFighter/FormMap.Designer.cs generated Normal file
View File

@ -0,0 +1,206 @@
namespace AirFighter
{
partial class FormMap
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBoxAirFighter = new System.Windows.Forms.PictureBox();
this.statusStripAircraft = new System.Windows.Forms.StatusStrip();
this.toolStripStatusSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusBodyColor = new System.Windows.Forms.ToolStripStatusLabel();
this.buttonCreate = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonCreateModification = new System.Windows.Forms.Button();
this.comboBoxMapSelection = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxAirFighter)).BeginInit();
this.statusStripAircraft.SuspendLayout();
this.SuspendLayout();
//
// pictureBoxAirFighter
//
this.pictureBoxAirFighter.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxAirFighter.Location = new System.Drawing.Point(0, 0);
this.pictureBoxAirFighter.Name = "pictureBoxAirFighter";
this.pictureBoxAirFighter.Size = new System.Drawing.Size(800, 428);
this.pictureBoxAirFighter.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxAirFighter.TabIndex = 0;
this.pictureBoxAirFighter.TabStop = false;
//
// statusStripAircraft
//
this.statusStripAircraft.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusSpeed,
this.toolStripStatusWeight,
this.toolStripStatusBodyColor});
this.statusStripAircraft.Location = new System.Drawing.Point(0, 428);
this.statusStripAircraft.Name = "statusStripAircraft";
this.statusStripAircraft.Size = new System.Drawing.Size(800, 22);
this.statusStripAircraft.TabIndex = 1;
//
// toolStripStatusSpeed
//
this.toolStripStatusSpeed.Name = "toolStripStatusSpeed";
this.toolStripStatusSpeed.Size = new System.Drawing.Size(42, 17);
this.toolStripStatusSpeed.Text = "Speed:";
//
// toolStripStatusWeight
//
this.toolStripStatusWeight.Name = "toolStripStatusWeight";
this.toolStripStatusWeight.Size = new System.Drawing.Size(48, 17);
this.toolStripStatusWeight.Text = "Weight:";
//
// toolStripStatusBodyColor
//
this.toolStripStatusBodyColor.Name = "toolStripStatusBodyColor";
this.toolStripStatusBodyColor.Size = new System.Drawing.Size(66, 17);
this.toolStripStatusBodyColor.Text = "BodyColor:";
//
// buttonCreate
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(12, 389);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(75, 23);
this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Create";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
//
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AirFighter.Properties.Resources.ArrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(702, 346);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 3;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AirFighter.Properties.Resources.ArrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(666, 382);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 4;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AirFighter.Properties.Resources.ArrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(738, 382);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 5;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AirFighter.Properties.Resources.ArrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(702, 382);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 6;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonCreateModification
//
this.buttonCreateModification.Location = new System.Drawing.Point(103, 389);
this.buttonCreateModification.Name = "buttonCreateModification";
this.buttonCreateModification.Size = new System.Drawing.Size(110, 23);
this.buttonCreateModification.TabIndex = 7;
this.buttonCreateModification.Text = "Modification";
this.buttonCreateModification.UseVisualStyleBackColor = true;
this.buttonCreateModification.Click += new System.EventHandler(this.buttonCreateModification_Click);
//
// comboBoxMapSelection
//
this.comboBoxMapSelection.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxMapSelection.FormattingEnabled = true;
this.comboBoxMapSelection.Items.AddRange(new object[] {
"1. Simple map"});
this.comboBoxMapSelection.Location = new System.Drawing.Point(12, 12);
this.comboBoxMapSelection.Name = "comboBoxMapSelection";
this.comboBoxMapSelection.Size = new System.Drawing.Size(121, 23);
this.comboBoxMapSelection.TabIndex = 8;
this.comboBoxMapSelection.SelectedIndexChanged += new System.EventHandler(this.comboBoxMapSelection_SelectedIndexChanged);
//
// FormMap
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.comboBoxMapSelection);
this.Controls.Add(this.buttonCreateModification);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxAirFighter);
this.Controls.Add(this.statusStripAircraft);
this.Name = "FormMap";
this.Text = "Map";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxAirFighter)).EndInit();
this.statusStripAircraft.ResumeLayout(false);
this.statusStripAircraft.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private PictureBox pictureBoxAirFighter;
private StatusStrip statusStripAircraft;
private ToolStripStatusLabel toolStripStatusSpeed;
private ToolStripStatusLabel toolStripStatusWeight;
private ToolStripStatusLabel toolStripStatusBodyColor;
private Button buttonCreate;
private Button buttonUp;
private Button buttonLeft;
private Button buttonRight;
private Button buttonDown;
private Button buttonCreateModification;
private ComboBox comboBoxMapSelection;
}
}

96
AirFighter/FormMap.cs Normal file
View File

@ -0,0 +1,96 @@
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 AirFighter
{
public partial class FormMap : Form
{
private AbstractMap _abstractMap;
public FormMap()
{
InitializeComponent();
_abstractMap = new SimpleMap();
}
private void SetData(DrawingAircraft plane)
{
toolStripStatusSpeed.Text = $"Speed: {plane.Plane.Speed}";
toolStripStatusWeight.Text = $"Weight: {plane.Plane.Weight}";
toolStripStatusBodyColor.Text = $"BodyColor: {plane.Plane.BodyColor.Name}";
pictureBoxAirFighter.Image = _abstractMap.CreateMap(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height, new DrawingObjectAircraft(plane) );
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random rnd = new Random();
var plane = new DrawingAircraft(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
SetData(plane);
}
private void buttonMove_Click(object sender, EventArgs e)
{
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
{
dir = Direction.Up;
}
break;
case "buttonDown":
{
dir = Direction.Down;
}
break;
case "buttonLeft":
{
dir = Direction.Left;
}
break;
case "buttonRight":
{
dir = Direction.Right;
}
break;
}
}
private void buttonCreateModification_Click(object sender, EventArgs e)
{
Random rnd = new Random();
var plane = new DrawingMilitaryAircraft(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)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData(plane);
}
private void comboBoxMapSelection_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxMapSelection.Text)
{
case "1. Simple map":
{
_abstractMap = new SimpleMap();
}
break;
}
}
}
}

63
AirFighter/FormMap.resx Normal file
View File

@ -0,0 +1,63 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="statusStripAircraft.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -11,7 +11,7 @@ namespace AirFighter
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new AirFighterForm());
Application.Run(new FormMap());
}
}
}

50
AirFighter/SimpleMap.cs Normal file
View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class SimpleMap : AbstractMap
{
Brush barrierColor = new SolidBrush(Color.Black);
Brush roadColor = new SolidBrush(Color.Gray);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_random.Next(0, 50) == 5)
{
_map[i, j] = _barrier;
}
else
{
_map[i, j] = _freeRoad;
}
}
}
}
}
}