/// <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) { timeElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds; rndVelocity.X = rnd.Next(-5, 5) / 10.0f; rndVelocity.Y = rnd.Next(-5, 5) / 10.0f; if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } MouseState mouse = Mouse.GetState(); if (mouse.LeftButton == ButtonState.Pressed) { activated = true; } else if ((mouse.LeftButton == ButtonState.Released) && activated) { mine = new Mine(mineSprite0, mouse.X, mouse.Y); if (!mines.Contains(mine)) { mines.Add(mine); } activated = false; } if (timeElapsed >= randomSpawn) { bear = new TeddyBear(bearSprite0, rndVelocity, rnd.Next(0, 800), rnd.Next(0, 600)); bears.Add(bear); timeElapsed = 0; } foreach (TeddyBear bear in bears) { bear.Update(gameTime); } if ((mines.Count != 0) && (bears.Count != 0)) { for (i = bears.Count - 1; i >= 0; i--) { for (a = mines.Count - 1; a >= 0; a--) { if (bears[i].CollisionRectangle.Intersects(mines[a].CollisionRectangle)) { explosion = new Explosion(explosionSprite0, mines[a].CollisionRectangle.X, mines[a].CollisionRectangle.Y); bears[i].Active = !bears[i].Active; mines[a].Active = !mines[a].Active; explosions.Add(explosion); break; } } } } foreach (Explosion explosion in explosions) { explosion.Update(gameTime); } for (i = bears.Count - 1; i >= 0; i--) { if (!bears[i].Active) { bears.Remove(bears[i]); } } for (i = mines.Count - 1; i >= 0; i--) { if (!mines[i].Active) { mines.Remove(mines[i]); } } for (i = explosions.Count - 1; i >= 0; i--) { if (!explosions[i].Playing) { explosions.Remove(explosions[i]); } } 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } //Removes all the inactive objects for (int i = teddys.Count - 1; i >= 0; i--) { if (!teddys[i].Active) { teddys.RemoveAt(i); } } for (int i = mines.Count - 1; i >= 0; i--) { if (!mines[i].Active) { mines.RemoveAt(i); } } for (int i = explosions.Count - 1; i >= 0; i--) { if (!explosions[i].Playing) { explosions.RemoveAt(i); } } //Creats a mine when you press left mouse button MouseState newState = Mouse.GetState(); if (newState.LeftButton == ButtonState.Pressed && oldState.LeftButton == ButtonState.Released) { Mine m = new Mine(mine, newState.X, newState.Y); mines.Add(m); } oldState = newState; //Creats a teddy bear after the spawn delay time has finished if (gameTime.TotalGameTime.Seconds >= spawnDelay) { spawnDelay += ((rand.Next(3) + 1)); double x = (rand.NextDouble() - 0.5); double y = (rand.NextDouble() - 0.5); Vector2 v = new Vector2((float)x, (float)y); TeddyBear t = new TeddyBear(teddy, v, rand.Next(WindowWidth), rand.Next(WindowHeight)); teddys.Add(t); } //Updates the movement of the teddybear for (int i = 0; i < teddys.Count; i++) { teddys[i].Update(gameTime); } //Creats an explosion each time a teddybear hits a bomb if (teddys.Count > 0 && mines.Count > 0) { foreach (TeddyBear t in teddys) { if (t.Active) { foreach (Mine m in mines) { if (m.Active) { Rectangle mineRect = m.CollisionRectangle; if (t.CollisionRectangle.Intersects(mineRect)) { t.Active = false; m.Active = false; Explosion e = new Explosion(explosion, t.Location.X, t.Location.Y); explosions.Add(e); } } } } } } base.Update(gameTime); }