using System; using System.Collections.Generic; 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.Net; using Microsoft.Xna.Framework.Storage; namespace SpriteTest2 { public class SpriteTest2 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Sprite s1, s2; Sprite[] flowers; public SpriteTest2() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { // two sprites: one drawn on top of the other as determined by the lvl value s1 = new Sprite(); s1.Position = new Vector2(200, 200); s1.Lvl = 0.3f; s1.Velocity = new Vector2(50, 50); s2 = new Sprite(); s2.Position = new Vector2(250, 250); s2.Lvl = 0.2f; s2.Velocity = new Vector2(-50, -50); Random r = new Random(); flowers = new Sprite[20]; for (int i = 0; i < 20; i++) { flowers[i] = new Sprite(); flowers[i].Position = new Vector2(r.Next(graphics.GraphicsDevice.Viewport.Width), r.Next(graphics.GraphicsDevice.Viewport.Height)); } base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); Texture2D texture = Content.Load("WalkingSquare"); s1.Texture = texture; s1.W = 100; s1.H = 100; s1.Rect = new Rectangle(0, 60, s1.W, s1.H); s2.Texture = texture; s2.W = 100; s2.H = 100; s2.Rect = new Rectangle(0, 60, s2.W, s2.H); s2.Color = Color.LawnGreen; Texture2D texture2 = Content.Load("flower"); foreach (Sprite s in flowers) { s.Texture = texture2; s.W = texture2.Width; s.H = texture2.Height; s.Rect = new Rectangle(0, 0, s.W, s.H); } } protected override void Update(GameTime gameTime) { s1.Update(gameTime); //s1.Bounce(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); //if (s1.Velocity.X >= 0) //{ // s1.Reflection = SpriteEffects.None; //} //else //{ // s1.Reflection = SpriteEffects.FlipHorizontally; //} s2.Update(gameTime); //s2.Bounce(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); //if (s2.Velocity.X >= 0) //{ // s2.Reflection = SpriteEffects.None; //} //else //{ // s2.Reflection = SpriteEffects.FlipHorizontally; //} // Background doesn't move -> no need to update it base.Update(gameTime); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None); foreach (Sprite s in flowers) { s.Draw(spriteBatch); } spriteBatch.End(); spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None); s1.Draw(spriteBatch); s2.Draw(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } } }