Advertisement
DaveVoyles

Dave Broken Inputs Pong

Apr 2nd, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.14 KB | None | 0 0
  1. I broke something with my inputs again. I can't get the paddles to go up or down. That's what I get when I fiddle with things.
  2.  
  3.  
  4.  
  5. #region File Description
  6. //-----------------------------------------------------------------------------
  7. // GameplayScreen.cs
  8. //
  9. // Microsoft XNA Community Game Platform
  10. // Copyright (C) Microsoft Corporation. All rights reserved.
  11. //-----------------------------------------------------------------------------
  12. #endregion
  13.  
  14. #region Using Statements
  15. using System;
  16. using System.Threading;
  17. using System.Linq;
  18. using System.Collections.Generic;
  19. using Microsoft.Xna.Framework;
  20. using Microsoft.Xna.Framework.Content;
  21. using Microsoft.Xna.Framework.Graphics;
  22. using Microsoft.Xna.Framework.Input;
  23. using Microsoft.Xna.Framework.Media;
  24. using Drawing;
  25. #endregion
  26.  
  27. namespace MyXnaGame
  28. {
  29.     /// <summary>
  30.     /// This screen implements the actual game logic. It is just a
  31.     /// placeholder to get the idea across: you'll probably want to
  32.     /// put some more interesting gameplay in here!
  33.     /// </summary>
  34.     class GameplayScreen : GameScreen
  35.     {    
  36.         Ball myBall;
  37.         Bat rightBat;
  38.         Bat leftBat;
  39.         Input myInputClass;      
  40.         HudContainer hudContainer = new HudContainer();      
  41.         SafeArea safeArea;
  42.         Vector2 safeTopLeft = Vector2.Zero; // (2.0f * (safeArea.dx), 1.5f * (safeArea.dy));
  43.         ContentManager content;
  44.         SpriteFont gameFont;
  45.         SpriteFont hudFont;
  46.         SpriteFont arial;
  47. //        public Input input;
  48.         private int screenWidth;
  49.         private int screenHeight;
  50.         private Texture2D backgroundTexture;
  51.         public Song PlayingSong { get; private set; }
  52.         Vector2 enemyPosition = new Vector2(100, 100);
  53.         Random random = new Random();
  54.         float pauseAlpha;
  55.  
  56.  
  57.  
  58.         public GameplayScreen()
  59.         {
  60.             safeArea = new SafeArea();
  61.             TransitionOnTime = TimeSpan.FromSeconds(1.5);
  62.             TransitionOffTime = TimeSpan.FromSeconds(0.5);
  63.         }
  64.  
  65.         public override void LoadContent()
  66.         {
  67.             if (content == null)
  68.                 content = new ContentManager(ScreenManager.Game.Services, "Content");
  69.             DrawingHelper.Initialize(ScreenManager.GraphicsDevice);
  70.             safeArea.LoadGraphicsContent(ScreenManager.GraphicsDevice);
  71.             gameFont = content.Load<SpriteFont>("gamefont");
  72.             hudFont = content.Load<SpriteFont>("menufont");          
  73.             hudContainer.LoadGraphicsContent(ScreenManager.GraphicsDevice, hudFont, safeArea);
  74.             var hudInfo = new HudTextComponent("...Ninja Cats!", HudComponent.PresetPosition.BottomRight);
  75.             hudContainer.Add(hudInfo);
  76.  
  77.             myInputClass = new Input();
  78.             PlayingSong = content.Load<Song>(@"sfx/boomer");
  79.             MediaPlayer.Play(PlayingSong);
  80.             backgroundTexture = content.Load<Texture2D>(@"gfx/mm7Background");            
  81.            
  82.             myBall = new Ball(content, new Vector2 (ScreenManager.GraphicsDevice.Viewport.Width,
  83.             ScreenManager.GraphicsDevice.Viewport.Height));        
  84.             rightBat = new Bat(content, new Vector2 (ScreenManager.GraphicsDevice.Viewport.Width,
  85.                 ScreenManager.GraphicsDevice.Viewport.Height), true);
  86.             leftBat = new Bat(content, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width,
  87.                 ScreenManager.GraphicsDevice.Viewport.Height), false);
  88.                      
  89.             // A real game would probably have more content than this sample, so
  90.             // it would take longer to load. We simulate that by delaying for a
  91.             // while, giving you a chance to admire the beautiful loading screen.
  92.             Thread.Sleep(1000);
  93.  
  94.             // once the load has finished, we use ResetElapsedTime to tell the game's
  95.             // timing mechanism that we have just finished a very long frame, and that
  96.             // it should not try to catch up.
  97.             ScreenManager.Game.ResetElapsedTime();
  98.         }
  99.  
  100.  
  101.         public override void UnloadContent()
  102.         {
  103.             content.Unload();
  104.         }
  105.  
  106.    
  107.         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  108.                                                        bool coveredByOtherScreen)
  109.         {
  110.             base.Update(gameTime, otherScreenHasFocus, false);
  111.  
  112.             // Gradually fade in or out depending on whether we are covered by the pause screen.
  113.             if (coveredByOtherScreen)
  114.                 pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
  115.             else
  116.                 pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);            
  117.         }
  118.        
  119.  
  120.         /// <summary>
  121.         /// Lets the game respond to player input. Unlike the Update method,
  122.         /// this will only be called when the gameplay screen is active.
  123.         /// </summary>
  124.         public override void HandleInput(InputState input) //, GameTime gameTime)
  125.         {
  126.             if (input == null)
  127.                 throw new ArgumentNullException("input");
  128.  
  129.             // Look up inputs for the active player profile.
  130.             int playerIndex = (int)ControllingPlayer.Value;
  131.  
  132.             KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
  133.             GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
  134.  
  135.             // The game pauses either if the user presses the pause button, or if
  136.             // they unplug the active gamepad. This requires us to keep track of
  137.             // whether a gamepad was ever plugged in, because we don't want to pause
  138.             // on PC if they are playing with a keyboard and have no gamepad at all!
  139.             bool gamePadDisconnected = !gamePadState.IsConnected &&
  140.                                        input.GamePadWasConnected[playerIndex];
  141.  
  142.             if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
  143.             {
  144.                 ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
  145.             }
  146.             if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
  147.             {
  148.                 ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
  149.             }
  150.             else
  151.             {
  152.                 // If/ElseIf is used so we don't move up AND down (by holding both buttons). It's either up OR down, not both
  153.                 if (myInputClass.LeftUp)
  154.                 {
  155.                     leftBat.position.Y-=10; //Decrease the Y position by 1 every frame. (Move towards top of screen)
  156.                 }
  157.                 else if (myInputClass.LeftDown)
  158.                 {
  159.                     leftBat.position.Y+=10; //Increase the Y position by 1 every frame. (Move towards bottom of screen)
  160.                 }
  161.  
  162.                 //If/ElseIf is used so we don't move up AND down (by holding both buttons). It's either up OR down, not both
  163.                 if (myInputClass.RightUp)
  164.                 {
  165.                     rightBat.position.Y-=10; //Decrease the Y position by 1 every frame. (Move towards top of screen)
  166.                 }
  167.                 else if (myInputClass.RightDown)
  168.                 {
  169.                     rightBat.position.Y+=10; //Increase the Y position by 1 every frame. (Move towards bottom of screen)
  170.                 }
  171.             }
  172.         }
  173.  
  174.  
  175.         public override void Draw(GameTime gameTime)
  176.         {
  177.             // Our player and enemy are both actually just text strings.
  178.             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  179.  
  180.             spriteBatch.Begin();
  181.  
  182.             spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, 800, 600), Color.White);        
  183.            
  184.             // draw hud
  185.             hudContainer.Draw(spriteBatch);
  186.             spriteBatch.DrawString(gameFont, "XNA Basic Starter Kit!",
  187.                                    enemyPosition, Color.Black);
  188.  
  189. #if DEBUG     // I've temporarily commented out the safeArea debug -DV  
  190. //            safeArea.Draw(spriteBatch);
  191. #endif
  192.  
  193.             // debug info
  194.             var safeAreaInfo = "dx=" + safeArea.dx + ",dy=" + safeArea.dy
  195.                 + ",w=" + safeArea.width + ",h=" + safeArea.height;
  196.                 //+ ", H = " + playerHorizDirection
  197.                 //+ ", V = " + playerVertDirection;
  198.             spriteBatch.DrawString(gameFont, safeAreaInfo, safeTopLeft, Color.Green);
  199.  
  200.             // Draws all pong related graphics
  201.             myBall.Draw(spriteBatch);
  202.             rightBat.Draw(spriteBatch);
  203.             leftBat.Draw(spriteBatch);
  204.            
  205.             spriteBatch.End();
  206.  
  207.            // If the game is transitioning on or off, fade it out to black.
  208.             if (TransitionPosition > 0 || pauseAlpha > 0)
  209.             {
  210.                 float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
  211.                 ScreenManager.FadeBackBufferToBlack(alpha);
  212.             }
  213.         }
  214.     }
  215. }
  216.  
  217.  
  218. ------------------------------------------------------------------------------------------------
  219.  
  220.  
  221. using System;
  222. using System.Collections.Generic;
  223. using System.Linq;
  224. using System.Text;
  225. using Microsoft.Xna.Framework.Input;  
  226.  
  227. namespace MyXnaGame
  228. {
  229.     class Input
  230.     {
  231.         public KeyboardState keyboardState;
  232.         public KeyboardState lastState;
  233.  
  234.         public Input()
  235.         {
  236.             keyboardState = Keyboard.GetState();
  237.             lastState = keyboardState;
  238.         }
  239.  
  240.         public void Update()
  241.         {
  242.             lastState = keyboardState;
  243.             keyboardState = Keyboard.GetState();
  244.         }
  245.  
  246.         public bool RightUp
  247.         {
  248.             get
  249.             {
  250.                 return keyboardState.IsKeyDown(Keys.Up);
  251.             }
  252.         }
  253.  
  254.         public bool RightDown
  255.         {
  256.             get
  257.             {
  258.                 return keyboardState.IsKeyDown(Keys.Down);
  259.             }
  260.         }
  261.  
  262.         public bool LeftUp
  263.         {
  264.             get
  265.             {
  266.                 return keyboardState.IsKeyDown(Keys.W);
  267.             }
  268.         }
  269.  
  270.         public bool LeftDown
  271.         {
  272.             get
  273.             {
  274.                 return keyboardState.IsKeyDown(Keys.S);
  275.             }
  276.         }
  277.     }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement