O'Reilly logo

Learning XNA 4.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding a Starfield

Given the way that you’ve created your particles and your particle engine, you’re perfectly set up for creating a field of stars in the background to make this look more like outer space. To do this, you’ll need to create a new version of your ParticleExplosion class that treats the particles a little bit differently. For example, the particles you use for stars in the background won’t move around the screen the way the ones in the explosions do. You also won’t be deleting stars from the list of stars to draw, nor will your star particle engine die out at some time: the stars will be the same at the end of the game as they were at the beginning.

Add a new class to your project by right-clicking the solution and selecting AddClass…. Name the class ParticleStarSheet. Then, replace the code in the file that is generated with the following:

using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace _3D_Game { class ParticleStarSheet { // Particle arrays and vertex buffer VertexPositionTexture[] verts; Color[] vertexColorArray; VertexBuffer particleVertexBuffer; // Behavior variables Vector3 maxPosition; int maxParticles; static Random rnd = new Random(); // Vertex and graphics info GraphicsDevice graphicsDevice; // Settings ParticleSettings particleSettings; // Effect Effect particleEffect; // Textures Texture2D particleColorsTexture; public ParticleStarSheet(GraphicsDevice graphicsDevice, Vector3 maxPosition, int maxParticles, Texture2D ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required