Code Sketch


quizo a sport trivia quiz app
By: Mhalsakant School
Category: Programming
// === QUIZO WITH AI TECHNOLOGY (SPORTS TRIVIA APP) ===

cleari()
originBottomLeft()

// Game State Variables (Starts directly at MENU, Password removed)
var gameState = "MENU" // MENU, PLAYING, WON, LOST
var selectedSport = "" // CRICKET, FOOTBALL, BASKETBALL, HOCKEY
var currentQuestionIndex = 0
var score = 0
var soundEffectMessage = "? Audio: System Initialized - Choose a Sport!"

// Class for Sports Questions
class SportQuestion(val qText: String, val options: Array[String], val correctIndex: Int)

// Expanded Question Banks (5 Questions per Sport = 20 Total Questions)
val cricketQuestions = Array(
    new SportQuestion("Who won the ICC Men's Cricket World Cup in 2011?", Array("A. Australia", "B. India", "C. Sri Lanka", "D. England"), 1),
    new SportQuestion("What is the maximum number of players in a standard cricket team on the field?", Array("A. 9", "B. 10", "C. 11", "D. 12"), 2),
    new SportQuestion("Who holds the record for the highest individual score in ODIs?", Array("A. Virat Kohli", "B. Sachin Tendulkar", "C. Rohit Sharma", "D. Chris Gayle"), 2),
    new SportQuestion("Which country hosted the Cricket World Cup in 2023?", Array("A. Australia", "B. England", "C. India", "D. South Africa"), 2),
    new SportQuestion("What is the length of a cricket pitch between the two wickets?", Array("A. 20 yards", "B. 22 yards", "C. 24 yards", "D. 18 yards"), 1)
)

val footballQuestions = Array(
    new SportQuestion("Which country won the FIFA World Cup in 2022?", Array("A. France", "B. Brazil", "C. Argentina", "D. Germany"), 2),
    new SportQuestion("How many players are on the field for one team in a football match?", Array("A. 9", "B. 10", "C. 11", "D. 12"), 2),
    new SportQuestion("Which player has won the most Ballon d'Or awards?", Array("A. Cristiano Ronaldo", "B. Lionel Messi", "C. Pele", "D. Diego Maradona"), 1),
    new SportQuestion("Which club has won the most UEFA Champions League titles?", Array("A. AC Milan", "B. Barcelona", "C. Real Madrid", "D. Bayern Munich"), 2),
    new SportQuestion("How long is a standard professional football match (excluding extra time)?", Array("A. 80 minutes", "B. 90 minutes", "C. 100 minutes", "D. 60 minutes"), 1)
)

val basketballQuestions = Array(
    new SportQuestion("Which team holds the record for most wins in an NBA regular season?", Array("A. Chicago Bulls", "B. Golden State Warriors", "C. Los Angeles Lakers", "D. Boston Celtics"), 1),
    new SportQuestion("How many players from one team are on the court at a time?", Array("A. 4", "B. 5", "C. 6", "D. 7"), 1),
    new SportQuestion("What is the regulation height of a basketball hoop?", Array("A. 9 feet", "B. 10 feet", "C. 11 feet", "D. 12 feet"), 1),
    new SportQuestion("Which player is famously known as 'Black Mamba'?", Array("A. LeBron James", "B. Michael Jordan", "C. Kobe Bryant", "D. Stephen Curry"), 2),
    new SportQuestion("How many points is a shot made from beyond the arc worth?", Array("A. 1 point", "B. 2 points", "C. 3 points", "D. 4 points"), 2)
)

val hockeyQuestions = Array(
    new SportQuestion("How many periods are there in a standard ice hockey game?", Array("A. 2", "B. 3", "C. 4", "D. 5"), 1),
    new SportQuestion("Which country has won the most Olympic gold medals in men's field hockey?", Array("A. India", "B. Pakistan", "C. Netherlands", "D. Australia"), 0),
    new SportQuestion("What is it called when a player scores three goals in a single game?", Array("A. Strike", "B. Hat-trick", "C. Grand Slam", "D. Home Run"), 1),
    new SportQuestion("How many players are on the ice for one team during regular play in ice hockey?", Array("A. 5", "B. 6", "C. 7", "D. 4"), 1),
    new SportQuestion("What object is used instead of a ball in ice hockey?", Array("A. Puck", "B. Stone", "C. Sphere", "D. Disc"), 0)
)

var currentQuestions: Array[SportQuestion] = cricketQuestions

// AI Quiz Host Robot Class
class AIQuizRobot() {
    def drawRobot(canvas: CanvasDraw, x: Double, y: Double) {
        // Head
        canvas.fill(0, 180, 255)
        canvas.stroke(0, 50, 100)
        canvas.strokeWeight(2)
        canvas.rect(x - 30, y - 60, 60, 45)

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

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

        // Speaker Matrix
        canvas.fill(40, 40, 40)
        canvas.rect(x - 15, y - 28, 30, 8)

        // Body
        canvas.fill(30, 30, 60)
        canvas.rect(x - 40, y - 15, 80, 70)
        canvas.fill(0, 255, 200)
        canvas.text("QUIZO AI", x - 26, y + 20)
    }
}

val aiRobot = new AIQuizRobot()

// Mouse Click Handling for UI Interactions
onMouseClick { (x, y) =>
    if (gameState == "MENU") {
        // Sport Selection Menu boxes (y coordinates)
        val boxWidth = 400.0
        val startX = (cwidth / 2.0) - (boxWidth / 2.0)
        
        if (x >= startX && x <= startX + boxWidth) {
            if (y >= 260 && y <= 300) {
                selectedSport = "CRICKET"
                currentQuestions = cricketQuestions
                startQuiz()
            } else if (y >= 310 && y <= 350) {
                selectedSport = "FOOTBALL"
                currentQuestions = footballQuestions
                startQuiz()
            } else if (y >= 360 && y <= 400) {
                selectedSport = "BASKETBALL"
                currentQuestions = basketballQuestions
                startQuiz()
            } else if (y >= 410 && y <= 450) {
                selectedSport = "HOCKEY"
                currentQuestions = hockeyQuestions
                startQuiz()
            }
        }
    } 
    else if (gameState == "PLAYING") {
        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 {
        // Reset back to Menu
        gameState = "MENU"
        soundEffectMessage = "? Audio: Returned to Sport Menu"
    }
}

def startQuiz() {
    currentQuestionIndex = 0
    score = 0
    soundEffectMessage = s"? Sound: Loading $selectedSport Arena (5 Unique Questions)..."
    gameState = "PLAYING"
}

def checkAnswer(selectedIndex: Int) {
    val q = currentQuestions(currentQuestionIndex)
    if (selectedIndex == q.correctIndex) {
        score += 10
        soundEffectMessage = "? Sound: Correct Answer Ding! (+10 pts)"
        currentQuestionIndex += 1
        if (currentQuestionIndex >= currentQuestions.length) {
            gameState = "WON"
            soundEffectMessage = "? Sound: Victory Fanfare! All 5 Questions Completed!"
        }
    } else {
        soundEffectMessage = "? Sound: Wrong Buzzer!"
        gameState = "LOST"
    }
}

def viewState(canvas: CanvasDraw) {
    // Dynamic Tech Background
    canvas.fill(15, 15, 35)
    canvas.noStroke()
    canvas.rect(0, 0, cwidth, cheight)

    // App Header Bar
    canvas.fill(0, 200, 255)
    canvas.text("QUIZO: AI TECHNOLOGY SPORTS TRIVIA", cwidth / 2.0 - 180, cheight - 30)
    
    // Sound Effect Indicator Footer
    canvas.fill(180, 180, 180)
    canvas.text(soundEffectMessage, 30, 30)

    // Draw AI Robot Mascot
    aiRobot.drawRobot(canvas, 80.0, 150.0)

    if (gameState == "MENU") {
        // Menu Sport Options UI
        canvas.fill(255, 255, 255)
        canvas.text("SELECT A SPORT TO START AI TRIVIA:", cwidth / 2.0 - 130, 200)

        val menuWidth = 400.0
        val menuX = (cwidth / 2.0) - (menuWidth / 2.0)
        val sportsList = Array("1. Cricket (5 Questions)", "2. Football (5 Questions)", "3. Basketball (5 Questions)", "4. Hockey (5 Questions)")

        var mY = 260.0
        for (i <- 0 until sportsList.length) {
            canvas.fill(40, 60, 140)
            canvas.stroke(0, 255, 255)
            canvas.rect(menuX, mY, menuWidth, 40)

            canvas.fill(255, 255, 255)
            canvas.text(sportsList(i), menuX + 20, mY + 25)
            mY += 50.0
        }
    } 
    else if (gameState == "PLAYING") {
        val q = currentQuestions(currentQuestionIndex)
        val qBoxWidth = 500.0
        val qBoxX = (cwidth / 2.0) - (qBoxWidth / 2.0)

        // Question Box
        canvas.fill(25, 35, 100)
        canvas.stroke(0, 255, 200)
        canvas.strokeWeight(2)
        canvas.rect(qBoxX, 150, qBoxWidth, 80)

        canvas.fill(255, 255, 255)
        canvas.text(s"[$selectedSport] Q${currentQuestionIndex + 1}/5: ${q.qText}", qBoxX + 15, 190)

        // Option Choices
        var optY = 260.0
        for (i <- 0 until 4) {
            canvas.fill(35, 45, 130)
            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(0, 255, 200)
        canvas.text(s"AI Score: $score Points", qBoxX, 480)

    } 
    else if (gameState == "WON") {
        canvas.fill(0, 255, 100)
        canvas.text("EXCELLENT! ALL 5 QUESTIONS COMPLETED!", cwidth / 2.0 - 150, cheight / 2.0)
        canvas.fill(255, 255, 255)
        canvas.text(s"Final Score: $score / 50 Points", cwidth / 2.0 - 75, cheight / 2.0 + 35)
        canvas.text("Click anywhere to return to Sport Menu.", cwidth / 2.0 - 110, cheight / 2.0 + 75)

    } 
    else if (gameState == "LOST") {
        canvas.fill(255, 50, 50)
        canvas.text("OOPS! WRONG ANSWER.", cwidth / 2.0 - 90, cheight / 2.0)
        canvas.fill(255, 255, 255)
        canvas.text(s"You scored: $score Points", cwidth / 2.0 - 60, cheight / 2.0 + 35)
        canvas.text("Click anywhere to return to Sport Menu.", cwidth / 2.0 - 110, cheight / 2.0 + 75)
    }
}

// Main Animation Render Loop
animateWithCanvasDraw { canvas =>
    viewState(canvas)
}