GNU/Linux ◆ xterm-256color ◆ bash 47 views

First time using Kotlin

treeGame.kts soucre:

import kotlin.random.Random

val treeLeaves = """
       ########
     ###########
    #############
    ##############
    ##############
     ############
      #########"""
val treeTrunk =
"""         ###
         ###
         ####
        ######
"""
fun setColour(colour:Int)       = print("\u001b[38;5;${colour}m\u001b[48;5;${colour}m")

fun resetColour() =     print("\u001b[37m\u001b[40m")

fun printLeaves(apples:Int) {
        var appleLeft = apples
        for (char in treeLeaves){
                if (char=='#'){
                        if (appleLeft!=0 && Random.nextInt(0,10)==0){
                                setColour(9)
                                appleLeft--
                        }else{
                                setColour(2)
                        }
                        print("#")
                } else {
                        resetColour()
                        print(char)
                }
        }
        resetColour()

        //if there is still apples left, do it again
        if (appleLeft!=0){
                print("\r")
                print("\u001b[8A")
                printLeaves(apples)
        }
        print("\n")
}

fun printSprite(sprite:String,colour:Int){
        for (char in sprite){
                if (char=='#'){
                        setColour(colour)
                } else {
                        resetColour()
                }
                print(char)
        }
        resetColour()
        print("\n")
}

val apples = Random.nextInt(0,5)

printLeaves(apples)
printSprite(treeTrunk,52)
println(apples)