| 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; |
| |
| namespace RainCatcher2 |
| { |
| public class Game1 : Microsoft.Xna.Framework.Game |
| { |
| GraphicsDeviceManager graphics; |
| SpriteBatch spriteBatch; |
| public Random random = new Random(); |
| public List<RainDrop> rainDropList = new List<RainDrop>(); |
| public List<RainBucket> rainBucketList = new List<RainBucket>(); |
| public Rectangle viewPortRect; |
| private float timer = 0f; |
| private float interval = 1000f; |
| private Texture2D blueTexture; |
| public Game1() |
| { |
| graphics = new GraphicsDeviceManager(this); |
| Content.RootDirectory = "Content"; |
| } |
| protected override void Initialize() |
| { |
| viewPortRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); |
| RainBucket blue = new RainBucket(this); |
| blue.texture = Content.Load<Texture2D>("bluebox"); |
| rainBucketList.Add(blue); |
| RainBucket red = new RainBucket(this); |
| red.texture = Content.Load<Texture2D>("bluebox"); |
| rainBucketList.Add(red); |
| RainBucket green = new RainBucket(this); |
| green.texture = Content.Load<Texture2D>("bluebox"); |
| rainBucketList.Add(green); |
| RainBucket yellow = new RainBucket(this); |
| yellow.texture = Content.Load<Texture2D>("bluebox"); |
| rainBucketList.Add(yellow); |
| base.Initialize(); |
| } |
| protected override void LoadContent() |
| { |
| spriteBatch = new SpriteBatch(GraphicsDevice); |
| blueTexture = Content.Load<Texture2D>("bluedrop"); |
| } |
| protected override void UnloadContent() |
| { |
| } |
| protected override void Update(GameTime gameTime) |
| { |
| timer -= gameTime.ElapsedGameTime.Milliseconds; |
| if (timer <= 0) |
| { |
| timer = interval; |
| RainDrop drop = new RainDrop(this); |
| switch (random.Next(3)) |
| { |
| case 0: drop.texture = blueTexture; break; |
| case 1: drop.texture = blueTexture; break; |
| case 2: drop.texture = blueTexture; break; |
| case 3: drop.texture = blueTexture; break; |
| |
| } |
| drop.position.X = random.Next(viewPortRect.Width - drop.texture.Width); |
| rainDropList.Add(drop); |
| } |
| for (int i = 0; i < rainDropList.Count; i++) |
| rainDropList[i].Update(gameTime); |
| for (int i = 0; i < rainBucketList.Count; i++) |
| rainBucketList[i].Update(gameTime); |
| for (int i = 0; i < rainBucketList.Count; i++) |
| { |
| for (int j = 0; j < rainDropList.Count; j++) |
| { |
| if (rainDropList[j].collissionRectangle.Intersects(rainBucketList[i].collissionRectangle)) |
| { |
| rainDropList.RemoveAt(j); |
| j--; |
| } |
| } |
| } |
| base.Update(gameTime); |
| } |
| protected override void Draw(GameTime gameTime) |
| { |
| GraphicsDevice.Clear(Color.Black); |
| spriteBatch.Begin(); |
| foreach (RainDrop drop in rainDropList) |
| { |
| spriteBatch.Draw(drop.texture, drop.position, Color.White); |
| } |
| foreach (RainBucket bucket in rainBucketList) |
| { |
| spriteBatch.Draw(bucket.texture, bucket.position, Color.White); |
| } |
| spriteBatch.End(); |
| base.Draw(gameTime); |
| } |
| } |
| public class GameObject |
| { |
| public Texture2D texture; |
| public Vector2 position; |
| public Rectangle collissionRectangle; |
| |
| protected Game1 game; |
| |
| public GameObject(Game1 game) |
| { |
| this.game = game; |
| } |
| |
| public virtual void Update(GameTime gameTime) |
| { |
| collissionRectangle = new Rectangle( |
| (int)position.X, |
| (int)position.Y, |
| texture.Width, |
| texture.Height |
| ); |
| } |
| } |
| public class RainDrop : GameObject |
| { |
| public float velocity = 0.1f; |
| public string color; |
| public RainDrop(Game1 game) : base(game) |
| { |
| } |
| public override void Update(GameTime gameTime) |
| { |
| position.Y += velocity * gameTime.ElapsedRealTime.Milliseconds; |
| base.Update(gameTime); |
| } |
| } |
| public class RainBucket : GameObject |
| { |
| public string color; |
| public RainBucket(Game1 game) : base(game) |
| { |
| } |
| public override void Update(GameTime gameTime) |
| { |
| for (int i = 0; i < game.rainBucketList.Count; i++) |
| { |
| game.rainBucketList[i].position.X = game.rainBucketList[i].position.X + game.rainBucketList[i].texture.Width; |
| } |
| //Clamps the buckets to the viewport boundaries |
| position = new Vector2( |
| (int)MathHelper.Clamp(Mouse.GetState().X, 0, game.GraphicsDevice.Viewport.Width - texture.Width * game.rainBucketList.Count), |
| (int)MathHelper.Clamp(Mouse.GetState().Y, 0, game.GraphicsDevice.Viewport.Height - texture.Height) |
| ); |
| base.Update(gameTime); |
| } |
| } |
| } |