/// <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); }
/// <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(); } keyi = key; key = Keyboard.GetState(); if (!endGame) { //update player player.Update(gameTime, key, keyi); //update lazers if (key.IsKeyDown(Keys.Space) && keyi.IsKeyUp(Keys.Space)) { Missile newMissile = new Missile(); newMissile.build(player.X + player.Rect.Width / 2 - shotMissleRect.Width * 5 / 2, player.Y - shotMissleRect.Height * 5); lazers.Add(newMissile.getNewMissle()); } if (lazers.Count > 0) { for (int i = 0; i < lazers.Count; i++) { Rectangle hold = lazers.ElementAt(i); hold.Y -= 10; lazers.Remove(lazers.ElementAt(i)); if (lazers.Count > 0) { lazers.Insert(i, hold); } else { lazers.Add(hold); } } } //update centipede centipede.Move(); mushrooms = centipede.hitHead(Content, lazers); mushrooms = centipede.hitTail(Content, lazers); centipede.UpdateMushroom(mushrooms); //update mushrooms bool added = false; for (int i = 0; i < 25; i++) { if (i < mushrooms.Count - 1 && mushrooms[i].mushroom == mushrooms[i + 1].mushroom) { mushrooms[i].randShroom(rand); } else if (i >= mushrooms.Count && i < 40 && gameTime.TotalGameTime.TotalMilliseconds % 5000 < 1 && !added) { mushrooms.Add(new Mushroom(Content, this, rand)); added = true; } } //mushroom collision for (int i = 0; i < lazers.Count(); i++) { for (int j = 0; j < mushrooms.Count(); j++) { if (i > 1) { if (lazers[i].Intersects(mushrooms[j].mushroom)) { mushrooms.RemoveAt(j); i -= 1; lazers.RemoveAt(i + 1); } } } } //update spider if (!spiderOn && gameTime.TotalGameTime.TotalMilliseconds % 5000 < 1) { spider = new Spider(graphics, centipedeSpriteSheet); spiderOn = true; } else if (spiderOn) { if (player.Rect.Intersects(spider.getPos())) { endGame = true; } for (int i = 0; i < lazers.Count(); i++) { if (lazers.ElementAt(i).Intersects(spider.getPos())) { spider = null; spiderOn = false; break; } } } if (spiderOn) { spider.Update(graphics, gameTime, mushrooms); } } else { if (key.IsKeyDown(Keys.R) && !keyi.IsKeyDown(Keys.R)) { endGame = false; spider = null; spiderOn = false; Initialize(); LoadContent(); } } base.Update(gameTime); }