Happy Independence Day

Happy Independence Day. Today India is celebrating its 67th year of Independence!!

Getting tired of static greetings, I created this interactive python coded animation, to celebrate the occasion.

Here’s the video of the animation!!

To give it a try follow this link. Click run and then drag your mouse within the black screen.

Here’s the python code for anyone interested.


import simplegui, random

# Constants
CANVAS_WIDTH = 640
CANVAS_HEIGHT = 480
MIN_PARTICLES = 10
MAX_PARTICLES = 40
MIN_SPEED = 1
MAX_SPEED = 3
MIN_SIZE = 2
MAX_SIZE = 5
MIN_LIFE = 10
MAX_LIFE = 40

LOCK = False
timer = None

class Particle:
 def __init__(self, x, y, dx, dy, size, life,color):
 self.x, self.y = x, y
 self.dx, self.dy = dx, dy
 self.size, self.life = size, life
 self.color =color
 def draw(self, canvas):
 canvas.draw_polygon(getRect(self.x, self.y, self.size, self.size), 1, self.color, self.color)
 def update(self):
 self.x += self.dx
 self.y += self.dy
 self.life -= 1
 def alive(self):
 return self.life > 0

# Globals
PARTICLES = []

# Helper Functions
def getRect(x, y, w, h):
 return [[x, y],[(x + w), y],[(x + w), (y + h)],[x, (y + h)]]
def addParticles(pos):
 global MIN_PARTICLES, MAX_PARTICLES, MIN_SPEED, MAX_SPEED, MIN_SIZE, MAX_SIZE, MIN_LIFE, MAX_LIFE, PARTICLES
 particle_count = random.randint(MIN_PARTICLES, MAX_PARTICLES)
 for i in range(particle_count):
 dx = random.randint(MIN_SPEED, MAX_SPEED) * random.choice([-1,1])
 dy = random.randint(MIN_SPEED, MAX_SPEED) * random.choice([-1,1])
 size = random.randint(MIN_SIZE, MAX_SIZE)
 life = random.randint(MIN_LIFE, MAX_LIFE)
 color = random.choice(["Orange","green"])
 x = pos[0] - (size / 2)
 y = pos[1] - (size / 2)
 PARTICLES.append(Particle(x, y, dx, dy, size, life,color))
# ----------------

# Event Handlers
def drag(pos):

 if not LOCK:
 addParticles(pos)
 lock()
def lock():
 global LOCK
 LOCK = True
 timer.start()

def draw(canvas):
 global PARTICLES
 if (LOCK):
 canvas.draw_text('Happy Independence Day!', (40, 450), 50, 'Green')
 else:
 canvas.draw_text('Happy Independence Day!', (40, 450), 50, 'Orange')
 for p in PARTICLES:
 p.update()
 p.draw(canvas)
 if not p.alive(): PARTICLES.remove(p)

def tick():
 timer.stop()
 global LOCK
 LOCK = False;

# Registering frame.
frame = simplegui.create_frame("Independence Day Animation", CANVAS_WIDTH, CANVAS_HEIGHT,1)
frame.set_draw_handler(draw)
frame.set_canvas_background("Black")
frame.set_mousedrag_handler(drag)
timer = simplegui.create_timer(50, tick)
frame.start()
#timer.start()

Don’t forget to share if you like it!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s