Code Sketch


yadnesh shahare
By: Mhalsakant School
// --- Interactive Small Robot with 20 MCQ Questions & Precise Mouse Click Detection in Kojo ---
cleari()
setBackground(Color(15, 20, 35))

case class Question(q: String, options: Seq[String], correctIndex: Int, points: Int)

val mcqList = Seq(
  Question("1. Bharatacha rashtriya dhavaj konta?", Seq("A) Tiranga", "B) Lal Jhanda", "C) Pivala Jhanda", "D) Nila Jhanda"), 0, 10),
  Question("2. Kojo hi kontya bhasha var adharit ahe?", Seq("A) Python", "B) Scala", "C) Java", "D) C++"), 1, 20),
  Question("3. Bharat kevha swatantra jhala?", Seq("A) 15 August 1947", "B) 26 January 1950", "C) 15 August 1950", "D) 26 January 1947"), 0, 30),
  Question("4. Robot che mukhya kaam kay aste?", Seq("A) Kam sope karne", "B) Nachne", "C) Song gaane", "D) Jhopne"), 0, 40),
  Question("5. Bharatacha rashtriya prani konta?", Seq("A) Sinh", "B) Wagh", "C) Hatti", "D) Ghoda"), 1, 50),
  Question("6. Bharatacha rashtriya pakshi konta?", Seq("A) Mor", "B) Kabutar", "C) Popat", "D) Chimani"), 0, 60),
  Question("7. Maharashtrachi rajdhani konti?", Seq("A) Pune", "B) Nagpur", "C) Mumbai", "D) Nashik"), 2, 70),
  Question("8. Bharatache pahile rashtrapati kon?", Seq("A) Dr. Rajendra Prasad", "B) Nehru", "C) Gandhi", "D) Patel"), 0, 80),
  Question("9. CPU cha full form kay asto?", Seq("A) Central Processing Unit", "B) Computer Personal Unit", "C) Central Program", "D) Control Unit"), 0, 90),
  Question("10. Ashoka Chakrat kiti spokes astat?", Seq("A) 12", "B) 16", "C) 24", "D) 32"), 2, 100),
  Question("11. Bharatacha rashtriya khel konta?", Seq("A) Cricket", "B) Hockey", "C) Football", "D) Kabaddi"), 1, 110),
  Question("12. Eka varshat kiti mahine astat?", Seq("A) 10", "B) 12", "C) 6", "D) 8"), 1, 120),
  Question("13. Eka aavditat kiti divas astat?", Seq("A) 5", "B) 6", "C) 7", "D) 8"), 2, 130),
  Question("14. Soorya kontya dishene ugavto?", Seq("A) Paschim", "B) Purva", "C) Uttar", "D) Dakshin"), 1, 140),
  Question("15. Prithvi konala pradakshina ghalte?", Seq("A) Surya", "B) Chandra", "C) Mangal", "D) Tara"), 0, 150),
  Question("16. Bharatache rashtriya geet konte?", Seq("A) Vande Mataram", "B) Jana Gana Mana", "C) Sare Jahan", "D) Ma Tujhe Salam"), 1, 160),
  Question("17. Kojo madhe picture draw command?", Seq("A) draw()", "B) print()", "C) run()", "D) show()"), 0, 170),
  Question("18. 1 kg madhe kiti grams astat?", Seq("A) 500g", "B) 1000g", "C) 100g", "D) 10g"), 1, 180),
  Question("19. Computer cha 'Brain' konala mhantat?", Seq("A) Mouse", "B) Keyboard", "C) CPU", "D) Monitor"), 2, 190),
  Question("20. Bharatacha rashtriya phul konte?", Seq("A) Gulab", "B) Kamal", "C) Javas", "D) Genda"), 1, 200)
)

var index = 0
var selectedOption = -1 
var answered = false
var totalScore = 0

// Y coordinates for the 4 options exactly matched with click zones
val optYCoords = Seq(-130.0, -165.0, -200.0, -235.0)

def drawScene(): Unit = {
  erasePictures()

  val w = canvasBounds.width
  val h = canvasBounds.height

  // --- SMALL ROBOT (Compact Size) ---
  val ry = 140.0

  // Small Head
  val head = Picture.rectangle(60, 45)
  head.setFillColor(Color(70, 130, 180))
  head.setPenColor(Color(255, 255, 255))
  head.setPosition(0, ry + 30.0)
  draw(head)

  // Eyes
  val eye1 = Picture.circle(4)
  eye1.setFillColor(Color(0, 255, 255))
  eye1.setPosition(-15, ry + 32)
  draw(eye1)
  val eye2 = Picture.circle(4)
  eye2.setFillColor(Color(0, 255, 255))
  eye2.setPosition(15, ry + 32)
  draw(eye2)

  // Small Torso
  val torso = Picture.rectangle(75, 70)
  torso.setFillColor(Color(100, 149, 237))
  torso.setPosition(0, ry - 15.0)
  draw(torso)

  // --- SCORE DISPLAY ---
  val scoreText = Picture.text(f"Total Score: $totalScore pts", 16)
  scoreText.setPenColor(Color(50, 255, 50))
  scoreText.setPosition(0, h / 2.0 - 25.0)
  draw(scoreText)

  // --- CURRENT QUESTION & POINTS ---
  val currentQ = mcqList(index)

  val qHeader = Picture.text(f"Question ${index + 1}/20  [+${currentQ.points} Points]", 15)
  qHeader.setPenColor(Color(255, 215, 0))
  qHeader.setPosition(0, ry - 60.0)
  draw(qHeader)

  // Large Question Text
  val qText = Picture.text(currentQ.q, 18)
  qText.setPenColor(Color(255, 153, 51))
  qText.setPosition(0, ry - 95.0)
  draw(qText)

  // --- OPTIONS (Large Text with precise coordinates) ---
  for (i <- 0 until 4) {
    var displayText = currentQ.options(i)
    var textColor = Color(255, 255, 255)

    if (answered) {
      if (i == currentQ.correctIndex) {
        displayText = currentQ.options(i) + "  ? (Correct!)"
        textColor = Color(50, 255, 50) 
      } else if (i == selectedOption) {
        displayText = currentQ.options(i) + "  ? (Wrong!)"
        textColor = Color(255, 80, 80) 
      }
    }

    val optText = Picture.text(displayText, 17)
    optText.setPenColor(textColor)
    optText.setPosition(0, optYCoords(i))
    draw(optText)
  }

  // Bottom prompt
  val promptMsg = if (!answered) "Click directly on Option A, B, C or D" else "Click anywhere for next question"
  val prompt = Picture.text(promptMsg, 14)
  prompt.setPenColor(Color(180, 180, 180))
  prompt.setPosition(0, -275.0)
  draw(prompt)
}

// Initial draw
drawScene()

// Precise Mouse Click Event handler
onMouseClick { (x, y) =>
  val currentQ = mcqList(index)

  if (!answered) {
    // Check click against each option's Y range (giving a nice big vertical window of �15 pixels around each option)
    var clickedOption = -1
    for (i <- 0 until 4) {
      val targetY = optYCoords(i)
      if (y >= targetY - 15.0 && y <= targetY + 15.0 && Math.abs(x) < 280.0) {
        clickedOption = i
      }
    }

    if (clickedOption != -1) {
      selectedOption = clickedOption
      answered = true
      if (clickedOption == currentQ.correctIndex) {
        totalScore = totalScore + currentQ.points
      }
      drawScene()
    }
  } else {
    // Move to the next question on any subsequent click
    answered = false
    selectedOption = -1
    index = (index + 1) % mcqList.length
    drawScene()
  }
}