Using Particles

Particle effects can really take a game from good to great.  Otter has some Entities included that can help in making sweet and simple particle effects.  Check out this example of clicking on the screen to make some simple explosions just using the Particle Entity and an explosion sprite sheet from one of my game jam games.

First grab the art asset that the example will use:

Now onto the code!

using Otter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ParticleStuff {
  class Program {
    static void Main(string[] args) {
      // Create a Game!
      var game = new Game("Particle Example");
      // Set the background color.
      game.Color = Color.Shade(0.15f);

      // Show the mouse for this example.
      game.MouseVisible = true;

      // Create a Scene.
      var scene = new Scene();
      // Add a ParticleController to the Scene.
      scene.Add(new ParticleController());

      // Start the Game.
      game.Start(scene);
    }
  }

  class ParticleController : Entity {
    public override void Update() {
      base.Update();

      if (Input.MouseButtonPressed(MouseButton.Left)) {
        // Add a new Particle using the "fire.png" sprite sheet.
        // The frames are 64 x 64.
        // Set a bunch of parameters for the Particle.
        Scene.Add(new Particle(Scene.MouseX, Scene.MouseY, "fire.png", 64, 64) {
          FrameCount = 11,
          LifeSpan = Rand.Int(10, 30),
          FlipX = Rand.Flip,
          FlipY = Rand.Flip,
          Angle = Rand.Angle
        });

        // Flash the screen slightly red for better effects, yeah!
        Scene.Add(new Flash(Color.Red) {
          Blend = BlendMode.Add,
          LifeSpan = 8,
          Alpha = 0.15f
        });
      }

      if (Input.MouseButtonPressed(MouseButton.Right)) {
        // Create and add 16 Particles to the Scene.
        for (int i = 0; i < 16; i++) {
          // Determine the angle to shoot the next Particle in.
          var angle = 360f / 16 * i;
          // Create and add the Particle and set the parameters to make it do fancy things.
          Scene.Add(new Particle(Scene.MouseX, Scene.MouseY, "fire.png", 64, 64) {
            FrameCount = 11,
            SpeedDir = angle,
            SpeedLen = 10,
            FinalSpeedLen = 0,
            LifeSpan = Rand.Int(10, 30),
            FlipX = Rand.Flip,
            FlipY = Rand.Flip,
            Angle = Rand.Angle,
            ScaleX = Rand.Float(0.85f, 1),
            LockScaleRatio = true
          });
        }

        // Flash the screen slightly red for extra effect.
        Scene.Add(new Flash(Color.Red) {
          Blend = BlendMode.Add,
          LifeSpan = 8,
          Alpha = 0.15f
        });
      }
    }
  }
}

Left click on the screen for just a one Particle explosion effect:

And right click for a bigger explosion that makes use of 16 particles being fired off at once:

Now you're really ready to make a video game!

Examples