Post: this is awesome.
02-24-2013, 05:16 AM #1
Complete Speed
Do a barrel roll!
(adsbygoogle = window.adsbygoogle || []).push({}); recently i found this online while looking at xna tutorials and this was something sweet.

just create a new windows game application in c#
and add the following 3 files

this is just added to whatever you named your project

    
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 ParticleEngine2D
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
ParticleEngine particleEngine;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
graphics.IsFullScreen = true;
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);

List<Texture2D> textures = new List<Texture2D>();
textures.Add(Content.Load<Texture2D>("circle"));
textures.Add(Content.Load<Texture2D>("star"));
textures.Add(Content.Load<Texture2D>("diamond"));
particleEngine = new ParticleEngine(textures, new Vector2(400, 240));
}

protected override void UnloadContent()
{
}

protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

particleEngine.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
particleEngine.Update();

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);

particleEngine.Draw(spriteBatch);

base.Draw(gameTime);
}
}
}



create a new class and name it:
particle.cs

    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace ParticleEngine2D
{
public class Particle
{
public Texture2D Texture { get; set; }
public Vector2 Position { get; set; }
public Vector2 Velocity { get; set; }
public float Angle { get; set; }
public float AngularVelocity { get; set; }
public Color Color { get; set; }
public float Size { get; set; }
public int TTL { get; set; }

public Particle(Texture2D texture, Vector2 position, Vector2 velocity,
float angle, float angularVelocity, Color color, float size, int ttl)
{
Texture = texture;
Position = position;
Velocity = velocity;
Angle = angle;
AngularVelocity = angularVelocity;
Color = color;
Size = size;
TTL = ttl;
}

public void Update()
{
TTL--;
Position += Velocity;
Angle += AngularVelocity;
}

public void Draw(SpriteBatch spriteBatch)
{
Rectangle sourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height);
Vector2 origin = new Vector2(Texture.Width / 2, Texture.Height / 2);

spriteBatch.Draw(Texture, Position, sourceRectangle, Color,
Angle, origin, Size, SpriteEffects.None, 0f);
}
}
}



create another new class:
and name this one ParticleEngine.cs


    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ParticleEngine2D
{
public class ParticleEngine
{
private Random random;
public Vector2 EmitterLocation { get; set; }
private List<Particle> particles;
private List<Texture2D> textures;

public ParticleEngine(List<Texture2D> textures, Vector2 location)
{
EmitterLocation = location;
this.textures = textures;
this.particles = new List<Particle>();
random = new Random();
}

public void Update()
{
int total = 10;

for (int i = 0; i < total; i++)
{
particles.Add(GenerateNewParticle());
}

for (int particle = 0; particle < particles.Count; particle++)
{
particles[particle].Update();
if (particles[particle].TTL <= 0)
{
particles.RemoveAt(particle);
particle--;
}
}
}

private Particle GenerateNewParticle()
{
Texture2D texture = textures[random.Next(textures.Count)];
Vector2 position = EmitterLocation;
Vector2 velocity = new Vector2(
1f * (float)(random.NextDouble() * 2 - 1),
1f * (float)(random.NextDouble() * 2 - 1));
float angle = 0;
float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
Color color = new Color(
(float)random.NextDouble(),
(float)random.NextDouble(),
(float)random.NextDouble());
float size = (float)random.NextDouble();
int ttl = 20 + random.Next(40);

return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
}

public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
for (int index = 0; index < particles.Count; index++)
{
particles[index].Draw(spriteBatch);
}
spriteBatch.End();
}
}
}



and then get these 3 images and add them as content references
You must login or register to view this content.
You must login or register to view this content.
You must login or register to view this content.

then finally run it and you should get something like this:
You must login or register to view this content.

and when you move the mouse around in the screen it will look like this:
You must login or register to view this content.

also i think you have to rename in a file called program.cs

it will have this line of code in it:

    
using System;

namespace WindowsGame2
{
#if WINDOWS || XBOX
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
#endif
}


game1 will be your project title name.

so just add
ParticleEngine2D.game1 instead of just game1 (or whatever you named your project.)

again i take no credit for this but it is really awesome. the guy has more tutorials on xna and stuff at this website.
You must login or register to view this content.

so anyways yeah. enjoy.
(adsbygoogle = window.adsbygoogle || []).push({});

The following 2 users say thank you to Complete Speed for this useful post:

LulzBus, Pichu

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo