示例#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);
            window      = new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);

            scoreBarTex = this.Content.Load <Texture2D>("Images/Scorebar");
            scoreBarPos = new Vector2(0, window.Y - scoreBarTex.Height);

            stage = new Vector2(graphics.PreferredBackBufferWidth, scoreBarPos.Y);

            font = this.Content.Load <SpriteFont>("Fonts/SpriteFont1");



            SoundEffect hitSound  = this.Content.Load <SoundEffect>("Music/click");
            SoundEffect missSound = this.Content.Load <SoundEffect>("Music/applause1");
            SoundEffect dingSound = this.Content.Load <SoundEffect>("Music/ding");


            //ball
            ballTex      = this.Content.Load <Texture2D>("Images/Ball");
            ballPos      = new Vector2(stage.X / 2 - ballTex.Width / 2, stage.Y / 2 - ballTex.Height / 2);
            ballSpeed    = GetSpeed();
            ball         = new Ball(this, spriteBatch, ballTex, ballPos, ballSpeed, stage, hitSound, dingSound, playerOneScore, playerTwoScore);
            ball.Enabled = false;
            this.Components.Add(ball);

            scoreBar1 = new Score(this, spriteBatch, font, "Archana Lohani: ", playerOneScore, new Vector2(0, window.Y - (scoreBarTex.Height / 2) - (font.MeasureString($"Archana Lohani: {playerOneScore}").Y / 2)), Color.White, ball, missSound);
            scoreBar2 = new Score(this, spriteBatch, font, "Ibrahim Qadamani: ", playerTwoScore, new Vector2(window.X - font.MeasureString($"Ibrahim Qadhmani: {playerTwoScore}").X, window.Y - (scoreBarTex.Height / 2) - (font.MeasureString($"Ibrahim Qadhmani: {playerTwoScore}").Y / 2)), Color.White, ball, missSound);
            this.Components.Add(scoreBar1);
            this.Components.Add(scoreBar2);


            Vector2 batSpeed = new Vector2(0, 4);

            //bat1
            batTex1 = this.Content.Load <Texture2D>("Images/BatLeft");
            batPos1 = new Vector2(10, stage.Y / 2 - batTex1.Height / 2);
            Input inputBat1 = new Input()
            {
                up   = Keys.A,
                down = Keys.Z,
            };

            bat1 = new Bat(this, spriteBatch, batTex1, batPos1, inputBat1, batSpeed, stage);
            this.Components.Add(bat1);


            //bat2
            batTex2 = this.Content.Load <Texture2D>("Images/BatRight");
            batPos2 = new Vector2(stage.X - 10 - batTex2.Width, stage.Y / 2 - batTex2.Height / 2);
            Input inputBat2 = new Input()
            {
                up   = Keys.Up,
                down = Keys.Down,
            };

            bat2 = new Bat(this, spriteBatch, batTex2, batPos2, inputBat2, batSpeed, stage);
            this.Components.Add(bat2);



            CollisionDetection cd1 = new CollisionDetection(this, bat1, ball, hitSound);

            this.Components.Add(cd1);

            CollisionDetection cd2 = new CollisionDetection(this, bat2, ball, hitSound);

            this.Components.Add(cd2);



            // TODO: use this.Content to load your game content here
        }
 /// <summary>
 /// The constructor of the CollisionDetection class
 /// </summary>
 /// <param name="game"></param>
 /// <param name="bat"></param>
 /// <param name="ball"></param>
 /// <param name="hitSound"></param>
 public CollisionDetection(Game game, Bat bat, Ball ball, SoundEffect hitSound) : base(game)
 {
     this.bat      = bat;
     this.ball     = ball;
     this.hitSound = hitSound;
 }