public int updateProj(Mushroom[,] mushrooms, Spider spider) { proj.Y -= 10; if (proj.Y < 0) { isFiring = false; } foreach (Mushroom m in mushrooms) { if (proj.Intersects(m.loc) && m.visible) { isFiring = false; if (m.hit()) { return(1); } } } if (proj.Intersects(spider.getLoc()) && spider.visible()) { isFiring = false; return(spider.hit()); } return(0); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } kb = Keyboard.GetState(); if (!hasGameStarted) { if (kb.IsKeyDown(Keys.Enter) && !kbO.IsKeyDown(Keys.Enter)) { hasGameStarted = true; } return; } if (gameOver) { if (kb.IsKeyDown(Keys.LeftShift) && !kbO.IsKeyDown(Keys.LeftShift)) { gameOver = false; reset(); } } if (kb.IsKeyDown(Keys.LeftAlt) && !kbO.IsKeyDown(Keys.LeftAlt)) { updateLevel();//This is to test what you are working on in multiple levels. (Secret skip button) } //player movement logic if (kb.IsKeyDown(Keys.W)) { if (!Collision(player, 0)) { player.moveUp(); } } if (kb.IsKeyDown(Keys.A)) { if (!Collision(player, 2)) { player.moveLeft(); } } if (kb.IsKeyDown(Keys.D)) { if (!Collision(player, 3)) { player.moveRight(); } } if (kb.IsKeyDown(Keys.S)) { if (!Collision(player, 1)) { player.moveDown(); } } if (kb.IsKeyDown(Keys.Space) && !player.isFiring) { player.fire(); } if (player.isFiring) { score += player.updateProj(level.mushrooms, spider); } if (player.isFiring) {//having 2 is neccesary for the way centipedeproj works, do not combine it breaks shooting for (int i = 0; i < centipedes.Count; i++) { player.isFiring = centipedeProj(centipedes.ElementAt(i)); if (!player.isFiring) { break; } } } //Centipede logic for (int c = 0; c < centipedes.Count; c++) { if (centipedes.ElementAt(c).size() == 0) { centipedes.Remove(centipedes.ElementAt(c--)); } else { if (centipedeCollision(centipedes.ElementAt(c), gameTime)) {//if a collision does not occur the update happens centipedes.ElementAt(c).Update(gameTime); } } } if (centipedes.Count == 0) { newCentipede(); updateLevel(); } if (kb.IsKeyDown(Keys.P) && !kbO.IsKeyDown(Keys.P)) { centipedes.ElementAt(0).hit(5); } player.changeColor(level.backgroundColor); kbO = kb; if (spider.visible()) { spider.Update(gameTime); } if (!spider.visible() && Globals.rng.Next(1, 250) == 1)//respawns spider after a random time { spider = new Spider(new Texture2D[] { Content.Load <Texture2D>("spider0"), Content.Load <Texture2D>("spider1") }, GraphicsDevice.Viewport.Height - (Player.top * 2), GraphicsDevice.Viewport.Height - 20, level.backgroundColor, Globals.rng.Next(1, 3)); } if (spider.loc.Intersects(player.getRec()) && spider.visible()) { gameOver = true; } foreach (Centipede c in centipedes) { for (int x = 0; x < c.body.Length; x++) { if (c.body[x] != null) { if (c.body[x].position.Intersects(player.getRec())) { gameOver = true; } } } } base.Update(gameTime); }