// --- Interactive Snake (Saap) that follows Mouse Movement in Kojo ---
cleari()
setBackground(Color(15, 20, 35))
// Number of segments in the snake's body
val numSegments = 18
val segmentRadius = 9.0
// Initialize positions for head and body segments
var xPos = Array.fill(numSegments)(0.0)
var yPos = Array.fill(numSegments)(0.0)
// Target position following the mouse pointer
var targetX = 0.0
var targetY = 0.0
def drawSnakeScene(): Unit = {
erasePictures()
val w = canvasBounds.width
val h = canvasBounds.height
// Draw snake body segments from tail to head
for (i <- (numSegments - 1) to 0 by -1) {
val circle = Picture.circle(segmentRadius - (i * 0.25))
if (i == 0) {
// Snake Head (Bright Neon Green)
circle.setFillColor(Color(0, 255, 100))
} else {
// Snake Body (Forest/Dark Green)
circle.setFillColor(Color(34, 139, 34))
}
circle.setPosition(xPos(i), yPos(i))
draw(circle)
}
// Snake Eyes on Head
val eye1 = Picture.circle(2.5)
eye1.setFillColor(Color(255, 255, 255))
eye1.setPosition(xPos(0) - 3, yPos(0) + 4)
draw(eye1)
val eye2 = Picture.circle(2.5)
eye2.setFillColor(Color(255, 255, 255))
eye2.setPosition(xPos(0) + 3, yPos(0) + 4)
draw(eye2)
// Title / Instructions
val title = Picture.text("? Mouse sarakha saap (Snake) magun yeil! (Move Mouse) ?", 16)
title.setPenColor(Color(255, 215, 0))
title.setPosition(0, h / 2.0 - 35.0)
draw(title)
}
// Initial draw
drawSnakeScene()
// Smooth Animation Loop for snake slithering effect following the mouse
animateWithState(0.0) { frame =>
// Move head smoothly towards mouse pointer position
xPos(0) = xPos(0) + (targetX - xPos(0)) * 0.15
yPos(0) = yPos(0) + (targetY - yPos(0)) * 0.15
// Make body segments follow the segment ahead (Snake trailing logic)
for (i <- 1 until numSegments) {
val dx = xPos(i - 1) - xPos(i)
val dy = yPos(i - 1) - yPos(i)
val dist = Math.sqrt(dx * dx + dy * dy)
val spacing = 11.0
if (dist > spacing) {
xPos(i) = xPos(i - 1) - (dx / dist) * spacing
yPos(i) = yPos(i - 1) - (dy / dist) * spacing
}
}
drawSnakeScene()
frame + 1.0
}
// Mouse Move Event updates target coordinates wherever the mouse goes
onMouseMove { (x, y) =>
targetX = x
targetY = y
}