Code Sketch


yoiii
By: Mhalsakant School
Category: Programming
// =====================================================
//      ? TURTLE DANGER PUZZLE PRO
// =====================================================
// TIMER + SCORE + DANGER SOUND + CHECKPOINT
// START + GOAL + DEAD ENDS + RESET + WIN
// NO CARS
// =====================================================

val fdStep = 50
val fdStep2 = 10
val rtStep = 90
val rtStep2 = 10

val bgColor = black

clear()
clearOutput()
beamsOn()

val width = canvasBounds.width
val height = canvasBounds.height

setBackground(bgColor)
setSpeed(fast)


// =====================================================
// ? SOUND
// =====================================================

val soundDir =
    "https://kojofiles.netlify.app/hunted7"

preloadMp3(
    s"$soundDir/DrumBeats.mp3"
)

preloadMp3(
    s"$soundDir/Cave.mp3"
)


// =====================================================
// ? GAME VARIABLES
// =====================================================

var moves = 0
var seconds = 0
var score = 1000

var started = false
var finished = false

var wrongMoves = 0
var checkpoints = 0


// =====================================================
// ? CORRECT PATH
// =====================================================

val path = Seq(
    "F","F","R","F","F","L",
    "F","R","F","F","R","F",
    "L","F","F","L","F","R",
    "F","F","R","F","L","F",
    "R","F","F","L","F","R",
    "F","F","L","F"
)


// =====================================================
// ? DRAW PUZZLE
// =====================================================

def drawPuzzle() {

    // MAIN BLUE PATH

    setPenColor(blue)
    setPenThickness(8)

    setPosition(-400, -250)
    setHeading(0)

    forward(100)
    right(90)

    forward(100)
    left(90)

    forward(100)
    right(90)

    forward(100)
    forward(100)
    left(90)

    forward(100)
    right(90)

    forward(100)
    forward(100)
    right(90)

    forward(100)
    left(90)

    forward(100)
    forward(100)
    left(90)

    forward(100)
    right(90)

    forward(100)
    forward(100)
    right(90)

    forward(100)
    left(90)

    forward(100)
    right(90)

    forward(100)
    forward(100)
    left(90)

    forward(100)

    // RED GOAL

    setPenColor(red)
    setPenThickness(18)

    forward(40)

    // GREEN START

    setPenColor(green)
    setPenThickness(18)

    setPosition(-400, -250)
    setHeading(0)

    forward(40)

    // DEAD END 1

    setPenColor(gray)
    setPenThickness(5)

    setPosition(-300, -250)
    setHeading(90)
    forward(70)

    // DEAD END 2

    setPosition(-200, -150)
    setHeading(270)
    forward(70)

    // DEAD END 3

    setPosition(-50, -150)
    setHeading(90)
    forward(70)

    // DEAD END 4

    setPosition(100, -50)
    setHeading(270)
    forward(70)

    // DEAD END 5

    setPosition(200, 50)
    setHeading(90)
    forward(70)

    // DEAD END 6

    setPosition(300, 100)
    setHeading(270)
    forward(70)

    // EXTRA DANGER PATHS

    setPenColor(gray)
    setPenThickness(4)

    setPosition(-350, -150)
    setHeading(0)
    forward(60)
    right(90)
    forward(40)

    setPosition(-100, -50)
    setHeading(0)
    forward(60)
    left(90)
    forward(40)

    setPosition(100, 50)
    setHeading(180)
    forward(60)
    right(90)
    forward(40)

    // RETURN TURTLE TO START

    setPenColor(purple)
    setPenThickness(5)

    setPosition(-400, -250)
    setHeading(0)
}


// =====================================================
// ? DANGER SOUND
// =====================================================

def dangerSound() {

    if (!finished) {

        playMp3(
            s"$soundDir/DrumBeats.mp3"
        )

        println("")
        println("??? DANGER! ???")
        println("?? TIME IS RUNNING!")
        println("")
    }
}


// =====================================================
// ? CHECKPOINT
// =====================================================

def checkpointSound() {

    playMp3(
        s"$soundDir/DrumBeats.mp3"
    )

    println("")
    println("================================")
    println("        ? CHECKPOINT!")
    println("================================")
    println("")
}


// =====================================================
// ? WRONG MOVE
// =====================================================

def wrongMove() {

    wrongMoves = wrongMoves + 1

    score = score - 50

    if (score < 0) {
        score = 0
    }

    playMp3(
        s"$soundDir/DrumBeats.mp3"
    )

    println("")
    println("================================")
    println("        ? WRONG MOVE!")
    println("================================")
    println("")
    println("?? DEAD END / WRONG DIRECTION")
    println("? -50 SCORE")
    println("")
    println(
        "? SCORE = " +
        score
    )
    println("")
}


// =====================================================
// ? SCORE
// =====================================================

def updateScore() {

    score =
        1000 -
        (moves * 8) -
        (seconds * 3) -
        (wrongMoves * 50)

    if (score < 0) {
        score = 0
    }
}


// =====================================================
// ? STATUS
// =====================================================

def showStatus() {

    updateScore()

    println(
        "? MOVES = " +
        moves +
        " | ? TIME = " +
        seconds +
        " | ? SCORE = " +
        score
    )
}


// =====================================================
// ? WIN
// =====================================================

def winGame() {

    finished = true
    started = false

    stopMp3()

    println("")
    println("")
    println("****************************************")
    println("*                                      *")
    println("*          ? YOU WIN! ?              *")
    println("*                                      *")
    println("****************************************")
    println("")
    println("? GOAL REACHED!")
    println("")
    println("? MOVES = " + moves)
    println("? TIME = " + seconds + " sec")
    println("? WRONG MOVES = " + wrongMoves)
    println("? FINAL SCORE = " + score)
    println("")

    if (wrongMoves == 0) {

        println("? PERFECT RUN!")
        println("? ZERO WRONG MOVES!")
    }

    println("")
    println("? AMAZING!")
    println("****************************************")
}


// =====================================================
// ? FORWARD
// =====================================================

def moveForward() {

    if (!finished) {

        started = true

        forward(fdStep)

        moves = moves + 1

        showStatus()

        if (moves % 5 == 0) {

            checkpoints = checkpoints + 1

            checkpointSound()
        }

        if (moves >= path.length) {

            winGame()
        }
    }
}


// =====================================================
// ? RIGHT
// =====================================================

def moveRight() {

    if (!finished) {

        started = true

        right(rtStep)

        moves = moves + 1

        showStatus()

        if (moves % 5 == 0) {

            checkpoints = checkpoints + 1

            checkpointSound()
        }

        if (moves >= path.length) {

            winGame()
        }
    }
}


// =====================================================
// ? LEFT
// =====================================================

def moveLeft() {

    if (!finished) {

        started = true

        left(rtStep)

        moves = moves + 1

        showStatus()

        if (moves % 5 == 0) {

            checkpoints = checkpoints + 1

            checkpointSound()
        }

        if (moves >= path.length) {

            winGame()
        }
    }
}


// =====================================================
// ? RESET
// =====================================================

def resetGame() {

    stopMp3()

    clear()
    clearOutput()
    beamsOn()

    setBackground(bgColor)
    setSpeed(fast)

    moves = 0
    seconds = 0
    score = 1000

    started = false
    finished = false

    wrongMoves = 0
    checkpoints = 0

    drawPuzzle()
    draw(panel)

    playMp3Loop(
        s"$soundDir/Cave.mp3"
    )

    println("")
    println("======================================")
    println("       ? PUZZLE READY!")
    println("======================================")
    println("")
    println("? GREEN = START")
    println("? BLUE = CORRECT PATH")
    println("? GRAY = DANGER / DEAD END")
    println("? RED = GOAL")
    println("")
    println("? FORWARD = ????")
    println("? RIGHT = ???????")
    println("? LEFT = ???????")
    println("")
    println("? TIMER STARTS ON FIRST MOVE")
    println("? START SCORE = 1000")
    println("? DANGER SOUND = ON")
    println("======================================")
}


// =====================================================
// ? BUTTONS
// =====================================================

val leftButton =
    Picture.button("? LEFT") {
        moveLeft()
    }

val forwardButton =
    Picture.button("? FORWARD") {
        moveForward()
    }

val rightButton =
    Picture.button("RIGHT ?") {
        moveRight()
    }

val resetButton =
    Picture.button("? RESET") {
        resetGame()
    }


// =====================================================
// ? CONTROL PANEL
// =====================================================

val panel =
    trans(
        -width / 2,
        -height / 2
    ) *
    scale(1.3) ->
    VPics(

        HPics(
            leftButton,
            forwardButton,
            rightButton
        ),

        HPics(
            resetButton
        )
    )


// =====================================================
// ? TIMER
// =====================================================

timer(1000) {

    if (started && !finished) {

        seconds = seconds + 1

        updateScore()

        println(
            "? TIME = " +
            seconds +
            " sec | " +
            "? MOVES = " +
            moves +
            " | " +
            "? SCORE = " +
            score
        )

        // DANGER EVERY 10 SECONDS

        if (seconds % 10 == 0) {

            dangerSound()
        }

        // FINAL DANGER

        if (seconds >= 40) {

            println("")
            println("??? FINAL DANGER! ???")
            println("?? HURRY UP!")
            println("")
        }
    }
}


// =====================================================
// ? DRAW EVERYTHING
// =====================================================

drawPuzzle()

draw(panel)


// =====================================================
// ? BACKGROUND SOUND
// =====================================================

playMp3Loop(
    s"$soundDir/Cave.mp3"
)


// =====================================================
// ? START MESSAGE
// =====================================================

println("")
println("==============================================")
println("       ? TURTLE DANGER PUZZLE PRO")
println("==============================================")
println("")
println("? START")
println("? BLUE = SAFE PATH")
println("? GRAY = DANGER")
println("? RED = GOAL")
println("")
println("? FORWARD")
println("? LEFT")
println("? RIGHT")
println("")
println("? TIMER")
println("? SCORE")
println("? DANGER SOUND")
println("? CHECKPOINTS")
println("? WIN")
println("? RESET")
println("")
println("==============================================")
println("          GOOD LUCK! ??")
println("==============================================")