Exemple #1
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            var screenBounds = GraphicsDevice.Viewport.Bounds;

            Texture2D paddleTexture = Content.Load<Texture2D>("paddle");
            PaddleBottom = new Paddle(paddleTexture);
            PaddleTop = new Paddle(paddleTexture);
            PaddleBottom.Position = new Vector2(screenBounds.Bottom - PaddleBottom.Size.Height);
            PaddleBottom.Position.X = MathHelper.Clamp(PaddleBottom.Position.X,
                 graphics.GraphicsDevice.Viewport.Bounds.Left,
                 graphics.GraphicsDevice.Viewport.Bounds.Right - PaddleBottom.Size.Width);

            PaddleTop.Position = new Vector2(screenBounds.Top);

            Texture2D ballTexture = Content.Load<Texture2D>("ball");
            Ball = new Ball(ballTexture);
            Ball.Position = screenBounds.Center.ToVector2();

            Texture2D backgroundTexture = Content.Load<Texture2D>("background");
            Background = new Background(backgroundTexture, screenBounds.Width, screenBounds.Height);

            HitSound = Content.Load<SoundEffect>("hit");
            Music = Content.Load<Song>("music");
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(Music);

            SpriteForDrawList.Add(Background);
            SpriteForDrawList.Add(PaddleBottom);
            SpriteForDrawList.Add(PaddleTop);
            SpriteForDrawList.Add(Ball);

        }
 public static void ReleaseBallObject(Ball obj)
 {
     lock (inactiveBall)
     {
         CleanUpBall(obj);
         inactiveBall.Add(obj);
         activeBall.Remove(obj);
     }
 }
 private static void CleanUpBall(Ball obj)
 {
     obj.Position = new Vector2(GameWorld.windowWidth / 2, GameWorld.windowHeight / 2);
     obj.Velocity = new Vector2(RandomPicker.Rnd.Next(-1, 2), RandomPicker.Rnd.Next(-4, 5));
     obj.Speed = 750;
     obj.PlayAnimation("MoveBall");
     if (obj.Velocity.X == 0)
     {
         if (RandomPicker.Rnd.Next(2) == 0)
         {
             obj.Velocity = new Vector2(-1, RandomPicker.Rnd.Next(-4, 5));
         }
         else
         {
             obj.Velocity = new Vector2(1, RandomPicker.Rnd.Next(-4, 5));
         }
     }
 }
 //Laver et nyt object
 public static Ball CreateBall()
 {
     lock(inactiveBall)
     {
         if (inactiveBall.Count != 0)
         {
             Ball obj = inactiveBall[0];
             activeBall.Add(obj);
             inactiveBall.RemoveAt(0);
             return obj;
         }
         else
         {
             Ball obj = new Ball(new Vector2(GameWorld.windowWidth / 2, GameWorld.windowHeight / 2));
             activeBall.Add(obj);
             return obj;
         }
     }
 }
Exemple #5
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Ask graphics device about screen bounds we are using. 
            var screenBounds = GraphicsDevice.Viewport.Bounds;

            // Load paddle texture using Content.Load static method 
            Texture2D paddleTexture = Content.Load<Texture2D>("paddle");

            // Create bottom and top paddles and set their initial position 
            PaddleBottom = new Paddle(paddleTexture);
            PaddleTop = new Paddle(paddleTexture);

            // Position both paddles with help screenBounds object 
            PaddleBottom.Position = new Vector2(0,screenBounds.Bottom - PaddleBottom.Size.Height);
            PaddleTop.Position = new Vector2(screenBounds.Top);

            // Load ball texture 
            Texture2D ballTexture = Content.Load<Texture2D>("ball");

            // Create new ball object and set its initial position 
            Ball = new Ball(ballTexture);
            Ball.Position = screenBounds.Center.ToVector2();
            // Load background texture and create a new background object. 
            Texture2D backgroundTexture = Content.Load<Texture2D>("background");
            Background = new Background(backgroundTexture, screenBounds.Width, 
                screenBounds.Height);

            // Load sounds 
            HitSound = Content.Load<SoundEffect>("hit");
            Music = Content.Load<Song>("music");
            MediaPlayer.IsRepeating = true;
            // Start playing background music 
            MediaPlayer.Play(Music);

            SpritesForDrawList.Add(Background);
            SpritesForDrawList.Add(PaddleBottom);
            SpritesForDrawList.Add(PaddleTop);
            SpritesForDrawList.Add(Ball);
        }
        private void InitGame()
        {
            _speed = 3;
            _padSpeed = 70;
            _ball =new Ball { X = 180, Y = 110, MovingRight = true };
            _player1 = new Pad { Y = 90};
            _player2 = new Pad { Y = 90 };

            LeftPoints = 0;
            RightPoints = 0;
            Level = 0;
            _isSinglePlayer = false;
            _levelBreak = false;
            DataContext = this;
            LeftPad.DataContext = _player1;
            RightPad.DataContext = _player2;
            Ball.DataContext = _ball;

            if(_timer.IsEnabled)
                _timer.Stop();
            _timer.Interval = TimeSpan.FromMilliseconds(10);
            _timer.Start();
        }