Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- #region File Description
- //-----------------------------------------------------------------------------
- // GameplayScreen.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Threading;
- using System.Linq;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using Drawing;
- #endregion
- namespace MyXnaGame
- {
- /// <summary>
- /// This screen implements the actual game logic. It is just a
- /// placeholder to get the idea across: you'll probably want to
- /// put some more interesting gameplay in here!
- /// </summary>
- class GameplayScreen : GameScreen
- {
- Ball myBall;
- Bat rightBat;
- Bat leftBat;
- Input myInputClass;
- HudContainer hudContainer = new HudContainer();
- SafeArea safeArea;
- Vector2 safeTopLeft = Vector2.Zero; // (2.0f * (safeArea.dx), 1.5f * (safeArea.dy));
- ContentManager content;
- SpriteFont gameFont;
- SpriteFont hudFont;
- SpriteFont arial;
- // public Input input;
- private int screenWidth;
- private int screenHeight;
- private Texture2D backgroundTexture;
- public Song PlayingSong { get; private set; }
- Vector2 enemyPosition = new Vector2(100, 100);
- Random random = new Random();
- float pauseAlpha;
- public GameplayScreen()
- {
- safeArea = new SafeArea();
- TransitionOnTime = TimeSpan.FromSeconds(1.5);
- TransitionOffTime = TimeSpan.FromSeconds(0.5);
- }
- public override void LoadContent()
- {
- if (content == null)
- content = new ContentManager(ScreenManager.Game.Services, "Content");
- DrawingHelper.Initialize(ScreenManager.GraphicsDevice);
- safeArea.LoadGraphicsContent(ScreenManager.GraphicsDevice);
- gameFont = content.Load<SpriteFont>("gamefont");
- hudFont = content.Load<SpriteFont>("menufont");
- hudContainer.LoadGraphicsContent(ScreenManager.GraphicsDevice, hudFont, safeArea);
- var hudInfo = new HudTextComponent("...Ninja Cats!", HudComponent.PresetPosition.BottomRight);
- hudContainer.Add(hudInfo);
- myInputClass = new Input();
- PlayingSong = content.Load<Song>(@"sfx/boomer");
- MediaPlayer.Play(PlayingSong);
- backgroundTexture = content.Load<Texture2D>(@"gfx/mm7Background");
- myBall = new Ball(content, new Vector2 (ScreenManager.GraphicsDevice.Viewport.Width,
- ScreenManager.GraphicsDevice.Viewport.Height));
- rightBat = new Bat(content, new Vector2 (ScreenManager.GraphicsDevice.Viewport.Width,
- ScreenManager.GraphicsDevice.Viewport.Height), true);
- leftBat = new Bat(content, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width,
- ScreenManager.GraphicsDevice.Viewport.Height), false);
- // A real game would probably have more content than this sample, so
- // it would take longer to load. We simulate that by delaying for a
- // while, giving you a chance to admire the beautiful loading screen.
- Thread.Sleep(1000);
- // once the load has finished, we use ResetElapsedTime to tell the game's
- // timing mechanism that we have just finished a very long frame, and that
- // it should not try to catch up.
- ScreenManager.Game.ResetElapsedTime();
- }
- public override void UnloadContent()
- {
- content.Unload();
- }
- public override void Update(GameTime gameTime, bool otherScreenHasFocus,
- bool coveredByOtherScreen)
- {
- base.Update(gameTime, otherScreenHasFocus, false);
- // Gradually fade in or out depending on whether we are covered by the pause screen.
- if (coveredByOtherScreen)
- pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
- else
- pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
- }
- /// <summary>
- /// Lets the game respond to player input. Unlike the Update method,
- /// this will only be called when the gameplay screen is active.
- /// </summary>
- public override void HandleInput(InputState input) //, GameTime gameTime)
- {
- if (input == null)
- throw new ArgumentNullException("input");
- // Look up inputs for the active player profile.
- int playerIndex = (int)ControllingPlayer.Value;
- KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
- GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
- // The game pauses either if the user presses the pause button, or if
- // they unplug the active gamepad. This requires us to keep track of
- // whether a gamepad was ever plugged in, because we don't want to pause
- // on PC if they are playing with a keyboard and have no gamepad at all!
- bool gamePadDisconnected = !gamePadState.IsConnected &&
- input.GamePadWasConnected[playerIndex];
- if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
- {
- ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
- }
- if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
- {
- ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
- }
- else
- {
- // If/ElseIf is used so we don't move up AND down (by holding both buttons). It's either up OR down, not both
- if (myInputClass.LeftUp)
- {
- leftBat.position.Y-=10; //Decrease the Y position by 1 every frame. (Move towards top of screen)
- }
- else if (myInputClass.LeftDown)
- {
- leftBat.position.Y+=10; //Increase the Y position by 1 every frame. (Move towards bottom of screen)
- }
- //If/ElseIf is used so we don't move up AND down (by holding both buttons). It's either up OR down, not both
- if (myInputClass.RightUp)
- {
- rightBat.position.Y-=10; //Decrease the Y position by 1 every frame. (Move towards top of screen)
- }
- else if (myInputClass.RightDown)
- {
- rightBat.position.Y+=10; //Increase the Y position by 1 every frame. (Move towards bottom of screen)
- }
- }
- }
- public override void Draw(GameTime gameTime)
- {
- // Our player and enemy are both actually just text strings.
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- spriteBatch.Begin();
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, 800, 600), Color.White);
- // draw hud
- hudContainer.Draw(spriteBatch);
- spriteBatch.DrawString(gameFont, "XNA Basic Starter Kit!",
- enemyPosition, Color.Black);
- #if DEBUG // I've temporarily commented out the safeArea debug -DV
- // safeArea.Draw(spriteBatch);
- #endif
- // debug info
- var safeAreaInfo = "dx=" + safeArea.dx + ",dy=" + safeArea.dy
- + ",w=" + safeArea.width + ",h=" + safeArea.height;
- //+ ", H = " + playerHorizDirection
- //+ ", V = " + playerVertDirection;
- spriteBatch.DrawString(gameFont, safeAreaInfo, safeTopLeft, Color.Green);
- // Draws all pong related graphics
- myBall.Draw(spriteBatch);
- rightBat.Draw(spriteBatch);
- leftBat.Draw(spriteBatch);
- spriteBatch.End();
- // If the game is transitioning on or off, fade it out to black.
- if (TransitionPosition > 0 || pauseAlpha > 0)
- {
- float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
- ScreenManager.FadeBackBufferToBlack(alpha);
- }
- }
- }
- }
- ------------------------------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework.Input;
- namespace MyXnaGame
- {
- class Input
- {
- public KeyboardState keyboardState;
- public KeyboardState lastState;
- public Input()
- {
- keyboardState = Keyboard.GetState();
- lastState = keyboardState;
- }
- public void Update()
- {
- lastState = keyboardState;
- keyboardState = Keyboard.GetState();
- }
- public bool RightUp
- {
- get
- {
- return keyboardState.IsKeyDown(Keys.Up);
- }
- }
- public bool RightDown
- {
- get
- {
- return keyboardState.IsKeyDown(Keys.Down);
- }
- }
- public bool LeftUp
- {
- get
- {
- return keyboardState.IsKeyDown(Keys.W);
- }
- }
- public bool LeftDown
- {
- get
- {
- return keyboardState.IsKeyDown(Keys.S);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement