C# OOP Project – Spawn Animated Fireworks in Windows Forms With Click Event

Hi, in this tutorial we will make a interesting little application where you can spawn and animate a fireworks animation with click event in windows form. This is was a novel idea and a very interesting project to make, I am excited to show you the solution we found on this project. This project is packed with features such as loading background images to the application and changing them dynamically during run time, playing the fireworks animation on top of the backgrounds transparently and with smooth animation. We will be using OOP programming to make this application. We will create a Class for the firework object and give instruction on what they should do when we it spawn it on the screen. We will be making this tutorial in Visual Studio 2022. You can use any version that’s available to you. Follow along with the tutorial video below and have fun making this little application.

Lesson Objectives –

  1. Make a Fireworks Animation app in Windows Form
  2. Load background images from a folder and save them to the list in C#
  3. Load fireworks animation sprite images from a folder and save them to a list in C#
  4. Create a Fireworks Class that will control the frame animation and settings for the object
  5. Add Click events to the main form and spawn the firework in the click location

 

Video Tutorial –

 

Download Background and Firework Images here

Get C# Fireworks Animation in Windows Forms Project on GitHub

 

Firework Class Source Code –

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

namespace Fireworks_Animation_MOO_ICT
{
    internal class Firework
    {
        public int height;
        public int width;
        public int frames;
        public int current_frame = 0;
        public Point position = new Point();
        public List<string> image_location = new List<string>();
        public bool animationComplete = false;
        public Image firework;

        // created by MOO ICT 2022
        // for educational purpose only

        public Firework()
        {
            image_location = Directory.GetFiles("images", "*.png").ToList();
            height = 200;
            width = 200;
            firework = Image.FromFile(image_location[0]);
            frames = image_location.Count;
        }


        public void AnimateFireWork()
        {

            if (current_frame < frames - 1)
            {
                current_frame++;
                firework = Image.FromFile(image_location[current_frame]);
            }
            else
            {
                current_frame = 0;
                animationComplete = true;
                firework = null;
                image_location.Clear();
            }



        }



    }
}

Form1.cs Source Code –

namespace Fireworks_Animation_MOO_ICT
{
    public partial class Form1 : Form
    {

        List<string> image_location = new List<string>();
        List<Firework> fireworks_list = new List<Firework>();
        int backgroundNumber;

        // create by MOO ICT 2022
        // for educational purpose only


        public Form1()
        {
            InitializeComponent();
            SetUp();
        }

        private void SetUp()
        {
            image_location = Directory.GetFiles("background", "*.jpg").ToList();
            this.BackgroundImage = Image.FromFile(image_location[0]);
            this.BackgroundImageLayout = ImageLayout.Stretch;
        }


        private void KeyIsUp(object sender, KeyEventArgs e)
        {
            if (backgroundNumber < image_location.Count -1)
            {
                backgroundNumber++;
            }
            else
            {
                backgroundNumber = 0;
            }

            this.BackgroundImage = Image.FromFile(image_location[backgroundNumber]);

        }

        private void FormMouseDown(object sender, MouseEventArgs e)
        {
            Point mousePosition = new Point();
            mousePosition.X = e.X;
            mousePosition.Y = e.Y;

            Firework newFirework = new Firework();
            newFirework.position.X = mousePosition.X - (newFirework.width/2);
            newFirework.position.Y = mousePosition.Y - (newFirework.height/2);
            fireworks_list.Add(newFirework);
        }

        private void FormPaintEvent(object sender, PaintEventArgs e)
        {
            foreach (Firework newFirework  in fireworks_list.ToList())
            {
                if (newFirework.animationComplete == false)
                {
                    e.Graphics.DrawImage(newFirework.firework, newFirework.position.X, newFirework.position.Y, newFirework.width, newFirework.height);
                }
            }
        }

        private void AnimationTimerEvent(object sender, EventArgs e)
        {
            if (fireworks_list != null)
            {
                foreach (Firework firework in fireworks_list.ToList())
                {

                    if (firework.animationComplete == false)
                    {
                        firework.AnimateFireWork();
                    }
                    else
                    {
                        fireworks_list.Remove(firework);
                    }
                }
            }


            this.Invalidate();

        }
    }
}

 

 

 

 




Comment on this tutorial and let us know how you got on -