예제 #1
0
 /// <summary>
 /// A constructor to assign necessary parameters to the Ball class
 /// </summary>
 /// <param name="game">Game to call</param>
 /// <param name="spriteBatch">SpriteBatch to call</param>
 /// <param name="tex">Texture of the ball</param>
 /// <param name="position">Position of the ball</param>
 /// <param name="speed">Speed of the ball</param>
 /// <param name="stage">Screen size</param>
 /// <param name="hitSound">Sound to play when the ball hit the paddle</param>
 /// <param name="hitWallSound">Sound to play when the ball hit the wall</param>
 /// <param name="missSound">Sound to play when the ball goes out of the screen</param>
 /// <param name="score">Game score</param>
 /// <param name="winMessage">Win message</param>
 public Ball(Game game,
             SpriteBatch spriteBatch,
             Texture2D tex,
             Vector2 position,
             Vector2 speed,
             Vector2 stage,
             SoundEffect hitSound,
             SoundEffect hitWallSound,
             SoundEffect missSound,
             Score score,
             WinMessage winMessage
             ) : base(game)
 {
     this.spriteBatch     = spriteBatch;
     this.tex             = tex;
     this.position        = position;
     this.initialPosition = position;
     this.speed           = speed;
     this.stage           = stage;
     this.hitSound        = hitSound;
     this.hitWallSound    = hitWallSound;
     this.missSound       = missSound;
     this.score           = score;
     this.winMessage      = winMessage;
 }
예제 #2
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.
            bool isVisible = true;

            spriteBatch = new SpriteBatch(GraphicsDevice);
            gamePhase   = GamePhase.Title;
            stage       = new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);

            //Load background image
            Texture2D backgroundTexture  = this.Content.Load <Texture2D>("Images/Background");
            Vector2   backgroundPosition = new Vector2(0, 0);

            backGround = new Sprite(this, spriteBatch, backgroundTexture, backgroundPosition);

            //Load start message
            Texture2D startMessageTexture  = this.Content.Load <Texture2D>("Images/StartMessage");
            Vector2   startMessagePosition = new Vector2(getCenter(startMessageTexture).X, getCenter(startMessageTexture).Y + 50);

            startMessage = new Sprite(this, spriteBatch, startMessageTexture, startMessagePosition, isVisible, 0.02f);

            //Load title
            Texture2D titleTexture  = this.Content.Load <Texture2D>("Images/Title");
            Vector2   titlePosition = getCenter(titleTexture);

            title = new Sprite(this, spriteBatch, titleTexture, titlePosition);

            //Load BGM
            songGameTitle = Content.Load <Song>("Musics/SongGameTitle");
            songGameStart = Content.Load <Song>("Musics/SongGameStart");
            SongGameOver  = Content.Load <Song>("Musics/SongGameOver");

            //Load Score
            SpriteFont scoreFont = Content.Load <SpriteFont>("Fonts/ScoreFont");

            score = new Score(this, spriteBatch, scoreFont, stage);

            //Load win message
            SpriteFont messageFont = Content.Load <SpriteFont>("Fonts/NameFont");

            winMessage = new WinMessage(this, spriteBatch, messageFont, stage);

            //Load player name
            SpriteFont playerName            = Content.Load <SpriteFont>("Fonts/NameFont");
            Vector2    playerOneNameLength   = playerName.MeasureString(PLAYER_ONE_NAME);
            Vector2    playerOneNamePosition = new Vector2((stage.X / 2) - playerOneNameLength.X - 20, stage.Y - 100);
            Vector2    playerTwoNamePosition = new Vector2((stage.X / 2) + 20, stage.Y - 100);

            playerOneName = new Font(this, spriteBatch, playerName, PLAYER_ONE_NAME, playerOneNamePosition, new Color(255, 204, 51), false);
            playerTwoName = new Font(this, spriteBatch, playerName, PLAYER_TWO_NAME, playerTwoNamePosition, new Color(255, 204, 51), false);

            //Load Ball
            ballTexture         = this.Content.Load <Texture2D>("Images/Ball");
            initialBallPosition = new Vector2(getCenter(ballTexture).X, getCenter(ballTexture).Y);
            Vector2     ballSpeed    = new Vector2(0, 0);
            SoundEffect hitSound     = this.Content.Load <SoundEffect>("Musics/SFBounce");
            SoundEffect hitWallSound = this.Content.Load <SoundEffect>("Musics/SFBounceWall");
            SoundEffect missSound    = this.Content.Load <SoundEffect>("Musics/SFScore");

            ball         = new Ball(this, spriteBatch, ballTexture, initialBallPosition, ballSpeed, stage, hitSound, hitWallSound, missSound, score, winMessage);
            ball.Enabled = false;

            //Load 2 paddles
            Texture2D paddleTexture = this.Content.Load <Texture2D>("Images/Paddle");

            playerOnePosition = new Vector2(10, getCenter(paddleTexture).Y);
            playerTwoPosition = new Vector2(graphics.PreferredBackBufferWidth - paddleTexture.Width - 10, getCenter(paddleTexture).Y);
            Vector2 paddleSpeed = new Vector2(0, SPEED);

            playerOnePaddle = new Paddle(this, spriteBatch, paddleTexture, playerOnePosition, paddleSpeed, stage, Keys.A, Keys.Z);
            playerTwoPaddle = new Paddle(this, spriteBatch, paddleTexture, playerTwoPosition, paddleSpeed, stage, Keys.Up, Keys.Down);

            //Load collision detection
            CollisionDetection collisionOne = new CollisionDetection(this, ball, playerOnePaddle, hitSound);
            CollisionDetection collisionTwo = new CollisionDetection(this, ball, playerTwoPaddle, hitSound);


            //Add all the components
            this.Components.Add(backGround);
            this.Components.Add(title);
            this.Components.Add(startMessage);
            this.Components.Add(ball);
            this.Components.Add(playerOnePaddle);
            this.Components.Add(playerTwoPaddle);
            this.Components.Add(score);
            this.Components.Add(winMessage);
            this.Components.Add(playerOneName);
            this.Components.Add(playerTwoName);
            this.Components.Add(collisionOne);
            this.Components.Add(collisionTwo);
            PlayMusic();
        }