Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Please excuse the mess. It's under construction. I receive this error:
- 'MyXnaGame.Ball' does not contain a definition for 'Ball' and no extension method 'Ball' accepting a first argument of type 'MyXnaGame.Ball' could be found (are you missing a using directive or an assembly reference?)
- I'm basically trying to draw my ball onto the screen. You'll see I've commented out his code for "mySprite", which is basically his little character he had running around the screen. I tried to copy that format, but I can't get this damn thing to work.
- #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 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
- {
- #region Fields
- // Sprite mySprite;
- //
- Ball myBall;
- HudContainer hudContainer = new HudContainer();
- Texture2D clockTexture;
- SafeArea safeArea;
- Vector2 safeTopLeft = Vector2.Zero; // (2.0f * (safeArea.dx), 1.5f * (safeArea.dy));
- ContentManager content;
- SpriteFont gameFont;
- SpriteFont hudFont;
- private Texture2D backgroundTexture;
- public Song PlayingSong { get; private set; }
- Vector2 enemyPosition = new Vector2(100, 100);
- Random random = new Random();
- float pauseAlpha;
- #endregion
- #region Initialization
- /// <summary>
- /// Constructor.
- /// </summary>
- public GameplayScreen()
- {
- //
- safeArea = new SafeArea();
- // mySprite = new Sprite();
- TransitionOnTime = TimeSpan.FromSeconds(1.5);
- TransitionOffTime = TimeSpan.FromSeconds(0.5);
- }
- /// <summary>
- /// Load graphics content for the game.
- /// </summary>
- public override void LoadContent()
- {
- if (content == null)
- content = new ContentManager(ScreenManager.Game.Services, "Content");
- DrawingHelper.Initialize(ScreenManager.GraphicsDevice);
- PlayingSong = content.Load<Song>(@"sfx/boomer");
- MediaPlayer.Play(PlayingSong);
- backgroundTexture = content.Load<Texture2D>(@"gfx/mm7Background");
- //
- gameFont = content.Load<SpriteFont>("gamefont");
- hudFont = content.Load<SpriteFont>("menufont");
- //
- safeArea.LoadGraphicsContent(ScreenManager.GraphicsDevice);
- //
- hudContainer.LoadGraphicsContent(ScreenManager.GraphicsDevice, hudFont, safeArea);
- var hudScore = new HudTextComponent("SCORE: 100", HudComponent.PresetPosition.TopLeft);
- var hudHealth = new HudTextComponent("HEALTH: 300", HudComponent.PresetPosition.TopRight);
- var hudLives = new HudTextComponent("LIVES: 3", HudComponent.PresetPosition.MiddleLeft);
- var hudAmmo = new HudTextComponent("Ammo: 447", HudComponent.PresetPosition.MiddleRight);
- var hudName = new HudTextComponent("Angry Zombie...", HudComponent.PresetPosition.BottomLeft);
- var hudInfo = new HudTextComponent("...Ninja Cats!", HudComponent.PresetPosition.BottomRight);
- // I've commente out the hud variables -DV
- /* //
- hudContainer.Add(hudScore);
- hudContainer.Add(hudHealth);
- hudContainer.Add(hudLives);
- hudContainer.Add(hudAmmo); ;
- hudContainer.Add(hudName);
- hudContainer.Add(hudInfo);
- */ //
- clockTexture = content.Load<Texture2D>("HudGraphics/clock");
- var hudClock = new HudGraphicComponent(clockTexture, HudComponent.PresetPosition.TopCenter);
- // hudContainer.Add(hudClock);
- //
- // mySprite.LoadContent(content);
- myBall.Ball(content);
- // 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();
- }
- /// <summary>
- /// Unload graphics content used by the game.
- /// </summary>
- public override void UnloadContent()
- {
- content.Unload();
- }
- #endregion
- #region Update and Draw
- /// <summary>
- /// Updates the state of the game. This method checks the GameScreen.IsActive
- /// property, so the game will stop updating when the pause menu is active,
- /// or if you tab away to a different application.
- /// </summary>
- 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);
- if (IsActive)
- {
- // Apply some random jitter to make the enemy move around.
- const float randomization = 10;
- enemyPosition.X += (float)(random.NextDouble() - 0.5) * randomization;
- enemyPosition.Y += (float)(random.NextDouble() - 0.5) * randomization;
- // Apply a stabilizing force to stop the enemy moving off the screen.
- Vector2 targetPosition = new Vector2(
- ScreenManager.GraphicsDevice.Viewport.Width / 2 - gameFont.MeasureString("XNA Basic Starter Kit!").X / 2,
- 200);
- enemyPosition = Vector2.Lerp(enemyPosition, targetPosition, 0.05f);
- // mySprite.Update(gameTime);
- myBall.UpdatePosition();
- }
- }
- /// <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);
- }
- /* else
- {
- mySprite.HandleInput(keyboardState, gamePadState, input, safeArea);
- }
- */
- }
- /// <summary>
- /// Draws the gameplay screen.
- /// </summary>
- 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);
- //
- // mySprite.Draw(spriteBatch);
- //
- myBall.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);
- }
- }
- #endregion
- }
- }
- -------------------------------------------------------------------------------------
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- namespace MyXnaGame
- {
- class Ball
- {
- private bool isVisible;
- private Vector2 position;
- private double direction;
- private Texture2D texture;
- private Rectangle size;
- private float speed;
- private float moveSpeed;
- private Vector2 resetPos;
- Random rand;
- public Ball(ContentManager content, Vector2 screenSize)
- {
- moveSpeed = 8f;
- speed = 0;
- texture = content.Load<Texture2D>(@"gfx/ball");
- direction = 0;
- size = new Rectangle(0, 0, texture.Width, texture.Height);
- resetPos = new Vector2(screenSize.X / 2, screenSize.Y / 2);
- position = resetPos;
- rand = new Random();
- isVisible = true;
- }
- public void UpdatePosition()
- {
- size.X = (int)position.X;
- size.Y = (int)position.Y;
- position.X += speed * (float)Math.Cos(direction);
- position.Y += speed * (float)Math.Sin(direction);
- CheckWallHit();
- }
- public Rectangle GetSize()
- {
- return size;
- }
- public double GetDirection()
- {
- return direction;
- }
- public Vector2 GetPosition()
- {
- return position;
- }
- public void BatHit(int block)
- {
- if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
- {
- switch (block)
- {
- case 1:
- direction = MathHelper.ToRadians(220);
- break;
- case 2:
- direction = MathHelper.ToRadians(215);
- break;
- case 3:
- direction = MathHelper.ToRadians(200);
- break;
- case 4:
- direction = MathHelper.ToRadians(195);
- break;
- case 5:
- direction = MathHelper.ToRadians(180);
- break;
- case 6:
- direction = MathHelper.ToRadians(180);
- break;
- case 7:
- direction = MathHelper.ToRadians(165);
- break;
- case 8:
- direction = MathHelper.ToRadians(130);
- break;
- case 9:
- direction = MathHelper.ToRadians(115);
- break;
- case 10:
- direction = MathHelper.ToRadians(110);
- break;
- }
- }
- else
- {
- switch (block)
- {
- case 1:
- direction = MathHelper.ToRadians(290);
- break;
- case 2:
- direction = MathHelper.ToRadians(295);
- break;
- case 3:
- direction = MathHelper.ToRadians(310);
- break;
- case 4:
- direction = MathHelper.ToRadians(345);
- break;
- case 5:
- direction = MathHelper.ToRadians(0);
- break;
- case 6:
- direction = MathHelper.ToRadians(0);
- break;
- case 7:
- direction = MathHelper.ToRadians(15);
- break;
- case 8:
- direction = MathHelper.ToRadians(50);
- break;
- case 9:
- direction = MathHelper.ToRadians(65);
- break;
- case 10:
- direction = MathHelper.ToRadians(70);
- break;
- }
- }
- }
- private void CheckWallHit()
- {
- while (direction > 2 * Math.PI) direction -= 2 * Math.PI;
- while (direction < 0) direction += 2 * Math.PI;
- if (position.Y <= 0 || (position.Y > resetPos.Y * 2 - size.Height))
- {
- direction = 2 * Math.PI - direction;
- }
- }
- public void Stop()
- {
- isVisible = false;
- speed = 0;
- }
- public void Reset(bool left)
- {
- if (left) direction = 0;
- else direction = Math.PI;
- position = resetPos;
- isVisible = true;
- speed = moveSpeed;
- }
- public void Draw(SpriteBatch batch)
- {
- if (isVisible)
- {
- batch.Draw(texture, position, Color.White);
- }
- }
- }
- }
RAW Paste Data