Code Sketch


@!$hw@ry@
By: Mhalsakant School
Category: Art



clear()
drawStage(ColorMaker.black)
val cb = canvasBounds

// Score sathi variable
var score = 0

// Player (Turtle/Rectangle) tayar karne
val player = Picture.rectangle(40, 40)
player.setFillColor(ColorMaker.green)
player.setPosition(cb.x + 150, cb.y + 150)
draw(player)

// Target (Red Box) tayar karne
val target = Picture.rectangle(30, 30)
target.setFillColor(ColorMaker.red)
target.setPosition(cb.x + 250, cb.y + 200)
draw(target)

println("Game Suru Zhala! Arrow keys vaprun player halva.")

// Game Loop (Animation ani Controls)
animate {
    // Keyboard Controls
    if (isKeyPressed(Kc.VK_UP)) {
        player.translate(0, 3)
    }
    if (isKeyPressed(Kc.VK_DOWN)) {
        player.translate(0, -3)
    }
    if (isKeyPressed(Kc.VK_LEFT)) {
        player.translate(-3, 0)
    }
    if (isKeyPressed(Kc.VK_RIGHT)) {
        player.translate(3, 0)
    }
    
    // Target sobat collision check karne (Score vadhavne)
    if (player.collidesWith(target)) {
        score = score + 1
        println("Arey Wah! Score: " + score)
        
        // Target la navin random thikani halavne
        val rx = random(cb.width.toInt - 50)
        val ry = random(cb.height.toInt - 50)
        target.setPosition(cb.x + rx, cb.y + ry)
    }
}

activateCanvas()