Code Sketch


yadnesh shahare using gemeni
By: Mhalsakant School
Category: Programming
// --- Kojo 2D Animation: King Kohli Cover Drive ---
cleari()
setBackground(Color(135, 206, 235)) // ???? ????

val groundY = -120.0

// 1. ??????? ??? ??? ?????
def drawPitch(): Unit = {
  // ????? ????? (Outfield)
  val pitch = Picture.rect(canvasBounds.width, 250)
  pitch.setFillColor(Color(46, 139, 87))
  pitch.setPenColor(noColor)
  pitch.setPosition(-canvasBounds.width/2, groundY - 250)
  draw(pitch)

  // ??? ????? (Cricket Pitch)
  val pitchStrip = Picture.rect(380, 70)
  pitchStrip.setFillColor(Color(210, 180, 140))
  pitchStrip.setPenColor(noColor)
  pitchStrip.setPosition(-190, groundY)
  draw(pitchStrip)

  // ???????? (Stumps)
  for (i <- 0 to 2) {
    val stump = Picture.rect(4, 38)
    stump.setFillColor(Color(245, 222, 179))
    stump.setPenColor(noColor)
    stump.setPosition(-140 + (i * 6), groundY + 20)
    draw(stump)
  }
}

// 2. ????? ????? (King Kohli #18 Player Model)
def drawKohli(batAngle: Double, bodyShift: Double): Unit = {
  val kX = -110.0 + bodyShift
  val kY = groundY + 20

  // ?. ?????? ??? (Legs with Pads)
  val leg1 = Picture.rect(10, 35)
  leg1.setFillColor(Color(255, 255, 255)) // White Pads
  leg1.setPenColor(Color(0, 0, 0))
  leg1.setPosition(kX - 8, kY)
  draw(leg1)

  val leg2 = Picture.rect(10, 35)
  leg2.setFillColor(Color(255, 255, 255))
  leg2.setPenColor(Color(0, 0, 0))
  leg2.setPosition(kX + 4, kY)
  draw(leg2)

  // ?. ??? ?????? ????? (Blue Jersey No. 18)
  val jersey = Picture.rect(22, 42)
  jersey.setFillColor(Color(0, 102, 204)) // India Blue
  jersey.setPenColor(noColor)
  jersey.setPosition(kX - 11, kY + 32)
  draw(jersey)

  // ????? ???? ??
  val num18 = Picture.text("18", 11)
  num18.setPenColor(Color(255, 215, 0)) // Golden Text
  num18.setPosition(kX - 7, kY + 45)
  draw(num18)

  // ?. ??????? ??? ????? (Helmet & Beard)
  val helmet = Picture.circle(11)
  helmet.setFillColor(Color(0, 51, 153)) // Blue Helmet
  helmet.setPenColor(Color(0, 0, 0))
  helmet.setPosition(kX, kY + 80)
  draw(helmet)

  val beard = Picture.rect(8, 4)
  beard.setFillColor(Color(30, 20, 10)) // Beard
  beard.setPenColor(noColor)
  beard.setPosition(kX + 2, kY + 74)
  draw(beard)

  // ?. ????? ???? (Bat Swing Movement)
  val batDx = Math.cos(Math.toRadians(batAngle)) * 48
  val batDy = Math.sin(Math.toRadians(batAngle)) * 48
  val bat = Picture.line(batDx, batDy)
  bat.setPenColor(Color(139, 69, 19)) // Wooden Bat
  bat.setPenThickness(7)
  bat.setPosition(kX + 5, kY + 45)
  draw(bat)
}

// 3. ??????? ????? (Cricket Ball)
def drawBall(x: Double, y: Double): Unit = {
  val ball = Picture.circle(6)
  ball.setFillColor(Color(220, 20, 60)) // Red Leather Ball
  ball.setPenColor(Color(255, 255, 255))
  ball.setPenThickness(1)
  ball.setPosition(x, y)
  draw(ball)
}

// ????????? ???????????
var ballX = 180.0
var ballY = groundY + 40.0
var ballVx = -6.0
var ballVy = 0.0

var batAngle = -60.0
var bodyShift = 0.0
var isShotHit = false

// ????? ????????? ???
animateWithState(0) { step =>
  erasePictures()

  // ????? ? ???????? ?????
  drawPitch()

  if (!isShotHit) {
    // ????? ?????????? ????
    ballX += ballVx

    // ??? ????????? ?????? ???? (Impact Point)
    if (ballX <= -90.0) {
      isShotHit = true
      batAngle = 30.0    // ????? ????-???? (Cover Drive Swing)
      bodyShift = 8.0    // ?????-????? ?????
      ballVx = 12.0      // ??? ????????? ??????? ???
      ballVy = 2.5
    }
  } else {
    // ??? ??????????? ????? ??????????? ????
    ballX += ballVx
    ballY += ballVy
  }

  // ?????? ??? ????? ??????
  drawKohli(if (isShotHit) 25.0 else batAngle, bodyShift)
  drawBall(ballX, ballY)

  // ?????/??? ?????
  if (isShotHit) {
    val title = Picture.text("? KING KOHLI ?", 26)
    title.setPenColor(Color(255, 215, 0))
    title.setPosition(-110, 130)
    draw(title)

    val subTitle = Picture.text("CLASSIC COVER DRIVE! ??", 18)
    subTitle.setPenColor(Color(255, 255, 255))
    subTitle.setPosition(-125, 90)
    draw(subTitle)
  }

  step + 1
}