/// <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"); //Drawing Right Paddle computer_paddle = new Paddle(Content.Load <Texture2D>("right_paddle"), new Vector2(444f, 268f), new Vector2(24f, 64f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); //Border Line Border = new Border_Line(Content.Load <Texture2D>("Center"), new Vector2(467f, -87f), new Vector2(32f, 32f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); //Drawing Left Paddle player_paddle = new Paddle(Content.Load <Texture2D>("left_paddle"), new Vector2(505f, 268f), new Vector2(24f, 64f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); //Ball ball = new Ball(Content.Load <Texture2D>("small_ball"), new Vector2(600f, 100f), 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, 3); //Block Class blockUp = new Block(Content.Load <Texture2D>("Up Arrow125"), new Vector2(503f, 0f), new Vector2(24f, 64f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); }
public bool Border_Collision_Player(Border_Line Border) { if ((this.position.X >= Border.size.X + Border.position.X)) { return(true); } return(false); }
public bool Border_Collision_Computer(Border_Line Border) { if ((this.position.X <= Border.size.X + Border.position.X) && // left side (this.position.Y + this.size.Y >= Border.position.Y) && // top boundary (this.position.Y <= Border.position.Y + Border.size.Y)) { return(true); } return(false); }
public void Move(Paddle player, Paddle computer, Border_Line Border) { // 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 > 420) && (this.position.X < 520)) { 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 * 1 + 1), this.velocity.Y + rnd.Next(-2, 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 >= Border.position.X && this.position.X < Border.position.X + Border.size.X && this.velocity.X > 0) { // Give the other guy a point scorePlayer += 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 < Border.position.X + Border.size.X && this.position.X > Border.position.X && this.velocity.X < 0) { // Give the other guy a point scoreComputer += 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); } // Check bottom boundary else if (this.position.Y + this.size.Y + this.velocity.Y > this.screenSize.Y) { this.position = new Vector2(position.X, 0f); } // Check top boundary else if (this.position.Y + this.velocity.Y < 0) { this.position = new Vector2(position.X, screenSize.Y - this.size.Y); } //check right boundary else if (this.position.X + this.size.X + this.velocity.X > this.screenSize.X) { this.position = new Vector2(0f, position.Y); } //check left boundary else if (this.position.X + this.velocity.X < 0) { this.position = new Vector2(screenSize.X - this.size.X, position.Y); } }