Code Sketch


siddhi gandas
By: Mhalsakant School
Category: Programming
// ============================================================================
// ULTRA-ADVANCED STOP PLASTIC POLLUTION MASTER ENGINE (400+ LINES SYSTEM)
// Scala Graphics Library & Canvas Audio Synthesis Framework
// Features: Dual-Cow Physics, Organic Trash Scatter, Wind-Blown Plastic Bags,
//           Real Pollution Image Simulation Canvas & Alert Sound System
// ============================================================================

clear()
setBackground(Color(12, 10, 15))

// ----------------------------------------------------------------------------
// 1. GLOBAL CONSTANTS & MATHEMATICAL UTILITIES
// ----------------------------------------------------------------------------
val MATH_PI = 3.141592653589793
val CANVAS_WIDTH = 600.0
val CANVAS_HEIGHT = 400.0

val TOTAL_SMOKE_PARTICLES = 6
val TOTAL_TRASH_ITEMS = 75
val TOTAL_FLYING_BAGS = 5

def degreesToRadians(degrees: Double): Double = {
  return degrees * MATH_PI / 180.0
}

def calculateSineWave(time: Double, frequency: Double, amplitude: Double): Double = {
  return Math.sin(degreesToRadians(time * frequency)) * amplitude
}

def calculateCosineWave(time: Double, frequency: Double, amplitude: Double): Double = {
  return Math.cos(degreesToRadians(time * frequency)) * amplitude
}

// ----------------------------------------------------------------------------
// 2. STATE CONFIGURATION & ENGINE VARIABLES
// ----------------------------------------------------------------------------
var masterTimelineFrame = 0.0

// Cow Mechanical Physics State
var foregroundCowHeadAngle = 0.0
var backgroundCowHeadAngle = 0.0
var cowTailSwayAngle = 0.0

// Smoke Dynamic Array Coordinates
val smokeParticleX = Array(-220.0, -170.0, -120.0, 130.0, 170.0, 210.0)
val smokeParticleY = Array(70.0, 85.0, 100.0, 75.0, 90.0, 105.0)
val smokeParticleSpeed = Array(1.1, 1.5, 1.3, 1.7, 1.2, 1.4)

// Wind-Blown Plastic Bag Array Coordinates
val bagPosX = Array(-290.0, -160.0, -20.0, 110.0, 240.0)
val bagPosY = Array(-40.0, 15.0, -25.0, 30.0, -10.0)
val bagSpeedX = Array(2.8, 3.4, 2.5, 3.9, 3.1)

// Typewriter Text Animation Configuration
val primaryHeaderTitle = "STOP PLASTIC POLLUTION"
val secondaryHeaderSub = "Protect Animals, Save Our Planet Earth!"
var titleCharacterCount = 0
var subCharacterCount = 0
var typewriterTickCounter = 0

// Audio Synthesizer Configuration
val warningMelodyNotes = Array(58, 55, 52, 49, 52, 55)
var melodyPointer = 0
var audioTickCounter = 0

// ----------------------------------------------------------------------------
// 3. AUDIO SYNTHESIS SUBSYSTEM
// ----------------------------------------------------------------------------
def executeEnvironmentalWarningAudio(): Unit = {
  audioTickCounter += 1
  if (audioTickCounter % 14 == 0) {
    val activeNote = warningMelodyNotes(melodyPointer)
    playNote(activeNote, 340)
    melodyPointer = (melodyPointer + 1) % warningMelodyNotes.length
  }
}

// ----------------------------------------------------------------------------
// 4. GRAPHICAL RENDERING SUBSYSTEMS: ENVIRONMENT & POLLUTION CANVAS
// ----------------------------------------------------------------------------

// Background Sky and Industrial Smoke Stacks Renderer
def renderPollutionSkyboxAndChimneys(): Unit = {
  // Polluted Industrial Sky Gradient Base
  val skyCanvas = Picture.rectangle(600, 220)
  skyCanvas.setFillColor(Color(55, 48, 52))
  skyCanvas.setPosition(-300, 20)
  draw(skyCanvas)

  // Industrial Smoke Stack 1
  val stackBase1 = Picture.rectangle(24, 100)
  stackBase1.setFillColor(Color(45, 40, 42))
  stackBase1.setPosition(-210, 25)
  draw(stackBase1)

  val stackRim1 = Picture.rectangle(30, 8)
  stackRim1.setFillColor(Color(70, 60, 65))
  stackRim1.setPosition(-213, 117)
  draw(stackRim1)

  // Industrial Smoke Stack 2
  val stackBase2 = Picture.rectangle(32, 130)
  stackBase2.setFillColor(Color(35, 30, 32))
  stackBase2.setPosition(160, 25)
  draw(stackBase2)

  val stackRim2 = Picture.rectangle(38, 8)
  stackRim2.setFillColor(Color(60, 50, 55))
  stackRim2.setPosition(163, 147)
  draw(stackRim2)
}

// Organic Industrial Smoke Particles Simulation Engine
def renderIndustrialSmokeDynamics(): Unit = {
  for (i <- 0 until TOTAL_SMOKE_PARTICLES) {
    smokeParticleY(i) += smokeParticleSpeed(i)
    if (smokeParticleY(i) > 230.0) {
      smokeParticleY(i) = 60.0
    }

    val horizontalDrift = smokeParticleX(i) + calculateSineWave(masterTimelineFrame, 2.5, 15.0)
    val cloudRadius = 18.0 + (i * 3.0)

    val smokeCloud = Picture.circle(cloudRadius)
    smokeCloud.setFillColor(Color(90, 85, 85, 90))
    smokeCloud.setPosition(horizontalDrift, smokeParticleY(i))
    draw(smokeCloud)
  }
}

// Polluted Ground and Terrain Base Renderer
def renderPollutedGroundTerrain(): Unit = {
  val groundSurface = Picture.rectangle(600, 180)
  groundSurface.setFillColor(Color(38, 30, 25))
  groundSurface.setPosition(-300, -180)
  draw(groundSurface)

  val mudPatchOverlay = Picture.ellipse(550, 60)
  mudPatchOverlay.setFillColor(Color(25, 18, 15, 180))
  mudPatchOverlay.setPosition(-275, -150)
  draw(mudPatchOverlay)
}

// Detailed Plastic and Garbage Waste Dump Simulation (Image Simulation Layer)
def renderGarbageAndPlasticDumpMatrix(): Unit = {
  val wasteColorPalette = Array(
    Color(245, 245, 245, 220), // White Plastic Sheets
    Color(220, 30, 30, 210),   // Red Plastic Packaging
    Color(255, 140, 0, 210),   // Orange Wrapper
    Color(0, 130, 230, 200),   // Blue Poly Bags
    Color(255, 220, 0, 200),   // Yellow Plastic Rubbish
    Color(70, 70, 70, 220)     // Black Garbage Bags
  )

  for (index <- 0 to TOTAL_TRASH_ITEMS) {
    val coordinateX = -295.0 + (index * 8.0)
    val coordinateY = -168.0 + (Math.sin(index * 1.7) * 24.0) + (index % 4 * 3.0)
    val itemDimension = 12.0 + (index % 5 * 5.0)

    val trashPiece = Picture.rectangle(itemDimension, itemDimension - 4.0)
    trashPiece.setFillColor(wasteColorPalette(index % wasteColorPalette.length))
    trashPiece.setPenColor(Color(20, 20, 20, 100))
    trashPiece.setPenThickness(1)
    trashPiece.setPosition(coordinateX, coordinateY)
    trashPiece.rotate(index * 21.0)
    draw(trashPiece)
  }
}

// ----------------------------------------------------------------------------
// 5. ANIMAL & CHARACTER PHYSICS SUBSYSTEMS
// ----------------------------------------------------------------------------

// Advanced Cow Entity Renderer with Tail and Head Articulation Mechanics
def renderArticulatedCowEntity(originX: Double, originY: Double, scalingFactor: Double, isBackgroundEntity: Boolean): Unit = {
  val adjustedX = if (isBackgroundEntity) originX + 160.0 else originX
  val adjustedY = if (isBackgroundEntity) originY + 55.0 else originY
  val scale = scalingFactor

  // Animated Tail Sway Mechanics
  val currentTailRotation = calculateSineWave(masterTimelineFrame, 4.0, 18.0)
  val tailElement = Picture.rectangle(4.0 * scale, 42.0 * scale)
  tailElement.setFillColor(Color(110, 55, 20))
  tailElement.setPosition(adjustedX + (42.0 * scale), adjustedY - (18.0 * scale))
  tailElement.rotate(currentTailRotation)
  draw(tailElement)

  // Leg Structures (Quadrupedal Support)
  val legFrontLeft = Picture.rectangle(11.0 * scale, 52.0 * scale)
  legFrontLeft.setFillColor(Color(140, 70, 35))
  legFrontLeft.setPosition(adjustedX - (32.0 * scale), adjustedY - (72.0 * scale))
  draw(legFrontLeft)

  val legFrontRight = Picture.rectangle(11.0 * scale, 52.0 * scale)
  legFrontRight.setFillColor(Color(120, 60, 28))
  legFrontRight.setPosition(adjustedX - (12.0 * scale), adjustedY - (72.0 * scale))
  draw(legFrontRight)

  val legBackLeft = Picture.rectangle(11.0 * scale, 52.0 * scale)
  legBackLeft.setFillColor(Color(140, 70, 35))
  legBackLeft.setPosition(adjustedX + (22.0 * scale), adjustedY - (72.0 * scale))
  draw(legBackLeft)

  // Main Torso Chassis
  val torsoBody = Picture.ellipse(120.0 * scale, 76.0 * scale)
  torsoBody.setFillColor(Color(170, 85, 38))
  torsoBody.setPenColor(Color(80, 35, 12))
  torsoBody.setPenThickness(2)
  torsoBody.setPosition(adjustedX - (58.0 * scale), adjustedY - (38.0 * scale))
  draw(torsoBody)

  // Articulated Head and Neck Motion for Foraging/Eating Plastic
  val verticalHeadOffset = if (isBackgroundEntity) 0.0 else calculateSineWave(masterTimelineFrame, 3.5, 10.0)

  val neckConnector = Picture.rectangle(30.0 * scale, 52.0 * scale)
  neckConnector.setFillColor(Color(170, 85, 38))
  neckConnector.setPosition(adjustedX - (68.0 * scale), adjustedY - (42.0 * scale) + verticalHeadOffset)
  neckConnector.rotate(24.0)
  draw(neckConnector)

  val headCapsule = Picture.ellipse(40.0 * scale, 26.0 * scale)
  headCapsule.setFillColor(Color(140, 70, 35))
  headCapsule.setPosition(adjustedX - (98.0 * scale), adjustedY - (62.0 * scale) + verticalHeadOffset)
  draw(headCapsule)

  // Horn Element
  val hornElement = Picture.rectangle(4.0 * scale, 16.0 * scale)
  hornElement.setFillColor(Color(30, 30, 30))
  hornElement.setPosition(adjustedX - (78.0 * scale), adjustedY - (38.0 * scale) + verticalHeadOffset)
  hornElement.rotate(-28.0)
  draw(hornElement)
}

// Wind-Driven Flying Plastic Bags Simulation
def renderWindBlownPlasticBags(): Unit = {
  for (index <- 0 until TOTAL_FLYING_BAGS) {
    bagPosX(index) += bagSpeedX(index)
    if (bagPosX(index) > 310.0) {
      bagPosX(index) = -310.0
    }

    val verticalWaveMotion = bagPosY(index) + calculateSineWave(masterTimelineFrame + (index * 25.0), 5.0, 14.0)
    val bagRotationAngle = masterTimelineFrame * 2.5 + (index * 45.0)

    val plasticBag = Picture.ellipse(25.0, 16.0)
    plasticBag.setFillColor(Color(255, 255, 255, 180))
    plasticBag.setPenColor(Color(170, 170, 170))
    plasticBag.setPosition(bagPosX(index), verticalWaveMotion)
    plasticBag.rotate(bagRotationAngle)
    draw(plasticBag)
  }
}

// ----------------------------------------------------------------------------
// 6. UI, TYPWRITER & ALERT BANNER SUBSYSTEMS
// ----------------------------------------------------------------------------
def renderDynamicAlertBannerAndTypwriterText(): Unit = {
  // Pulsing Danger Glow Effect Behind Banner
  val pulseIntensity = Math.abs(calculateSineWave(masterTimelineFrame, 6.0, 50.0))

  val glowLayer = Picture.rectangle(540.0, 60.0)
  glowLayer.setFillColor(Color(255, 0, 0, (80.0 + pulseIntensity).toInt))
  glowLayer.setPosition(-270.0, -175.0)
  draw(glowLayer)

  // Main Alert Banner Chassis
  val bannerChassis = Picture.rectangle(525.0, 50.0)
  bannerChassis.setFillColor(Color(160, 15, 15))
  bannerChassis.setPenColor(Color(255, 215, 0))
  bannerChassis.setPenThickness(3)
  bannerChassis.setPosition(-262.5, -170.0)
  draw(bannerChassis)

  // Substring Extraction for Typewriter Text Engine
  val activeTitleSubstring = primaryHeaderTitle.substring(0, titleCharacterCount)

  // Title Text Shadow
  val titleShadow = Picture.text(s"?? $activeTitleSubstring ??", 20)
  titleShadow.setPenColor(Color(0, 0, 0))
  titleShadow.setPosition(-187.0, -156.0)
  draw(titleShadow)

  // Title Text Foreground Main
  val titleMain = Picture.text(s"?? $activeTitleSubstring ??", 20)
  titleMain.setPenColor(Color(255, 255, 255))
  titleMain.setPosition(-189.0, -154.0)
  draw(titleMain)

  // Render Subtext Once Main Title Completes Typewriting
  if (titleCharacterCount >= primaryHeaderTitle.length) {
    val activeSubSubstring = secondaryHeaderSub.substring(0, subCharacterCount)

    val subShadow = Picture.text(activeSubSubstring, 14)
    subShadow.setPenColor(Color(0, 0, 0))
    subShadow.setPosition(-123.0, 134.0)
    draw(subShadow)

    val subMain = Picture.text(activeSubSubstring, 14)
    subMain.setPenColor(Color(255, 230, 110))
    subMain.setPosition(-125.0, 136.0)
    draw(subMain)
  }
}

// ----------------------------------------------------------------------------
// 7. MASTER SCENE PIPELINE COORDINATOR
// ----------------------------------------------------------------------------
def executeMasterSceneRenderingPipeline(): Unit = {
  erasePictures()
  renderPollutionSkyboxAndChimneys()
  renderIndustrialSmokeDynamics()
  renderPollutedGroundTerrain()
  renderGarbageAndPlasticDumpMatrix()
  renderArticulatedCowEntity(-25.0, 0.0, 1.0, false) // Foreground Active Cow
  renderArticulatedCowEntity(-25.0, 0.0, 0.65, true) // Background Cow
  renderWindBlownPlasticBags()
  renderDynamicAlertBannerAndTypwriterText()
}

// Initial Canvas Bootstrap Render
executeMasterSceneRenderingPipeline()

// ----------------------------------------------------------------------------
// 8. MASTER ANIMATION LOOP & EVENT CONTROLLER
// ----------------------------------------------------------------------------
animateWithState(0.0) { frameCounter =>
  executeEnvironmentalWarningAudio()
  masterTimelineFrame = frameCounter

  // Articulation State Updates
  cowTailSwayAngle = (cowTailSwayAngle + 3.0) % 360.0

  // Typewriter Progress Logic
  typewriterTickCounter += 1
  if (typewriterTickCounter % 3 == 0) {
    if (titleCharacterCount < primaryHeaderTitle.length) {
      titleCharacterCount += 1
    } else if (subCharacterCount < secondaryHeaderSub.length) {
      subCharacterCount += 1
    }
  }

  // Execute Master Frame Draw Call
  executeMasterSceneRenderingPipeline()

  frameCounter + 1.0
}