Code Sketch


Rock, Paper, Scissors
By: Soham Dabral
Category: Programming
//https://drive.google.com/drive/u/0/folders/1r1qunl9I6zUbIYwivp5ltVm0fJOR6w46
//Download the above files and put it in your downloads folder

//An unbeatable rock, paper, scissor game
//use the buttons on the screen
//to play

//Directory
val assetsDir = "~/Downloads/"

//Intro
val intro = Picture.image(s"$assetsDir/SD.png ")
drawCentered(intro)

//Creator
val creator = Picture.text("Soham Dabral", Font("Serif", 40))
creator.setPosition(-100, -150)
creator.setPenColor(cm.grey)
draw(creator)

//Pause
pause(3)

cleari()
clearOutput()
setBackground(cm.white)
disablePanAndZoom()

//Heading
val Rock = Picture.text("Rock,", Font("Arial", 50))
Rock.setPosition(-225, 200)
Rock.setPenColor(ColorMaker.hsl(22, 1.00, 0.50))
draw(Rock)

val Paper = Picture.text("Paper,", Font("Arial", 50))
Paper.setPosition(-80, 200)
Paper.setPenColor(ColorMaker.hsl(204, 1.00, 0.50))
draw(Paper)

val Scissor = Picture.text("Scissor", Font("Arial", 50))
Scissor.setPosition(80, 200)
Scissor.setPenColor(cm.green)
draw(Scissor)

//Box color
val boxClr = cm.linearGradient(0, 40, ColorMaker.hsl(0, 0.00, 0.73), 0, 0, cm.black, false)

//Box
val box = Picture.rectangle(500, 50).withPenColor(noColor).withPenThickness(1).withOpacity(0.75)
val play = Picture.text("Play", Font("Cambria", 40))
play.setPenColor(cm.black)
val playButton = picStack(
    box.thatsTranslated(-250, -50),
    play.thatsTranslated(-50, 0),
)
playButton.setFillColor(boxClr)
drawCentered(playButton)

playButton.onMouseClick { (x, y) =>
    Rock.invisible()
    Paper.invisible()
    Scissor.invisible()
    playButton.invisible()
    //You text
    val you = Picture.text("You", Font("Serif", 30))
    you.setPosition(-250, 200)
    you.setPenColor(cm.green)
    draw(you)
    //Computer text
    val computer = Picture.text("Computer", Font("Serif", 30))
    computer.setPosition(150, 200)
    computer.setPenColor(cm.blue)
    draw(computer)

    //Rock button
    val button = Picture.rectangle(175, 100)
    val rock = Picture.text("Rock", Font("Serif", 40))
    val rockButton = picStack(
        button,
        rock.thatsTranslated(40, 80)
    )
    rockButton.setPosition(-219 * 1.4, -200)
    rockButton.setFillColor(cm.black)
    rockButton.setPenColor(cm.white)
    draw(rockButton)

    //Paper button
    val paper = Picture.text("Paper", Font("Serif", 40))
    val paperButton = picStack(
        button,
        paper.thatsTranslated(40, 80)
    )
    paperButton.setPosition(-219 / 2, -200)
    paperButton.setFillColor(cm.black)
    paperButton.setPenColor(cm.white)
    draw(paperButton)

    //Scissor button
    val scissor = Picture.text("Scissor", Font("Serif", 40))
    val scissorButton = picStack(
        button,
        scissor.thatsTranslated(30, 80)
    )
    scissorButton.setPosition(219 / 2, -200)
    scissorButton.setFillColor(cm.black)
    scissorButton.setPenColor(cm.white)
    draw(scissorButton)

    //Images
    val rockImg = Picture.image(s"$assetsDir/Rock.png")
    val paperImg = Picture.image(s"$assetsDir/Paper.png")
    val scissorImg = Picture.image(s"$assetsDir/Scissor.png")

    //Main game
    rockButton.onMouseClick { (x, y) =>
        //If you plays rock
        rockImg.setPosition(-300, -100)
        rockImg.setScale(1.5)
        draw(rockImg)

        //Computer plays paper
        paperImg.setPosition(100, -50)
        draw(paperImg)

        //Message
        drawCenteredMessage("You lose :(", red, 40)
    }

    paperButton.onMouseClick { (x, y) =>
        //If you plays paper
        paperImg.setPosition(-300, -50)
        draw(paperImg)

        //Computer plays scissor
        scissorImg.setPosition(300, 100)
        scissorImg.setScale(1.25)
        scissorImg.setRotation(180)
        draw(scissorImg)

        //Message
        drawCenteredMessage("You lose :(", red, 40)
    }

    scissorButton.onMouseClick { (x, y) =>
        //If you plays scissor
        scissorImg.setPosition(-300, 0)
        scissorImg.setScale(1.25)
        draw(scissorImg)

        //Computer plays rock
        rockImg.setPosition(100, -100)
        rockImg.setScale(1.5)
        draw(rockImg)

        //Message
        drawCenteredMessage("You lose :(", red, 40)
    }
}