Code Sketch


yadnesh shahare using ai
By: Mhalsakant School
// --- Interactive Snake & Speed Dot Game in Kojo ---
clear()
setBackground(Color(15, 20, 35))

val numSegments = 18
val segmentRadius = 9.0

var xPos = Array.fill(numSegments)(0.0)
var yPos = Array.fill(numSegments)(0.0)

var targetX = 0.0
var targetY = 0.0

// --- ??? ????? ????????? (Game Variables) ---
var score = 0
var foodX = 0.0
var foodY = 0.0
var timeLeft = 5.0      // ? ????????? ?????
var gameOver = false

// Random ?????? Dot (Food) ???? ?????????? ??????
def respawnFood(): Unit = {
  // Double ?? Int ????? ????????? ???? ??? ????? ???? ??? (.toInt)
  val w = (canvasBounds.width - 100).toInt
  val h = (canvasBounds.height - 100).toInt
  
  foodX = random(-w / 2, w / 2).toDouble
  foodY = random(-h / 2, h / 2).toDouble
  timeLeft = 5.0 // ? ????? ????? ???
}

// ????????? ????? Dot ???? ???
respawnFood()

def drawSnakeScene(): Unit = {
  erasePictures()

  val h = canvasBounds.height

  if (!gameOver) {
    // 1. Dot (??????? ????) ????? - ??? ??????
    val food = Picture.circle(8)
    food.setFillColor(Color(255, 50, 50))
    food.setPosition(foodX, foodY)
    draw(food)

    // 2. ?????? ???? ???? ????
    for (i <- (numSegments - 1) to 0 by -1) {
      val circle = Picture.circle(segmentRadius - (i * 0.25))
      if (i == 0) {
        circle.setFillColor(Color(0, 255, 100)) // ??? ?? ???? (Neon Green)
      } else {
        circle.setFillColor(Color(34, 139, 34))  // ?????? ???? (Dark Green)
      }
      circle.setPosition(xPos(i), yPos(i))
      draw(circle)
    }

    // 3. ?????? ????
    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)

    // 4. Score ??? Timer ??????
    val infoText = Picture.text(s"Score: $score  |  Time Left: ${timeLeft.toInt}s", 18)
    infoText.setPenColor(Color(255, 215, 0))
    infoText.setPosition(0, h / 2.0 - 40.0)
    draw(infoText)

  } else {
    // --- Game Over Screen ---
    val goText = Picture.text("? GAME OVER! ?", 28)
    goText.setPenColor(Color(255, 50, 50))
    goText.setPosition(0, 20)
    draw(goText)

    val finalScore = Picture.text(s"Your Final Score: $score", 20)
    finalScore.setPenColor(Color(255, 255, 255))
    finalScore.setPosition(0, -20)
    draw(finalScore)
  }
}

// ??? ????????? ??? (Animation Loop)
animateWithState(0.0) { frame =>
  if (!gameOver) {
    // ?. ????? ??? ????
    timeLeft -= (1.0 / 60.0)
    if (timeLeft <= 0) {
      gameOver = true
    }

    // ?. ?????? ???? ???????? ?????? ??????
    xPos(0) = xPos(0) + (targetX - xPos(0)) * 0.15
    yPos(0) = yPos(0) + (targetY - yPos(0)) * 0.15

    // ?. ?????? ???? ???? ????
    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
      }
    }

    // ?. ??? Dot ??? ??????? ?? ??????
    val dxFood = xPos(0) - foodX
    val dyFood = yPos(0) - foodY
    val distToFood = Math.sqrt(dxFood * dxFood + dyFood * dyFood)

    // ?? ?????? ???? Dot ???? ?? ??????? ??? ??? ?? Point ?????!
    if (distToFood < 15.0) {
      score += 1
      respawnFood() // ???? Dot ???? ??? ??? ??? ? ????? ????? ???
    }

    drawSnakeScene()
  }
  frame + 1.0
}

// ???? ???????? (Mouse Tracking)
onMouseMove { (x, y) =>
  if (!gameOver) {
    targetX = x
    targetY = y
  }
}