Creating Fireworks in MATLAB: Celebrating T1’s Championship

Faker has won six championships!It’s quite emotional; I used to watch Faker win championships back in school, and he is still dominating the arena! The trophies he holds are not just a testament to his victories but also the best proof of the esports spirit — remaining humble at the peak and resilient in the lows, with a decade of unwavering focus and dedication, which is truly admirable. Here, I have created a fireworks display in MATLAB (the background is, of course, black, but in a rush, the code still has room for optimization).

% MATLAB Fireworks Effect Generation

clear; clc; close all;

% Initialize figure window (black background = night sky)

figure(‘Color’, ‘black’, ‘Name’, ‘MATLAB Fireworks Show’, …

‘NumberTitle’, ‘off’, ‘Position’, [100, 50, 1200, 800], …

‘KeyPressFcn’, @(src,evt) setappdata(src,’exitFlag’,true));

hold on; axis equal; axis off;

xlim([0, 1200]); ylim([0, 800]);

% Fireworks parameters (optimized)

numFireworks = 10; % Increase the number of fireworks

particlesPerFirework = 200; % Increase the number of particles

gravity = 0.2; % Gravity effect

fadeSpeed = 0.008; % Fade speed (optimized)

% Pre-compute colors (more vibrant colors)

baseColors = [1,0,0; 0,1,0; 0,0,1; 1,1,0; 1,0,1; 0,1,1; 1,1,1; …

1,0.5,0; 0.5,0,1; 0,0.5,1]; % Red, Green, Blue, Yellow, Purple, Cyan, White, Orange, Purple, Blue

colors = zeros(numFireworks, 3);

for i = 1:numFireworks

colors(i,:) = baseColors(mod(i-1, size(baseColors,1))+1,:) + rand(1,3)*0.3;

colors(i,:) = min(colors(i,:), 1); % Ensure color values are within [0,1]

end

% Initialize firework states

fireworkPos = rand(numFireworks,2).*[1000, 0] + [100, 800];

particlePos = zeros(particlesPerFirework, 2, numFireworks);

particleVel = zeros(particlesPerFirework, 2, numFireworks);

particleAlpha = ones(particlesPerFirework, numFireworks);

isBurst = false(numFireworks,1);

launchSpeed = 12 + rand(numFireworks,1)*10;

burstHeight = 250 + rand(numFireworks,1)*300;

% Performance optimization: Pre-allocate graphic objects

trailLines = gobjects(numFireworks, 1);

particlePatches = gobjects(particlesPerFirework, numFireworks);

% Main loop (optimized version)

setappdata(gcf, ‘exitFlag’, false);

frameCount = 0;

while true

% Check exit condition

if getappdata(gcf, ‘exitFlag’)

break;

end

% Clear previous frame

delete(findobj(gca, ‘Type’, ‘line’));

delete(findobj(gca, ‘Type’, ‘patch’));

% Iterate through each firework

for i = 1:numFireworks

if ~isBurst(i)

% Launch phase

fireworkPos(i,2) = fireworkPos(i,2) – launchSpeed(i);

% Draw launch trajectory (with glow effect)

trailLines(i) = line(fireworkPos(i,1), fireworkPos(i,2), …

‘Color’, colors(i,:), ‘LineWidth’, 3, …

‘Marker’, ‘o’, ‘MarkerSize’, 6, …

‘MarkerFaceColor’, colors(i,:));

% Reached burst height

if fireworkPos(i,2) <= burstHeight(i)

isBurst(i) = true;

% Random particle dispersion pattern

theta = rand(particlesPerFirework,1) * 2 * pi;

speed = 3 + rand(particlesPerFirework,1)*4;

particleVel(:,1,i) = speed .* cos(theta);

particleVel(:,2,i) = speed .* sin(theta);

particlePos(:,:,i) = repmat(fireworkPos(i,:), particlesPerFirework, 1);

end

else

% Burst phase (adding gravity effect)

particleVel(:,2,i) = particleVel(:,2,i) – gravity;

particlePos(:,:,i) = particlePos(:,:,i) + particleVel(:,:,i);

particleAlpha(:,i) = particleAlpha(:,i) – fadeSpeed;

% Draw particles (optimized drawing performance)

activeParticles = particleAlpha(:,i) > 0;

if any(activeParticles)

for j = find(activeParticles)’

sizeFactor = 2 + particleAlpha(j,i)*3; % Particle size gradient

patch(particlePos(j,1)+[-1,1,1,-1]*sizeFactor, …

particlePos(j,2)+[-1,-1,1,1]*sizeFactor, …

colors(i,:), ‘FaceColor’, colors(i,:), …

‘EdgeColor’, ‘none’, ‘FaceAlpha’, particleAlpha(j,i));

end

end

% Reset conditions

if all(particleAlpha(:,i) <= 0)

isBurst(i) = false;

fireworkPos(i,:) = [rand()*1000 + 100, 800];

particleAlpha(:,i) = 1;

launchSpeed(i) = 12 + rand()*10;

burstHeight(i) = 250 + rand()*300;

end

end

end

% Add random new firework launches

if rand() < 0.02 && sum(~isBurst) < numFireworks/2

available = find(~isBurst);

if ~isempty(available)

i = available(1);

fireworkPos(i,:) = [rand()*1000 + 100, 800];

launchSpeed(i) = 12 + rand()*10;

burstHeight(i) = 250 + rand()*300;

end

end

% Refresh screen

drawnow limitrate; % Use limitrate to optimize performance

frameCount = frameCount + 1;

% Display frame rate every 100 frames

if mod(frameCount, 100) == 0

fprintf(‘Frame: %d\n’, frameCount);

end

end

% Graceful exit

clf;

set(gcf, ‘Color’, ‘black’);

text(600, 400, ‘Fireworks Show Ended!’, ‘FontSize’, 28, …

‘Color’, ‘yellow’, ‘HorizontalAlignment’, ‘center’, …

‘FontWeight’, ‘bold’);

text(600, 350, ‘Press any key to close’, ‘FontSize’, 16, …

‘Color’, ‘white’, ‘HorizontalAlignment’, ‘center’);

pause(2);

close(gcf);

Dynamic effects can be run in MATLAB.

Leave a Comment