예제 #1
0
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            if (this.ballDirection.X < 0) {
                this.direction = new Vector2(0, 0);
                Velocity = Vector2.Zero;
                return;
            }
            else if (this.ballPosition.Y + 10 < BoundingBox.Center.Y - 10) { // ball center is above = move up
                Velocity += new Vector2(0.05f);
                if (Velocity.Y > maxSpeed.Y)
                    Velocity = maxSpeed;
                this.direction = new Vector2(0, -1);
            }
            else if (this.ballPosition.Y + 10 > BoundingBox.Center.Y + 10) { // ball center is below
                Velocity += new Vector2(0.05f);
                if (Velocity.Y > maxSpeed.Y)
                    Velocity = maxSpeed;
                this.direction = new Vector2(0, 1);
            }
            else if (Velocity.Y > 0) {
                Velocity -= new Vector2(0.05f);
                if (Velocity.Y < 0.1f)
                    Velocity = Vector2.Zero;
            }

            position += direction * Velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            CheckBounds(gameObjects);
        }
예제 #2
0
파일: Paddle.cs 프로젝트: rubik951/Pong
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            if (playerType == PlayerTypes.Human)
            {
                var newVelocity = Vector2.Zero;

                if (Mouse.GetState().X > 0 && Mouse.GetState().X < (gameBoundaries.Width / 2) && Mouse.GetState().Y > 0 && Mouse.GetState().Y < (gameBoundaries.Height / 2))
                    newVelocity = new Vector2(0, -5f);
                else if (Mouse.GetState().X > 0 && Mouse.GetState().X < (gameBoundaries.Width / 2) && Mouse.GetState().Y > (gameBoundaries.Height / 2) && Mouse.GetState().Y < gameBoundaries.Height)
                    newVelocity = new Vector2(0, 5f);

                Velocity = newVelocity;
            }
            else if (playerType == PlayerTypes.Computer)
            {
                var random = new Random();
                var reactionThreshold = random.Next(0, 20);

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

            base.Update(gameTime, gameObjects);
        }
예제 #3
0
파일: PlayerPaddle.cs 프로젝트: koniin/Pong
 protected override void CheckBounds(GameObjects gameObjects)
 {
     if (Position.Y + Height > screenBounds.Height)
         SetPosition(screenBounds.Height - Height);
     else if (Position.Y < 0)
         SetPosition(0);
 }
예제 #4
0
파일: PlayerPaddle.cs 프로젝트: koniin/Pong
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            position += direction * Velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            HandleInput(Keyboard.GetState());

            CheckBounds(gameObjects);
        }
예제 #5
0
 public void Update(GameTime gameTime, GameObjects gameObjects)
 {
     if (gameObjects.Ball.Location.X + gameObjects.Ball.GetWidth() < 0)
     {
         ComputerScore++;
         gameObjects.Ball.velocity = new Vector2(0, 0);
         gameObjects.Ball.AttachTo(gameObjects.PlayerPaddle);
     }
     if (gameObjects.Ball.Location.X > gameObjects.Ball.boundaries.Width)
     {
         PlayerScore++;
         gameObjects.Ball.velocity = new Vector2(0, 0);
         gameObjects.Ball.AttachTo(gameObjects.PlayerPaddle);
     }
 }
예제 #6
0
파일: Game1.cs 프로젝트: rubik951/Pong
        protected override void LoadContent()
        {
            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, new Vector2(gameBoundaries.Width - paddleTexture.Width, 0), 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 };
        }
예제 #7
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 paddle = Content.Load <Texture2D>("Pong paddle");

            playerPaddle   = new Paddle(paddle, Vector2.Zero, Color.Black, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), PlayerTypes.Human);
            computerPaddle = new Paddle(paddle, new Vector2(Window.ClientBounds.Width - paddle.Width, 0), Color.Black, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), PlayerTypes.Computer);
            ball           = new Ball(Content.Load <Texture2D>("Pong Ball"), Vector2.Zero, Color.Blue, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height));
            ball.AttachTo(playerPaddle);


            score = new Score(Content.Load <SpriteFont>("Gamefont"), new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height));

            gameObjects = new GameObjects {
                PlayerPaddle = playerPaddle, ComputerPaddle = computerPaddle, Ball = ball, Score = score
            };
        }
예제 #8
0
파일: Paddle.cs 프로젝트: thiecrow/Pong
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            if (playerType == PlayerTypes.Computer)
            {
                if (gameObjects.Ball.Location.Y + gameObjects.Ball.GetHeigth() / 2 > gameObjects.ComputerPaddle.Location.Y + gameObjects.ComputerPaddle.GetHeigth() / 2 + 50)
                {
                    velocity = new Vector2(0, speed);
                }
                else if (gameObjects.Ball.Location.Y + gameObjects.Ball.GetHeigth() / 2 < gameObjects.ComputerPaddle.Location.Y + gameObjects.ComputerPaddle.GetHeigth() / 2 - 50)
                {
                    velocity = new Vector2(0, -speed);
                }
                else
                {
                    velocity = new Vector2(0, 0);
                }
            }
            if (playerType == PlayerTypes.Human)
            {
                //Move paddle up
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                {
                    velocity = new Vector2(0, -speed);
                }

                //move paddle down
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                {
                    velocity = new Vector2(0, speed);
                }

                //stop paddle when no input
                if (Keyboard.GetState().IsKeyUp(Keys.Up) && Keyboard.GetState().IsKeyUp(Keys.Down))
                {
                    velocity = new Vector2(0, 0);
                }
            }

            base.Update(gameTime, gameObjects);

            checkBounds();
        }
예제 #9
0
파일: Ball.cs 프로젝트: thiecrow/Pong
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            if (attachedToPaddle != null)
            {
                Location.X = attachedToPaddle.Location.X + attachedToPaddle.GetWidth() - 1;
                Location.Y = attachedToPaddle.Location.Y + attachedToPaddle.GetHeigth() / 2 - texture.Height / 2;
            }
            else
            {
                if (BoundingBox.Intersects(gameObjects.PlayerPaddle.BoundingBox) || BoundingBox.Intersects(gameObjects.ComputerPaddle.BoundingBox))
                {
                    velocity.X = -velocity.X;
                }
            }

            Launch();
            base.Update(gameTime, gameObjects);

            checkBounds();
        }
예제 #10
0
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            scoresFont  = Content.Load <SpriteFont>("font");
            var bounds = new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height);

            font           = Content.Load <SpriteFont>("font");
            dividerTexture = TextureGenerator.CreateTexture(GraphicsDevice, 10, 10);
            var    baseTexture = TextureGenerator.CreateTexture(GraphicsDevice, 10, 100);
            Player playerLeft  = new Player(baseTexture, new Vector2(20, gameHeight / 2 - 50), bounds, 1);
            Player playerRight = new Player(baseTexture, new Vector2(gameWidth - 40, gameHeight / 2 - 50), bounds, 2);

            gameObjects = new GameObjects
            {
                playerLeft  = playerLeft,
                playerRight = playerRight,
                Ball        = new Ball(TextureGenerator.CreateTexture(GraphicsDevice, 10, 10), new Vector2(gameWidth / 2, gameHeight / 2), bounds),
                //SoundManager = soundManager,
                Score = new ScoreScreen(font, new Vector2(gameWidth / 2 - (int)(gameWidth * 0.30), 20), new Vector2(gameWidth / 2 + (int)(gameWidth * 0.30), 20))
            };
        }
예제 #11
0
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            if (this.ballDirection.X < 0)
            {
                this.direction = new Vector2(0, 0);
                Velocity       = Vector2.Zero;
                return;
            }
            else if (this.ballPosition.Y + 10 < BoundingBox.Center.Y - 10)   // ball center is above = move up
            {
                Velocity += new Vector2(0.05f);
                if (Velocity.Y > maxSpeed.Y)
                {
                    Velocity = maxSpeed;
                }
                this.direction = new Vector2(0, -1);
            }
            else if (this.ballPosition.Y + 10 > BoundingBox.Center.Y + 10)   // ball center is below
            {
                Velocity += new Vector2(0.05f);
                if (Velocity.Y > maxSpeed.Y)
                {
                    Velocity = maxSpeed;
                }
                this.direction = new Vector2(0, 1);
            }
            else if (Velocity.Y > 0)
            {
                Velocity -= new Vector2(0.05f);
                if (Velocity.Y < 0.1f)
                {
                    Velocity = Vector2.Zero;
                }
            }

            position += direction * Velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            CheckBounds(gameObjects);
        }
예제 #12
0
파일: Paddle.cs 프로젝트: joshuarose/Pong
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            #if ANDROID
            float PADDLESPEED = 6.5f;
            #else
            float PADDLESPEED = 6.5f;
            #endif

            if (_playertype == PlayerTypes.Computer)
            {
                var random = new Random();
                var reactionThreshold = random.Next(30, 130);
                // Computer player movement
                if (gameObjects.Ball.Location.Y + gameObjects.Ball.Height < Location.Y + reactionThreshold)
                {
                    Velocity = new Vector2(0, -PADDLESPEED);
                }

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

            }

            if (_playertype == PlayerTypes.Human)
            {
                //Move paddle up
                if (Keyboard.GetState().IsKeyDown(Keys.Up) || gameObjects.TouchInput.Up)
                    Velocity = new Vector2(0, -PADDLESPEED);

                //move paddle down
                if (Keyboard.GetState().IsKeyDown(Keys.Down) || gameObjects.TouchInput.Down)
                    Velocity = new Vector2(0, PADDLESPEED);
            }

            base.Update(gameTime, gameObjects);
        }
예제 #13
0
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            if (_playerType == PlayerType.Human)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Left))
                {
                    // move paddle up
                    Velocity = new Vector2(0, -GameConstants.PaddleSpeed);
                }
                else if (Keyboard.GetState().IsKeyDown(Keys.Right))
                {
                    // move paddle down
                    Velocity = new Vector2(0, GameConstants.PaddleSpeed);
                }
            }
            else if (_playerType == PlayerType.Computer)
            {
                var random               = new Random();
                var reactionThreshold    = random.Next(GameConstants.AiReactionThresholdMin, GameConstants.AiReactionThresholdMax);
                var reactionThreshOffset = reactionThreshold / 100 * texture.Height;

                if (gameObjects.Ball.Bottom > Top + reactionThreshOffset)
                //if (gameObjects.Ball.Location.Y + gameObjects.Ball.Height < Location.Y + reactionThreshold)
                {
                    var newVal = new Vector2(0, AiSpeed());
                    Velocity = newVal;
                }
                else if (gameObjects.Ball.Top < Bottom - reactionThreshOffset)
                //else if (gameObjects.Ball.Location.Y > Location.Y + Height + reactionThreshold)
                {
                    var newVal = new Vector2(0, -AiSpeed());
                    Velocity = newVal;
                }
            }

            base.Update(gameTime, gameObjects);
        }
예제 #14
0
파일: Ball.cs 프로젝트: rubik951/Pong
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            if (attachedToPaddle != null && Mouse.GetState().LeftButton == ButtonState.Pressed && Mouse.GetState().X > (gameBoundaries.Width/2) && Mouse.GetState().X < gameBoundaries.Width)
            {
                var newVelocity = new Vector2(3.5f, attachedToPaddle.Velocity.Y);
                Velocity = newVelocity;
                attachedToPaddle = null;
            }

            if (attachedToPaddle != null)
            {
                Location.X = attachedToPaddle.Location.X + attachedToPaddle.Width + 5;
                Location.Y = attachedToPaddle.Location.Y + attachedToPaddle.Height / 3;
            }
            else
            {
                if(BoundingBox.Intersects(gameObjects.PlayerPaddle.BoundingBox) || BoundingBox.Intersects(gameObjects.ComputerPaddle.BoundingBox))
                {
                    Velocity = new Vector2(-Velocity.X, Velocity.Y);
                }
            }

            base.Update(gameTime, gameObjects);
        }
예제 #15
0
파일: Sprite.cs 프로젝트: koniin/Pong
 protected abstract void CheckBounds(GameObjects gameObjects);
예제 #16
0
파일: Sprite.cs 프로젝트: koniin/Pong
 public virtual void Update(GameTime gameTime, GameObjects gameObjects)
 {
     position += direction * Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
 }
예제 #17
0
 protected abstract void CheckBounds(GameObjects gameObjects);
예제 #18
0
 public virtual void Update(GameTime gameTime, GameObjects gameObjects)
 {
     position += direction * Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
 }
예제 #19
0
 public virtual void Update(GameTime gameTime, GameObjects gameObjects)
 {
     Location += (Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);
     CheckBounds(gameObjects);
 }
예제 #20
0
파일: Sprite.cs 프로젝트: rubik951/Pong
 public virtual void Update(GameTime gameTime, GameObjects gameObjects)
 {
     Location += Velocity;
     CheckBounds();
 }
예제 #21
0
 public virtual void Update(GameTime gameTime, GameObjects gameObjects)
 {
     Location += velocity;
 }