0 Members and 1 Guest are viewing this topic.
private int w, h;public int Width { get { return this.w; } }public int Height { get { return this.h; } }protected override void Initialize(){ this.w = this.Window.ClientBounds.Width; this.h = this.Window.ClientBounds.Height; //There's more code in here, just not needed for this tutorial}
private Point[] snow = new Point[225]; //You can increase/decrease this number depending on the density you wantprivate bool isSnowing = false; //I only set this to true (so far) in the main menuprivate Texture2D snowTexture; //Our texture for each snow particle. I'll attach mine, though it's very simpleprivate Point quitPoint; //The point at which we need to recycle the snow particleprivate Point startPoint; //The point where the snow particle gets recycled toprivate Random random; //Will be used for generating new random numbers
protected override void LoadContent(){ //Load our texture. I placed it in a folder called "Textures," if you don't put it in any folder, don't use "Textures\\" //or if you use a different folder, replaces Textures with its name. this.snowTexture = this.Content.Load<Texture2D>("Textures\\snow"); //Set the snow's quit point. It's the bottom of the screen plus the texture's height so it looks like the snow goes completely off screen this.quitPoint = new Point(0, this.Height + this.snowTexture.Height); //Set the snow's start point. It's the top of the screen minus the texture's height so it looks like it comes from somewhere, rather than appearing this.startPoint = new Point(0, 0 - this.snowTexture.Height);}
public void DrawSnow(){}protected override void Draw(){ GraphicsDevice.Clear(Color.White); this.spriteBatch.Begin(); this.DrawSnow(); base.Draw(gameTime); //I was told sometime that putting this after base.Draw(...) was better this.spriteBatch.End();}
public void DrawSnow(){ //If it's not supposed to be snowing, exit if (!isSnowing) return; //This will be used as the index within our snow array int i; //NOTE: The following conditional is not exactly the best "initializer." //If snow has not been initialized if (this.snow[0] == new Point(0, 0)) { //Make the random a new random this.random = new Random(); //For every snow particle within our snow array, for (i = 0; i < this.snow.Length; i++) //Give it a new, random x and y. This will give the illusion that it was already snowing //and won't cluster the particles this.snow[i] = new Point( (random.Next(0, (this.Width - this.snowTexture.Width))), (random.Next(0, (this.Height)))); } //Make the random a new random (again, if just starting) this.random = new Random(); //Go back to the beginning of the snow array i = 0; //Begin displaying the snow foreach (Point snowPnt in this.snow) { //Get the exact rectangle for the snow particle Rectangle snowParticle = new Rectangle( snowPnt.X, snowPnt.Y, this.snowTexture.Width, this.snowTexture.Height); //Draw the snow particle (change white if you want any kind of tinting) this.spriteBatch.Draw(this.snowTexture, snowParticle, Color.White); //Make the current particle go down, but randomize it for a staggering snow this.snow[i].Y += random.Next(0, 5); //Make sure the point's location is not below quit point's if (this.snow[i].Y >= this.quitPoint.Y) //If it is, give it a random X value, and the starting point variable's Y value this.snow[i] = new Point( (random.Next(0, (this.Width - this.snowTexture.Width))), this.startPoint.Y); //Increment our position in the array ("go to the next snow particle") i++; }}
This looks nice. By the way does Xbox 360 versions of XNA games requires heavy changes to the code?