//pre: clsSprite object initialized and in use //post: returns true if a collision is detected public bool Collides(clsSprite otherSprite) { return(this.position.X + this.size.X >= otherSprite.position.X && this.position.X <= otherSprite.position.X + otherSprite.size.X && this.position.Y + this.size.Y >= otherSprite.position.Y && this.position.Y <= otherSprite.position.Y + otherSprite.size.Y); /* These lines of code denote a collision detection using a cartesian coord. system. returns true if detected */ }
//pre: clsSprite object(s) initialized and in use //post: AI for npc sprite. Chase down the otherSprite public void ChaseBallSprite(clsSprite otherSprite) { if (this.position.Y < otherSprite.position.Y) { velocity = new Vector2(0, cpuChaseSpeed); } if (this.position.Y > otherSprite.position.Y) { velocity = new Vector2(0, -cpuChaseSpeed); } position += velocity; }
/// <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); // TODO: use this.Content to load your game content here gameFont = Content.Load <SpriteFont>("SpriteFont1"); //Load backgrounds mControllerDetectScreenBakcground = Content.Load <Texture2D>("ControllerDetectScreen"); mTitleScreenBackground = Content.Load <Texture2D>("TitleScreen"); mPauseMenu = Content.Load <Texture2D>("paused"); //Initialize the current screen state to the screen we want to display first mCurrentScreen = ScreenState.ControllerDetect; //Load Menu //main.LoadContent(Content); // Load (2) 2D texture sprites <ball.png> mySpriteBall = new clsSprite(Content.Load <Texture2D>("ball32"), new Vector2(screenWidth - 72f, 0f), new Vector2(32f, 32f), screenWidth, screenHeight); // (0,0) is top left of screen, (64, 64) is size of sprite playerPaddle = new clsSprite(Content.Load <Texture2D>("pong_paddle"), /* (218,118) represents the top right of screen, 64^2 is size */ new Vector2(40f, 118f), new Vector2(10f, 64f), screenWidth, screenHeight); enemyPaddle = new clsSprite(Content.Load <Texture2D>("pong_paddle"), new Vector2(screenWidth - 40f, 118f), new Vector2(10f, 64f), screenWidth, screenHeight); // Load Sound Effect Resource soundEffect = Content.Load <SoundEffect>("LASER"); movingSoundEffect = Content.Load <SoundEffect>("LASER"); audioEngine = new AudioEngine("Content\\Lab3Sounds.xgs"); waveBank = new WaveBank(audioEngine, "Content\\Wave Bank.xwb"); soundBank = new SoundBank(audioEngine, "Content\\Sound Bank.xsb"); streamingWaveBank = new WaveBank(audioEngine, "Content\\Music.xwb"); audioEngine.Update(); // musicCue = soundBank.GetCue("Galactic_Damages"); // musicCue.Play(); mySpriteBall.velocity = new Vector2(-5, 5); /* setting initial x and y velocity (values arbitrary) */ }
//Pre: A collision has occured on the left hand side of the screen. //Post: The velocity of the pong is adjusted based on where it hit the CPU paddle private void lhsCPUCollision(clsSprite lhs, clsSprite rhs) { GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f); lhs.position.X += 1; // prevents sprites getting stuck together if ((lhs.position.Y + lhs.halfSizeY) > (rhs.position.Y + rhs.halfSizeY)) { lhs.velocity = new Vector2(gameVelocity, gameVelocity); } if ((lhs.position.Y + lhs.halfSizeY) <= (rhs.position.Y + rhs.halfSizeY)) { lhs.velocity = new Vector2(gameVelocity, -gameVelocity); } }
//Pre: A collision has occured between the pongBall and the left hand side player sprite //Post: modifies the velocity and direction based on player movements (if player isn't moving, position determines trajectory) private void lhsPlayerCollision(clsSprite lhs, clsSprite rhs) { GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f); KeyboardState keyboardState = Keyboard.GetState(); lhs.position.X += 1; // This helps prevent sticking collision (ball direction ->) if (keyboardState.IsKeyDown(Keys.Up) || ((lhs.position.Y + lhs.halfSizeY) < (rhs.position.Y + rhs.halfSizeY))) // if we are moving up, so will the ball { lhs.velocity = new Vector2(gameVelocity, -gameVelocity); } if (keyboardState.IsKeyDown(Keys.Down) || ((lhs.position.Y + lhs.halfSizeY) > (rhs.position.Y + rhs.halfSizeY))) // if we are moving down, so will the ball { lhs.velocity = new Vector2(gameVelocity, gameVelocity); } }
// Pre: Game has started and sprites are in motion. // Post: Method runs recursively in the update method. Sprite follows mouse Y position. private void mouseControls(clsSprite playerSprite) { if (playerPaddle.position.X < Mouse.GetState().X) { playerPaddle.position += new Vector2(0, 0); } if (playerPaddle.position.X > Mouse.GetState().X) { playerPaddle.position += new Vector2(0, 0); } if (playerPaddle.position.Y < Mouse.GetState().Y) { playerPaddle.position += new Vector2(0, gameVelocity); // No x direction movement allowed } if (playerPaddle.position.Y > Mouse.GetState().Y) { playerPaddle.position += new Vector2(0, -gameVelocity); // No X direction movement allowed } }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Methods for Game1. Must be Private!! // Pre: Game has started. // Post: The method runs recursively as a part of the update method. Player alters Sprite Velocity private void keyBoardControls(clsSprite playerSprite) { KeyboardState keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.Up)) { playerSprite.position += new Vector2(0, -gameVelocity); } if (keyboardState.IsKeyDown(Keys.Down)) { playerSprite.position += new Vector2(0, gameVelocity); } if (keyboardState.IsKeyDown(Keys.Left)) { playerSprite.position += new Vector2(0, 0); // No x movement in pong } if (keyboardState.IsKeyDown(Keys.Right)) { playerSprite.position += new Vector2(0, 0); // No x movement in pong } }
//pre: clsSprite object(s) initialized and in use //post: returns true if circular sprite collision is detected public bool CircleCollides(clsSprite otherSprite) { // Check if two circle sprites collided return(Vector2.Distance(this.center, otherSprite.center) <= this.radius + otherSprite.radius); }