NEWS

[C#] Chia sẽ source code Firework Effect trong Winform

[C#] Chia sẽ source code Firework Effect trong Winform
Đăng bởi: Thảo Meo - Lượt xem: 4713 13:40:43, 05/07/2021DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục chia sẽ đến các bạn script Firework (pháo hoa) trên lập trình C#, Winform.

[C#] Firework Effect in Winform

Dưới đây là giao diện demo ứng dụng:

firework_CSHARP

Đầu tiên, các bạn tạo cho mình 1 class Firework.cs:

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

namespace Firework
{
    public class FireWork
    {
        const int MaxRays = 10;      
        double start;    
        double stop;      
        double len;      
        double curpos;
        SolidBrush brush;       
        int nrays;       
        int cx;       
        int cy;
        double[] sintab;
        double[] costab;
        double descent;
        static Random rand = new Random();
        const double Step = 0.5;

        public bool Update()
        {
            curpos += Step;
            return curpos <= stop + len;
        }

     
        public void Paint(Graphics g)
        {
            double lower = Math.Max(curpos - len, start);
            double upper = Math.Min(curpos, stop);

            for (double pos = lower; pos < upper; pos += Step)
            {               
                double quad = descent * pos;
                double quadsq = quad * quad;

                for (int i = 0; i < nrays; ++i)
                    g.FillRectangle(brush,
                            (int)(cx + pos * costab[i]),
                            (int)(cy + pos * sintab[i] + quadsq),
                            1, 1);
            }
        } 
        public FireWork(int xsize, int ysize)
        {
            cx = rand.Next(xsize);
            cy = rand.Next(ysize);
            descent = rand.NextDouble() * 0.1 + 0.05;

            start = rand.NextDouble() * 10 + 5;
            stop = rand.NextDouble() * 50 + 50;
            len = rand.NextDouble() * 50 + 25;

            curpos = start;

            brush = new SolidBrush(Color.FromArgb(rand.Next(128, 256),
                                  rand.Next(128, 256),
                                  rand.Next(128, 256)));

            nrays = rand.Next(5, MaxRays + 1);

            double angleInc = 2 * Math.PI / nrays;
            double angle = rand.NextDouble() * angleInc;

            costab = new double[nrays];
            sintab = new double[nrays];
            for (int i = 0; i < nrays; ++i, angle += angleInc)
            {
                costab[i] = Math.Cos(angle);
                sintab[i] = Math.Sin(angle);
            }
        }
    }
}

Và ở form main chúng ta sẽ sử dụng 1 timer để chạy hiệu ứng

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Firework
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
			Text = "Fireworks";
			Name = "Fireworks";
			SetStyle(ControlStyles.UserPaint, true);
			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			SetStyle(ControlStyles.DoubleBuffer, true);

			ClientSize = new Size(700, 600);	
			Timer timer = new Timer();
			timer.Tick += new EventHandler(Tick);
			timer.Interval = UpdateInterval;
			timer.Start();
		}
		const int MaxFireWorks = 10;

		FireWork[] fireworks = new FireWork[MaxFireWorks];

		static Random rand = new Random();
	
		void Tick(Object o, EventArgs e)
		{
		
			for (int i = 0; i < MaxFireWorks; ++i)
				if (fireworks[i] != null)
					if (!fireworks[i].Update())					
						fireworks[i] = null;
			
			if (rand.Next(10) == 0)
				for (int i = 0; i < MaxFireWorks; ++i)
					if (fireworks[i] == null)
					{
						fireworks[i] = new FireWork(ClientRectangle.Width,
									ClientRectangle.Height);
						break;
					}

			Invalidate();
		
			Update();
		} 
		protected override void OnPaint(PaintEventArgs e)
		{
			e.Graphics.Clear(Color.Black);
			foreach (FireWork fw in fireworks)
				if (fw != null)
					fw.Paint(e.Graphics);
		}
	
		const int UpdateInterval = 1;

		

	}
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Chia sẽ source code Firework Effect trong Winform
Đăng bởi: Thảo Meo - Lượt xem: 4713 13:40:43, 05/07/2021DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.