Guest User

Dave Pong

a guest
Mar 27th, 2012
31
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Please excuse the mess. It's under construction. I receive this error:
  2.  
  3. '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?)  
  4.  
  5. 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.
  6.  
  7.  
  8.  
  9.  
  10.  
  11. #region File Description
  12. //-----------------------------------------------------------------------------
  13. // GameplayScreen.cs
  14. //
  15. // Microsoft XNA Community Game Platform
  16. // Copyright (C) Microsoft Corporation. All rights reserved.
  17. //-----------------------------------------------------------------------------
  18. #endregion
  19.  
  20. #region Using Statements
  21. using System;
  22. using System.Threading;
  23. using Microsoft.Xna.Framework;
  24. using Microsoft.Xna.Framework.Content;
  25. using Microsoft.Xna.Framework.Graphics;
  26. using Microsoft.Xna.Framework.Input;
  27. using Microsoft.Xna.Framework.Media;
  28. using Drawing;
  29. #endregion
  30.  
  31. namespace MyXnaGame
  32. {
  33.     /// <summary>
  34.     /// This screen implements the actual game logic. It is just a
  35.     /// placeholder to get the idea across: you'll probably want to
  36.     /// put some more interesting gameplay in here!
  37.     /// </summary>
  38.     class GameplayScreen : GameScreen
  39.     {
  40.         #region Fields
  41. //        Sprite mySprite;
  42.         //
  43.         Ball myBall;
  44.  
  45.         HudContainer hudContainer = new HudContainer();
  46.         Texture2D clockTexture;
  47.  
  48.         SafeArea safeArea;
  49.         Vector2 safeTopLeft = Vector2.Zero; // (2.0f * (safeArea.dx), 1.5f * (safeArea.dy));
  50.         ContentManager content;
  51.         SpriteFont gameFont;
  52.         SpriteFont hudFont;
  53.         private Texture2D backgroundTexture;
  54.         public Song PlayingSong { get; private set; }
  55.  
  56.         Vector2 enemyPosition = new Vector2(100, 100);
  57.  
  58.         Random random = new Random();
  59.  
  60.         float pauseAlpha;
  61.  
  62.         #endregion
  63.  
  64.         #region Initialization
  65.  
  66.  
  67.         /// <summary>
  68.         /// Constructor.
  69.         /// </summary>
  70.         public GameplayScreen()
  71.         {
  72.             //
  73.             safeArea = new SafeArea();
  74. //            mySprite = new Sprite();
  75.            
  76.             TransitionOnTime = TimeSpan.FromSeconds(1.5);
  77.             TransitionOffTime = TimeSpan.FromSeconds(0.5);
  78.         }
  79.  
  80.  
  81.         /// <summary>
  82.         /// Load graphics content for the game.
  83.         /// </summary>
  84.         public override void LoadContent()
  85.         {
  86.             if (content == null)
  87.                 content = new ContentManager(ScreenManager.Game.Services, "Content");
  88.  
  89.             DrawingHelper.Initialize(ScreenManager.GraphicsDevice);
  90.             PlayingSong = content.Load<Song>(@"sfx/boomer");
  91.             MediaPlayer.Play(PlayingSong);
  92.             backgroundTexture = content.Load<Texture2D>(@"gfx/mm7Background");
  93.             //
  94.             gameFont = content.Load<SpriteFont>("gamefont");
  95.             hudFont = content.Load<SpriteFont>("menufont");
  96.             //
  97.             safeArea.LoadGraphicsContent(ScreenManager.GraphicsDevice);
  98.             //
  99.             hudContainer.LoadGraphicsContent(ScreenManager.GraphicsDevice, hudFont, safeArea);
  100.             var hudScore = new HudTextComponent("SCORE: 100", HudComponent.PresetPosition.TopLeft);
  101.             var hudHealth = new HudTextComponent("HEALTH: 300", HudComponent.PresetPosition.TopRight);
  102.             var hudLives = new HudTextComponent("LIVES: 3", HudComponent.PresetPosition.MiddleLeft);
  103.             var hudAmmo = new HudTextComponent("Ammo: 447", HudComponent.PresetPosition.MiddleRight);
  104.             var hudName = new HudTextComponent("Angry Zombie...", HudComponent.PresetPosition.BottomLeft);
  105.             var hudInfo = new HudTextComponent("...Ninja Cats!", HudComponent.PresetPosition.BottomRight);
  106.  
  107.             // I've commente out the hud variables -DV
  108. /*            //
  109.             hudContainer.Add(hudScore);
  110.             hudContainer.Add(hudHealth);
  111.             hudContainer.Add(hudLives);
  112.             hudContainer.Add(hudAmmo); ;
  113.             hudContainer.Add(hudName);
  114.             hudContainer.Add(hudInfo);
  115.  */          //
  116.             clockTexture = content.Load<Texture2D>("HudGraphics/clock");
  117.             var hudClock = new HudGraphicComponent(clockTexture, HudComponent.PresetPosition.TopCenter);
  118.  //           hudContainer.Add(hudClock);
  119.             //
  120. //            mySprite.LoadContent(content);
  121.             myBall.Ball(content);
  122.              
  123.            
  124.  
  125.             // A real game would probably have more content than this sample, so
  126.             // it would take longer to load. We simulate that by delaying for a
  127.             // while, giving you a chance to admire the beautiful loading screen.
  128.             Thread.Sleep(1000);
  129.  
  130.             // once the load has finished, we use ResetElapsedTime to tell the game's
  131.             // timing mechanism that we have just finished a very long frame, and that
  132.             // it should not try to catch up.
  133.             ScreenManager.Game.ResetElapsedTime();
  134.         }
  135.  
  136.  
  137.         /// <summary>
  138.         /// Unload graphics content used by the game.
  139.         /// </summary>
  140.         public override void UnloadContent()
  141.         {
  142.             content.Unload();
  143.         }
  144.  
  145.  
  146.         #endregion
  147.  
  148.         #region Update and Draw
  149.  
  150.  
  151.         /// <summary>
  152.         /// Updates the state of the game. This method checks the GameScreen.IsActive
  153.         /// property, so the game will stop updating when the pause menu is active,
  154.         /// or if you tab away to a different application.
  155.         /// </summary>
  156.         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  157.                                                        bool coveredByOtherScreen)
  158.         {
  159.             base.Update(gameTime, otherScreenHasFocus, false);
  160.  
  161.             // Gradually fade in or out depending on whether we are covered by the pause screen.
  162.             if (coveredByOtherScreen)
  163.                 pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
  164.             else
  165.                 pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
  166.  
  167.             if (IsActive)
  168.             {
  169.                 // Apply some random jitter to make the enemy move around.
  170.                 const float randomization = 10;
  171.  
  172.                 enemyPosition.X += (float)(random.NextDouble() - 0.5) * randomization;
  173.                 enemyPosition.Y += (float)(random.NextDouble() - 0.5) * randomization;
  174.  
  175.                 // Apply a stabilizing force to stop the enemy moving off the screen.
  176.                 Vector2 targetPosition = new Vector2(
  177.                     ScreenManager.GraphicsDevice.Viewport.Width / 2 - gameFont.MeasureString("XNA Basic Starter Kit!").X / 2,
  178.                     200);
  179.  
  180.                 enemyPosition = Vector2.Lerp(enemyPosition, targetPosition, 0.05f);
  181.  
  182. //                mySprite.Update(gameTime);
  183.                 myBall.UpdatePosition();
  184.  
  185.             }
  186.         }
  187.  
  188.  
  189.         /// <summary>
  190.         /// Lets the game respond to player input. Unlike the Update method,
  191.         /// this will only be called when the gameplay screen is active.
  192.         /// </summary>
  193.         public override void HandleInput(InputState input) //, GameTime gameTime)
  194.         {
  195.             if (input == null)
  196.                 throw new ArgumentNullException("input");
  197.  
  198.             // Look up inputs for the active player profile.
  199.             int playerIndex = (int)ControllingPlayer.Value;
  200.  
  201.             KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
  202.             GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
  203.  
  204.             // The game pauses either if the user presses the pause button, or if
  205.             // they unplug the active gamepad. This requires us to keep track of
  206.             // whether a gamepad was ever plugged in, because we don't want to pause
  207.             // on PC if they are playing with a keyboard and have no gamepad at all!
  208.             bool gamePadDisconnected = !gamePadState.IsConnected &&
  209.                                        input.GamePadWasConnected[playerIndex];
  210.  
  211.             if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
  212.             {
  213.                 ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
  214.             }
  215. /*            else
  216.             {
  217.                 mySprite.HandleInput(keyboardState, gamePadState, input, safeArea);
  218.             }
  219.  */
  220.         }
  221.  
  222.  
  223.  
  224.         /// <summary>
  225.         /// Draws the gameplay screen.
  226.         /// </summary>
  227.         public override void Draw(GameTime gameTime)
  228.         {
  229.  
  230.  
  231.             // Our player and enemy are both actually just text strings.
  232.             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  233.  
  234.             spriteBatch.Begin();
  235.  
  236.             spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, 800, 600), Color.White);
  237.  
  238.             // draw hud
  239.             hudContainer.Draw(spriteBatch);
  240.  
  241.             spriteBatch.DrawString(gameFont, "XNA Basic Starter Kit!",
  242.                                    enemyPosition, Color.Black);
  243.  
  244. #if DEBUG     // I've temporarily commented out the safeArea debug -DV  
  245. //            safeArea.Draw(spriteBatch);
  246. #endif
  247.  
  248.             // debug info
  249.             var safeAreaInfo = "dx=" + safeArea.dx + ",dy=" + safeArea.dy
  250.                 + ",w=" + safeArea.width + ",h=" + safeArea.height;
  251.                 //+ ", H = " + playerHorizDirection
  252.                 //+ ", V = " + playerVertDirection;
  253.             spriteBatch.DrawString(gameFont, safeAreaInfo, safeTopLeft, Color.Green);
  254.  
  255.             //
  256. //            mySprite.Draw(spriteBatch);
  257.             //
  258.             myBall.Draw(spriteBatch);
  259.  
  260.            
  261.             spriteBatch.End();
  262.  
  263.            // If the game is transitioning on or off, fade it out to black.
  264.             if (TransitionPosition > 0 || pauseAlpha > 0)
  265.             {
  266.                 float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
  267.  
  268.                 ScreenManager.FadeBackBufferToBlack(alpha);
  269.             }
  270.         }
  271.  
  272.  
  273.         #endregion
  274.     }
  275. }
  276.  
  277.  
  278.  
  279. -------------------------------------------------------------------------------------
  280.  
  281.  
  282. using System;
  283. using Microsoft.Xna.Framework;
  284. using Microsoft.Xna.Framework.Content;
  285. using Microsoft.Xna.Framework.Graphics;
  286.  
  287. namespace MyXnaGame
  288. {
  289.     class Ball
  290.     {
  291.         private bool isVisible;
  292.         private Vector2 position;
  293.         private double direction;
  294.         private Texture2D texture;
  295.         private Rectangle size;
  296.         private float speed;
  297.         private float moveSpeed;
  298.         private Vector2 resetPos;
  299.         Random rand;
  300.  
  301.         public Ball(ContentManager content, Vector2 screenSize)
  302.         {
  303.             moveSpeed = 8f;
  304.             speed = 0;
  305.             texture = content.Load<Texture2D>(@"gfx/ball");
  306.             direction = 0;
  307.             size = new Rectangle(0, 0, texture.Width, texture.Height);
  308.             resetPos = new Vector2(screenSize.X / 2, screenSize.Y / 2);
  309.             position = resetPos;
  310.             rand = new Random();
  311.             isVisible = true;
  312.         }
  313.  
  314.         public void UpdatePosition()
  315.         {
  316.             size.X = (int)position.X;
  317.             size.Y = (int)position.Y;
  318.             position.X += speed * (float)Math.Cos(direction);
  319.             position.Y += speed * (float)Math.Sin(direction);
  320.             CheckWallHit();
  321.         }
  322.  
  323.         public Rectangle GetSize()
  324.         {
  325.             return size;
  326.         }
  327.  
  328.         public double GetDirection()
  329.         {
  330.             return direction;
  331.         }
  332.  
  333.         public Vector2 GetPosition()
  334.         {
  335.             return position;
  336.         }
  337.  
  338.         public void BatHit(int block)
  339.         {
  340.             if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
  341.             {
  342.                 switch (block)
  343.                 {
  344.                     case 1:
  345.                         direction = MathHelper.ToRadians(220);
  346.                         break;
  347.                     case 2:
  348.                         direction = MathHelper.ToRadians(215);
  349.                         break;
  350.                     case 3:
  351.                         direction = MathHelper.ToRadians(200);
  352.                         break;
  353.                     case 4:
  354.                         direction = MathHelper.ToRadians(195);
  355.                         break;
  356.                     case 5:
  357.                         direction = MathHelper.ToRadians(180);
  358.                         break;
  359.                     case 6:
  360.                         direction = MathHelper.ToRadians(180);
  361.                         break;
  362.                     case 7:
  363.                         direction = MathHelper.ToRadians(165);
  364.                         break;
  365.                     case 8:
  366.                         direction = MathHelper.ToRadians(130);
  367.                         break;
  368.                     case 9:
  369.                         direction = MathHelper.ToRadians(115);
  370.                         break;
  371.                     case 10:
  372.                         direction = MathHelper.ToRadians(110);
  373.                         break;
  374.                 }
  375.             }
  376.             else
  377.             {
  378.                 switch (block)
  379.                 {
  380.                     case 1:
  381.                         direction = MathHelper.ToRadians(290);
  382.                         break;
  383.                     case 2:
  384.                         direction = MathHelper.ToRadians(295);
  385.                         break;
  386.                     case 3:
  387.                         direction = MathHelper.ToRadians(310);
  388.                         break;
  389.                     case 4:
  390.                         direction = MathHelper.ToRadians(345);
  391.                         break;
  392.                     case 5:
  393.                         direction = MathHelper.ToRadians(0);
  394.                         break;
  395.                     case 6:
  396.                         direction = MathHelper.ToRadians(0);
  397.                         break;
  398.                     case 7:
  399.                         direction = MathHelper.ToRadians(15);
  400.                         break;
  401.                     case 8:
  402.                         direction = MathHelper.ToRadians(50);
  403.                         break;
  404.                     case 9:
  405.                         direction = MathHelper.ToRadians(65);
  406.                         break;
  407.                     case 10:
  408.                         direction = MathHelper.ToRadians(70);
  409.                         break;
  410.                 }
  411.             }
  412.         }
  413.  
  414.         private void CheckWallHit()
  415.         {
  416.             while (direction > 2 * Math.PI) direction -= 2 * Math.PI;
  417.             while (direction < 0) direction += 2 * Math.PI;
  418.             if (position.Y <= 0 || (position.Y > resetPos.Y * 2 - size.Height))
  419.             {
  420.                 direction = 2 * Math.PI - direction;
  421.             }
  422.         }
  423.  
  424.         public void Stop()
  425.         {
  426.             isVisible = false;
  427.             speed = 0;
  428.         }
  429.  
  430.         public void Reset(bool left)
  431.         {
  432.             if (left) direction = 0;
  433.             else direction = Math.PI;
  434.             position = resetPos;
  435.             isVisible = true;
  436.             speed = moveSpeed;
  437.         }
  438.  
  439.         public void Draw(SpriteBatch batch)
  440.         {
  441.             if (isVisible)
  442.             {
  443.                 batch.Draw(texture, position, Color.White);
  444.             }
  445.         }
  446.     }
  447. }
RAW Paste Data