public int ShootAliens(AlienFleet alienFleet) { int numHit = 0; Rectangle bulletRect = new Rectangle((int)X, (int)Y, (int)Width, (int)Height); for (int i = 0; i < 11; i++) { for (int j = 0; j < 5; j++) { Alien alien = alienFleet.aliens[i, j]; if (alien.Visible) { Rectangle alienRect = new Rectangle((int)alien.X, (int)alien.Y, (int)alien.Width, (int)alien.Height); if (HitTest(bulletRect, alienRect)) { numHit++; Visible = false; PlaySound(gameContent.alienShotSound); alien.Visible = false; } } } } return(numHit); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); gameContent = new GameContent(Content); screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width; screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height; if (screenWidth >= 2500) { screenWidth = 2500; } if (screenHeight >= 1250) { screenHeight = 1250; } graphics.PreferredBackBufferWidth = screenWidth; graphics.PreferredBackBufferHeight = screenHeight; graphics.ApplyChanges(); speed1 = gameContent.speed1Sound.CreateInstance(); speed2 = gameContent.speed2Sound.CreateInstance(); speed3 = gameContent.speed3Sound.CreateInstance(); speed4 = gameContent.speed4Sound.CreateInstance(); int spaceShipX = (screenWidth - gameContent.imgSpaceShip.Width / 4) / 2; int spaceShipY = screenHeight - 150; spaceShip = new SpaceShip(spaceShipX, spaceShipY, screenWidth, spriteBatch, gameContent); shields = new Shield[3]; for (int x = 0; x < 3; x++) { shields[x] = new Shield(screenWidth / 3 * x + screenWidth / 8, spaceShip.Y - 150, spriteBatch, GraphicsDevice); } alienFleet = new AlienFleet(50, 150, screenWidth, spaceShip.Y, spriteBatch, gameContent); ufo = new UFO(0, 60, screenWidth, spriteBatch, gameContent); gameBorder = new GameBorder(screenWidth, screenHeight, spriteBatch, gameContent); }
public void RestartGame() { livesRemaining = 3; aliensKilled = 0; alienFireRate = 5000; firstTime = false; hitBottom = false; level = 1; score = 0; int spaceShipX = (screenWidth - gameContent.imgSpaceShip.Width / 4) / 2; int spaceShipY = screenHeight - 150; spaceShip = new SpaceShip(spaceShipX, spaceShipY, screenWidth, spriteBatch, gameContent); shields = new Shield[3]; for (int x = 0; x < 3; x++) { shields[x] = new Shield(screenWidth / 3 * x + screenWidth / 8, spaceShip.Y - 150, spriteBatch, GraphicsDevice); } alienFleet = new AlienFleet(50, 150, screenWidth, spaceShip.Y, spriteBatch, gameContent); ufo = new UFO(0, 60, screenWidth, spriteBatch, gameContent); gameBorder = new GameBorder(screenWidth, screenHeight, spriteBatch, gameContent); }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); spaceShip.Draw(); foreach (Shield shield in shields) { shield.Draw(); } alienFleet.Draw(); ufo.Draw(spaceShip); gameBorder.Draw(); spriteBatch.DrawString(gameContent.labelFont, "Lives: " + livesRemaining.ToString(), new Vector2(40, 10), Color.White); Vector2 levelSpace = gameContent.labelFont.MeasureString("Level: " + level.ToString()); spriteBatch.DrawString(gameContent.labelFont, "Level: " + level.ToString(), new Vector2((screenWidth - levelSpace.X) / 2, 10), Color.White); spriteBatch.DrawString(gameContent.labelFont, "Score: " + score.ToString(), new Vector2(screenWidth - 200, 10), Color.White); if (readyToStart) { if (spaceShip.Visible && !hitBottom) { ufo.Visible = false; string startMsg = "Press <Space> or Click to start the level"; Vector2 startSpace = gameContent.labelFont.MeasureString(startMsg); spriteBatch.DrawString(gameContent.labelFont, startMsg, new Vector2((screenWidth - startSpace.X) / 2, screenHeight / 16), Color.White); } else if (livesRemaining > 0) { for (int x = 0; x < 11; x++) { for (int y = 0; y < 5; y++) { foreach (Blaster blaster in alienFleet.aliens[x, y].blasters) { blaster.Visible = false; } } } spaceShip.RespawnSpaceShip(); livesRemaining--; readyToStart = false; } else { string endMsg = "Game Over"; Vector2 endSpace = gameContent.labelFont.MeasureString(endMsg); spriteBatch.DrawString(gameContent.labelFont, endMsg, new Vector2((screenWidth - endSpace.X) / 2, screenHeight / 16 - endSpace.Y), Color.White); string startMsg = "Press <Space> or Click to start the level"; Vector2 startSpace = gameContent.labelFont.MeasureString(startMsg); spriteBatch.DrawString(gameContent.labelFont, startMsg, new Vector2((screenWidth - startSpace.X) / 2, screenHeight / 16 + endSpace.Y), Color.White); } } else if (aliensKilled % 55 == 0 && firstTime) { alienFleet = new AlienFleet(50, 150, screenWidth, spaceShip.Y, spriteBatch, gameContent); foreach (Bullet bullet in spaceShip.bullets) { bullet.Visible = false; } readyToStart = true; firstTime = false; alienFireRate /= 2; level++; livesRemaining++; foreach (Shield shield in shields) { for (int x = 0; x < level; x++) { shield.Rebuild(); } } } spriteBatch.End(); base.Draw(gameTime); }