/
Game1.cs
358 lines (287 loc) · 13.6 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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace Block_Breaker
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
const int WindowHeight = 600;
const int WindowWidth = 800;
Texture2D Ball;
Texture2D Paddle;
Texture2D Block;
Vector2 PaddlePos = new Vector2(350, 500);
Vector2 BallPos;
Vector2 BallSpeed;
List<Vector2> BricksPos;
SpriteFont Lives;
int Numberlives = 5;
int score;
public Game1()
{
graphics = new GraphicsDeviceManager(this)
{
// sets the height and width of the screen
PreferredBackBufferWidth = WindowWidth,
PreferredBackBufferHeight = WindowHeight
};
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
//adds in a ball at the same position as the paddle and set the speed
BallPos = new Vector2(PaddlePos.X + Paddle.Width /2 , PaddlePos.Y - Paddle.Height /2);
BallSpeed = new Vector2( -1 , - 1);
//begins the array of positions
BricksPos = new List<Vector2>();
//adding in the position of the first row of bricks
BricksPos.Add(new Vector2(050, 080));
BricksPos.Add(new Vector2(150, 080));
BricksPos.Add(new Vector2(250, 080));
BricksPos.Add(new Vector2(350, 080));
BricksPos.Add(new Vector2(450, 080));
BricksPos.Add(new Vector2(550, 080));
BricksPos.Add(new Vector2(650, 080));
//adding in the positions of the second row of bricks
BricksPos.Add(new Vector2(050, 110));
BricksPos.Add(new Vector2(650, 110));
BricksPos.Add(new Vector2(250, 110));
BricksPos.Add(new Vector2(450, 110));
//adding in the positions of the third row of bricks
BricksPos.Add(new Vector2(050, 140));
BricksPos.Add(new Vector2(150, 140));
BricksPos.Add(new Vector2(250, 140));
BricksPos.Add(new Vector2(350, 140));
BricksPos.Add(new Vector2(450, 140));
BricksPos.Add(new Vector2(550, 140));
BricksPos.Add(new Vector2(650, 140));
//adding in the position of the fourth row of bricks
BricksPos.Add(new Vector2(050, 170));
BricksPos.Add(new Vector2(150, 170));
BricksPos.Add(new Vector2(250, 170));
BricksPos.Add(new Vector2(350, 170));
BricksPos.Add(new Vector2(450, 170));
BricksPos.Add(new Vector2(550, 170));
BricksPos.Add(new Vector2(650, 170));
//adding in the positions of the fifth row of bricks
BricksPos.Add(new Vector2(050, 200));
BricksPos.Add(new Vector2(150, 200));
BricksPos.Add(new Vector2(250, 200));
BricksPos.Add(new Vector2(350, 200));
BricksPos.Add(new Vector2(450, 200));
BricksPos.Add(new Vector2(550, 200));
BricksPos.Add(new Vector2(650, 200));
//adding in the positions of the sixth row of bricks
BricksPos.Add(new Vector2(050, 230));
BricksPos.Add(new Vector2(150, 230));
BricksPos.Add(new Vector2(250, 230));
BricksPos.Add(new Vector2(350, 230));
BricksPos.Add(new Vector2(450, 230));
BricksPos.Add(new Vector2(550, 230));
BricksPos.Add(new Vector2(650, 230));
//adding in the positions of the seventh row of bricks
BricksPos.Add(new Vector2(050, 260));
BricksPos.Add(new Vector2(150, 260));
BricksPos.Add(new Vector2(250, 260));
BricksPos.Add(new Vector2(350, 260));
BricksPos.Add(new Vector2(450, 260));
BricksPos.Add(new Vector2(550, 260));
BricksPos.Add(new Vector2(650, 260));
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//loads in the textures of the ball, paddle and blocks
Ball = Content.Load<Texture2D>("breakout ball");
Paddle = Content.Load<Texture2D>("block breaker paddle");
Block = Content.Load<Texture2D>("Breakout brick");
Lives = Content.Load<SpriteFont>("Lives");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
//makes it so that if all the blocks are destroyed then the game will close adds the player another live and 100 points
if (BricksPos.Count == 0)
{
BallPos = new Vector2(PaddlePos.X + Paddle.Width / 2, PaddlePos.Y - Paddle.Height / 2);
BallSpeed = new Vector2(-1, -1);
score += 100;
//adding in the position of the first row of bricks
BricksPos.Add(new Vector2(050, 080));
BricksPos.Add(new Vector2(150, 080));
BricksPos.Add(new Vector2(250, 080));
BricksPos.Add(new Vector2(350, 080));
BricksPos.Add(new Vector2(450, 080));
BricksPos.Add(new Vector2(550, 080));
BricksPos.Add(new Vector2(650, 080));
//adding in the positions of the second row of bricks
BricksPos.Add(new Vector2(050, 110));
BricksPos.Add(new Vector2(650, 110));
BricksPos.Add(new Vector2(350, 110));
//adding in the positions of the third row of bricks
BricksPos.Add(new Vector2(050, 140));
BricksPos.Add(new Vector2(150, 140));
BricksPos.Add(new Vector2(250, 140));
BricksPos.Add(new Vector2(350, 140));
BricksPos.Add(new Vector2(450, 140));
BricksPos.Add(new Vector2(550, 140));
BricksPos.Add(new Vector2(650, 140));
//adding in the position of the fourth row of bricks
BricksPos.Add(new Vector2(050, 170));
BricksPos.Add(new Vector2(150, 170));
BricksPos.Add(new Vector2(250, 170));
BricksPos.Add(new Vector2(350, 170));
BricksPos.Add(new Vector2(450, 170));
BricksPos.Add(new Vector2(550, 170));
BricksPos.Add(new Vector2(650, 170));
//adding in the positions of the fifth row of bricks
BricksPos.Add(new Vector2(050, 200));
BricksPos.Add(new Vector2(150, 200));
BricksPos.Add(new Vector2(250, 200));
BricksPos.Add(new Vector2(350, 200));
BricksPos.Add(new Vector2(450, 200));
BricksPos.Add(new Vector2(550, 200));
BricksPos.Add(new Vector2(650, 200));
//adding in the positions of the sixth row of bricks
BricksPos.Add(new Vector2(050, 230));
BricksPos.Add(new Vector2(150, 230));
BricksPos.Add(new Vector2(250, 230));
BricksPos.Add(new Vector2(350, 230));
BricksPos.Add(new Vector2(450, 230));
BricksPos.Add(new Vector2(550, 230));
BricksPos.Add(new Vector2(650, 230));
//adding in the positions of the seventh row of bricks
BricksPos.Add(new Vector2(050, 260));
BricksPos.Add(new Vector2(150, 260));
BricksPos.Add(new Vector2(250, 260));
BricksPos.Add(new Vector2(350, 260));
BricksPos.Add(new Vector2(450, 260));
BricksPos.Add(new Vector2(550, 260));
BricksPos.Add(new Vector2(650, 260));
Numberlives++;
}
if (Numberlives > 0)
{
BallPos += BallSpeed;
// has the left key controlling the direction it goes only allowing it to go left if it doesnt go off the screen
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
if (PaddlePos.X > 0)
PaddlePos.X -= 7;
}
// has the right key control the right direction but doesnt allow it to go off he screen
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
if (PaddlePos.X < 800 - Paddle.Width)
PaddlePos.X += 7;
}
}
//creates the rectangles for the objects and adds collision properties to them
Rectangle PaddleRectangle = new Rectangle((int)PaddlePos.X, (int)PaddlePos.Y, Paddle.Width, Paddle.Height);
Rectangle BallRectangle = new Rectangle((int)BallPos.X, (int)BallPos.Y, Ball.Width, Ball.Height);
// makes it so that the ball will bounce off of the paddle
if (BallRectangle.Intersects(PaddleRectangle) && BallSpeed.Y < 0)
{
BallSpeed.Y = -BallSpeed.Y + 0.5f;
}
if (BallRectangle.Intersects(PaddleRectangle) && BallSpeed.Y > 0)
{
BallSpeed.Y = -BallSpeed.Y - 0.5f;
}
//add in the actual boundaries of the game on the top and he two sides but not the bottom
if (BallPos.Y < 0 )
{
BallSpeed.Y = -BallSpeed.Y;
}
if (BallPos.X < 0 || BallPos.X > 800)
{
BallSpeed.X = -BallSpeed.X;
}
//tell the ball what to do if it goes below the paddle and off the screen
if (BallPos.Y + Ball.Height > 800)
{
BallPos = new Vector2(PaddlePos.X + Paddle.Width / 2, PaddlePos.Y - Paddle.Height / 2);
BallSpeed = new Vector2(-1, -1);
Numberlives --;
}
//checks if the ball collides with the bricks
for (int i = BricksPos.Count - 1; i >= 0; i--)
{
Vector2 pos = BricksPos[i];
Rectangle brickRectangle = new Rectangle((int)pos.X, (int)pos.Y, Block.Width, Block.Height);
if (BallRectangle.Intersects(brickRectangle))
{
BallSpeed.Y = -BallSpeed.Y;
BricksPos.Remove(pos);
score++;
break;
}
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
//drawas all the objects to the game
spriteBatch.Draw(Paddle, PaddlePos, Color.White);
spriteBatch.Draw(Ball, BallPos, Color.White);
spriteBatch.DrawString(Lives, "Lives = " + Numberlives, new Vector2(020,020), Color.White);
spriteBatch.DrawString(Lives, "Score = " + score, new Vector2(580, 020), Color.White);
//draws allthe bricks on the board
foreach (var pos in BricksPos)
spriteBatch.Draw(Block, pos, Color.Turquoise);
// makes it so that if there are no more lives it displays some text to tell you
if (Numberlives <= 0)
{
spriteBatch.DrawString(Lives, "You ran out of Lives, press [Esc] to leave", new Vector2(200, 400), Color.White);
}
if (score >= 300)
{
spriteBatch.DrawString(Lives, "You won, GG press [Esc] to exit", score, new Vector2(200, 400), Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}