-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPongGame.cs
720 lines (615 loc) · 31.3 KB
/
PongGame.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*My version of a pong clone. Players vs. Player. They can enter their names and play to the best of 5 scores
Game can be paused and it loops for a new game every time someone wins
Version 1.5: fixed a bug where when the ball would hit the top or the bottom of a paddle it would hit the paddle multiple times
and show a weird, unintended behaviour
Created by OuterGazer*/
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace PongGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class PongGame : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Allows for input from the keyboard for pausing the game and releasing the ball in service state
KeyboardState keyboard, lastKeyPress;
//Allows for entering names from the player. Class finds itself at the bottom of this file
KeyboardInput enterPlayerName = new KeyboardInput();
// Game Data
/// <summary>
/// The different states of the game.
/// </summary>
enum GameState
{
MainMenu,
EnterNamePlayer1,
EnterNamePlayer2,
Service,
Playing,
AfterScore,
Paused,
End
}
GameState state;
enum BallHitLast
{
RightPlayer,
LeftPlayer
}
BallHitLast whoHitLast;
/// <summary>
/// To keep track of which player scored the last to give the ball when resuming the game
/// </summary>
enum LastScore
{
LeftPlayer,
RightPlayer
}
LastScore whoScored;
//Message Font
SpriteFont messageFont;
SpriteFont instructionsFont;
// Textures for the ball, paddles and background
Texture2D ballTexture;
Texture2D lPaddleTexture;
Texture2D rPaddleTexture;
Texture2D backgroundTexture;
// Rectangles for the ball, paddles and background
Rectangle rPaddleRectangle;
Rectangle lPaddleRectangle;
Rectangle ballRectangle;
Rectangle backgroundRectangle;
// Speed of the ball
float ballXSpeed;
float ballYSpeed;
//Speed of the paddles
int rPaddleSpeed;
int lPaddleSpeed;
//amount of times the ball has bounced against a paddle
int ballBounce;
//Score of the players
int leftScore;
int rightScore;
// Distance of the paddle from the edge of the screen
int margin;
//names of the players
string player_1 = "";
string player_2 = "";
public PongGame()
{
this.graphics = new GraphicsDeviceManager(this);
this.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();
}
/// <summary>
/// After the end of a game, this method triggers if the players want to pla again.
/// sets all values and positions to standard.
/// </summary>
protected void ResetGame()
{
//Set the game to the playing state
this.state = GameState.Service;
//Sets the score to zero
this.leftScore = 0;
this.rightScore = 0;
this.StandardPlacement();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
//Sets the game first into the main menu
this.state = GameState.MainMenu;
// Create a new SpriteBatch, which can be used to draw textures.
this.spriteBatch = new SpriteBatch(GraphicsDevice);
this.messageFont = Content.Load<SpriteFont>("MessageFont");
this.instructionsFont = Content.Load<SpriteFont>("InstructionsFont");
this.ballTexture = Content.Load<Texture2D>("ball");
this.lPaddleTexture = Content.Load<Texture2D>("lpaddle");
this.rPaddleTexture = Content.Load<Texture2D>("rpaddle");
this.backgroundTexture = Content.Load<Texture2D>("SpaceBackground");
this.margin = Window.ClientBounds.Width / 100;
this.lPaddleRectangle = new Rectangle(
0, 0,
Window.ClientBounds.Width / 30,
Window.ClientBounds.Height / 6);
this.rPaddleRectangle = new Rectangle(
0, 0,
Window.ClientBounds.Width / 30,
Window.ClientBounds.Height / 6);
this.ballRectangle = new Rectangle(
0, 0,
Window.ClientBounds.Width / 30,
Window.ClientBounds.Width / 30);
this.backgroundRectangle = new Rectangle(
0, 0, //top left hand corner
Window.ClientBounds.Width,
Window.ClientBounds.Height); //size of the client screen display
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected void StandardPlacement()
{
this.ballBounce = 0;
this.ballXSpeed = 5.0f;
this.ballYSpeed = 5.0f;
this.lPaddleSpeed = 5;
this.rPaddleSpeed = 5;
this.lPaddleRectangle.Location = new Point(
this.margin,
Window.ClientBounds.Height / 2 - Window.ClientBounds.Height / 16);
this.rPaddleRectangle.Location = new Point(
Window.ClientBounds.Width - lPaddleRectangle.Width - this.margin,
Window.ClientBounds.Height / 2 - Window.ClientBounds.Height / 16);
switch (this.whoScored)
{
case LastScore.RightPlayer:
this.ballRectangle.Location = new Point(
this.margin + this.lPaddleRectangle.Width,
Window.ClientBounds.Height / 2 - this.ballRectangle.Height / 2);
//this.whoHitLast = BallHitLast.LeftPlayer;
break;
case LastScore.LeftPlayer:
this.ballRectangle.Location = new Point(
Window.ClientBounds.Width - this.rPaddleRectangle.Width - this.margin - this.ballRectangle.Width,
Window.ClientBounds.Height / 2 - this.ballRectangle.Height / 2);
this.whoHitLast = BallHitLast.LeftPlayer;
break;
}
}
/// <summary>
/// Standard movement for paddles
/// </summary>
protected void PaddleMovement()
{
//Movement for left paddle
if (this.keyboard.IsKeyDown(Keys.W))
this.lPaddleRectangle.Y -= this.lPaddleSpeed;
if (this.keyboard.IsKeyDown(Keys.S))
this.lPaddleRectangle.Y += this.lPaddleSpeed;
//The game window limits are set as boundaries so the paddle won't leave the screen
if (this.lPaddleRectangle.Bottom >= GraphicsDevice.Viewport.Height)
this.lPaddleRectangle.Y = GraphicsDevice.Viewport.Height - this.lPaddleRectangle.Height;
if (this.lPaddleRectangle.Top <= 0)
this.lPaddleRectangle.Y = 0;
//Movement for right paddle
if (this.keyboard.IsKeyDown(Keys.Up))
this.rPaddleRectangle.Y -= this.rPaddleSpeed;
if (this.keyboard.IsKeyDown(Keys.Down))
this.rPaddleRectangle.Y += this.rPaddleSpeed;
//The game window limits are set as boundaries so the paddle won't leave the screen
if (this.rPaddleRectangle.Bottom >= GraphicsDevice.Viewport.Height)
this.rPaddleRectangle.Y = GraphicsDevice.Viewport.Height - this.rPaddleRectangle.Height;
if (this.rPaddleRectangle.Top <= 0)
this.rPaddleRectangle.Y = 0;
}
/// <summary>
/// Changes the direction of the ball accordingly when the ball hits the top or bottom of a paddle
/// It also sums up one bounce and increases the speed of the ball and paddles
/// </summary>
protected void ChangeBallDirectionWhenTopOrBottomHit()
{
if(this.ballBounce < 8)
{
this.ballBounce++;
this.ballXSpeed = -this.ballXSpeed * 1.125f; //We increase the speed of the ball every bounce against a paddle
this.ballYSpeed = -this.ballYSpeed * 1.125f;
this.lPaddleSpeed += 1; //we also increase the speed of the paddles bit by bit
this.rPaddleSpeed += 1;
}
else
{
this.ballXSpeed = -this.ballXSpeed;
this.ballYSpeed = -this.ballYSpeed;
}
}
/// <summary>
/// When the ball hits the top or bottom of a paddle a nasty bug occurs where the ball gets inside the paddle's rectangle
/// This method gets rid if that by making the ball bounce in the same direction it came from
/// </summary>
/// <returns>True if the ball hits a paddle top or bottom, false if hits a side</returns>
protected bool CheckIfBallHitPaddleTopOrBottom()
{
if (this.ballRectangle.Bottom >= this.lPaddleRectangle.Bottom &&
this.ballRectangle.Left <= this.lPaddleRectangle.Right &&
this.whoHitLast == BallHitLast.RightPlayer)
{
this.ChangeBallDirectionWhenTopOrBottomHit();
this.whoHitLast = BallHitLast.LeftPlayer;
return true;
}
if(this.ballRectangle.Top <= this.lPaddleRectangle.Top &&
this.ballRectangle.Left <= this.lPaddleRectangle.Right &&
this.whoHitLast == BallHitLast.RightPlayer)
{
this.ChangeBallDirectionWhenTopOrBottomHit();
this.whoHitLast = BallHitLast.LeftPlayer;
return true;
}
if (this.ballRectangle.Bottom >= this.rPaddleRectangle.Bottom &&
this.ballRectangle.Right >= this.rPaddleRectangle.Left &&
this.whoHitLast == BallHitLast.LeftPlayer)
{
this.ChangeBallDirectionWhenTopOrBottomHit();
this.whoHitLast = BallHitLast.RightPlayer;
return true;
}
if (this.ballRectangle.Top <= this.rPaddleRectangle.Top &&
this.ballRectangle.Right >= this.rPaddleRectangle.Left &&
this.whoHitLast == BallHitLast.LeftPlayer)
{
this.ChangeBallDirectionWhenTopOrBottomHit();
this.whoHitLast = BallHitLast.RightPlayer;
return true;
}
return false;
}
/// <summary>
/// Standard behaviour when the ball intersects any of the paddles
/// </summary>
protected void BallIntersectsPaddle(Rectangle paddleRectangle)
{
if((this.ballYSpeed < 0 &&
this.ballRectangle.Center.Y < paddleRectangle.Center.Y) ||
(this.ballYSpeed > 0 &&
this.ballRectangle.Center.Y > paddleRectangle.Center.Y))
{
//do nothing, the rest of the code takes care
//this is the situation where the ball and the paddle go in the same direction and the ball hits the bottom or top
//it should keep going in the same Y direction and only change the X, which will happen in the code below
//otherwise, for example, the ball going down hits the bottom of a paddle also going down and ballwould go up again
//when it should keep going down
}
else
{
//this code triggers if ball and paddle are moving in opposite directions in order to send the ball straight in the
//direction it came from
if (this.CheckIfBallHitPaddleTopOrBottom())
return;
}
if (this.ballBounce < 8)
{
this.ballBounce++;
this.ballXSpeed = -this.ballXSpeed * 1.125f; //We increase the speed of the ball every bounce against a paddle
this.ballYSpeed = this.ballYSpeed * 1.125f;
this.lPaddleSpeed += 1; //we also increase the speed of the paddles bit by bit
this.rPaddleSpeed += 1;
}
else
{
this.ballXSpeed = -this.ballXSpeed;
}
}
/// <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)
{
//Allows for input from the keyboard
this.lastKeyPress = this.keyboard;
this.keyboard = Keyboard.GetState();
// Allows the game to exit
if (this.keyboard.IsKeyDown(Keys.Escape))
this.Exit();
switch (this.state)
{
case GameState.MainMenu:
if ((this.keyboard.IsKeyUp(Keys.Space)) && (this.lastKeyPress.IsKeyDown(Keys.Space)))
this.state = GameState.EnterNamePlayer1;
break;
case GameState.EnterNamePlayer1:
this.enterPlayerName.Update();
this.player_1 = this.enterPlayerName.playername;
if ((this.keyboard.IsKeyUp(Keys.Enter)) && (this.lastKeyPress.IsKeyDown(Keys.Enter)))
{
this.enterPlayerName.playername = "";
this.state = GameState.EnterNamePlayer2;
}
break;
case GameState.EnterNamePlayer2:
this.enterPlayerName.Update();
this.player_2 = this.enterPlayerName.playername;
if ((this.keyboard.IsKeyUp(Keys.Enter)) && (this.lastKeyPress.IsKeyDown(Keys.Enter)))
this.ResetGame();
break;
case GameState.Service:
this.PaddleMovement();
if(this.whoScored == LastScore.LeftPlayer)
{
this.ballRectangle.Location = new Point(
Window.ClientBounds.Width - rPaddleRectangle.Width - this.margin - this.ballRectangle.Width,
this.rPaddleRectangle.Y + this.rPaddleRectangle.Height / 2 - this.ballRectangle.Height / 2);
//this.whoHitLast = BallHitLast.RightPlayer;
}
else
{
this.ballRectangle.Location = new Point(
this.margin + this.lPaddleRectangle.Width,
this.lPaddleRectangle.Y + this.lPaddleRectangle.Height / 2 - this.ballRectangle.Height / 2);
this.whoHitLast = BallHitLast.LeftPlayer;
}
if ((this.keyboard.IsKeyUp(Keys.Space)) && (this.lastKeyPress.IsKeyDown(Keys.Space)))
this.state = GameState.Playing;
break;
case GameState.Playing:
//Allows the game to pause
if ((this.keyboard.IsKeyUp(Keys.P)) && (this.lastKeyPress.IsKeyDown(Keys.P)))
this.state = GameState.Paused;
//Start of normal gameplay
this.ballRectangle.X += (int)this.ballXSpeed; //Ball movement for the horizontal component
if (this.ballRectangle.X < 0 ||
this.ballRectangle.X + this.ballRectangle.Width > GraphicsDevice.Viewport.Width)
{
this.ballXSpeed = -this.ballXSpeed; //if encounter left or right bound, change direction
if (this.ballRectangle.X < 0) //right player scores when ball bounces off the left margin
{
this.rightScore++;
this.whoScored = LastScore.RightPlayer;
this.state = GameState.AfterScore;
}
if (this.ballRectangle.X + this.ballRectangle.Width > GraphicsDevice.Viewport.Width)
{
this.leftScore++; //left player scores when ball bounces off the right margin
this.whoScored = LastScore.LeftPlayer;
this.state = GameState.AfterScore;
}
}
this.ballRectangle.Y += (int)this.ballYSpeed; //Ball movement for the vertical component
if (this.ballRectangle.Y < 0 ||
this.ballRectangle.Y + this.ballRectangle.Height > GraphicsDevice.Viewport.Height)
{
this.ballYSpeed = -this.ballYSpeed; //if encounter upper or lower bound, change direction
}
this.PaddleMovement();
//Colision detection between ball and paddles. It changes the X direction of the ball
//if it colides with the top or bottom of a paddle a nasty bug appears, to avoid that we change bot the x and y direction
if (this.ballRectangle.Intersects(this.lPaddleRectangle) && this.whoHitLast == BallHitLast.RightPlayer)
{
this.BallIntersectsPaddle(this.lPaddleRectangle);
this.whoHitLast = BallHitLast.LeftPlayer;
}
if (this.ballRectangle.Intersects(this.rPaddleRectangle) && this.whoHitLast == BallHitLast.LeftPlayer)
{
this.BallIntersectsPaddle(this.rPaddleRectangle);
this.whoHitLast = BallHitLast.RightPlayer;
}
//Game finishes when one of the players reaches a score of 5
if ((this.leftScore == 5) || (this.rightScore == 5))
this.state = GameState.End;
break;
case GameState.AfterScore:
if ((this.keyboard.IsKeyUp(Keys.Space)) && (this.lastKeyPress.IsKeyDown(Keys.Space)))
{
this.StandardPlacement();
this.state = GameState.Service;
}
break;
case GameState.Paused:
if ((this.keyboard.IsKeyUp(Keys.P)) && (this.lastKeyPress.IsKeyDown(Keys.P)))
this.state = GameState.Playing;
break;
case GameState.End:
if ((this.keyboard.IsKeyUp(Keys.Space)) && (this.lastKeyPress.IsKeyDown(Keys.Space)))
this.ResetGame();
break;
}
base.Update(gameTime);
}
/// <summary>
/// Contains all the drawing calls for the playing state of the game
/// </summary>
protected void DrawPlayingState()
{
string playerNames = this.player_1 + " vs " + this.player_2;
string gameScore = this.leftScore + ":" + this.rightScore;
Vector2 centeredPlayerNames = this.instructionsFont.MeasureString(playerNames);
Vector2 centeredScore = this.messageFont.MeasureString(gameScore);
this.spriteBatch.Draw(this.backgroundTexture, this.backgroundRectangle, Color.White);
this.spriteBatch.DrawString(this.instructionsFont,
playerNames,
new Vector2((this.graphics.GraphicsDevice.Viewport.Width / 2) -
(centeredPlayerNames.Length() / 2), 15),
Color.White);
this.spriteBatch.DrawString(this.messageFont,
gameScore,
new Vector2((this.graphics.GraphicsDevice.Viewport.Width / 2) -
(centeredScore.Length() / 2), 30), Color.White);
this.spriteBatch.Draw(this.ballTexture, this.ballRectangle, Color.White);
this.spriteBatch.Draw(this.lPaddleTexture, this.lPaddleRectangle, Color.White);
this.spriteBatch.Draw(this.rPaddleTexture, this.rPaddleRectangle, Color.White);
}
/// <summary>
/// Draws the pertinent string messages to prompt each player to enter their names
/// </summary>
/// <param name="playerNumber">The player number, either Player 1 or Player 2</param>
/// <param name="playerName">The name of the player</param>
/// <param name="gameTime">The current time snapshot of the game</param>
protected void DrawPlayerName(string playerNumber, ref string playerName, GameTime gameTime)
{
this.spriteBatch.Draw(this.backgroundTexture, this.backgroundRectangle, Color.White);
this.spriteBatch.DrawString(this.instructionsFont, "Enter the name of " + playerNumber + ", press Enter when done:",
new Vector2(Window.ClientBounds.Width / 8,
Window.ClientBounds.Height / 4),
Color.White);
//this if else statement allows a blinking underscore to let the player know the game
//is waiting for input
if (gameTime.TotalGameTime.Duration().Seconds % 2 == 0)
{
this.spriteBatch.DrawString(this.instructionsFont, playerName,
new Vector2(Window.ClientBounds.Width / 8,
Window.ClientBounds.Height / 4 + 50),
Color.White);
}
else
{
this.spriteBatch.DrawString(this.instructionsFont, playerName + "_",
new Vector2(Window.ClientBounds.Width / 8,
Window.ClientBounds.Height / 4 + 50),
Color.White);
}
}
/// <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);
this.spriteBatch.Begin();
switch (this.state)
{
case GameState.MainMenu:
this.spriteBatch.Draw(this.backgroundTexture, this.backgroundRectangle, Color.White);
this.spriteBatch.DrawString(this.instructionsFont,
"Welcome to Pong! An exciting game about bouncing a ball!\n\n" +
"Game Controls:\n" +
"Player 1 (on the left) moves up or down with W and S keys\n" +
"Player 2 (on the right) moves up or down with the Up and Down arrowkeys\n" +
"Both players release the ball by pressing the spacebar\n" +
"Game can be paused/unpaused at any given time by pressing P\n" +
"To exit the game press Esc at any given time\n\n" +
"The first player to reach a score of 5 wins the game!\n\n" +
"Press the spacebar to begin the game!",
new Vector2(Window.ClientBounds.Width / 8,
Window.ClientBounds.Height / 4 - 30),
Color.White);
break;
case GameState.EnterNamePlayer1:
this.DrawPlayerName("Player 1", ref this.player_1, gameTime);
break;
case GameState.EnterNamePlayer2:
this.DrawPlayerName("Player 2", ref this.player_2, gameTime);
break;
case GameState.Service:
this.DrawPlayingState();
break;
case GameState.Playing:
this.DrawPlayingState();
break;
case GameState.AfterScore:
this.DrawPlayingState();
if(this.whoScored == LastScore.LeftPlayer)
{
this.spriteBatch.DrawString(this.instructionsFont,
this.player_1 + " scored!\nPress the spacebar to continue.",
new Vector2(Window.ClientBounds.Width / 3,
Window.ClientBounds.Height / 2 - Window.ClientBounds.Height / 16),
Color.White);
}
else
{
this.spriteBatch.DrawString(this.instructionsFont,
this.player_2 + " scored!\nPress the spacebar to continue.",
new Vector2(Window.ClientBounds.Width / 3,
Window.ClientBounds.Height / 2 - Window.ClientBounds.Height / 16),
Color.White);
}
break;
case GameState.Paused:
this.DrawPlayingState();
this.spriteBatch.DrawString(this.messageFont, "Game paused. Press P to resume.",
new Vector2(Window.ClientBounds.Width / 8 ,
Window.ClientBounds.Height / 2 - Window.ClientBounds.Height / 16),
Color.White);
break;
case GameState.End:
this.spriteBatch.Draw(this.backgroundTexture, this.backgroundRectangle, Color.White);
if(this.leftScore == 5)
{
this.spriteBatch.DrawString(this.instructionsFont,
this.player_1 + " has reached " + this.leftScore + "\n" + this.player_1 + " wins!",
new Vector2(Window.ClientBounds.Width / 3,
Window.ClientBounds.Height / 2 - Window.ClientBounds.Width / 16),
Color.White);
}
else
{
this.spriteBatch.DrawString(this.instructionsFont,
this.player_2 + " has reached " + this.rightScore + "\n" + this.player_2 + " wins!",
new Vector2(Window.ClientBounds.Width / 3,
Window.ClientBounds.Height / 2 - Window.ClientBounds.Width / 16),
Color.White);
}
this.spriteBatch.DrawString(this.instructionsFont, "Press the spacebar to play again\nor Esc to exit.",
new Vector2(Window.ClientBounds.Width / 3,
Window.ClientBounds.Height / 2),
Color.White);
break;
}
this.spriteBatch.End();
base.Draw(gameTime);
}
}
public class KeyboardInput
{
private Keys[] previousFrameKey;
public KeyboardInput()
{
this.previousFrameKey = new Keys[0];
}
public void Update()
{
KeyboardState keyboard = Keyboard.GetState();
Keys[] currentPressedKeys = keyboard.GetPressedKeys();
//check if any of the previous update's keys are no longer pressed
foreach (Keys key in this.previousFrameKey)
{
if (!currentPressedKeys.Contains(key))
OnKeyUp(key);
}
//check if the currently pressed keys were already pressed
foreach (Keys key in currentPressedKeys)
{
if (!this.previousFrameKey.Contains(key))
OnKeyDown(key);
}
//save the currently pressed keys so we can compare on the next update
this.previousFrameKey = currentPressedKeys;
}
private void OnKeyDown(Keys key)
{
//because a simple key stroke goes on for several frames, we avoid getting single letters multiple times repeated
return;
}
public string playername = "";
private void OnKeyUp(Keys key)
{
//Letters get added to the name only when the key is released
if(key.ToString() != "Enter")
this.playername += key.ToString();
}
}
}