-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGame1.cs
632 lines (531 loc) · 19 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using Devcade;
using static Devcade.Input;
namespace BrickBreaker
{
/// <summary>
/// Tracks the state of the game to allow menus to be drawn
/// </summary>
public enum GameState
{
Menu,
Playing,
GameOver,
Win,
Pause,
Instructions,
LifeLost
}
/// <summary>
/// Manages the game and game updates
/// </summary>
public class Game1 : Game
{
/// <summary>
/// Stores a reference to the game object so data from Game1 can be accessed from other classes
/// </summary>
public Game1 game { get; }
/// <summary>
/// Random object
/// </summary>
private Random rng;
/// <summary>
/// Manages the graphics calls in the game
/// </summary>
private GraphicsDeviceManager _graphics;
/// <summary>
/// Manages the draw calls for the game
/// </summary>
private SpriteBatch _spriteBatch;
/// <summary>
/// Paytone One font, size 72
/// </summary>
private SpriteFont paytoneOne72;
/// <summary>
/// Paytone One font, size 36
/// </summary>
private SpriteFont paytoneOne36;
/// <summary>
/// Noto Sans font, size 48
/// </summary>
private SpriteFont notoSans48;
/// <summary>
/// Noto Sans font, size 36
/// </summary>
private SpriteFont notoSans36;
/// <summary>
/// Noto Sans font, size 20
/// </summary>
private SpriteFont notoSans20;
/// <summary>
/// Noto Sans font, size 16
/// </summary>
private SpriteFont notoSans16;
/// <summary>
/// Current state of the game
/// </summary>
public GameState currState;
/// <summary>
/// Boumds of the game window
/// </summary>
private Rectangle window;
/// <summary>
/// Menu screen object
/// </summary>
private Menu gameMenu;
/// <summary>
/// Instructions screen object
/// </summary>
private Instructions instructionsScreen;
/// <summary>
/// Pause screen object
/// </summary>
private PauseMenu pauseMenu;
/// <summary>
/// Main game object
/// </summary>
private MainGame mainGame;
/// <summary>
/// Life lost screen object
/// </summary>
private LifeLost lifeLostScreen;
/// <summary>
/// Game over screen object
/// </summary>
private GameOver gameOverScreen;
/// <summary>
/// Win screen object
/// </summary>
private Win winScreen;
/// <summary>
/// Red button on the cabinet
/// </summary>
private Texture2D redButton;
/// <summary>
/// Blue button on the cabinet
/// </summary>
private Texture2D blueButton;
/// <summary>
/// Green button on the cabinet
/// </summary>
private Texture2D greenButton;
/// <summary>
/// White button on the cabinet
/// </summary>
private Texture2D whiteButton;
/// <summary>
/// Paddle object
/// </summary>
private Paddle paddle;
/// <summary>
/// Texture of the paddle
/// </summary>
private Texture2D paddleTexture;
/// <summary>
/// Ball object
/// </summary>
private Ball ball;
/// <summary>
/// Texture of the ball
/// </summary>
private Texture2D ballTexture;
/// <summary>
/// Alternate texture of the ball
/// </summary>
internal Texture2D altBallTexture;
/// <summary>
/// List of bricks
/// </summary>
private List<Brick> brickList;
/// <summary>
/// Texture of the bricks
/// </summary>
private Texture2D brickTexture;
/// <summary>
/// List of colors the bricks can have
/// </summary>
private Color[] brickColors;
/// <summary>
/// Player's current score
/// </summary>
public int score;
/// <summary>
/// Number of lives remaining
/// </summary>
public int lives;
/// <summary>
/// Sets up the content and window for the game
/// </summary>
public Game1()
{
game = this;
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = false;
}
/// <summary>
/// Initializes the necessary fields
/// </summary>
protected override void Initialize()
{
#region Setting Window Size
#if DEBUG
_graphics.PreferredBackBufferWidth = 420;
_graphics.PreferredBackBufferHeight = 980;
_graphics.ApplyChanges();
#else
_graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width;
_graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height;
_graphics.ApplyChanges();
#endif
#endregion
rng = new Random();
currState = GameState.Menu;
window = GraphicsDevice.Viewport.Bounds;
// Sets up the game objects and the textures
paddleTexture = new Texture2D(GraphicsDevice, 1, 1);
paddleTexture.SetData(new[] { Color.White });
brickTexture = new Texture2D(GraphicsDevice, 1, 1);
brickTexture.SetData(new[] { Color.White });
// Initializes the list of bricks
brickList = new List<Brick>();
// Initializes the list of colors the bricks can have
brickColors = new[]
{
Color.Red, Color.Orange,
Color.Yellow, Color.LawnGreen,
Color.Green, Color.DarkGreen,
Color.Aqua, Color.CornflowerBlue,
Color.DodgerBlue, Color.Magenta,
Color.Purple, Color.Pink
};
score = 0;
lives = 5;
Input.Initialize();
base.Initialize();
}
/// <summary>
/// Loads content from the content manager
/// </summary>
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
#region Loading Fonts
// Paytone One fonts
paytoneOne72 = Content.Load<SpriteFont>("PaytoneOneCabinet"); // Cabinet
paytoneOne36 = Content.Load<SpriteFont>("PaytoneOne"); // Debug
// Noto Sans fonts
notoSans48 = Content.Load<SpriteFont>("NotoSansCabinet"); // Cabinet
notoSans36 = Content.Load<SpriteFont>("NotoSansCabinetSmall"); // Cabinet
notoSans20 = Content.Load<SpriteFont>("NotoSans"); // Debug
notoSans16 = Content.Load<SpriteFont>("NotoSansSmall"); // Debug
#endregion
#region Loading Textures
// Cabinet button textures
redButton = Content.Load<Texture2D>("red-button");
blueButton = Content.Load<Texture2D>("blue-button");
greenButton = Content.Load<Texture2D>("green-button");
whiteButton = Content.Load<Texture2D>("white-button");
// Ball Textures
ballTexture = Content.Load<Texture2D>("ball");
altBallTexture = Content.Load<Texture2D>("surprised-pikachu");
#endregion
CreateGameObjects();
CreateGameStates();
}
/// <summary>
/// Loops every frame and updates the game window
/// </summary>
/// <param name="gameTime">The time elapsed in the game</param>
protected override void Update(GameTime gameTime)
{
// Adds a keybind to exit the game
if (Keyboard.GetState().IsKeyDown(Keys.Escape)
|| GetButtonDown(1, ArcadeButtons.Menu)
&& GetButtonDown(2, ArcadeButtons.Menu))
{
Exit();
}
switch (currState)
{
case GameState.Menu:
gameMenu.UpdateMenu();
break;
case GameState.Instructions:
instructionsScreen.UpdateInstructions();
break;
case GameState.Pause:
if (Keyboard.GetState().IsKeyDown(Keys.U) ||
GetButtonDown(1, ArcadeButtons.B3) ||
GetButtonDown(2, ArcadeButtons.B3))
{
currState = GameState.Playing;
}
break;
case GameState.Playing:
if (Keyboard.GetState().IsKeyDown(Keys.P) ||
GetButtonDown(1, ArcadeButtons.B4) ||
GetButtonDown(2, ArcadeButtons.B4))
{
currState = GameState.Pause;
}
// Ball is below the bottom of the screen
if (ball.Position.Y > window.Bottom + 50)
{
lives -= 1;
currState = lives <= 0 ? GameState.GameOver : GameState.LifeLost;
}
// Checks if all bricks are broken
else if (brickList[0].AllBricksBroken(brickList))
{
currState = GameState.Win;
}
else
{
mainGame.UpdateGame(paddle, ball, brickList);
}
break;
case GameState.LifeLost:
lifeLostScreen.UpdateLifeLost();
break;
case GameState.GameOver:
gameOverScreen.UpdateGameOver();
break;
case GameState.Win:
winScreen.UpdateWin();
break;
}
Input.Update();
base.Update(gameTime);
}
/// <summary>
/// Loops every frame and draws the content on the screen
/// </summary>
/// <param name="gameTime">The time elapsed in the game</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
_spriteBatch.Begin();
switch (currState)
{
case GameState.Menu:
gameMenu.DrawText();
break;
case GameState.Instructions:
instructionsScreen.DrawText();
break;
case GameState.Pause:
pauseMenu.DrawPauseMenu();
break;
case GameState.Playing:
if (lives > 0)
{
mainGame.DrawGameInfo(score, lives);
mainGame.DrawGame(paddle, ball, brickList);
}
break;
case GameState.LifeLost:
lifeLostScreen.DrawLifeLost();
break;
case GameState.GameOver:
gameOverScreen.DrawGameOver();
break;
case GameState.Win:
winScreen.DrawWinScreen();
break;
}
_spriteBatch.End();
base.Draw(gameTime);
}
/// <summary>
/// Initializes the objects for all of the game states and menus
/// </summary>
public void CreateGameStates()
{
gameMenu = new Menu(_spriteBatch,
GraphicsDevice,
game,
notoSans48,
notoSans36,
notoSans20,
notoSans16,
paytoneOne72,
paytoneOne36,
redButton,
blueButton,
greenButton,
whiteButton);
instructionsScreen = new Instructions(_spriteBatch,
GraphicsDevice,
game,
notoSans48,
notoSans36,
notoSans20,
notoSans16,
paytoneOne72,
paytoneOne36,
redButton,
blueButton,
greenButton,
whiteButton);
pauseMenu = new PauseMenu(_spriteBatch,
GraphicsDevice,
game,
notoSans48,
notoSans36,
notoSans20,
notoSans16,
paytoneOne72,
paytoneOne36,
redButton,
blueButton,
greenButton,
whiteButton);
mainGame = new MainGame(_spriteBatch,
GraphicsDevice,
game,
notoSans48,
notoSans36,
notoSans20,
notoSans16,
paytoneOne72,
paytoneOne36,
redButton,
blueButton,
greenButton,
whiteButton,
score,
lives);
lifeLostScreen = new LifeLost(_spriteBatch,
GraphicsDevice,
game,
notoSans48,
notoSans36,
notoSans20,
notoSans16,
paytoneOne72,
paytoneOne36,
redButton,
blueButton,
greenButton,
whiteButton);
gameOverScreen = new GameOver(_spriteBatch,
GraphicsDevice,
game,
notoSans48,
notoSans36,
notoSans20,
notoSans16,
paytoneOne72,
paytoneOne36,
redButton,
blueButton,
greenButton,
whiteButton);
winScreen = new Win(_spriteBatch,
GraphicsDevice,
game,
notoSans48,
notoSans36,
notoSans20,
notoSans16,
paytoneOne72,
paytoneOne36,
redButton,
blueButton,
greenButton,
whiteButton);
}
/// <summary>
/// Creates the paddle, ball, and bricks when the game first loads
/// </summary>
public void CreateGameObjects()
{
score = 0;
lives = 5;
paddle = new Paddle(
paddleTexture,
new Rectangle(
window.Center.X - paddleTexture.Width / 2,
window.Bottom - 100,
150,
20),
Color.Gray,
window);
int randInt = rng.Next(0, 2);
Vector2 tempVel = randInt == 0 ? new Vector2(-6, -10) : new Vector2(6, -10);
ball = new Ball(
ballTexture,
altBallTexture,
new Vector2(
paddle.Bounds.Center.X - ballTexture.Width / 2f,
paddle.Position.Y - 30),
tempVel,
paddle,
window);
brickList = new List<Brick>();
#region Brick Sizes
// Number of rows and columns of bricks
// Size of the bricks will scale to fit the desired area of the window
int numRows = 12;
int numCols = 5;
// Number of pixels between bricks
int brickSpacing = 5;
// Farthest down that a brick can be drawn
int brickAreaHeight = window.Height / 2;
// Y-coordinate of the first row of bricks
int brickAreaTopOffset = 80;
// Calculates the dimensions of the bricks based on the window size and the above values
int brickWidth = (window.Width - (numCols * brickSpacing + brickSpacing)) / numCols;
int brickHeight = (brickAreaHeight - (numRows * brickSpacing + brickSpacing)) / numRows;
#endregion
// Adds all the bricks to the list
for (int row = 0; row < numRows; row++)
{
for (int col = 0; col < numCols; col++)
{
Brick currBrick = new Brick(brickTexture, new Rectangle(
5 + col * (brickWidth + brickSpacing),
brickAreaTopOffset + row * (brickHeight + brickSpacing),
brickWidth,
brickHeight),
brickColors[row],
ball,
window,
game
);
brickList.Add(currBrick);
}
}
}
/// <summary>
/// Resets the paddle to its initial position
/// </summary>
public void ResetPaddle()
{
paddle.Position = new Vector2(window.Center.X - paddleTexture.Width / 2f, window.Height - 100);
}
/// <summary>
/// Resets the position and hitbox of the ball to the initial position
/// </summary>
public void ResetBall()
{
ball.Position = new Vector2(paddle.Bounds.Center.X - ballTexture.Width / 2f, paddle.Bounds.Top - ballTexture.Height - 10);
ball.Velocity = rng.Next(0, 2) == 0 ? new Vector2(-6, -10) : new Vector2(6, -10);
}
/// <summary>
/// Sets all bricks to not broken
/// </summary>
public void ResetBricks()
{
for (int i = 0; i < brickList.Count; i++)
{
brickList[i].Broken = false;
}
}
}
}