/
Breakout.cs
391 lines (290 loc) · 13.1 KB
/
Breakout.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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Breakout_Clone
{
public class Breakout : Microsoft.Xna.Framework.Game
{
Viewport height;
public enum GameState { StartMenu, NewGame, Options, Help, About, Exit, EndGame };
public enum NewGameMenu {None, Normal, Hard };
public GameState state;
public NewGameMenu New_Game_state;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState keyState;
KeyboardState currKey, PrevKey;
Ball ball_1;
Ball ball_2;
Paddle paddle;
Brick_Manager theBrick_Manager;
Score_Manager theScore_Manager;
Menu_Manager theMenu_Manager;
public static AudioEngine theAudioEngine;
public static SoundBank theSoundBank;
public static WaveBank theWaveBank;
private static Music_Manager theMusic_Manager;
public bool gameover;
public Breakout()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
state = GameState.StartMenu;
New_Game_state = NewGameMenu.None;
}
protected override void Initialize()
{
height = graphics.GraphicsDevice.Viewport;
height.Height = 800;
graphics.GraphicsDevice.Viewport = height;
this.graphics.PreferredBackBufferHeight = 800;
this.graphics.ApplyChanges();
ball_1 = new Ball(new Vector2(400, 400), // init Position
2, // Speed multi, multiplies final speed vector
// after all other modifications in the game lifetime
new Vector2(1, 1)); // init Direction
ball_2 = new Ball(new Vector2(400, 400),
2,
new Vector2(1, 1));
paddle = new Paddle(this.graphics);
theBrick_Manager = new Brick_Manager(this.graphics);
theScore_Manager = new Score_Manager(this.graphics);
theMenu_Manager = new Menu_Manager(this.graphics);
// Initializing audio objects
theAudioEngine = new AudioEngine("Content\\Audio\\Breakout_Audio.xgs");
theWaveBank = new WaveBank(theAudioEngine, "Content\\Audio\\Wave Bank.xwb");
theSoundBank = new SoundBank(theAudioEngine, "Content\\Audio\\Sound Bank.xsb");
theMusic_Manager = new Music_Manager();
gameover = false;
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
ball_1.LoadContent(this.Content);
ball_2.LoadContent(this.Content);
paddle.LoadContent(this.Content);
theBrick_Manager.LoadContent_Bricks(this.Content);
theScore_Manager.LoadContent_Score(this.Content);
theMenu_Manager.LoadContent_Menu(this.Content);
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//theMenu_Manager.option_selection();
switch (state)
{
case GameState.StartMenu:
#region StartMenu sequence
theMusic_Manager.playTheme(state, New_Game_state);
theMenu_Manager.option_selection();
currKey = Keyboard.GetState();
if (currKey.IsKeyUp(Keys.Enter) && PrevKey.IsKeyDown(Keys.Enter))
state = (GameState)theMenu_Manager.selected;
PrevKey = currKey;
break;
#endregion
case GameState.NewGame:
Keyboard_Listener2();
theMusic_Manager.playTheme(state, New_Game_state);
theMenu_Manager.option_Subselection();
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
New_Game_state = (NewGameMenu)theMenu_Manager.selected_sub;
switch (New_Game_state)
{
case NewGameMenu.Normal:
#region vers1Game sequence
Keyboard_Listener();
if (ball_1.isActive)
{
Update_OneBallGame(gameTime);
if (ball_1.Position.Y > paddle.Position.Y)
{
ball_1.isActive = false;
gameover = true;
}
}
else if (gameover)
{
gameover = false;
theSoundBank.PlayCue("LBMiss");
Fade_Controls.beingFade = true;
Fade_Controls.fadeAnim = true;
}
else if (Fade_Controls.beingFade)
{
Fade_Controls.Dec_AlphaPerTime(gameTime);
}
break;
#endregion
case NewGameMenu.Hard:
#region vers2Game sequence
Keyboard_Listener();
if (ball_1.isActive && ball_2.isActive)
{
Update_TwoBallGame(gameTime);
if (ball_1.Position.Y > paddle.Position.Y)
{
ball_1.isActive = false;
gameover = true;
}
if (ball_2.Position.Y > paddle.Position.Y)
{
ball_2.isActive = false;
gameover = true;
}
}
else if (gameover)
{
gameover = false;
theSoundBank.PlayCue("LBMiss");
Fade_Controls.beingFade = true;
Fade_Controls.fadeAnim = true;
}
else if (Fade_Controls.beingFade)
{
Fade_Controls.Dec_AlphaPerTime(gameTime);
}
break;
#endregion
}
break;
case GameState.Options:
Keyboard_Listener2();
break;
case GameState.Help:
Keyboard_Listener2();
break;
case GameState.About:
Keyboard_Listener2();
break;
case GameState.Exit:
#region Exit sequence
this.Exit();
break;
#endregion
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
switch (state)
{
case GameState.StartMenu:
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
theMenu_Manager.Draw_Menu(this.spriteBatch);
spriteBatch.End();
break;
case GameState.NewGame:
spriteBatch.Begin();
switch (New_Game_state)
{
case NewGameMenu.Normal:
ball_1.Draw(this.spriteBatch);
paddle.Draw(this.spriteBatch);
theBrick_Manager.Draw_Bricks(this.spriteBatch);
theScore_Manager.Draw_Score(this.spriteBatch);
break;
case NewGameMenu.Hard:
ball_1.Draw(this.spriteBatch);
if (Collisions.ballCollided)
ball_2.Draw(this.spriteBatch);
paddle.Draw(this.spriteBatch);
theBrick_Manager.Draw_Bricks(this.spriteBatch);
theScore_Manager.Draw_Score(this.spriteBatch);
break;
default:
theMenu_Manager.Draw_SubMenu(this.spriteBatch);
break;
}
if (Fade_Controls.fade_done)
{
theMenu_Manager.Draw_FailMenu(this.spriteBatch);
}
spriteBatch.End();
break;
case GameState.Options:
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
theMenu_Manager.Draw_OptionsMenu(this.spriteBatch);
spriteBatch.End();
break;
case GameState.Help:
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
theMenu_Manager.Draw_HelpMenu(this.spriteBatch);
spriteBatch.End();
break;
case GameState.About:
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
theMenu_Manager.Draw_AboutMenu(this.spriteBatch);
spriteBatch.End();
break;
default:
break;
}
base.Draw(gameTime);
}
public void Update_OneBallGame(GameTime gameTime)
{
if (ball_1.Position.Y <= paddle.Position.Y) // Game stops is ball is beyond paddle Y coordinates
{
ball_1.Update(gameTime);
Collisions.Check_Border_colision(graphics, ball_1);
Collisions.upperWallHit_shrink(graphics, ball_1, paddle);
}
paddle.Update();
Collisions.Check_Border_colision(graphics, paddle);
Collisions.Check_Sprite_colision(ball_1, paddle, false);
paddle.Rebound_from_Paddle(ball_1);
theBrick_Manager.check_Collisions(ball_1, true);
}
private void Update_TwoBallGame(GameTime gameTime)
{
ball_1.Update(gameTime);
Collisions.Check_Border_colision(graphics, ball_1);
Collisions.upperWallHit_shrink(graphics, ball_1, paddle);
Collisions.Check_Sprite_colision(ball_1, paddle, false);
paddle.Rebound_from_Paddle(ball_1);
theBrick_Manager.check_Collisions(ball_1, true);
if (Collisions.ballCollided)
{
ball_2.Update(gameTime);
Collisions.Check_Border_colision(graphics, ball_2);
Collisions.upperWallHit_shrink(graphics, ball_2, paddle);
Collisions.Check_Sprite_colision(ball_2, paddle, false);
paddle.Rebound_from_Paddle(ball_2);
theBrick_Manager.check_Collisions(ball_2, true);
Collisions.Check_Sprite_colision(ball_1, ball_2, false);
}
paddle.Update();
Collisions.Check_Border_colision(graphics, paddle);
}
public void Keyboard_Listener()
{
keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Escape))
this.Exit();
}
public void Keyboard_Listener2()
{
keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Back))
state = GameState.StartMenu;
}
}
}