Code Sketch


kbc
By: Mhalsakant School
// === KAUN BANEGA CROREPATI (KBC) ROBOT GAME ===

cleari()
originBottomLeft()

// Game State Variables
var currentQuestionIndex = 0
var currentPrize = 0
var gameState = "PLAYING" // PLAYING, WON, LOST
var selectedOption = -1
var feedbackTimer = 0.0

// KBC Prize Money List (100 to 7 Crore)
val prizeMoney = Array(100, 1000, 10000, 50000, 100000, 320000, 640000, 1250000, 2500000, 5000000, 10000000, 70000000)

// Questions, Options, and Correct Answer Index (0 to 3)
class Question(val qText: String, val options: Array[String], val correctIndex: Int)

val questions = Array(
    new Question("What is India's space agency called?", Array("A. NASA", "B. ISRO", "C. SpaceX", "D. ESA"), 1),
    new Question("Which planet is known as the Red Planet?", Array("A. Venus", "B. Earth", "C. Mars", "D. Jupiter"), 2),
    new Question("Who was the first Indian in space?", Array("A. Rakesh Sharma", "B. Kalpana Chawla", "C. Vikram Sarabhai", "D. APJ Abdul Kalam"), 0),
    new Question("Which of these is a launch vehicle built by ISRO?", Array("A. Falcon 9", "B. Saturn V", "C. PSLV", "D. Starship"), 2),
    new Question("What is the escape velocity of Earth approximately?", Array("A. 5 km/s", "B. 11.2 km/s", "C. 20 km/s", "D. 3 km/s"), 1),
    new Question("Which celestial body is Earth's only natural satellite?", Array("A. Titan", "B. Europa", "C. Moon", "D. Phobos"), 2),
    new Question("In which year did India's Mangalyaan reach Mars?", Array("A. 2012", "B. 2014", "C. 2016", "D. 2018"), 1),
    new Question("Who is known as the Missile Man of India?", Array("A. Homi Bhabha", "B. Vikram Sarabhai", "C. APJ Abdul Kalam", "D. Satish Dhawan"), 2),
    new Question("Which planet has the most prominent ring system?", Array("A. Jupiter", "B. Saturn", "C. Uranus", "D. Neptune"), 1),
    new Question("Chandrayaan-3 successfully landed near which pole of the Moon?", Array("A. North Pole", "B. South Pole", "C. Equator", "D. East Pole"), 1),
    new Question("What fuel does the cryogenic upper stage of LVM3 use?", Array("A. Solid Fuel", "B. Liquid Hydrogen & Liquid Oxygen", "C. Kerosene", "D. Petrol"), 1),
    new Question("For 7 CRORE: Which is the farthest planet from the Sun in our Solar System?", Array("A. Uranus", "B. Neptune", "C. Pluto", "D. Saturn"), 1)
)

// KBC Robot Class
class KBCRobot() {
    def drawRobot(canvas: CanvasDraw, x: Double, y: Double) {
        // Robot Head
        canvas.fill(240, 200, 50) // Golden KBC theme
        canvas.stroke(100, 80, 10)
        canvas.strokeWeight(2)
        canvas.rect(x - 30, y - 60, 60, 45)

        // Robot Antenna & Lights
        canvas.line(x, y - 60, x, y - 80)
        canvas.fill(0, 255, 0)
        canvas.ellipse(x, y - 85, 10, 10)

        // Robot Eyes (LED Screen style)
        canvas.fill(20, 20, 20)
        canvas.rect(x - 22, y - 48, 16, 10)
        canvas.rect(x + 6, y - 48, 16, 10)
        canvas.fill(0, 255, 255)
        canvas.ellipse(x - 14, y - 43, 5, 5)
        canvas.ellipse(x + 14, y - 43, 5, 5)

        // Robot Mouth / Speaker
        canvas.fill(50, 50, 50)
        canvas.rect(x - 15, y - 28, 30, 8)

        // Robot Body
        canvas.fill(50, 50, 80)
        canvas.rect(x - 40, y - 15, 80, 70)
        canvas.fill(255, 215, 0)
        canvas.text("KBC HOST", x - 32, y + 20)
    }
}

val kbcRobot = new KBCRobot()

// Mouse Click to Answer Options
onMouseClick { (x, y) =>
    if (gameState == "PLAYING") {
        // Check click on Option boxes (Assuming 4 option boxes on screen)
        // Option 1: y between 260-300, Option 2: 310-350, etc.
        val boxWidth = 500.0
        val startX = (cwidth / 2.0) - (boxWidth / 2.0)
        
        if (x >= startX && x <= startX + boxWidth) {
            if (y >= 260 && y <= 300) checkAnswer(0)
            else if (y >= 310 && y <= 350) checkAnswer(1)
            else if (y >= 360 && y <= 400) checkAnswer(2)
            else if (y >= 410 && y <= 450) checkAnswer(3)
        }
    } else {
        // Restart Game on Click
        currentQuestionIndex = 0
        currentPrize = 0
        gameState = "PLAYING"
    }
}

def checkAnswer(selectedIndex: Int) {
    val q = questions(currentQuestionIndex)
    if (selectedIndex == q.correctIndex) {
        currentPrize = prizeMoney(currentQuestionIndex)
        currentQuestionIndex += 1
        if (currentQuestionIndex >= questions.length) {
            gameState = "WON"
        }
    } else {
        gameState = "LOST"
    }
}

def viewState(canvas: CanvasDraw) {
    // Background (Dark Blue KBC Studio Theme)
    canvas.fill(10, 10, 40)
    canvas.noStroke()
    canvas.rect(0, 0, cwidth, cheight)

    // Title Banner
    canvas.fill(255, 215, 0)
    canvas.text("KAUN BANEGA CROREPATI - ISRO & SPACE EDITION", cwidth / 2.0 - 200, cheight - 30)

    // Draw Robot Host
    kbcRobot.drawRobot(canvas, 100.0, 150.0)

    // Display Current Prize Money Board
    canvas.fill(20, 20, 60)
    canvas.stroke(255, 215, 0)
    canvas.rect(cwidth - 220, 50, 200, 350)
    canvas.fill(255, 255, 255)
    canvas.text("PRIZE LADDER", cwidth - 170, 75)

    var ladderY = 100.0
    for (i <- prizeMoney.length - 1 to 0 by -1) {
        if (i == currentQuestionIndex && gameState == "PLAYING") {
            canvas.fill(255, 140, 0) // Current active prize highlight
        } else if (i < currentQuestionIndex) {
            canvas.fill(50, 205, 50) // Won levels
        } else {
            canvas.fill(200, 200, 200)
        }
        canvas.text(s"Q${i+1}: Rs. ${prizeMoney(i)}", cwidth - 200, ladderY)
        ladderY += 25.0
    }

    if (gameState == "PLAYING") {
        val q = questions(currentQuestionIndex)

        // Question Box
        canvas.fill(20, 30, 90)
        canvas.stroke(255, 215, 0)
        canvas.strokeWeight(2)
        val qBoxWidth = 500.0
        val qBoxX = (cwidth / 2.0) - (qBoxWidth / 2.0)
        canvas.rect(qBoxX, 150, qBoxWidth, 80)

        canvas.fill(255, 255, 255)
        canvas.text(s"Q${currentQuestionIndex + 1}: ${q.qText}", qBoxX + 20, 190)

        // Options Boxes
        var optY = 260.0
        for (i <- 0 until 4) {
            canvas.fill(30, 40, 120)
            canvas.stroke(255, 255, 255)
            canvas.rect(qBoxX, optY, qBoxWidth, 40)

            canvas.fill(255, 255, 255)
            canvas.text(q.options(i), qBoxX + 20, optY + 25)
            optY += 50.0
        }

        canvas.fill(255, 215, 0)
        canvas.text(s"Current Prize Won: Rs. $currentPrize", qBoxX, 480)

    } else if (gameState == "WON") {
        canvas.fill(0, 255, 0)
        canvas.text("CONGRATULATIONS! YOU WON 7 CRORE RUPEES!", cwidth / 2.0 - 180, cheight / 2.0)
        canvas.fill(255, 255, 255)
        canvas.text("Click anywhere to play again.", cwidth / 2.0 - 100, cheight / 2.0 + 40)

    } else if (gameState == "LOST") {
        canvas.fill(255, 0, 0)
        canvas.text("WRONG ANSWER! GAME OVER.", cwidth / 2.0 - 120, cheight / 2.0)
        canvas.fill(255, 255, 255)
        canvas.text(s"You take home: Rs. $currentPrize", cwidth / 2.0 - 100, cheight / 2.0 + 30)
        canvas.text("Click anywhere to try again.", cwidth / 2.0 - 100, cheight / 2.0 + 70)
    }
}

animateWithCanvasDraw { canvas =>
    viewState(canvas)
}