-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame1.cs
338 lines (290 loc) · 11.2 KB
/
Game1.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using GenserSprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
// Created by: Ethan Genser
namespace ConwaysGameOfLife
{
enum GameState { Start, Paused, Running}
public class Game1 : Game
{
#region _Variables_
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 screenMiddle;
float timer;
float updatesPerSecond;
GameState gameState;
Key enter;
long updates;
bool[,] currentGeneration;
Sprite background;
Cursor cursor;
public static Button speed2Button;
public static Button speed5Button;
public static Button speed10Button;
public static Button speed30Button;
public static Button speedPButton;
public static Button startButton;
public static Button pauseButton;
Button exitButton;
Button restartButton;
Button[,] cellButtons;
SpriteFont CourierNew;
#endregion
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// Grapics
graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width;
graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height;
graphics.IsFullScreen = true;
graphics.ApplyChanges();
Sprite.Content = Content;
Sprite.GraphicsDevice = GraphicsDevice;
screenMiddle = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
updates = 0;
currentGeneration = new bool[81,40];
enter = new Key(Keys.Enter);
gameState = GameState.Start;
cellButtons = new Button[81, 40];
for (int x = 0; x < 81; x++)
for (int y = 0; y < 40; y++)
{
cellButtons[x, y] = new Button(AnimationEffect.None, PressEffect.Press, new Vector2(screenMiddle.X + ((x - 40) * 16) , screenMiddle.Y + (int)(((float)y - 21.5) * 16)), 1);
}
background = new Sprite(screenMiddle);
cursor = new Cursor(1);
speed2Button = new Button(AnimationEffect.Shade, PressEffect.StickyPress, new Vector2(screenMiddle.X - 560, screenMiddle.Y + 350), 1);
speed2Button.pressed = true;
speed5Button = new Button(AnimationEffect.Shade, PressEffect.StickyPress, new Vector2(screenMiddle.X - 495, screenMiddle.Y + 350), 1);
speed10Button = new Button(AnimationEffect.Shade, PressEffect.StickyPress, new Vector2(screenMiddle.X - 430, screenMiddle.Y + 350), 1);
speed30Button = new Button(AnimationEffect.Shade, PressEffect.StickyPress, new Vector2(screenMiddle.X - 365, screenMiddle.Y + 350), 1);
speedPButton = new Button(AnimationEffect.Shade, PressEffect.StickyPress, new Vector2(screenMiddle.X - 300, screenMiddle.Y + 350), 1);
startButton = new Button(AnimationEffect.Shade, PressEffect.StickyPress, new Vector2(screenMiddle.X - 100, screenMiddle.Y + 338), 1);
pauseButton = new Button(AnimationEffect.Shade, PressEffect.StickyPress, new Vector2(screenMiddle.X, screenMiddle.Y + 338), 1);
restartButton = new Button(AnimationEffect.Shade, PressEffect.Press, new Vector2(screenMiddle.X + 100, screenMiddle.Y + 338), 1);
exitButton = new Button(AnimationEffect.Shade, PressEffect.Press, new Vector2(GraphicsDevice.Viewport.Width - 15, 15), 1);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Sprite.spriteBatch = spriteBatch;
CourierNew = Content.Load<SpriteFont>("Courier New");
background.LoadContent("Background");
cursor.LoadContent("Cursor2");
foreach (Button b in cellButtons)
b.LoadContent(@"Cell");
speed2Button.LoadContent(@"Buttons\Speed_2");
speed5Button.LoadContent(@"Buttons\Speed_5");
speed10Button.LoadContent(@"Buttons\Speed_10");
speed30Button.LoadContent(@"Buttons\Speed_30");
speedPButton.LoadContent(@"Buttons\Speed_P");
startButton.LoadContent(@"Buttons\Play");
pauseButton.LoadContent(@"Buttons\Pause");
restartButton.LoadContent(@"Buttons\Restart");
exitButton.LoadContent(@"Buttons\Exit");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
background.Update();
cursor.Update(gameTime);
speed2Button.Update(cursor, 1);
speed5Button.Update(cursor, 1);
speed10Button.Update(cursor, 1);
speed30Button.Update(cursor, 1);
speedPButton.Update(cursor, 1);
startButton.Update(cursor, 2);
pauseButton.Update(cursor, 2);
restartButton.Update(cursor, 0);
exitButton.Update(cursor, 0);
enter.Update();
// Exit button
if (exitButton.pressed)
Exit();
// Restart button
if (restartButton.pressed)
{
startButton.pressed = false;
pauseButton.pressed = false;
updates = 0;
for (int x = 0; x < 81; x++)
{
for (int y = 0; y < 40; y++)
{
currentGeneration[x, y] = false;
}
}
}
// Speed selection
if (speed2Button.pressed)
updatesPerSecond = 250;
else if (speed5Button.pressed)
updatesPerSecond = 100;
else if (speed10Button.pressed)
updatesPerSecond = 50;
else if (speed30Button.pressed)
updatesPerSecond = 16.6666f;
// Play/Pause buttons
if (startButton.pressed)
gameState = GameState.Running;
else if (pauseButton.pressed)
gameState = GameState.Paused;
else
gameState = GameState.Start;
// Start logic
if (gameState == GameState.Start)
{
for (int x = 0; x < 81; x++)
{
for (int y = 0; y < 40; y++)
{
cellButtons[x, y].Update(cursor, 0);
if (cellButtons[x, y].pressed)
currentGeneration[x, y] ^= true;
}
}
}
// Running logic
if (gameState == GameState.Running)
{
if (!speedPButton.pressed)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > updatesPerSecond)
{
updates++;
currentGeneration = nextGeneration(currentGeneration);
timer = 0;
}
}
else
{
if (enter.pressed)
{
updates++;
currentGeneration = nextGeneration(currentGeneration);
}
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
background.Draw();
speed2Button.Draw();
speed5Button.Draw();
speed10Button.Draw();
speed30Button.Draw();
speedPButton.Draw();
startButton.Draw();
pauseButton.Draw();
restartButton.Draw();
exitButton.Draw();
spriteBatch.DrawString(CourierNew, updates.ToString(), new Vector2(screenMiddle.X + 520, screenMiddle.Y + 325), Color.Black, 0f, Vector2.Zero, .75f, SpriteEffects.None, 0f);
for (int x = 0; x < 81; x++)
{
for (int y = 0; y < 40; y++)
{
if (currentGeneration[x, y])
cellButtons[x, y].Draw();
}
}
cursor.Draw();
spriteBatch.End();
base.Draw(gameTime);
}
protected bool[,] nextGeneration(bool[,] oldGeneration)
{
bool[,] returnValue = new bool[81, 40];
for (int x = 0; x < 81; x++)
{
for (int y = 0; y < 40; y++)
{
if (oldGeneration[x, y])
{
// Each cell with one or no neighbors dies, as if by solitude. Each cell with four or more neighbors dies, as if by overpopulation.
if (GetNeighbors(oldGeneration, x, y) <= 1 || GetNeighbors(oldGeneration, x, y) >= 4)
returnValue[x, y] = false;
else
returnValue[x, y] = true;
}
else
{
if (GetNeighbors(oldGeneration, x, y) == 3)
returnValue[x, y] = true;
else
returnValue[x, y] = false;
}
}
}
return returnValue;
}
protected int GetNeighbors(bool[,] oldGeneration, int x, int y)
{
int neighbors = 0;
// Checks up
if (y != 0)
{
if (oldGeneration[x, y - 1])
neighbors++;
}
// Checks up-right
if (x != 80 && y != 0)
{
if (oldGeneration[x + 1, y - 1])
neighbors++;
}
// Checks right
if (x != 80)
{
if (oldGeneration[x + 1, y])
neighbors++;
}
// Checks down-right
if (x != 80 && y != 39)
{
if (oldGeneration[x + 1, y + 1])
neighbors++;
}
// Checks down
if (y != 39)
{
if (oldGeneration[x, y + 1])
neighbors++;
}
// Checks down-left
if (x != 0 && y != 39)
{
if (oldGeneration[x - 1, y + 1])
neighbors++;
}
// Checks left
if (x != 0)
{
if (oldGeneration[x - 1, y])
neighbors++;
}
// Checks up-left
if (x != 0 && y != 0)
{
if (oldGeneration[x - 1, y - 1])
neighbors++;
}
return neighbors;
}
}
}
// Started: 8/13/17 9:32pm
// Finished: 8/14/17 2:28am
// Total work time: 4hr + 56min