-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameGUI.cs
180 lines (156 loc) · 5.95 KB
/
GameGUI.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace Life
{
public partial class GameGUI : Form
{
private const int GRID_WIDTH = 450;
private const int BUTTON_WIDTH = 25;
private const int CELLS = GRID_WIDTH / BUTTON_WIDTH;
private GameEngine engine = null;
Color CELL_COLOUR = Color.Blue;
public GameGUI()
{
InitializeComponent();
engine = new GameEngine(CELLS, CELLS);
pauseButton.Enabled = false;
}
private void GameGUI_Load(object sender, EventArgs e)
{
for (var j = 0; j + BUTTON_WIDTH <= GRID_WIDTH; j += BUTTON_WIDTH)
for (var i = 0; i + BUTTON_WIDTH <= GRID_WIDTH; i += BUTTON_WIDTH)
{
Button newButton = new Button();
newButton.Size = new Size(BUTTON_WIDTH, BUTTON_WIDTH);
newButton.Location = new Point(i, j);
newButton.Click += NewButton_Click;
mainGroupBox.Controls.Add(newButton);
}
UpdateColours();
}
private void UpdateColours()
{
for(var i = 0; i < mainGroupBox.Controls.Count; i++)
{
var y = i / engine.Height;
var x = i % engine.Width;
if (engine[y, x].A == 0)
mainGroupBox.Controls[i].BackColor = Color.LightGray;
else
mainGroupBox.Controls[i].BackColor = engine[y, x];
}
}
private void NewButton_Click(object sender, EventArgs e)
{
if (timer.Enabled == true)
return;
var i = mainGroupBox.Controls.IndexOf(sender as Button);
var y = i / engine.Height;
var x = i % engine.Width;
if (engine[y, x].A == 0)
{
engine[y, x] = CELL_COLOUR;
mainGroupBox.Controls[i].BackColor = CELL_COLOUR;
}
else
{
engine[y, x] = Color.FromArgb(0, 0, 0, 0);
mainGroupBox.Controls[i].BackColor = Color.LightGray;
}
}
private void timer_Tick(object sender, EventArgs e)
{
engine.Tick();
genTextBox.Text = engine.Ticks.ToString();
UpdateColours();
}
private void startButton_Click(object sender, EventArgs e)
{
timer.Start();
startButton.Enabled = false;
saveButton.Enabled = false;
pauseButton.Enabled = true;
radioColorRed.Enabled = false;
radioColorBlue.Enabled = false;
radioColorGreen.Enabled = false;
}
private void pauseButton_Click(object sender, EventArgs e)
{
timer.Stop();
saveButton.Enabled = true;
startButton.Enabled = true;
pauseButton.Enabled = false;
radioColorRed.Enabled = true;
radioColorBlue.Enabled = true;
radioColorGreen.Enabled = true;
}
private void resetButton_Click(object sender, EventArgs e)
{
timer.Stop();
saveButton.Enabled = true;
startButton.Enabled = true;
pauseButton.Enabled = false;
radioColorRed.Enabled = true;
radioColorBlue.Enabled = true;
radioColorGreen.Enabled = true;
engine = new GameEngine(engine.Height, engine.Width);
genTextBox.Text = engine.Ticks.ToString();
UpdateColours();
}
private void saveButton_Click(object sender, EventArgs e)
{
const int maxColor = 255;
const int factor = 10;
Stream streamFile;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "PPM Image Format (*.ppm)|*.ppm";
saveDialog.FilterIndex = 2;
saveDialog.RestoreDirectory = false;
if (saveDialog.ShowDialog(this) == DialogResult.OK)
{
try
{
if ((streamFile = saveDialog.OpenFile()) != null)
{
StreamWriter writer = new StreamWriter(streamFile);
writer.WriteLine("P3");
writer.WriteLine($"# {saveDialog.FileName}");
writer.WriteLine($"{engine.Width * factor} {engine.Height * factor}");
writer.WriteLine($"{maxColor}");
for (int y = 0; y < engine.Height * factor; y++)
{
for (int x = 0; x < engine.Width * factor; x++)
{
var yFactored = y / factor;
var xFactored = x / factor;
if (engine[yFactored, xFactored].A == 0)
writer.Write($"{maxColor} {maxColor} {maxColor} ");
else
writer.Write($"{engine[yFactored, xFactored].R} {engine[yFactored, xFactored].G} {engine[yFactored, xFactored].B} ");
}
}
writer.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void radioColorRed_CheckedChanged(object sender, EventArgs e)
{
CELL_COLOUR = Color.Red;
}
private void radioColorGreen_CheckedChanged(object sender, EventArgs e)
{
CELL_COLOUR = Color.Green;
}
private void radioColorBlue_CheckedChanged(object sender, EventArgs e)
{
CELL_COLOUR = Color.Blue;
}
}
}