using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; using System.Text; namespace Ejemplo2D { public class Engine : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; AnimSprite Megaman = new AnimSprite(); List FondosCielo = new List(); List FondosPicos = new List(); AnimSprite Fondo = new AnimSprite(); int TiempoFlow; //Variable que nos servirá para controlar el tiempo en el juego. public Engine() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "content"; graphics.PreferredBackBufferWidth = 640; // dimensiones de nuestra ventana en la que correra nuestro juego graphics.PreferredBackBufferHeight = 480; //graphics.IsFullScreen = true; // Establece que el juego será en pantalla completa } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { int PosTmp = 0; spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice); //instanciamos nuestro objeto spriteBach //************************************* Carga Fondos ************************************* //---------------------- Cielo Azul ------------------------- for (int i = 1;i < 5; i++) { AnimSprite FondoTmp = new AnimSprite(PosTmp, 0, 0, new Vector2(0, 0)); FondoTmp.CargaImagen(Content.Load("Sprites/Fondo3" )); FondosCielo.Add(FondoTmp); PosTmp = FondoTmp.Largo * i; } PosTmp = 0; //----------------------- Picos ------------------------------------ for (int i = 1; i < 8; i++) { AnimSprite FondoTmp = new AnimSprite(PosTmp, 325, 0, new Vector2(0, 0)); FondoTmp.CargaImagen(Content.Load("Sprites/Fondo2")); FondosPicos.Add(FondoTmp); PosTmp = FondoTmp.Largo * i; } //----------------------------- Piso -------------------------------- Fondo = new AnimSprite(0, 353, 0, new Vector2(0, 0)); Fondo.CargaImagen(Content.Load("Sprites/Fondo1")); //*********************************************************************************************** // Carga nuestros sprites for (int i = 1; i<=7; i++) Megaman.CargaImagen(Content.Load("Sprites/MM"+ i)); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { TiempoFlow = TiempoFlow + gameTime.ElapsedGameTime.Milliseconds; // incrementa nuestra variable TiempoFlow con el tiempo transcurrido if (TiempoFlow > 50) { foreach (AnimSprite Tmp in FondosCielo) Tmp.XPos -= 1; TiempoFlow = 0; // Termina el juego al presionar la tecla Esc if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) this.Exit(); // Cacha el estado de las teclas presionadas en el teclado KeyboardState kState = Keyboard.GetState(); ////////////////////////////////////////////////////////D E R E C H A///////////////////////////////////////////////////////////////////////// if (kState.IsKeyDown(Keys.Right) && kState.IsKeyUp(Keys.Left)) { //Condición de avance del personaje if (Megaman.XPos < 540 || (Megaman.XPos = graphics.PreferredBackBufferWidth-Fondo.Largo+10) { //Cielo for(int i= 0; i < FondosCielo.Count;i++) FondosCielo[i].XPos -= 3; //Picos for (int i = 0; i < FondosPicos.Count; i++) { FondosPicos[i].XPos -= 6; if (FondosPicos[i].XPos + FondosPicos[i].Largo < 0) { if (i == 0) FondosPicos[i].XPos = FondosPicos[FondosPicos.Count - 1].XPos + FondosPicos[FondosPicos.Count - 1].Largo-6; else FondosPicos[i].XPos = FondosPicos[i - 1].XPos + FondosPicos[i - 1].Largo-6; } } // Piso Fondo.XPos -= 10; } //Megaman if (Megaman.UltimaDir == 0 && Megaman.XPos < graphics.PreferredBackBufferWidth - 40) Megaman.UpdateMov(); else Megaman.FrameActual = 6; Megaman.UltimaDir = 0; } ////////////////////////////////////////////////////////////I Z Q U I E R D A ///////////////////////////////////////////////////////////////// else if (kState.IsKeyDown(Keys.Left) && kState.IsKeyUp(Keys.Right)) { if (Megaman.XPos > 100 || (Megaman.XPos > 0 && Fondo.XPos >= 0)) Megaman.XPos -= 10; else if (Fondo.XPos<0) { //Cielo for (int i = FondosCielo.Count-1; i >= 0; i--) { FondosCielo[i].XPos += 3; if (FondosCielo[i].XPos > graphics.PreferredBackBufferWidth) { if (i == FondosCielo.Count - 1) FondosCielo[i].XPos = FondosCielo[0].XPos - FondosCielo[i].Largo + 3; else FondosCielo[i].XPos = FondosCielo[i + 1].XPos - FondosCielo[i].Largo + 3; } } //Picos for (int i = FondosPicos.Count - 1; i >= 0; i--) { FondosPicos[i].XPos += 6; if (FondosPicos[i].XPos > graphics.PreferredBackBufferWidth) { if (i == FondosPicos.Count - 1) FondosPicos[i].XPos = FondosPicos[0].XPos - FondosPicos[i].Largo + 6; else FondosPicos[i].XPos = FondosPicos[i + 1].XPos - FondosPicos[i].Largo + 6; } } //Piso Fondo.XPos += 10; } if (Megaman.UltimaDir == 1 && Megaman.XPos > 0) Megaman.UpdateMov(); else Megaman.FrameActual = 6; Megaman.UltimaDir = 1; } else { Megaman.FrameActual = 6; } //Verificacion para el fondo movil for (int i = 0; i < FondosCielo.Count; i++) { if (FondosCielo[i].XPos + FondosCielo[i].Largo < 0) { if (i == 0) FondosCielo[i].XPos = FondosCielo[FondosCielo.Count - 1].XPos + FondosCielo[FondosCielo.Count - 1].Largo; else FondosCielo[i].XPos = FondosCielo[i - 1].XPos + FondosCielo[i - 1].Largo; } } } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { // GraphicsDevice.Clear(Color.White); foreach (AnimSprite Tmp in FondosCielo) Tmp.Draw(spriteBatch); foreach (AnimSprite Tmp in FondosPicos) Tmp.Draw(spriteBatch); Fondo.Draw(spriteBatch); Megaman.Draw(spriteBatch); base.Draw(gameTime); } } }