/// <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); //Load menu menu = Content.Load <Texture2D>("Ping_Pong_Menu"); //gamePlayMenu = Content.Load<Texture2D>("play-menu"); playMenu = Content.Load <Texture2D>("menu-play"); computerMenu = Content.Load <Texture2D>("menu-computer"); creditMenu = Content.Load <Texture2D>("menu-credits"); exitMenu = Content.Load <Texture2D>("menu-exit"); hoverPlay = Content.Load <Texture2D>("hover-play"); hoverDemo = Content.Load <Texture2D>("hover-computer"); hoverCredit = Content.Load <Texture2D>("hover-credit"); hoverExit = Content.Load <Texture2D>("hover-exit"); //End load menu //Load background background = Content.Load <Texture2D>("table"); // Load the SoundEffect resource ballHit = Content.Load <SoundEffect>("ballhit"); killShot = Content.Load <SoundEffect>("killshot"); miss = Content.Load <SoundEffect>("miss"); shotGunMenu = Content.Load <SoundEffect>("shotgun"); song = Content.Load <Song>("backgroundMusic"); MediaPlayer.Play(song); MediaPlayer.Volume = 10f; // Create a SoundEffect instance that can be manipulated later // seInstance.IsLooped = true; //Set font Font1 = Content.Load <SpriteFont>("Courier New"); // TODO: use this.Content to load your game content here //Draw ball ball = new clsSprite(Content.Load <Texture2D>("small_ball"), new Vector2(graphics.PreferredBackBufferHeight / 2, graphics.PreferredBackBufferWidth / 2), new Vector2(35f, 35f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); //Paddle for computer //Left hand side , position (0,height/2), size(10,height) computer = new paddle(Content.Load <Texture2D>("left_paddle"), new Vector2(50f, graphics.PreferredBackBufferHeight / 2), new Vector2(50f, 133f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); //Paddle for human //Righ hand side, position( width , height/2), size(10,height); human = new paddle(Content.Load <Texture2D>("right_paddle"), new Vector2(graphics.PreferredBackBufferWidth - 100f, graphics.PreferredBackBufferHeight / 2), new Vector2(50f, 133f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); rightComputer = new paddle(Content.Load <Texture2D>("right_paddle"), new Vector2(graphics.PreferredBackBufferWidth - 100f, graphics.PreferredBackBufferHeight / 2), new Vector2(50f, 133f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); // set the speed the sprites will move //Set random number between 0 and 10 ball.velocity = new Vector2(10.1f, 10.1f); computer.velocity = new Vector2(0, 5.1f); }
} // sprite radius //AI working here public void Move(clsSprite ball) { //This is for computer player, computer should follow the ball //Computer should follow the ball Y coordinate // checking top boundary if (ball.position.X <= screenSize.X && ball.velocity.X <= 0) { Random getrandom = new Random(); Console.WriteLine("{0}", getrandom.Next(1, 20)); if (this.position.Y + this.velocity.Y <= 0) { velocity = new Vector2(0, 8.5f); } //Checking bottom boundary else if (this.position.Y + this.size.Y + this.velocity.Y >= screenSize.Y) { velocity = new Vector2(0, -8.5f); } //Keep track of the the paddle Y is more than ball Y else if (this.center.Y - ball.center.Y >= getrandom.Next(1, 20)) { velocity = new Vector2(0, -8.5f); } //Keep track of the the paddle Y is less than ball Y else if (this.center.Y - ball.center.Y <= getrandom.Next(1, 20)) { velocity = new Vector2(0, 8.5f); } //If paddle Y == ball Y else if (this.center.Y - ball.center.Y == getrandom.Next(1, 20)) { velocity = new Vector2(0, 8.5f); } // since we adjusted the velocity, just add it to the current position position += velocity; } }
//This is for right side computer player, computer should follow the ball //Computer should follow the ball Y coordinate public void MoveRight(clsSprite ball) { //Moving when ball toward computer if (ball.velocity.X >= 0) { Random getrandom = new Random(); // checking top boundary if (this.position.Y + this.velocity.Y <= 0) { velocity = new Vector2(0, 8.5f); } //Checking bottom boundary else if (this.position.Y + this.size.Y + this.velocity.Y >= screenSize.Y) { velocity = new Vector2(0, -8.5f); } //Keep track of the the paddle Y is more than ball Y else if (this.center.Y - ball.center.Y >= getrandom.Next(1, 21)) { velocity = new Vector2(0, -8.5f); } //Keep track of the the paddle Y is less than ball Y else if (this.center.Y - ball.center.Y <= getrandom.Next(1, 21)) { velocity = new Vector2(0, 8.5f); } //If paddle Y == ball Y else if (this.center.Y - ball.center.Y == getrandom.Next(1, 21)) { velocity = new Vector2(0, 8.5f); } // since we adjusted the velocity, just add it to the current position position += velocity; } }