Приложение 2. Текст программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FORESTPORTED
{
public partial class Form1 : Form
{
List<Tree> Forest;
Graphics g;
public Form1()
{
InitializeComponent();
Forest = new List<Tree>();
g = this.CreateGraphics();
}
public float Lengther(Vector2 a, Vector2 b)
{
float c;
c = (float)Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
return c;
}
public List<Tree> Finder(object sender, Args e)
{
List<Tree> d = new List<Tree>();
foreach (Tree t in Forest)
{
if(e.position!=t.Position)
if (Lengther(e.position, t.Position) < 50)
d.Add(t);
}
return d;
}
public void addtree(Vector2 Pos, int type)
{
Tree t = new Tree("birch.png", Pos, type);
if (type == 2)
t = new Pinetree("pinetree.png", Pos, type);
if (type == 1)
t = new Birch("birch.png", Pos, type);
t.Event += new Tree.mydel(this.Finder);
Forest.Add(t);
}
private void timer1_Tick(object sender, EventArgs e)
{
Update1();
Draw();
}
public void Update1()
{
for (int i = 0; i < Forest.Count; i++)
{
if (Forest[i].die)
Forest.Remove(Forest[i]);
}
foreach (Tree t in Forest)
t.Update(0.01f);
}
public void Draw()
{
foreach (Tree t in Forest)
t.Draw(g);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
using (Font font1 = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel))
g.DrawString("Made by Rasstrigin A. II-42V", font1, Brushes.Black, new Point(100, 100));
}
if (e.KeyCode == Keys.Space)
{
Random r = new Random();
Random q = new Random();
addtree(new Vector2(r.Next(400), r.Next(200) + 300), q.Next(2));
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
addtree(new Vector2(e.X, e.Y), 1);
}
if (e.Button == MouseButtons.Right)
{
addtree(new Vector2(e.X, e.Y), 2);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public class Vector2
{
public float X;
public float Y;
public Vector2(float x, float y)
{
X = x;
Y = y;
}
}
public class Sprite
{
public Image texture;
public Vector2 Position;
public Sprite(string Name, Vector2 pos)
{
Position = pos;
texture = Image.FromFile(Name);
}
public virtual void Draw(Graphics g)
{
g.DrawImage(texture, new Point((int)Position.X,(int)Position.Y));
}
public void Update()
{
}
}
public class Args : EventArgs
{
public Vector2 position;
public Args()
: base()
{
}
}
public class Tree : Sprite
{
public int type;
public float Size;
public bool die;
public float Depth;
public delegate List<Tree> mydel(object sender, Args e);
public event mydel Event;
public Tree(string Name, Vector2 pos, int type1)
: base(Name, pos)
{
Size = 0.1f;
die = false;
}
public override void Draw(Graphics g)
{
Rectangle rect = new Rectangle((int)Position.X,(int)Position.Y, (int)(Size * texture.Width), (int)(Size * texture.Height));
g.DrawImage(texture, rect);
}
public virtual void Update(float speed)
{
Size += speed * 0.01f;
Args e = new Args();
e.position = Position;
int a = 0, b = 0;
foreach (Tree t in Event(this, e))
{
if (t.type == 1)
a++;
if (t.type == 2)
b++;
}
if (a > b && type == 2 || a < b && type == 1)
{
die = true;
}
if (this.Size > 0.5f)
{
die = true;
}
}
}
public class Birch : Tree
{
public Birch( string Name, Vector2 pos, int type1)
: base(Name, pos, type1)
{
type = type1;
texture = Image.FromFile("birch.png");
Bitmap bmp = (Bitmap)texture;
bmp.MakeTransparent(Color.Magenta);
texture = (Image)bmp;
}
}
public class Pinetree : Tree
{
public Pinetree(string Name, Vector2 pos, int type1)
: base(Name, pos, type1)
{
type = type1;
texture = Image.FromFile("pinetree.png");
Bitmap bmp = (Bitmap)texture;
bmp.MakeTransparent(Color.Magenta);
texture = (Image)bmp;
}
}
}