public bool Computer_Collision(Paddle paddle) { //computer if ((this.position.X <= paddle.position.X + paddle.size.X) && // left side (this.position.Y + this.size.Y >= paddle.position.Y) && // top boundary (this.position.Y <= paddle.position.Y + paddle.size.Y)) // bottom boundary return true; else return false; }
public newGameMulti(Game game, Paddle _computer_paddle, Paddle _player_paddle, Song _bgm, SoundEffect _p1, SoundEffect _p2, SoundEffect _ballhit, Ball _ball, GraphicsDeviceManager _graphics, SpriteBatch _spriteBatch, Texture2D _bgi) : base(game) { computer_paddle = _computer_paddle; player_paddle = _player_paddle; //sourceRectpleft = _sourceRectpleft; //sourceRectpright = _sourceRectpright; bgm = _bgm; p1 = _p1; p2 = _p2; ballhit = _ballhit; ball = _ball; graphics = _graphics; spriteBatch = _spriteBatch; bgi = _bgi; }
public bool Corner_Collision(Paddle paddle) { // check if the ball hit the corner of a paddle in the kill shot zone // assumes we have a collision, so this function should only be called if that is true if (((this.position.Y + this.size.Y) <= (paddle.position.Y + Paddle.PADDLE_KILL_SHOT_FRACTION)) // upper corner || ((this.position.Y) >= (paddle.position.Y + paddle.size.Y - Paddle.PADDLE_KILL_SHOT_FRACTION))) // lower corner return true; else return false; }
//collision with paddle? could have combined these, but this is easier reading public bool Player_Collision(Paddle paddle) { //player if ((this.position.X + this.size.X >= paddle.position.X) && // right side (this.position.Y + this.size.Y >= paddle.position.Y) && //top boundary (this.position.Y < paddle.position.Y + paddle.size.Y)) //bottom boundary return true; else return false; }
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); }; }
/// <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); Services.AddService(typeof(SpriteBatch), spriteBatch); //Load the SoundEffect resources // Load the SoundEffect resource p1 = Content.Load<SoundEffect>("p1m"); p2 = Content.Load<SoundEffect>("p2m"); bgm = Content.Load<Song>("bgm"); ballhit = Content.Load<SoundEffect>("ball"); win1Scr=Content.Load<Texture2D>("p1win"); bgi = Content.Load<Texture2D>("bg-Recovered"); MediaPlayer.IsRepeating = true; menuBack = Content.Load<Texture2D>("title"); menuTxt = Content.Load<Texture2D>("menu1"); menu = new MenuScreen(this, menuBack, menuTxt); Components.Add(menu); menu.Show(); winScrPl = new Win1Screen(this, win1Scr, spriteBatch); Font1 = Content.Load<SpriteFont>("Courier New"); sourceRectpleft = new Rectangle(0, 0, 48, 124); sourceRectpright = new Rectangle(0, 0, 48, 124); sourceRectBall = new Rectangle(0, 0, 40, 40); computer_paddle = new Paddle(Content.Load<Texture2D>("p1"), new Vector2(20,384), new Vector2(42, 124), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); player_paddle = new Paddle(Content.Load<Texture2D>("p2"), new Vector2(950,384), new Vector2(42, 124), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); ball = new Ball(Content.Load<Texture2D>("ballsprite"), new Vector2(384, 284), new Vector2(40, 40), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); newGameSingle1 = new newGameSingle(this, computer_paddle, player_paddle, bgm, p1, p2, ballhit, ball, graphics, spriteBatch, bgi); newGameMulti1 = new newGameMulti(this, computer_paddle, player_paddle, bgm, p1, p2, ballhit, ball, graphics, spriteBatch, bgi); // set the speed the objects will move // the ball always starts in the middle and moves toward the player ball.Reset(); computer_paddle.velocity = new Vector2(0, 2); }
/// <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 the SoundEffect resources // Load the SoundEffect resource ballhit = Content.Load<SoundEffect>("ballhit"); killshothit = Content.Load<SoundEffect>("killshot"); paddlemiss = Content.Load<SoundEffect>("miss"); Font1 = Content.Load<SpriteFont>("Courier New"); computer_paddle = new Paddle(Content.Load<Texture2D>("left_paddle"), new Vector2(20f, 268f), new Vector2(24f, 64f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); player_paddle = new Paddle(Content.Load<Texture2D>("right_paddle"), new Vector2(756f, 268f), new Vector2(24f, 64f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); ball = new Ball(Content.Load<Texture2D>("small_ball"), new Vector2(384f, 284f), new Vector2(32f, 32f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); // set the speed the objects will move // the ball always starts in the middle and moves toward the player ball.Reset(); computer_paddle.velocity = new Vector2(0, 2); }