Example 2: Particle generator

result.mp4
using System;
using System.Collections.Generic;
using odl;

namespace TestNamespace
{
    class Program
    {
        public static Random Random = new Random();
        static Viewport MainViewport;

        public static void Main(string[] args)
        {
            Graphics.Start();

            Window win = new Window();
            win.Initialize(true, true);

            MainViewport = new Viewport(0, 0, win.Width, win.Height);

            win.Show();
            List<Particle> Particles = new List<Particle>();

            while (Graphics.CanUpdate())
            {
                for (int i = Random.Next(1, 4); i >= 0; i--)
                {
                    Particles.Add(new Particle(MainViewport));
                }
                for (int i = 0; i < Particles.Count; i++)
                {
                    Particles[i].Update();
                    if (Particles[i].Disposed())
                    {
                        Particles.RemoveAt(i);
                        i--;
                    }
                }
                Graphics.Update();
            }

            for (int i = 0; i < Particles.Count; i++)
            {
                Particles[i].Dispose();
            }

            if (!MainViewport.Disposed) MainViewport.Dispose();

            Graphics.Stop();
        }
    }

    class Particle
    {
        Sprite Sprite;

        int XSpeed = 0;
        int YSpeed = 0;
        int RotationSpeed = 0;

        public Particle(Viewport Viewport)
        {
            this.Sprite = new Sprite(Viewport);
            this.Sprite.Bitmap = new Bitmap(7, 7);
            this.Sprite.Bitmap.Unlock();
            Color c = new Color((byte) Program.Random.Next(0, 256), (byte) Program.Random.Next(0, 256), (byte) Program.Random.Next(0, 256));
            this.Sprite.Bitmap.FillRect(2, 2, 3, 3, c);
            this.Sprite.Bitmap.DrawLine(3, 0, 3, 6, c);
            this.Sprite.Bitmap.DrawLine(0, 3, 6, 3, c);
            this.Sprite.Bitmap.Lock();
            this.Sprite.X = Viewport.Width / 2 - 3;
            this.Sprite.Y = Viewport.Height / 2 - 3;
            this.Sprite.OX = 3;
            this.Sprite.OY = 3;
            this.Sprite.Angle = Program.Random.Next(0, 360);
            XSpeed = Program.Random.Next(-10, 10);
            YSpeed = Program.Random.Next(-10, 10);
            RotationSpeed = Program.Random.Next(-20, 20);
        }

        public void Update()
        {
            this.Sprite.X += XSpeed;
            this.Sprite.Y += YSpeed;
            this.Sprite.Angle += RotationSpeed;
            if (this.Sprite.X < -3 || this.Sprite.Y < -3 || this.Sprite.X >= this.Sprite.Viewport.Width + 3 || this.Sprite.Y >= this.Sprite.Viewport.Height + 3)
                this.Dispose();
        }

        public void Dispose()
        {
            if (!this.Sprite.Disposed) this.Sprite.Dispose();
        }

        public bool Disposed()
        {
            return this.Sprite.Disposed;
        }
    }
}

Last updated