예제 #1
0
파일: Ball.cs 프로젝트: k0vl/Pong
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            //first, Check if firing ball
            if( (Keyboard.GetState().IsKeyDown(Keys.Space) || gameObjects.TouchInput.Tapped) && attachedToPaddle != null)
            {
                var newVelocity = new Vector2(500f, attachedToPaddle.Velocity.Y * 0.75f);
                Velocity = newVelocity;
                attachedToPaddle = null;
            }

            //second, update postion if still attached to paddle
            else if(attachedToPaddle != null)
            {
                Location.X = attachedToPaddle.Location.X + attachedToPaddle.Width;
                Location.Y = attachedToPaddle.Location.Y + (attachedToPaddle.Height-this.Height)/2;
                Velocity = Vector2.Zero;
            }

            //third, check bounce and boost if fired
            else
            {
                CheckBounce(gameObjects);
            }

            base.Update(gameTime, gameObjects);
        }
예제 #2
0
파일: Paddle.cs 프로젝트: k0vl/Pong
        public override void Update(GameTime gameTime, GameObjects gameObjects)
        {
            if(playerType == PlayerTypes.Human)
            {
                if(Keyboard.GetState().IsKeyDown(Keys.Up) || gameObjects.TouchInput.Up)
                {
                    Velocity = new Vector2(0, -PADDLE_SPEED);
                }

                if(Keyboard.GetState().IsKeyDown(Keys.Down) || gameObjects.TouchInput.Down)
                {
                    Velocity = new Vector2(0, PADDLE_SPEED);
                }
            }
            else if(playerType == PlayerTypes.Computer)
            {
                if(gameObjects.Ball[0].Location.Y + gameObjects.Ball[0].Height < Location.Y - reactionTreshold)
                {
                    Velocity = new Vector2(0, -PADDLE_SPEED);
                }
                if(gameObjects.Ball[0].Location.Y > Location.Y + Height + reactionTreshold)
                {
                    Velocity = new Vector2(0, PADDLE_SPEED);
                }
            }

            base.Update(gameTime, gameObjects);
        }
예제 #3
0
파일: Controller.cs 프로젝트: k0vl/Pong
        public void CheckBoost(GameObjects gameObjects)
        {
            if(gameObjects.Boost.Count != 0 && gameObjects.Ball.Count != 0){

                List<Ball> toAdd = new List<Ball>();
                List<Boost> toRemove = new List<Boost>();
                foreach(Ball ball in gameObjects.Ball)
                {
                    foreach(Boost boost in gameObjects.Boost)
                    {
                        if(ball.BoundingBox.Intersects(boost.BoundingBox))
                        {
                            if(boost.boostTypes == BoostTypes.Speed && ball.speedFlag < 1)
                            {
                                ball.Velocity = new Vector2(ball.Velocity.X * 1.5f, ball.Velocity.Y * 1.5f);
                                ball.speedFlag += 1;
                            }
                            else if(boost.boostTypes == BoostTypes.Redirect)
                            {
                                ball.Velocity = new Vector2(ball.Velocity.X, 0);
                            }
                            else if(boost.boostTypes == BoostTypes.Slow && ball.speedFlag > -1)
                            {
                                ball.Velocity = new Vector2(ball.Velocity.X / 1.5f, ball.Velocity.Y / 1.5f);
                                ball.speedFlag -= 1;
                            }
                            else if(boost.boostTypes == BoostTypes.Deflect)
                            {
                                ball.Velocity = new Vector2(-ball.Velocity.X, ball.Velocity.Y);
                            }
                            else if(boost.boostTypes == BoostTypes.Split)
                            {
                                Ball newball1 = new Ball(gameObjects.BallTexture, ball.Location, gameObjects.GameBoundries,ball.speedFlag);
                                newball1.Velocity=new Vector2(ball.Velocity.X, 0);
                                Ball newball2 = new Ball(gameObjects.BallTexture, ball.Location, gameObjects.GameBoundries,ball.speedFlag);
                                newball2.Velocity=new Vector2(ball.Velocity.X, -ball.Velocity.Y);
                                toAdd.Add(newball1);
                                toAdd.Add(newball2);
                            }
                            else
                            {
                            }

                            toRemove.Add(boost);
                        }
                    }
                }

                gameObjects.Ball.AddRange(toAdd);
                gameObjects.Boost.RemoveAll(x => toRemove.Contains(x));
            }
        }
예제 #4
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);

                        #if __ANDROID__
            gameBoundries = new Rectangle(0, 0, Window.ClientBounds.Height, Window.ClientBounds.Width);
                        #else
            gameBoundries = new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height);
                        #endif

            Texture2D paddleTexture = Content.Load <Texture2D>("Paddle");

            Vector2 humanPaddleLocation = new Vector2(
                PADDLE_OFFSET
                , (gameBoundries.Height - paddleTexture.Height * Game1.DeviceScale) / 2
                );
            Vector2 computerPaddleLocation = new Vector2(
                gameBoundries.Width - paddleTexture.Width * Game1.DeviceScale - PADDLE_OFFSET
                , (gameBoundries.Height - paddleTexture.Height * Game1.DeviceScale) / 2
                );

            List <Texture2D> boostTexture = new List <Texture2D>();
            for (int i = 1; i <= 7; i++)
            {
                boostTexture.Add(Content.Load <Texture2D>("Boost/Boost" + i));
            }

            gameObjects = new GameObjects {
                PlayerPaddle   = new Paddle(paddleTexture, humanPaddleLocation, gameBoundries, PlayerTypes.Human),
                ComputerPaddle = new Paddle(paddleTexture, computerPaddleLocation, gameBoundries, PlayerTypes.Computer),
                Ball           = new List <Ball> {
                    new Ball(Content.Load <Texture2D>("Ball"), Vector2.Zero, gameBoundries)
                },
                Score         = new Score(Content.Load <SpriteFont>("fonts/Arial"), gameBoundries),
                Controller    = new Controller(),
                Boost         = new List <Boost> {
                },
                GameBoundries = gameBoundries,
                BoostTexture  = boostTexture,
                BallTexture   = Content.Load <Texture2D>("Ball")
            };

            gameObjects.Ball[0].AttachTo(gameObjects.PlayerPaddle);
        }
예제 #5
0
파일: Ball.cs 프로젝트: k0vl/Pong
        public void CheckBounce(GameObjects gameObjects)
        {
            if (BoundingBox.Intersects(gameObjects.PlayerPaddle.BoundingBox))
            {
                float paddleCenter = gameObjects.PlayerPaddle.Location.Y + (gameObjects.PlayerPaddle.Height / 2);
                float ballCenter   = Location.Y + (this.Height / 2);
                float VelocityY    = Velocity.Y + Paddle.PADDLE_SPEED * (ballCenter - paddleCenter) / gameObjects.PlayerPaddle.Height;

                Velocity = new Vector2(Math.Abs(Velocity.X), VelocityY);
            }
            else if (BoundingBox.Intersects(gameObjects.ComputerPaddle.BoundingBox))
            {
                float paddleCenter = gameObjects.ComputerPaddle.Location.Y + (gameObjects.ComputerPaddle.Height / 2);
                float ballCenter   = Location.Y + (this.Height / 2);
                float VelocityY    = Velocity.Y + Paddle.PADDLE_SPEED * (ballCenter - paddleCenter) / gameObjects.ComputerPaddle.Height;

                Velocity = new Vector2(-Math.Abs(Velocity.X), VelocityY);
            }
        }
예제 #6
0
파일: Ball.cs 프로젝트: k0vl/Pong
        public void CheckBounce(GameObjects gameObjects)
        {
            if(BoundingBox.Intersects(gameObjects.PlayerPaddle.BoundingBox))
            {
                float paddleCenter = gameObjects.PlayerPaddle.Location.Y+(gameObjects.PlayerPaddle.Height/2);
                float ballCenter = Location.Y+(this.Height/2);
                float VelocityY =  Velocity.Y + Paddle.PADDLE_SPEED * (ballCenter - paddleCenter)/gameObjects.PlayerPaddle.Height;

                Velocity = new Vector2(Math.Abs(Velocity.X), VelocityY);
            }
            else if(BoundingBox.Intersects(gameObjects.ComputerPaddle.BoundingBox))
            {
                float paddleCenter = gameObjects.ComputerPaddle.Location.Y+(gameObjects.ComputerPaddle.Height/2);
                float ballCenter = Location.Y+(this.Height/2);
                float VelocityY =  Velocity.Y + Paddle.PADDLE_SPEED * (ballCenter - paddleCenter)/gameObjects.ComputerPaddle.Height;

                Velocity = new Vector2(-Math.Abs(Velocity.X), VelocityY);
            }
        }
예제 #7
0
        private void CheckWin(GameObjects gameObjects)
        {
            List <Ball> toRemove = new List <Ball>();

            foreach (Ball ball in gameObjects.Ball)
            {
                if (ball.Location.X + ball.Width < 0)
                {
                    ComputerScore++; toRemove.Add(ball); gameObjects.Score.UpdateScore(PlayerScore, ComputerScore);
                }
                else if (ball.Location.X > gameObjects.GameBoundries.Width)
                {
                    PlayerScore++; toRemove.Add(ball); gameObjects.Score.UpdateScore(PlayerScore, ComputerScore);
                }
            }

            gameObjects.Ball.RemoveAll(x => toRemove.Contains(x));

            if (gameObjects.Ball.Count == 0)
            {
                Reset(gameObjects);
            }
        }
예제 #8
0
파일: Score.cs 프로젝트: k0vl/Pong
 public void Update(GameTime gameTime, GameObjects gameObjects)
 {
     #if __ANDROID__
     float xposition = this.position.X, yposition = this.position.Y;
     if(Activity1.Accl[0] > 3)
     {
         yposition = gameObjects.GameBoundries.Height - 100 - Height/2;
         rotation = 0;
         xposition = (gameObjects.GameBoundries.Width /2)- (Width/2);
     } //- (Width/2)
     else if(Activity1.Accl[0] < -3)
     {
         yposition = 100 + Height/2;
         rotation = (float)Math.PI; //in radian, rotate 180 degree
         xposition = (gameObjects.GameBoundries.Width /2)+ (Width/2);
     }
     else
     {}
     this.position = new Vector2(xposition, yposition);
     #else
     this.position = new Vector2((gameObjects.GameBoundries.Width /2) - (Width/2), gameObjects.GameBoundries.Height - 100);
     #endif
 }
예제 #9
0
파일: Score.cs 프로젝트: k0vl/Pong
 public void Update(GameTime gameTime, GameObjects gameObjects)
 {
                 #if __ANDROID__
     float xposition = this.position.X, yposition = this.position.Y;
     if (Activity1.Accl[0] > 3)
     {
         yposition = gameObjects.GameBoundries.Height - 100 - Height / 2;
         rotation  = 0;
         xposition = (gameObjects.GameBoundries.Width / 2) - (Width / 2);
     }             //- (Width/2)
     else if (Activity1.Accl[0] < -3)
     {
         yposition = 100 + Height / 2;
         rotation  = (float)Math.PI;                //in radian, rotate 180 degree
         xposition = (gameObjects.GameBoundries.Width / 2) + (Width / 2);
     }
     else
     {
     }
     this.position = new Vector2(xposition, yposition);
                 #else
     this.position = new Vector2((gameObjects.GameBoundries.Width / 2) - (Width / 2), gameObjects.GameBoundries.Height - 100);
                 #endif
 }
예제 #10
0
 public virtual void Update(GameTime gameTime, GameObjects gameObjects)
 {
     Location += (Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds * Game1.DeviceScale);
     CheckBounds();
 }
예제 #11
0
 public void Update(GameTime gameTime, GameObjects gameObjects)
 {
     CheckWin(gameObjects);
     CheckBoost(gameObjects);
 }
예제 #12
0
파일: Boost.cs 프로젝트: k0vl/Pong
 public override void Update(GameTime gameTime, GameObjects gameObjects)
 {
 }
예제 #13
0
파일: Sprite.cs 프로젝트: k0vl/Pong
 public virtual void Update(GameTime gameTime, GameObjects gameObjects)
 {
     Location += (Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds * Game1.DeviceScale);
     CheckBounds();
 }
예제 #14
0
파일: Controller.cs 프로젝트: k0vl/Pong
 public void Reset(GameObjects gameObjects)
 {
     gameObjects.Score.UpdateScore(PlayerScore, ComputerScore);
     gameObjects.PlayerPaddle.Reset();
     gameObjects.ComputerPaddle.Reset();
     gameObjects.Ball.Add(new Ball(gameObjects.BallTexture, Vector2.Zero, gameObjects.GameBoundries));
     gameObjects.Ball[0].AttachTo(gameObjects.PlayerPaddle);
     gameObjects.Boost.Clear();
 }
예제 #15
0
파일: Controller.cs 프로젝트: k0vl/Pong
 public void Update(GameTime gameTime, GameObjects gameObjects)
 {
     CheckWin(gameObjects);
     CheckBoost(gameObjects);
 }
예제 #16
0
 public override void Update(GameTime gameTime, GameObjects gameObjects)
 {
 }
예제 #17
0
파일: Controller.cs 프로젝트: k0vl/Pong
        private void CheckWin(GameObjects gameObjects)
        {
            List<Ball> toRemove = new List<Ball>();

            foreach(Ball ball in gameObjects.Ball)
            {
                if(ball.Location.X + ball.Width < 0)
                {ComputerScore++; toRemove.Add(ball); gameObjects.Score.UpdateScore(PlayerScore, ComputerScore);}
                else if(ball.Location.X > gameObjects.GameBoundries.Width)
                {PlayerScore++; toRemove.Add(ball); gameObjects.Score.UpdateScore(PlayerScore, ComputerScore);}
            }

            gameObjects.Ball.RemoveAll(x => toRemove.Contains(x));

            if(gameObjects.Ball.Count == 0)
            {
                Reset(gameObjects);
            }
        }
예제 #18
0
파일: Game1.cs 프로젝트: k0vl/Pong
        /// <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);

            #if __ANDROID__
            gameBoundries = new Rectangle(0, 0, Window.ClientBounds.Height, Window.ClientBounds.Width);
            #else
            gameBoundries = new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height);
            #endif

            Texture2D paddleTexture = Content.Load<Texture2D>("Paddle");

            Vector2 humanPaddleLocation = new Vector2(
                PADDLE_OFFSET
                , (gameBoundries.Height - paddleTexture.Height * Game1.DeviceScale)/2
            );
            Vector2 computerPaddleLocation = new Vector2(
                gameBoundries.Width - paddleTexture.Width * Game1.DeviceScale - PADDLE_OFFSET
                , (gameBoundries.Height - paddleTexture.Height * Game1.DeviceScale)/2
            );

            List<Texture2D> boostTexture = new List<Texture2D>();
            for(int i = 1; i <= 7; i++)
            {
                boostTexture.Add(Content.Load<Texture2D>("Boost/Boost"+i));
            }

            gameObjects = new GameObjects{
                PlayerPaddle = new Paddle(paddleTexture, humanPaddleLocation, gameBoundries, PlayerTypes.Human),
                ComputerPaddle = new Paddle(paddleTexture, computerPaddleLocation , gameBoundries, PlayerTypes.Computer),
                Ball = new List<Ball>{new Ball(Content.Load<Texture2D>("Ball"), Vector2.Zero, gameBoundries)},
                Score = new Score(Content.Load<SpriteFont>("fonts/Arial"), gameBoundries),
                Controller = new Controller(),
                Boost = new List<Boost>{},
                GameBoundries = gameBoundries,
                BoostTexture = boostTexture,
                BallTexture = Content.Load<Texture2D>("Ball")
            };

            gameObjects.Ball[0].AttachTo(gameObjects.PlayerPaddle);
        }