/// <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(); } //Conditional statement for mouse left click lastMouseState = currentMouseState; currentMouseState = Mouse.GetState(); if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed) { Mine mine = new Mine(mineSprite, Mouse.GetState().X, Mouse.GetState().Y); //mine activated at mouses location mine.Active = true; mineList.Add(mine); } //Random time between 1-3 seconds int randtime = rand.Next(1000, 3001); timer += gameTime.ElapsedGameTime.Milliseconds; // Increment the timer by the elapsed game time. Vector2 randVect = new Vector2(); randText = rand.Next(0, textureList.Count); randVect.X = (float)rand.Next(-5, 5) / 10; randVect.Y = (float)rand.Next(-5, 5) / 10; randX = rand.Next(0, graphics.PreferredBackBufferWidth - 1); //random pixel in the windowWidth randY = rand.Next(0, graphics.PreferredBackBufferHeight - 1); //random pixel in the windowHeight if (timer >= randtime) // Check to see if X amount of seconds has passed. { while (randVect.X.Equals(0) && randVect.Y.Equals(0)) //if velocity is 0 give it a new velocity { randVect.X = (float)rand.Next(-5, 5) / 10; randVect.Y = (float)rand.Next(-5, 5) / 10; } bearList.Add(new TeddyBear(textureList[randText], randVect, randX, randY)); timer = 0; // Reset the timer. } //checks collisons between bears and mines foreach (TeddyBear bear in bearList) { foreach (Mine mine in mineList) { //if the bear and mine are intersecting, there is a collision if (bear.CollisionRectangle.Intersects(mine.CollisionRectangle) && mine.Active && bear.Active) { explosionList.Add(new Explosion(explosionSprite, mine.Location.X, mine.Location.Y)); bear.Active = false; mine.Active = false; foreach (Explosion explosion in explosionList) { explosion.Play(mine.Location.X, mine.Location.Y); } } } bear.Update(gameTime); } //remove inactive bears and mines for (int i = bearList.Count - 1; i >= 0; i--) { if (!bearList[i].Active) { bearList.Remove(bearList[i]); } } for (int i = mineList.Count - 1; i >= 0; i--) { if (!mineList[i].Active) { mineList.Remove(mineList[i]); } } //update explosion foreach (Explosion explosion in explosionList) { if (explosion.Playing) { explosion.Update(gameTime); } } 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) { 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); }