Exemple #1
0
        public void Move(Paddle player, Paddle computer)
        {
            //  Balls have more complex movement than paddles.  If they hit top or bottom, invert Y velocity.
            //  If they hit a paddle, change velocity depending upon where on the paddle we hit
            //  If we the ball misses the paddle and falls off the right or left edge, increase the score of
            //  the other side, decrease this side's number of lives (if implemented), wait a second or two,
            //  put the ball back in the center, and give a random direction.
            //  We might also want (someday) to gradually increase velocity when the player hits the ball back.

            // reset collision_occured when we cross centerline
            // this avoids the odd case where we collide with the paddle while we are leaving the scene
            if ((this.position.X > 350) && (this.position.X < 450))
            {
                this.collision_occured = false;
            }

            // check to see if we hit a paddle
            if (Player_Collision(player) && this.collision_occured == false)
            {
                collision_occured = true;
                playit            = true;
                // We hit a paddle; where did we hit?
                // If normal, just invert velocity as appropriate, and leave with departure angle equal to attack angle
                // If corner, hit a kill shot
                if (Corner_Collision(player))
                {
                    // kill shot
                    velocity = new Vector2((-this.velocity.X * 2), this.velocity.Y + rnd.Next(-1, 2));
                }
                else
                {
                    // normal ball return; just invert X velocity; play with velocity just a bit

                    velocity = new Vector2(-this.velocity.X, rnd.Next(((int)this.velocity.Y - 1), ((int)this.velocity.Y + 2)));
                }
            }

            else if (Computer_Collision(computer) && this.collision_occured == false)
            {
                collision_occured = true;// We hit a paddle; where did we hit?
                playit            = true;
                // If normal, just invert velocity as appropriate, and leave with departure angle equal to attack angle
                // If corner, hit a kill shot
                if (Corner_Collision(computer))
                {
                    // kill shot
                    velocity = new Vector2((-this.velocity.X * 2), this.velocity.Y);
                }
                else
                {
                    // normal ball return; just invert X velocity
                    velocity = new Vector2(-this.velocity.X, rnd.Next(((int)this.velocity.Y - 1), ((int)this.velocity.Y + 2)));
                }
            }

            //we might be at an edge, do the right thing if needed

            //  Check right boundary to see if we missed the paddle
            else if (this.position.X + this.size.X + this.velocity.X > this.screenSize.X)
            {
                // Give the other guy a point
                scoreComputer += 1;
                // Play the paddle miss sound
                //TODO
                // Go again
                Reset();
                //
            }

            //  Check left boundary to see if we missed the paddle
            else if (this.position.X + this.velocity.X < 0)
            {
                // Give the other guy a point
                scorePlayer += 1;
                // Play the paddle miss sound
                //TODO
                // Go again
                Reset();
            }

            //  Check bottom boundary
            else if (this.position.Y + this.size.Y + this.velocity.Y > this.screenSize.Y)
            {
                this.velocity = new Vector2(this.velocity.X, -this.velocity.Y);
            }

            // Check top boundary
            else if (this.position.Y + this.velocity.Y < 0)
            {
                this.velocity = new Vector2(this.velocity.X, -this.velocity.Y);
            }
            ;