protected override void Update(GameTime gameTime) { KeyboardState teclado = Keyboard.GetState(); if (haTerminado) { contador += gameTime.ElapsedGameTime.TotalSeconds; if (contador >= 5) { Exit(); } } if (!haTerminado) { if (teclado.IsKeyDown(Keys.Escape)) { Exit(); } //Colocar bomba jugador1 if (teclado.IsKeyDown(Keys.E)) { Bomba bAux = new Bomba(jugador1.X - (jugador1.X % 40), jugador1.Y - (jugador1.Y % 40), jugador1.LongitudBomba); //He cambiado el .Equals() para que compare solo las posiciones X e Y if (!bombas1.Contains(bAux)) { bAux.SetImagen(Content.Load <Texture2D>("bomba")); bombas1.Add(bAux); } } //Colocar bomba jugador2 if (teclado.IsKeyDown(Keys.Space)) { Bomba bAux = new Bomba(jugador2.X - (jugador2.X % 40), jugador2.Y - (jugador2.Y % 40), jugador2.LongitudBomba); //He cambiado el .Equals() para que compare solo las posiciones X e Y if (!bombas2.Contains(bAux)) { bAux.SetImagen(Content.Load <Texture2D>("bomba2")); bombas2.Add(bAux); } } //Jugador 1 jugador1.SetImagen(Content.Load <Texture2D>("jugador1")); if (teclado.IsKeyDown(Keys.W)) { jugador1.Y -= (int)(jugador1.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador1.SetImagen(Content.Load <Texture2D>("jugador1U")); } if (teclado.IsKeyDown(Keys.A)) { jugador1.X -= (int)(jugador1.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador1.SetImagen(Content.Load <Texture2D>("jugador1L")); } if (teclado.IsKeyDown(Keys.S)) { jugador1.Y += (int)(jugador1.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador1.SetImagen(Content.Load <Texture2D>("jugador1D")); } if (teclado.IsKeyDown(Keys.D)) { jugador1.X += (int)(jugador1.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador1.SetImagen(Content.Load <Texture2D>("jugador1R")); } //Jugador 2 jugador2.SetImagen(Content.Load <Texture2D>("jugador2")); if (teclado.IsKeyDown(Keys.Up)) { jugador2.Y -= (int)(jugador2.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador2.SetImagen(Content.Load <Texture2D>("jugador2U")); } if (teclado.IsKeyDown(Keys.Left)) { jugador2.X -= (int)(jugador2.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador2.SetImagen(Content.Load <Texture2D>("jugador2L")); } if (teclado.IsKeyDown(Keys.Down)) { jugador2.Y += (int)(jugador2.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador2.SetImagen(Content.Load <Texture2D>("jugador2D")); } if (teclado.IsKeyDown(Keys.Right)) { jugador2.X += (int)(jugador2.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador2.SetImagen(Content.Load <Texture2D>("jugador2R")); } //Bombas1 controlBombas(bombas1, gameTime); //Bombas2 controlBombas(bombas2, gameTime); //Colisiones jugador1 if (colisiona(jugador1.X, jugador1.Y, 30)) { if (teclado.IsKeyDown(Keys.W)) { jugador1.Y += (int)(jugador1.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (teclado.IsKeyDown(Keys.A)) { jugador1.X += (int)(jugador1.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (teclado.IsKeyDown(Keys.S)) { jugador1.Y -= (int)(jugador1.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (teclado.IsKeyDown(Keys.D)) { jugador1.X -= (int)(jugador1.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } } //Colisiones jugador2 if (colisiona(jugador2.X, jugador2.Y, 30, false)) { if (teclado.IsKeyDown(Keys.Up)) { jugador2.Y += (int)(jugador2.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (teclado.IsKeyDown(Keys.Left)) { jugador2.X += (int)(jugador2.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (teclado.IsKeyDown(Keys.Down)) { jugador2.Y -= (int)(jugador2.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (teclado.IsKeyDown(Keys.Right)) { jugador2.X -= (int)(jugador2.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } } for (int i = 0; i < mejoras.Count; i++) { //mejora con jugador1 if (new Rectangle(mejoras[i].X + 10, mejoras[i].Y + 10, 20, 20).Intersects( new Rectangle(jugador1.X, jugador1.Y, 30, 30))) { jugador1.LongitudBomba += 1; mejoras.RemoveAt(i); } //mejora con jugador2 if (new Rectangle(mejoras[i].X + 10, mejoras[i].Y + 10, 20, 20).Intersects( new Rectangle(jugador2.X, jugador2.Y, 30, 30))) { jugador2.LongitudBomba += 1; mejoras.RemoveAt(i); } } } base.Update(gameTime); }
protected override void Update(GameTime gameTime) { KeyboardState teclado = Keyboard.GetState(); //Pausa if (teclado.IsKeyDown(Keys.P) && !pulsada) { pausado = pausado ? false : true; } pulsada = teclado.IsKeyDown(Keys.P) ? true : false; if (pausado) { if (teclado.IsKeyDown(Keys.Escape)) { Exit(); } } if (!pausado) { //Colocar bomba if (teclado.IsKeyDown(Keys.E)) { Bomba bAux = new Bomba(jugador.X - (jugador.X % 40), jugador.Y - (jugador.Y % 40), jugador.LongitudBomba); //He cambiado el .Equals() para que compare solo las posiciones X e Y if (!bombas.Contains(bAux)) { bAux.SetImagen(Content.Load <Texture2D>("bomba")); bombas.Add(bAux); } } //Movimiento jugador jugador.SetImagen(Content.Load <Texture2D>("jugador1")); if (teclado.IsKeyDown(Keys.W)) { jugador.Y -= (int)(jugador.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador.SetImagen(Content.Load <Texture2D>("jugador1U")); } if (teclado.IsKeyDown(Keys.A)) { jugador.X -= (int)(jugador.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador.SetImagen(Content.Load <Texture2D>("jugador1L")); } if (teclado.IsKeyDown(Keys.S)) { jugador.Y += (int)(jugador.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador.SetImagen(Content.Load <Texture2D>("jugador1D")); } if (teclado.IsKeyDown(Keys.D)) { jugador.X += (int)(jugador.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); jugador.SetImagen(Content.Load <Texture2D>("jugador1R")); } //Se mueven los enemigos foreach (Enemigo e in enemigos) { e.Mover(gameTime); } //HACER FUNCIONAMIENTO DE LAS BOMBAS for (int i = 0; i < bombas.Count; i++) { if ((int)(bombas[i].Contador += gameTime.ElapsedGameTime.TotalSeconds) == 2 && !bombas[i].HaExplotado()) { bombas[i].Explotar(paredes, muros); foreach (Explosion e in bombas[i].GetExplosion()) { e.SetImagen(Content.Load <Texture2D>("explosion")); //Colisiones de las explosiones con los muros para destruirlos for (int j = 0; j < muros.Count; j++) { if (new Rectangle(muros[j].X, muros[j].Y, 40, 40).Intersects( new Rectangle(e.X, e.Y, 40, 40))) { muros.RemoveAt(j); } } } } if (bombas[i].GetExplosion().Count > 0) { foreach (Explosion e in bombas[i].GetExplosion()) { //Colision de las bombas con los enemigos para matarlos for (int j = 0; j < enemigos.Count; j++) { if (new Rectangle(enemigos[j].X, enemigos[j].Y, 40, 40).Intersects( new Rectangle(e.X, e.Y, 40, 40))) { puntuacion += enemigos[j].GetType() == typeof(EnemigoFinal) ? 300 : 100; enemigos.RemoveAt(j); } } if (new Rectangle(jugador.X, jugador.Y, 30, 30).Intersects( new Rectangle(e.X, e.Y, 40, 40))) { Exit(); } } } if (bombas[i].Contador >= 4) { bombas.RemoveAt(i); } } //Se comprueban colisiones de los enemigos foreach (Enemigo e in enemigos) { if (colisiona(e.X, e.Y, 40, true)) { e.X -= (int)(e.GetVelocidadX() * (float)gameTime.ElapsedGameTime.TotalSeconds); e.Y -= (int)(e.GetVelocidadY() * (float)gameTime.ElapsedGameTime.TotalSeconds); e.CambiarDireccion(); } //Aquí se comprueba si pasa por al lado de un hueco y hay una probabilidad de que cambie de direccion //Para que no entren en bucles if (e.GetType() != typeof(Enemigo3)) { if ((e.Y % 80 == 0 && e.GetVelocidadX() == 0 || e.X % 40 == 0 && e.X % 80 != 0 && e.GetVelocidadY() == 0) && r.Next(0, 3) == 2) { e.CambiarDireccion(); } } //Si el enemigo final te toca mueres if (new Rectangle(e.X + 10, e.Y + 10, 30, 30).Intersects( new Rectangle(jugador.X, jugador.Y, 30, 30))) { Thread.Sleep(1500); Exit(); } } //Se comprueban colisiones con las paredes if (colisiona(jugador.X, jugador.Y, 30)) { if (teclado.IsKeyDown(Keys.W)) { jugador.Y += (int)(jugador.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (teclado.IsKeyDown(Keys.A)) { jugador.X += (int)(jugador.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (teclado.IsKeyDown(Keys.S)) { jugador.Y -= (int)(jugador.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (teclado.IsKeyDown(Keys.D)) { jugador.X -= (int)(jugador.GetVelocidad() * (float)gameTime.ElapsedGameTime.TotalSeconds); } } //Calcular segundos if (tiempo > 0) { tiempo -= gameTime.ElapsedGameTime.TotalSeconds; } else if (!final) { enemigosFinales(); } //Salida de la puerta if (new Rectangle(salida.X + 10, salida.Y + 10, 20, 20).Intersects( new Rectangle(jugador.X, jugador.Y, 30, 30)) && enemigos.Count == 0) { puntuacion += 10000; muerto = false; Exit(); } //Colision con la mejora if (mejora != null && new Rectangle(mejora.X + 10, mejora.Y + 10, 20, 20).Intersects( new Rectangle(jugador.X, jugador.Y, 30, 30))) { mejora = null; puntuacion += 1000; jugador.LongitudBomba += 1; } } base.Update(gameTime); }