Code Sketch


harshada c.
By: Mhalsakant School

cleari()
originBottomLeft()

class Sketch {

  var t = 0.0
  var robotX = 100.0
  var bob = 0.0
  var arm = 0.0

  // Required by canvasSketch
  def setup(c: CanvasDraw): Unit = {
    c.background(225, 240, 250)
  }

  // Draw background
  def drawBackground(c: CanvasDraw): Unit = {

    c.background(225, 240, 250)

    // Ground
    c.noStroke()
    c.fill(70, 160, 100)
    c.rect(0, 0, cwidth, 50)

    // Road
    c.fill(55, 65, 80)
    c.rect(0, 25, cwidth, 25)

    // Moving road markings
    c.stroke(255, 255, 255)
    c.strokeWeight(3)

    for (i <- 0 until 12) {
      val x = i * 70.0 - (t * 4.0 % 70.0)
      c.line(x, 37, x + 30, 37)
    }

    // Sun
    c.noStroke()
    c.fill(255, 205, 50)
    c.ellipse(cwidth - 65, cheight - 60, 55, 55)
  }

  // Draw robot
  def drawRobot(c: CanvasDraw): Unit = {

    val x = robotX
    val y = 145.0 + bob
    val swing = math.sin(arm) * 18.0

    // Robot shadow
    c.noStroke()
    c.fill(0, 0, 0, 40)
    c.ellipse(x, 48, 75, 12)

    // Legs
    c.stroke(180, 195, 210)
    c.strokeWeight(10)

    c.line(
      x - 15, y - 15,
      x - 15 + swing, y - 45
    )

    c.line(
      x + 15, y - 15,
      x + 15 - swing, y - 45
    )

    // Feet
    c.fill(40, 55, 75)
    c.stroke(30, 40, 50)
    c.strokeWeight(2)

    c.ellipse(x - 15 + swing, y - 49, 25, 12)
    c.ellipse(x + 15 - swing, y - 49, 25, 12)

    // Arms
    c.stroke(200, 215, 230)
    c.strokeWeight(10)

    c.line(
      x - 28, y + 30,
      x - 48, y + 8 - swing * 0.5
    )

    c.line(
      x + 28, y + 30,
      x + 48, y + 8 + swing * 0.5
    )

    // Hands
    c.fill(0, 190, 230)
    c.stroke(40, 70, 90)
    c.strokeWeight(2)

    c.ellipse(x - 48, y + 5 - swing * 0.5, 15, 15)
    c.ellipse(x + 48, y + 5 + swing * 0.5, 15, 15)

    // Body
    c.fill(210, 220, 235)
    c.stroke(40, 55, 75)
    c.strokeWeight(3)
    c.rect(x - 30, y - 15, 60, 60)

    // Chest panel
    c.fill(20, 75, 110)
    c.rect(x - 20, y + 2, 40, 28)

    // Glowing chest
    val glow = (160 + math.sin(t * 5) * 80).toInt

    c.noStroke()
    c.fill(0, 220, 255, glow)
    c.ellipse(x, y + 16, 20, 20)

    c.fill(240, 255, 255)
    c.ellipse(x, y + 16, 8, 8)

    // Neck
    c.fill(100, 120, 140)
    c.rect(x - 8, y + 43, 16, 9)

    // Head
    val hy = y + 70

    c.fill(225, 235, 245)
    c.stroke(40, 55, 75)
    c.strokeWeight(3)
    c.rect(x - 32, hy - 17, 64, 42)

    // Ears
    c.fill(0, 170, 210)
    c.ellipse(x - 35, hy + 2, 9, 17)
    c.ellipse(x + 35, hy + 2, 9, 17)

    // Blinking eyes
    val eyeH =
      if (math.sin(t * 2) > 0.94) 2.0 else 9.0

    c.fill(10, 25, 35)
    c.noStroke()
    c.ellipse(x - 13, hy + 3, 11, eyeH)
    c.ellipse(x + 13, hy + 3, 11, eyeH)

    // Eye glow
    c.fill(60, 230, 255)
    c.ellipse(x - 13, hy + 3, 4, 4)
    c.ellipse(x + 13, hy + 3, 4, 4)

    // Mouth
    c.stroke(30, 80, 100)
    c.strokeWeight(2)
    c.line(x - 7, hy - 7, x + 7, hy - 7)

    // Antenna
    c.line(x, hy + 25, x, hy + 37)

    c.noStroke()
    c.fill(255, 80, 80)
    c.ellipse(x, hy + 40, 10, 10)

    // Robot label
    c.fill(20, 55, 85)
    c.text("AI ROBOT", x - 35, y + 125)
  }

  // Main animation loop
  def drawLoop(c: CanvasDraw): Unit = {

    t += 0.05
    arm += 0.16
    bob = math.sin(t * 3) * 3

    drawBackground(c)

    // Move robot
    robotX += 1.8

    // Restart after leaving the screen
    if (robotX > cwidth + 60) {
      robotX = -60
    }

    drawRobot(c)

    // Title
    c.noStroke()
    c.fill(20, 60, 100)
    c.text("MOVING AI ROBOT", 20, cheight - 25)
  }
}

// Start animation
val sketch = new Sketch
canvasSketch(sketch)