-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
82 lines (75 loc) · 2.36 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 GameOfLife
{
public partial class Form1 : Form
{
private Graphics graphics;
private int resolution;
private GameEngine gameEngine;
public Form1()
{
InitializeComponent();
}
private void StartGame()
{
if (timer1.Enabled) return;
nudResolution.Enabled = false;
nudDensity.Enabled = false;
resolution = (int) nudResolution.Value;
gameEngine = new GameEngine(
pictureBox1.Height / resolution,
pictureBox1.Width / resolution,
(int)nudDensity.Value
);
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
graphics = Graphics.FromImage(pictureBox1.Image);
timer1.Start();
}
void StopGame()
{
if (!timer1.Enabled) return;
nudResolution.Enabled = true;
nudDensity.Enabled = true;
timer1.Stop();
}
private void UpdateGen()
{
graphics.Clear(Color.Black);
var field = gameEngine.GetCurrentGen();
for (int x = 0; x < field.GetLength(0); x++)
{
for (int y = 0; y < field.GetLength(1); y++)
{
Brush color;
if (field[x, y] is GrassCell) color = Brushes.Green;
else if (field[x, y] is GrassEaterCell) color = Brushes.Yellow;
else if (field[x, y] is PredatorCell) color = Brushes.Crimson;
else color = Brushes.Gray;
graphics.FillRectangle(color, x * resolution, y * resolution, resolution, resolution);
}
}
pictureBox1.Refresh();
gameEngine.NextGen();
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateGen();
}
private void bStart_Click(object sender, EventArgs e)
{
StartGame();
}
private void bStop_Click(object sender, EventArgs e)
{
StopGame();
}
}
}