Advertisement
Guest User

C#/XNA Pong (fixed movement)

a guest
Feb 28th, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.17 KB | None | 0 0
  1. /* Game1.cs */
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Audio;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.GamerServices;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework.Input;
  12. using Microsoft.Xna.Framework.Media;
  13.  
  14. namespace GroundZero_sPong {
  15.    
  16.     public class Game1 : Microsoft.Xna.Framework.Game {
  17.         GraphicsDeviceManager graphics;
  18.         SpriteBatch spriteBatch;
  19.  
  20.         KeyboardState _KBS;
  21.         KeyboardState oldKBS;
  22.  
  23.         SpriteFont helpFont, score, info;
  24.         Texture2D background, padTexture, ball;
  25.         Vector2 ballPos, ballMvmnt, leftScorePos, rightScorePos, helpPos, helpOrgn, infoPos, infoOrgn;
  26.         Rectangle borders, left, right;
  27.         SoundEffect pongSound, scoreSound, victorySound;
  28.         bool paused, endgame;
  29.         byte speed;
  30.         const int maxScore = 25;
  31.         string helpString, infoString;
  32.         ball Ball;
  33.         pad Left;
  34.         pad Right;
  35.  
  36.         public Game1() {
  37.             graphics = new GraphicsDeviceManager(this);
  38.             Content.RootDirectory = "Content";
  39.         }
  40.  
  41.         protected override void Initialize() {
  42.             #region WindowSize
  43.             graphics.PreferredBackBufferWidth = 800;
  44.             graphics.PreferredBackBufferHeight = 500;
  45.             graphics.IsFullScreen = false;
  46.             graphics.ApplyChanges();
  47.  
  48.             borders = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
  49.             #endregion
  50.  
  51.             #region HelpInfo
  52.             helpString = "";
  53.             helpPos = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
  54.  
  55.             infoString = "";
  56.             infoPos = new Vector2(graphics.PreferredBackBufferWidth / 2, 100);
  57.             #endregion
  58.  
  59.             #region Pads
  60.             left = new Rectangle(15, 0, 10, 50); // 15 spacing
  61.             right = new Rectangle(borders.Right - 15 - 10, 0, 10, 50);
  62.             speed = 5;
  63.             #endregion
  64.  
  65.             #region ball
  66.             ballMvmnt = new Vector2(4, -4);
  67.             ballPos = new Vector2(borders.Width / 2, borders.Height / 2);
  68.             #endregion
  69.  
  70.             #region score
  71.             leftScorePos = new Vector2(10, 5);
  72.             rightScorePos = new Vector2(graphics.PreferredBackBufferWidth - 10, 5);
  73.             #endregion
  74.  
  75.             paused = true;
  76.             endgame = false;
  77.  
  78.             oldKBS = Keyboard.GetState();
  79.  
  80.             base.Initialize();
  81.         }
  82.  
  83.         protected override void LoadContent() {
  84.             spriteBatch = new SpriteBatch(GraphicsDevice);
  85.  
  86.             helpFont = Content.Load<SpriteFont>("help");
  87.             score = Content.Load<SpriteFont>("score");
  88.             info = Content.Load<SpriteFont>("info");
  89.  
  90.             background = Content.Load<Texture2D>("background");
  91.             padTexture = Content.Load<Texture2D>("pad");
  92.             ball = Content.Load<Texture2D>("ball");
  93.  
  94.             pongSound = Content.Load<SoundEffect>("pongSound");
  95.             scoreSound = Content.Load<SoundEffect>("scoreSound");
  96.             victorySound = Content.Load<SoundEffect>("victorySound");
  97.  
  98.             Ball = new ball(10, borders, ballPos, ballMvmnt, scoreSound, pongSound, true);
  99.             Left = new pad(borders, left, speed, true);
  100.             Right = new pad(borders, right, speed, true);
  101.         }
  102.  
  103.         protected override void UnloadContent() {
  104.             // TODO: Unload any non ContentManager content here
  105.         }
  106.  
  107.         protected override void Update(GameTime gameTime) {
  108.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  109.                 this.Exit();
  110.  
  111.             _KBS = Keyboard.GetState();
  112.  
  113.             #region Game
  114.             if (_KBS.IsKeyDown(Keys.F1) || _KBS.IsKeyDown(Keys.Space)) {
  115.                 if (!oldKBS.IsKeyDown(Keys.F1) && !oldKBS.IsKeyDown(Keys.Space)) {
  116.                     Ball.Pause();
  117.                     Left.Pause();
  118.                     Right.Pause();
  119.                     paused = !paused;
  120.                 }
  121.             }
  122.  
  123.             if (paused) {
  124.                 infoString = "GAME PAUSED";
  125.                 helpString = "Move the left paddle with W/Z & S and the right paddle with the up & down arrow key." + Environment.NewLine +
  126.                     "First one to reach " + maxScore + " points wins!" + Environment.NewLine +
  127.                     Environment.NewLine +  "Pauze and resume by pressing space or F1.";
  128.             } else {
  129.                 infoString = "";
  130.                 helpString = "";
  131.  
  132.                 #region left
  133.                 if (_KBS.IsKeyDown(Keys.W) || _KBS.IsKeyDown(Keys.Z))
  134.                     Left.MoveUp();
  135.                 else if (_KBS.IsKeyDown(Keys.S))
  136.                     Left.MoveDown();
  137.                 #endregion
  138.  
  139.                 #region right
  140.                 if (_KBS.IsKeyDown(Keys.Up))
  141.                     Right.MoveUp();
  142.                 else if (_KBS.IsKeyDown(Keys.Down))
  143.                     Right.MoveDown();
  144.                 #endregion
  145.             }
  146.             #endregion
  147.  
  148.             #region endGame
  149.             if (Ball.ScoreLeft >= maxScore) {
  150.                 infoString = "Congratulations Player 1" + Environment.NewLine + "You won the game!";
  151.                 if (!endgame) {
  152.                     victorySound.Play();
  153.                     endgame = true;
  154.                 }
  155.             } else if (Ball.ScoreRight >= maxScore) {
  156.                 infoString = "Congratulations Player 2" + Environment.NewLine + "You won the game!";
  157.                 if (!endgame) {
  158.                     victorySound.Play();
  159.                     endgame = true;
  160.                 }
  161.             }
  162.             if (endgame) {
  163.                 ballPos = new Vector2(borders.Width / 2, borders.Height / 2);
  164.                 Ball.Pause();
  165.                 Left.Pause();
  166.                 Right.Pause();
  167.                 helpString = "Press space to restart";
  168.                 if (_KBS.IsKeyDown(Keys.Space)) {
  169.                     Game1 GameX = new Game1();
  170.                     this.Exit();
  171.                 }
  172.             }
  173.             #endregion
  174.  
  175.             // update keyboardstate
  176.             oldKBS = _KBS;
  177.  
  178.             base.Update(gameTime);
  179.         }
  180.  
  181.         protected override void Draw(GameTime gameTime) {
  182.             GraphicsDevice.Clear(Color.CornflowerBlue);
  183.  
  184.             helpOrgn = helpFont.MeasureString(helpString) / 2;
  185.  
  186.             infoOrgn = info.MeasureString(infoString) / 2;
  187.  
  188.             string scoreLeft = Ball.ScoreLeft.ToString();
  189.             string scoreRight = Ball.ScoreRight.ToString();
  190.             rightScorePos.X = graphics.PreferredBackBufferWidth - 10 - score.MeasureString(scoreRight).X;
  191.  
  192.             spriteBatch.Begin();
  193.  
  194.             spriteBatch.Draw(background, new Vector2(0), Color.Green);
  195.  
  196.             spriteBatch.DrawString(helpFont, helpString, helpPos, Color.White, 0, helpOrgn, 1, SpriteEffects.None, 0);
  197.  
  198.             spriteBatch.DrawString(info, infoString, infoPos, Color.Tomato, 0, infoOrgn, 1, SpriteEffects.None, 0);
  199.  
  200.             spriteBatch.DrawString(score, scoreLeft, leftScorePos, Color.White);
  201.             spriteBatch.DrawString(score, scoreRight, rightScorePos, Color.White);
  202.  
  203.             spriteBatch.End();
  204.  
  205.             Ball.Draw(spriteBatch, ball, Color.White, Left.recPad, Right.recPad);
  206.             Left.Draw(spriteBatch, padTexture, Color.White);
  207.             Right.Draw(spriteBatch, padTexture, Color.White);
  208.  
  209.             base.Draw(gameTime);
  210.         }
  211.     }
  212. }
  213.  
  214. /* pad.cs */
  215. using System;
  216. using System.Collections.Generic;
  217. using System.Linq;
  218. using Microsoft.Xna.Framework;
  219. using Microsoft.Xna.Framework.Audio;
  220. using Microsoft.Xna.Framework.Content;
  221. using Microsoft.Xna.Framework.GamerServices;
  222. using Microsoft.Xna.Framework.Graphics;
  223. using Microsoft.Xna.Framework.Input;
  224. using Microsoft.Xna.Framework.Media;
  225.  
  226. namespace GroundZero_sPong {
  227.     class pad {
  228.         private Rectangle borders;
  229.         public Rectangle recPad;
  230.         private byte speed;
  231.         private bool paused;
  232.  
  233.         public pad(Rectangle gameBorders, Rectangle recPad, byte speed, bool startPaused) {
  234.             this.borders = gameBorders;
  235.             this.recPad = recPad;
  236.             this.speed = speed;
  237.  
  238.             if (startPaused) Pause();
  239.         }
  240.  
  241.         /* This made me unable to move the recPad (I used RecPad where I now use recPad)
  242.         public Rectangle RecPad {
  243.             get { return recPad; }
  244.             set { recPad = value; }
  245.         } */
  246.  
  247.         public void Pause() {
  248.             paused = !paused;
  249.         }
  250.  
  251.         public void MoveUp() {
  252.             if (!paused)
  253.                 recPad.Offset(0, -speed);
  254.  
  255.             CheckBorders();
  256.         }
  257.  
  258.         public void MoveDown() {
  259.             if (!paused)
  260.                 recPad.Offset(0, speed);
  261.  
  262.             CheckBorders();
  263.         }
  264.  
  265.         private void CheckBorders() {
  266.             MathHelper.Clamp(recPad.Y, borders.Top, borders.Bottom - recPad.Height);
  267.         }
  268.  
  269.         public void Draw(SpriteBatch sBatch, Texture2D texture, Color color) {
  270.             sBatch.Begin();
  271.             sBatch.Draw(texture, recPad, color);
  272.             sBatch.End();
  273.         }
  274.  
  275.     }/*pad*/
  276. }/*GroundZero_sPong*/
  277.  
  278. /* ball.cs */
  279. using System;
  280. using System.Collections.Generic;
  281. using System.Linq;
  282. using Microsoft.Xna.Framework;
  283. using Microsoft.Xna.Framework.Audio;
  284. using Microsoft.Xna.Framework.Content;
  285. using Microsoft.Xna.Framework.GamerServices;
  286. using Microsoft.Xna.Framework.Graphics;
  287. using Microsoft.Xna.Framework.Input;
  288. using Microsoft.Xna.Framework.Media;
  289.  
  290. namespace GroundZero_sPong {
  291.     class ball {
  292.         private Vector2 movement, movementSaved;
  293.         private Rectangle borders, myBounds, left, right;
  294.         SoundEffect score, bounce;
  295.         private byte scoreLeft, scoreRight;
  296.         private bool paused;
  297.  
  298.         public byte ScoreLeft {
  299.             get { return scoreLeft; }
  300.             private set { scoreLeft = value; }
  301.         }
  302.  
  303.         public byte ScoreRight {
  304.             get { return scoreRight; }
  305.             private set { scoreRight = value; }
  306.         }
  307.  
  308.         public ball(byte radius, Rectangle borders, Vector2 topLeft, Vector2 movement, SoundEffect score, SoundEffect bounce, bool startPaused) {
  309.             this.borders = borders;
  310.             this.movement = movement;
  311.             this.score = score;
  312.             this.bounce = bounce;
  313.  
  314.             myBounds = new Rectangle((int)topLeft.X, (int)topLeft.Y, radius, radius);
  315.  
  316.             if (startPaused) Pause();
  317.         }
  318.  
  319.         public void Pause() {
  320.             if (paused) { // unpause
  321.                 movement = movementSaved;
  322.                 paused = false;
  323.             } else { // pause
  324.                 movementSaved = movement;
  325.                 movement = new Vector2(0);
  326.                 paused = true;
  327.             }
  328.         }
  329.  
  330.         private void Move() {
  331.             myBounds.Offset((int)movement.X, (int)movement.Y);
  332.  
  333.             if (myBounds.Top < borders.Top) {
  334.                 myBounds.Offset(-borders.Top, 0);
  335.                 movement.Y *= -1;
  336.             } else if (myBounds.Bottom > borders.Bottom) {
  337.                 myBounds.Offset(borders.Bottom - myBounds.Bottom, 0);
  338.                 movement.Y *= -1;
  339.             }
  340.  
  341.             CheckBounce();
  342.             CheckScore();
  343.         }
  344.  
  345.         private void CheckScore() {
  346.             bool scored = false;
  347.             if (myBounds.Left < borders.Left) {
  348.                 ScoreLeft++;
  349.                 myBounds.Location = new Point(left.Right + 1, right.Center.Y);
  350.                 scored = true;
  351.             } else if (myBounds.Right > borders.Right) {
  352.                 ScoreRight++;
  353.                 myBounds.Location = new Point(right.Left - myBounds.Width - 1, right.Center.Y);
  354.                 scored = true;
  355.             }
  356.  
  357.             if (scored) {
  358.                 score.Play();
  359.                 movement.X *= -1;
  360.             }
  361.         }
  362.  
  363.         public void CheckBounce() {
  364.             if ((myBounds.Intersects(left) && movement.X < 0) || (myBounds.Intersects(right) && movement.X > 0)) {
  365.                 movement.X *= -1;
  366.                 bounce.Play();
  367.             }
  368.         }
  369.  
  370.         public void Draw(SpriteBatch sBatch, Texture2D texture, Color color, Rectangle left, Rectangle right) {
  371.             this.left = left;
  372.             this.right = right;
  373.  
  374.             Move();
  375.  
  376.             sBatch.Begin();
  377.             sBatch.Draw(texture, myBounds, color);
  378.             sBatch.End();
  379.         }
  380.     }
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement