Example #1
0
        public void check_for_score(Puck puck)
        {
            if (puck.pos.X <= -18 && (puck.pos.Y < 4 && puck.pos.Y > -4))
            {
                //player on the right hand side +1
                game.score2 += 1;

                //reset the speed of the puck
                puck.velocity.X = 0;
                puck.velocity.Y = 0;

                //reset the position of the puck
                puck.pos.X = -11;
                puck.pos.Y = 0;
            }
            if (puck.pos.X >= 18 && (puck.pos.Y < 4 && puck.pos.Y > -4))
            {
                //player on the left hand side +1
                game.score1 += 1;

                //reset the speed of the puck
                puck.velocity.X = 0;
                puck.velocity.Y = 0;

                //reset the position of the puck
                puck.pos.X = 11;
                puck.pos.Y = 0;
            }
        }
Example #2
0
 public void calc_rebound_wall(Puck puck)
 {
     if (puck.pos.Y > game.boundaryTop - radius || puck.pos.Y < game.boundaryBottom + radius)
     {
         puck.velocity.Y *= -0.95f;
     }
     if (puck.pos.X > game.boundaryRight - radius || puck.pos.X < game.boundaryLeft + radius)
     {
         puck.velocity.X *= -0.95f;
     }
 }
Example #3
0
        public void resetGame()
        {
            //reset the score to 0
            score1 = 0;
            score2 = 0;

            gameObjects = new List <GameObject>();
            player1     = new Player(this, new Vector3(boundaryLeft + 2, 0, 0), PlayerNumber.P1);
            player2     = new Player(this, new Vector3(boundaryRight - 2, 0, 0), PlayerNumber.P2);
            puck        = new Puck(this);
            arena       = new Arena(this);
            gameObjects.Add(player1);
            gameObjects.Add(player2);
            gameObjects.Add(puck);
            gameObjects.Add(arena);
        }
Example #4
0
        protected override void LoadContent()
        {
            // Initialise game object containers.
            gameObjects        = new List <GameObject>();
            addedGameObjects   = new Stack <GameObject>();
            removedGameObjects = new Stack <GameObject>();

            // Create game objects.
            player1 = new Player(this, new Vector3(boundaryLeft + 2, 0, 0), PlayerNumber.P1);
            player2 = new Player(this, new Vector3(boundaryRight - 2, 0, 0), PlayerNumber.P2);
            puck    = new Puck(this);
            arena   = new Arena(this);
            gameObjects.Add(player1);
            gameObjects.Add(player2);
            gameObjects.Add(puck);
            gameObjects.Add(arena);

            screenWidth = Windows.UI.Xaml.Window.Current.Bounds.Width;
            // Create an input layout from the vertices

            base.LoadContent();
        }
Example #5
0
        private void AIUpdate(GameTime gameTime)
        {
            // Decision making for the player two AI
            Puck puck = game.puck;

            if (puck.pos.X + 1 > pos.X)
            {
                // the puck is behind P2
                // this line works but causes jerky movement in P2
                velocity.X = puck.velocity.Length() * 0.9f;
            }

            else if (puck.velocity.X < 0 && puck.pos.X < 4)
            {
                // Puck is heading away from the P2
                // attempt to return to the idle position
                float diffX = pos.X - idlePos.X;
                float diffY = pos.Y - idlePos.Y;
                float dist  = (float)Math.Sqrt(Math.Pow(diffX, 2) + Math.Pow(diffY, 2));

                if (dist > 1.5)
                {
                    velocity.X = -diffX * 0.05f;
                    velocity.Y = -diffY * 0.05f;
                }
            }
            else if (/*puck.velocity.Length() < 1 && */ puck.pos.X > 0)
            {
                // puck is slowly moving within range of P2
                // try to strike the puck

                float diffX;
                float diffY;
                float dist;


                if (puck.velocity.Length() > 1)
                {
                    diffX = pos.X - (puck.pos.X + puck.velocity.X * puck.velocity.X);
                    diffY = pos.Y - (puck.pos.Y + puck.velocity.Y * puck.velocity.Y);
                    dist  = (float)Math.Sqrt(Math.Pow(diffX, 2) + Math.Pow(diffY, 2));
                }
                else
                {
                    diffX = pos.X - (puck.pos.X + puck.velocity.X * 2);
                    diffY = pos.Y - (puck.pos.Y + puck.velocity.Y * 2);
                    dist  = (float)Math.Sqrt(Math.Pow(diffX, 2) + Math.Pow(diffY, 2));
                }


                velocity.X = -diffX * 0.075f;
                velocity.Y = -diffY * 0.075f;
            }
            else
            {
                // puck is moving quickly toward P2
                // try to get between the puck and the goal
            }

            velocity *= game.AIspeed;
        }