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.Xml; using System.Xml.Serialization; using System.IO; using System; using System.Collections.Generic; using System.Linq; namespace WindowsGame1 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; //Global Variable Declarations Texture2D backgroundTexture; Texture2D sprite; Texture2D sprite1; Point frameCounter = new Point(0,0); Point frameCounter1 = new Point(0, 0); Vector2 position = Vector2.Zero; Vector2 speed = Vector2.Zero; SpriteEffects se =SpriteEffects.FlipHorizontally; double frameDelay = 0.1; double frameDelay1 = 0.5; Level level; //xml level code that will be serialized into this object SpriteFont spriteFont; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { XmlSerializer s = new XmlSerializer(typeof(Level)); TextReader r = new StreamReader("Content/Level.xml"); level = (Level)s.Deserialize(r); r.Close(); foreach (Level.character c in level.allCharacters) { c.sprite = Content.Load(c.textureAsset); c.bound = new BoundingSphere(new Vector3(c.position , 0), 50); } base.Initialize(); } protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); spriteFont = Content.Load("Arial"); backgroundTexture = Content.Load("Background"); sprite = Content.Load("Boy"); sprite1 = Content.Load("Tree"); //================= Help me here ============================ // //create new sprite here called Fence or wall //png file will be added into the content called Fence.png } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { KeyboardState kb = new KeyboardState(); kb = Keyboard.GetState(); speed = Vector2.Zero; bool moving = false; if (kb.IsKeyDown(Keys.Right)) { speed.X = 2; moving = true; se = SpriteEffects.FlipHorizontally; frameCounter.Y = 0; } if (kb.IsKeyDown(Keys.Up)) { speed.Y = -2; moving = true; frameCounter.Y = sprite.Height * 2 / 3; } if (kb.IsKeyDown(Keys.Left)) { speed.X = -2; moving = true; se = SpriteEffects.None; frameCounter.Y = 0; } if (kb.IsKeyDown(Keys.Down)) { speed.Y = 2; moving = true; frameCounter.Y = sprite.Height / 3; } position += speed; if (moving) { frameDelay -= gameTime.ElapsedGameTime.TotalSeconds; if (frameDelay < 0) { frameDelay = 0.1; frameCounter.X += sprite.Width / 8; if (frameCounter.X >= sprite.Width) frameCounter.X = 0; } } frameDelay1 -= gameTime.ElapsedGameTime.TotalSeconds; if (frameDelay1 < 0) { frameDelay1 = 0.5; frameCounter1.X += sprite1.Width / 5; if (frameCounter1.X >= sprite1.Width) frameCounter1.X = 0; } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Green); spriteBatch.Begin(); //Draw Background (Background with trees, rock, garden etc...) spriteBatch.Draw(backgroundTexture, new Rectangle(0,0,Window.ClientBounds.Width, Window.ClientBounds.Height), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0); //Draw Sprite1 (Tree) spriteBatch.Draw(sprite1, new Vector2(300, 500), new Rectangle(frameCounter1.X, frameCounter1.Y, sprite1.Width / 6, sprite1.Height), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0); //Draw Sprite (Boy character) spriteBatch.Draw(sprite, position, new Rectangle(frameCounter.X, frameCounter.Y, sprite.Width/8, sprite.Height/3), Color.White,0,Vector2.Zero,1,se,0); //================= Help me here ============================ // //Draw new sprite (Fence) //The boy cannot past through the fence //create a bounding sphere at the players current position BoundingSphere playerSphere = new BoundingSphere(new Vector3(position,0),1); //In this loop we draw each Male/Female character... foreach (Level.character c in level.allCharacters) { spriteBatch.Draw(c.sprite, c.position, new Rectangle(0, 0, c.sprite.Height, c.sprite.Height), Color.White, 0, Vector2.Zero,c.scale, SpriteEffects.None, 0); if (c.bound.Intersects(playerSphere)) { spriteBatch.DrawString(spriteFont, c.greeting, (c.position + new Vector2(10, -10)), Color.White); } } spriteBatch.End(); base.Draw(gameTime); } } }