Code Sketch
yadnesh or bhavesh
Category: Programming
// --- LETTER-BY-LETTER ANTHEM LYRICS WITH TRICOLOR FIREWORKS ---
cleari()
originBottomLeft()
// ?. "?? ?? ??" ?? ??????? ?????
setNoteInstrument(Instrument.PIANO)
val fullJanaGanaTune = Array(
(53, 200), (55, 200), (57, 200), (57, 200), (57, 200), (57, 200), (57, 200),
(57, 200), (57, 200), (55, 200), (57, 200), (58, 200),
(57, 200), (57, 200), (57, 200), (55, 200), (55, 200), (55, 200), (53, 200), (55, 200), (53, 300),
(55, 200), (57, 200), (55, 200), (53, 300),
(53, 200), (60, 200), (60, 200), (60, 200), (60, 200), (60, 200), (60, 200),
(60, 200), (58, 200), (60, 200), (58, 200), (57, 200), (55, 200), (57, 200), (58, 300),
(57, 200), (57, 200), (57, 200), (57, 200), (55, 200), (58, 200), (57, 200),
(55, 200), (55, 200), (53, 200), (55, 200), (53, 300),
(53, 200), (55, 200), (57, 200), (57, 200), (57, 200), (55, 200), (57, 200), (58, 300),
(57, 200), (55, 200), (57, 200), (53, 200), (55, 200), (53, 300),
(53, 200), (55, 200), (57, 200), (57, 200), (57, 200), (57, 200), (57, 200),
(55, 200), (57, 200), (58, 200), (57, 200), (55, 200), (53, 300),
(60, 250), (58, 250), (60, 300),
(58, 250), (57, 250), (58, 300),
(57, 250), (55, 250), (57, 300),
(53, 200), (53, 200), (55, 200), (55, 200), (57, 200), (57, 200), (58, 400)
)
var tuneIndex = 0
// ????????????? ??????? ??????? (???? ????? ???????????)
val fullAnthemText =
"Jana Gana Mana Adhinayaka Jaya He\n" +
"Bharata Bhagya Vidhata\n" +
"Punjaba Sindhu Gujarata Maratha\n" +
"Dravida Utkala Banga\n" +
"Vindhya Himachala Yamuna Ganga\n" +
"Uchhala Jaladhi Taranga\n" +
"Tava Subha Name Jage\n" +
"Tava Subha Asisa Mange\n" +
"Gahe Tava Jaya Gatha\n" +
"Jana Gana Mangala Dayaka Jaya He\n" +
"Bharata Bhagya Vidhata\n" +
"Jaya He, Jaya He, Jaya He\n" +
"Jaya Jaya Jaya Jaya He!"
var charIndex = 0
def playJanaGanaStep() {
if (tuneIndex < fullJanaGanaTune.length) {
val (note, duration) = fullJanaGanaTune(tuneIndex)
playNote(note, duration)
tuneIndex = (tuneIndex + 1) % fullJanaGanaTune.length
}
}
val gravity = Vector2D(0, -0.1)
val messageWords = Array("HAPPY", "INDEPENDENCE", "DAY")
var wordIndex = 0
var displayedText = ""
var flagVisible = false
var waveAngle = 0.0
val tricolorPalette = Array(
cm.rgb(255, 153, 51), // ?????
cm.rgb(255, 255, 255), // ??????
cm.rgb(18, 136, 7) // ?????
)
class Particle(x0: Double, y0: Double, col: Color, seed: Boolean) {
var location = Vector2D(x0, y0)
private var velocity =
if (seed) Vector2D(0, random(6, 12))
else randomExplosionVector
private var acceleration = Vector2D(0, 0)
var lifespan = 255.0
private var exploded = false
def randomExplosionVector: Vector2D = {
val rv1 = Vector2D(randomNormalDouble, randomNormalDouble)
val r1 = randomDouble(2, 4)
rv1 * r1
}
def applyForce(force: Vector2D) {
acceleration += force
}
def shouldExplode: Boolean = !exploded && lifespan <= 0
def checkExplode() {
if (seed && velocity.y < 0) {
lifespan = 0
}
}
def isDead: Boolean = {
if (seed) exploded else lifespan <= 0
}
def explode() {
playJanaGanaStep()
exploded = true
// ?. ???????? ???? ?????
if (wordIndex < messageWords.length) {
if (displayedText.nonEmpty) displayedText += " "
displayedText += messageWords(wordIndex)
wordIndex += 1
if (wordIndex >= messageWords.length) {
flagVisible = true
}
}
// ?. ?????? ?-? ?????? ???? ?????? (?????? ????? ??????????)
if (charIndex < fullAnthemText.length) {
charIndex = math.min(charIndex + 3, fullAnthemText.length)
}
}
def step() {
velocity += acceleration
location += velocity
if (!seed) {
lifespan -= 5
velocity *= 0.95
}
acceleration *= 0
checkExplode()
}
def view(canvas: CanvasDraw) {
canvas.stroke(col)
if (seed) {
canvas.strokeWeight(4)
} else {
canvas.strokeWeight(2)
}
canvas.point(location.x, location.y)
}
}
class Firework() {
private val fireworkColor = tricolorPalette(random(tricolorPalette.length))
private val firework = new Particle(random(cwidth), 0, fireworkColor, true)
private val particles = ArrayBuffer.empty[Particle]
def done: Boolean = {
firework.isDead && particles.isEmpty
}
def step() {
particles.filterInPlace(p => !p.isDead)
if (!firework.isDead) {
firework.applyForce(gravity)
firework.step()
if (firework.shouldExplode) {
repeat(90) {
particles.append(
new Particle(firework.location.x, firework.location.y, fireworkColor, false)
)
}
firework.explode()
}
}
repeatFor(particles) { p =>
p.applyForce(gravity)
p.step()
}
}
def view(canvas: CanvasDraw) {
firework.view(canvas)
repeatFor(particles) { p =>
p.view(canvas)
}
}
}
val fireworks = ArrayBuffer.empty[Firework]
def updateState() {
fireworks.filterInPlace(f => !f.done)
if (randomDouble(1) < 0.08) {
fireworks.append(new Firework())
}
repeatFor(fireworks) { f =>
f.step()
}
waveAngle += 0.08
}
// ??????? ??????
def drawWavingTricolorFlag(canvas: CanvasDraw) {
val fw = 160.0
val fh = 28.0
val fx = (cwidth - fw) / 2.0 + 100.0
val fy = cheight / 2.0 - 20.0
canvas.stroke(180, 180, 180)
canvas.strokeWeight(5)
canvas.line(fx, fy + fh * 3 + 15, fx, fy - 140)
val stepSize = 4.0
var x = 0.0
while (x < fw) {
val yOffset = math.sin(waveAngle + (x * 0.03)) * 8.0
canvas.stroke(255, 153, 51)
canvas.strokeWeight(stepSize + 1)
canvas.line(fx + x, fy + fh * 2 + yOffset, fx + x, fy + fh * 3 + yOffset)
canvas.stroke(255, 255, 255)
canvas.line(fx + x, fy + fh + yOffset, fx + x, fy + fh * 2 + yOffset)
canvas.stroke(18, 136, 7)
canvas.line(fx + x, fy + yOffset, fx + x, fy + fh + yOffset)
x += stepSize
}
val chakraX = fx + fw / 2.0
val chakraY = fy + fh + (fh / 2.0) + (math.sin(waveAngle + ((fw / 2.0) * 0.03)) * 8.0)
val radius = 11.0
canvas.stroke(0, 0, 128)
canvas.strokeWeight(2)
canvas.noFill()
canvas.ellipse(chakraX, chakraY, radius * 2, radius * 2)
canvas.strokeWeight(1)
for (i <- 0 until 24) {
val angle = i * (2 * math.Pi / 24)
val xEnd = chakraX + math.cos(angle) * radius
val yEnd = chakraY + math.sin(angle) * radius
canvas.line(chakraX, chakraY, xEnd, yEnd)
}
}
// ?????-???-????? (Typewriter Effect) ?????????? ??????
def drawLetterByLetterAnthem(canvas: CanvasDraw) {
val visibleSubstring = fullAnthemText.substring(0, charIndex)
val lines = visibleSubstring.split("\n")
val startX = 20.0
var startY = cheight - 90.0
for (i <- lines.indices) {
if (i == lines.length - 1) {
canvas.fill(255, 153, 51) // ???? ???? ???????? ????? ????? ???
} else {
canvas.fill(220, 220, 220) // ?????? ?????? ???? ?????? ???
}
canvas.text(lines(i), startX, startY)
startY -= 20.0
}
}
def viewState(canvas: CanvasDraw) {
canvas.fill(0, 0, 0, 40)
canvas.noStroke()
canvas.rect(0, 0, cwidth, cheight)
repeatFor(fireworks) { f =>
f.view(canvas)
}
// ?. ?????? ??????????? ????? ?????????? ?????
drawLetterByLetterAnthem(canvas)
// ?. HAPPY INDEPENDENCE DAY ??????
if (displayedText.nonEmpty) {
canvas.fill(255, 215, 0)
canvas.text(displayedText, (cwidth / 2.0) - 100, cheight - 40)
}
// ?. ?????? ????
if (flagVisible) {
drawWavingTricolorFlag(canvas)
}
}
animateWithCanvasDraw { canvas =>
updateState()
viewState(canvas)
}