Code Sketch


Bixuu02
By: Mhalsakant School
Category: Programming
// ============================================================
// INDIAN SCIENTISTS GUIDE & PROFILES APP BY BIXUU
// ============================================================

cleari()
originBottomLeft()

// ============================================================
// APP STATES & VARIABLES
// ============================================================
var screen = 0 // 0: Welcome Letter, 1: Scientists List, 2: Scientist Details
var selectedScientist = ""

// Background Star Animation Class
class Star(val x: Double, val y: Double, val size: Double) {
    var phase = randomDouble(6.28)
    def step() { phase += 0.04 }
    def view(canvas: CanvasDraw) {
        val brightness = 150 + (math.sin(phase) * 80).toInt
        canvas.noStroke()
        canvas.fill(200, 220, 255, brightness)
        canvas.ellipse(x, y, size, size)
    }
}

val stars = ArrayBuffer.empty[Star]
for (i <- 0 until 50) {
    stars.append(new Star(randomDouble(cwidth), randomDouble(cheight), 1.0 + randomDouble(2.5)))
}

// ============================================================
// SCIENTISTS LIST
// ============================================================
val scientistsList = Array(
    "Dr. A.P.J. Abdul Kalam",
    "Aryabhata",
    "Dr. Homi J. Bhabha",
    "Dr. Vikram Sarabhai",
    "Sir C.V. Raman",
    "Srinivasa Ramanujan",
    "Dr. Jagadish Chandra Bose",
    "Kalpana Chawla"
)

// ============================================================
// SCIENTIST DETAILS LOOKUP
// ============================================================
def getScientistDetails(sciName: String): Array[String] = {
    sciName match {
        case "Dr. A.P.J. Abdul Kalam" => Array(
            "Birth Date: October 15, 1931",
            "Birth Place: Rameswaram, Tamil Nadu",
            "Family: Jainulabdeen (Father), Ashiamma (Mother)",
            "Creations: SLV-III, Agni & Prithvi Missiles",
            "Known As: Missile Man of India"
        )
        case "Aryabhata" => Array(
            "Birth Date: 476 CE",
            "Birth Place: Kusumapura (Patna, Bihar)",
            "Family: Ancient Indian Mathematician & Astronomer",
            "Creations: Invented Zero, Calculated Pi (?)",
            "Legacy: India's 1st Satellite named after him"
        )
        case "Dr. Homi J. Bhabha" => Array(
            "Birth Date: October 30, 1909",
            "Birth Place: Mumbai, Maharashtra",
            "Family: Jehangir Hormusji Bhabha (Father)",
            "Creations: Father of Indian Nuclear Programme",
            "Institutions: TIFR and BARC Founder"
        )
        case "Dr. Vikram Sarabhai" => Array(
            "Birth Date: August 12, 1919",
            "Birth Place: Ahmedabad, Gujarat",
            "Family: Ambalal Sarabhai (Father), Mrinalini Sarabhai (Wife)",
            "Creations: Father of Indian Space Program",
            "Institutions: Founder of ISRO"
        )
        case "Sir C.V. Raman" => Array(
            "Birth Date: November 7, 1888",
            "Birth Place: Tiruchirappalli, Tamil Nadu",
            "Family: R. Chandrasekara Iyer (Father)",
            "Creations: Discovered 'Raman Effect'",
            "Awards: Nobel Prize in Physics (1930)"
        )
        case "Srinivasa Ramanujan" => Array(
            "Birth Date: December 22, 1887",
            "Birth Place: Erode, Tamil Nadu",
            "Family: K. Srinivasa Iyengar (Father), Komalammal (Mother)",
            "Creations: Contributions to Number Theory & Infinite Series",
            "Legacy: World-renowned Mathematical Genius"
        )
        case "Dr. Jagadish Chandra Bose" => Array(
            "Birth Date: November 30, 1858",
            "Birth Place: Mymensingh, Bengal (Bangladesh)",
            "Family: Bhagawan Chandra Bose (Father)",
            "Creations: Invented Crescograph, Pioneer in Wireless Radio",
            "Legacy: Proved plants have feelings"
        )
        case _ => Array(
            "Birth Date: March 17, 1962",
            "Birth Place: Karnal, Haryana",
            "Family: Banarsi Lal Chawla (Father), montagnes (Mother)",
            "Creations: First Indian-born Woman in Space (NASA)",
            "Legacy: Astronaut and Space Mission Specialist"
        )
    }
}

// ============================================================
// SCREENS UI LOGIC
// ============================================================
def drawWelcomeScreen(canvas: CanvasDraw) {
    canvas.background(15, 25, 45)
    repeatFor(stars) { s => s.view(canvas) }

    canvas.fill(255, 252, 245)
    canvas.stroke(0, 150, 255)
    canvas.strokeWeight(4)
    canvas.rect(cwidth/2 - 250, cheight/2 - 190, 500, 380)

    canvas.fill(0, 102, 204)
    canvas.text("INDIAN SCIENTISTS & CREATIONS GUIDE", cwidth/2 - 160, cheight/2 + 150)

    canvas.fill(40, 40, 40)
    canvas.text("Hello Science Enthusiast,", cwidth/2 - 220, cheight/2 + 110)
    canvas.text("Explore legendary Indian scientists like Dr. APJ", cwidth/2 - 220, cheight/2 + 80)
    canvas.text("Abdul Kalam, Aryabhata, Homi Bhabha & more.", cwidth/2 - 220, cheight/2 + 55)
    canvas.text("Check their birth dates, places, family & rockets.", cwidth/2 - 220, cheight/2 + 30)
    
    canvas.fill(0, 120, 0)
    canvas.text("Click below to start exploring scientists.", cwidth/2 - 220, cheight/2 - 10)

    canvas.fill(0, 120, 204)
    canvas.noStroke()
    canvas.rect(cwidth/2 - 150, cheight/2 - 110, 300, 50)
    canvas.fill(255, 255, 255)
    canvas.text("EXPLORE SCIENTISTS", cwidth/2 - 90, cheight/2 - 80)
}

def drawScientistsListScreen(canvas: CanvasDraw) {
    canvas.background(15, 25, 40)
    repeatFor(stars) { s => s.view(canvas) }

    canvas.fill(255, 215, 0)
    canvas.text("SELECT A SCIENTIST TO VIEW PROFILE", cwidth/2 - 145, cheight - 30)

    for (i <- 0 until scientistsList.length) {
        val col = i % 2
        val row = i / 2
        
        val bx = if (col == 0) cwidth/2 - 235 else cwidth/2 + 10
        val by = (cheight/2 + 130) - (row * 52)
        
        canvas.fill(30, 80, 130)
        canvas.stroke(255, 215, 0)
        canvas.strokeWeight(2)
        canvas.rect(bx, by, 225, 42)

        canvas.fill(255, 255, 255)
        canvas.noStroke()
        canvas.text((i + 1) + ". " + scientistsList(i), bx + 10, by + 26)
    }

    canvas.fill(100, 100, 100)
    canvas.noStroke()
    canvas.rect(cwidth/2 - 80, 12, 160, 28)
    canvas.fill(255, 255, 255)
    canvas.text("BACK TO HOME", cwidth/2 - 45, 28)
}

def drawScientistDetailsScreen(canvas: CanvasDraw) {
    canvas.background(25, 15, 35)
    repeatFor(stars) { s => s.view(canvas) }

    canvas.fill(255, 215, 0)
    canvas.text("SCIENTIST PROFILE: " + selectedScientist, cwidth/2 - 150, cheight - 35)

    canvas.fill(250, 248, 235)
    canvas.stroke(0, 150, 255)
    canvas.strokeWeight(3)
    canvas.rect(cwidth/2 - 240, cheight/2 - 140, 480, 260)

    canvas.fill(30, 30, 30)
    canvas.noStroke()

    val details = getScientistDetails(selectedScientist)
    for (i <- 0 until details.length) {
        val dy = (cheight/2 + 65) - (i * 38)
        canvas.fill(0, 80, 150)
        canvas.text("- " + details(i), cwidth/2 - 210, dy)
    }

    canvas.fill(0, 120, 150)
    canvas.rect(cwidth/2 - 110, 15, 220, 32)
    canvas.fill(255, 255, 255)
    canvas.text("BACK TO LIST", cwidth/2 - 45, 31)
}

// ============================================================
// CLICK HANDLER LOGIC
// ============================================================
def handleClick(x: Double, y: Double) {
    if (screen == 0) {
        if (x >= cwidth/2 - 150 && x <= cwidth/2 + 150 && y >= cheight/2 - 110 && y <= cheight/2 - 60) {
            screen = 1
        }
    } 
    else if (screen == 1) {
        if (x >= cwidth/2 - 80 && x <= cwidth/2 + 80 && y >= 12 && y <= 42) {
            screen = 0
        } else {
            for (i <- 0 until scientistsList.length) {
                val col = i % 2
                val row = i / 2
                val bx = if (col == 0) cwidth/2 - 235 else cwidth/2 + 10
                val by = (cheight/2 + 130) - (row * 52)
                
                if (x >= bx && x <= bx + 225 && y >= by && y <= by + 42) {
                    selectedScientist = scientistsList(i)
                    screen = 2
                }
            }
        }
    }
    else if (screen == 2) {
        if (x >= cwidth/2 - 110 && x <= cwidth/2 + 110 && y >= 15 && y <= 47) {
            screen = 1
        }
    }
}

onMouseClick { (x, y) => handleClick(x, y) }

// ============================================================
// MAIN ANIMATION LOOP
// ============================================================
animateWithCanvasDraw { canvas =>
    repeatFor(stars) { s => s.step() }

    if (screen == 0) drawWelcomeScreen(canvas)
    else if (screen == 1) drawScientistsListScreen(canvas)
    else if (screen == 2) drawScientistDetailsScreen(canvas)
}