Creating a Simple Fireworks Display with Python

Python, a versatile and powerful programming language, can be used for a wide range of applications, including creating visualizations and animations. In this article, we’ll explore how to create a simple fireworks display using Python, without the need for complex graphics libraries.

Why Use Python for Fireworks?

Python’s simplicity and readability make it an excellent choice for beginners and experts alike. While libraries like matplotlib and PIL provide advanced capabilities for graphics and image processing, we can still achieve visually appealing results with basic Python code. Creating a simple fireworks display allows us to appreciate the beauty of Python’s capabilities while focusing on the core concepts.

Simple Fireworks Code

To create a simple fireworks display, we’ll use the random module to generate random positions, colors, and sizes for the fireworks particles. We’ll then use nested loops to simulate the explosion of the fireworks and the movement of the particles.

Here’s a basic example of the code:

pythonimport random
import time

# Define the screen size
screen_width = 80
screen_height = 20

# Initialize the screen with empty spaces
screen = [[' ' for _ in range(screen_width)] for _ in range(screen_height)]

# Function to draw the screen
def draw_screen(screen):
for row in screen:
print(''.join(row))

# Function to generate a random color
def generate_color():
return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

# Function to simulate a fireworks explosion
def fireworks_explosion(x, y):
# Define the range of the explosion
explosion_range = 10

# Loop through the range of the explosion
for i in range(-explosion_range, explosion_range + 1):
for j in range(-explosion_range, explosion_range + 1):
# Check if the position is within the screen boundaries
if 0 <= x + i < screen_width and 0 <= y + j < screen_height:
# Calculate the brightness based on the distance from the center
distance = (i**2 + j**2) ** 0.5
brightness = int(255 / (distance + 1)) if distance > 0 else 255

# Set the color and character based on the brightness
color = generate_color()
char = '*' if brightness > 128 else '.'

# Apply the color and character to the screen
screen[y + j][x + i] = '\033[38;2;{};{};{}m{}{}\033[0m'.format(color[0], color[1], color[2], char, ' ' * (brightness // 8))

# Simulate a fireworks display
for _ in range(10): # Adjust this to increase/decrease the number of fireworks
x = random.randint(0, screen_width - 1)
y = random.randint(0, screen_height // 2) # Keep the fireworks near the top of the screen
fireworks_explosion(x, y)
draw_screen(screen)
time.sleep(0.5) # Adjust this to increase/decrease the animation speed

# Clear the screen for the next fireworks explosion
for row in screen:
for i in range(screen_width):
screen[row.index(row)][i] = ' '

Note: The code above uses ANSI escape codes to set the color of the characters on the terminal. This feature is supported by most modern terminals, but it might not work in all environments.

Conclusion

In this article, we’ve created a simple fireworks display using Python’s basic capabilities. While the code is relatively simple, it demonstrates the power of Python in creating visually appealing animations. By adjusting the parameters and adding more features, you can further enhance the fireworks display and create more complex animations.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *