示例#1
0
        public override void Update(GameTime gameTime,GameObjects gameObjects)
        {
            if(_playerType==PlayerTypes.Computer)
            {
                var random = new Random();
                var reactionThreshold = random.Next(30, 50);

                if (gameObjects.Ball.Location.Y + gameObjects.Ball.Height < Location.Y + reactionThreshold )
                {
                    Velocity = new Vector2(0, -7.5f);
                }
                if (gameObjects.Ball.Location.Y > Location.Y + Height + reactionThreshold )
                {
                    Velocity = new Vector2(0, 7.5f);
                }
            }

            if (_playerType == PlayerTypes.Human)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Up) || gameObjects.TouchInput.Up)
                    Velocity = new Vector2(0, -8f);

                if (Keyboard.GetState().IsKeyDown(Keys.Down) || gameObjects.TouchInput.Down)
                    Velocity = new Vector2(0, 8f);
            }
            base.Update(gameTime,gameObjects);
        }
示例#2
0
        public void Update(GameTime gameTime, GameObjects gameObjects)
        {
            if(gameObjects.Ball.Location.X + gameObjects.Ball.Width < 0)
            {
                ComputerScore++;
                gameObjects.Ball.AttachTo(gameObjects.PlayerPaddle);
            }

            if(gameObjects.Ball.Location.X > _gameBoundaries.Width)
            {
                PlayerScore++;
                gameObjects.Ball.AttachTo(gameObjects.PlayerPaddle);
            }
        }
示例#3
0
文件: Ball.cs 项目: AdrianMH/PongMono
        public override void Update(GameTime gameTime,GameObjects gameObjects)
        {
            if((Keyboard.GetState().IsKeyDown(Keys.Space) || gameObjects.TouchInput.Tapped) && attachedToPaddle!=null)
            {
                var newVelocity = new Vector2(BALL_SPEED, attachedToPaddle.Velocity.Y * .65f);
                Velocity = newVelocity;
                attachedToPaddle = null;
            }

            if (attachedToPaddle != null)
            {
                Location.X = attachedToPaddle.Location.X + attachedToPaddle.Width;
                Location.Y = attachedToPaddle.Location.Y;
            }
            else
            {
                if(BoundingBox.Intersects(gameObjects.PlayerPaddle.BoundingBox) || BoundingBox.Intersects(gameObjects.ComputerPaddle.BoundingBox))
                {
                    Velocity = new Vector2(-Velocity.X, Velocity.Y);
                }
            }
            base.Update(gameTime,gameObjects);
        }
示例#4
0
        public virtual void Update(GameTime gameTime,GameObjects gameObjects)
        {
            Location += Velocity;

            CheckBounds();
        }
示例#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);

            var gameBoundaries=new Rectangle(0,0,Window.ClientBounds.Width, Window.ClientBounds.Height );
            var paddleTexture = Content.Load<Texture2D>("Paddle");

            playerPaddle = new Paddle(paddleTexture, Vector2.Zero, gameBoundaries,PlayerTypes.Human);
            computerPaddle = new Paddle(paddleTexture,Vector2.Zero ,gameBoundaries,PlayerTypes.Computer);

            var computerPaddleLocation = new Vector2(gameBoundaries.Width - paddleTexture.Width, 0);
            computerPaddle = new Paddle(paddleTexture, computerPaddleLocation, gameBoundaries,PlayerTypes.Computer);

            ball=new Ball(Content.Load<Texture2D>("Ball"), Vector2.Zero, gameBoundaries);
            ball.AttachTo(playerPaddle);

            score = new Score(Content.Load<SpriteFont>("GameFont"),gameBoundaries);

            gameObjects = new GameObjects {PlayerPaddle = playerPaddle, ComputerPaddle = computerPaddle, Ball = ball,Score = score};
        }