Code Sketch


sniper_0718
By: Mhalsakant School
Category: Art
cleari()

// --- Game Dimensions ---
val canvasWidth = 800 //> val canvasWidth: Int = 800
val canvasHeight = 600 //> val canvasHeight: Int = 600
val roadWidth = 340 //> val roadWidth: Int = 340

// --- Realistic Terrain Background ---
val bg = Picture.rectangle(canvasWidth, canvasHeight) //> val bg: net.kogics.kojo.picture.RectanglePic = Picture with Id: 1838583740
bg.setFillColor(Color(10, 25, 10, 255))
bg.setPenColor(noColor)
bg.setPosition(-canvasWidth / 2, -canvasHeight / 2)
bg.draw()

val road = Picture.rectangle(roadWidth, canvasHeight) //> val road: net.kogics.kojo.picture.RectanglePic = Picture with Id: 284745523
road.setFillColor(Color(35, 35, 38, 255))
road.setPenColor(noColor)
road.setPosition(-roadWidth / 2, -canvasHeight / 2)
road.draw()

// --- Road Boundary Marking Lines ---
val lineLeft = Picture.rectangle(6, canvasHeight) //> val lineLeft: net.kogics.kojo.picture.RectanglePic = Picture with Id: 2010565638
lineLeft.setFillColor(white)
lineLeft.setPenColor(noColor)
lineLeft.setPosition(-roadWidth / 2 + 5, -canvasHeight / 2)
lineLeft.draw()

val lineRight = Picture.rectangle(6, canvasHeight) //> val lineRight: net.kogics.kojo.picture.RectanglePic = Picture with Id: 1911162223
lineRight.setFillColor(white)
lineRight.setPenColor(noColor)
lineRight.setPosition(roadWidth / 2 - 11, -canvasHeight / 2)
lineRight.draw()

// --- Very Slow Speed Lane Markers ---
val dashCount = 8 //> val dashCount: Int = 8
val dashHeight = 40 //> val dashHeight: Int = 40
val dashGap = 50 //> val dashGap: Int = 50
var dashOffset = 0.0 //> var dashOffset: Double = 0.0

val yellowDashesL = Array.fill(dashCount) {
  val d = Picture.rectangle(4, dashHeight)
  d.setFillColor(Color(255, 204, 0, 255))
  d.setPenColor(noColor)
  d.draw()
  d
} //> val yellowDashesL: Array[net.kogics.kojo.picture.RectanglePic] = Array(Picture with Id: 779269587, Picture with Id: 1328775674, Picture with Id: 1703862511, Picture with Id: 1756827891, Picture with Id: 1397812251, Picture with Id: 12553178, Picture with Id: 312773306, Picture with Id: 1111765141)

val yellowDashesR = Array.fill(dashCount) {
  val d = Picture.rectangle(4, dashHeight)
  d.setFillColor(Color(255, 204, 0, 255))
  d.setPenColor(noColor)
  d.draw()
  d
} //> val yellowDashesR: Array[net.kogics.kojo.picture.RectanglePic] = Array(Picture with Id: 46819515, Picture with Id: 691217630, Picture with Id: 1209312821, Picture with Id: 2111131447, Picture with Id: 747335558, Picture with Id: 472820509, Picture with Id: 1910936483, Picture with Id: 639485535)

def updateDashes(): Unit = {
  dashOffset = (dashOffset - 8) % (dashHeight + dashGap)
  for (i <- 0 until dashCount) {
    val yPos = (canvasHeight / 2) - (i * (dashHeight + dashGap)) + dashOffset
    yellowDashesL(i).setPosition(-5, yPos)
    yellowDashesR(i).setPosition(1, yPos)
  }
} //> def updateDashes(): Unit

// --- Night Overlay ---
val nightOverlay = Picture.rectangle(canvasWidth, canvasHeight) //> val nightOverlay: net.kogics.kojo.picture.RectanglePic = Picture with Id: 192190548
nightOverlay.setFillColor(Color(0, 0, 0, 160))
nightOverlay.setPenColor(noColor)
nightOverlay.setPosition(-canvasWidth / 2, -canvasHeight / 2)
nightOverlay.draw()

// --- Realistic Truck From Behind (Drawn First so cars stay on top) ---
val truckBody = Picture.rectangle(110, 160) //> val truckBody: net.kogics.kojo.picture.RectanglePic = Picture with Id: 1545682705
truckBody.setFillColor(Color(70, 70, 80, 255))
truckBody.setPenColor(black)

val truckCab = Picture.rectangle(100, 50) //> val truckCab: net.kogics.kojo.picture.RectanglePic = Picture with Id: 2049565767
truckCab.setFillColor(Color(180, 20, 20, 255))
truckCab.setPenColor(black)

truckBody.draw()
truckCab.draw()

// --- Player Fortuner 1 (Left Car) ---
val playerBody = Picture.rectangle(52, 95) //> val playerBody: net.kogics.kojo.picture.RectanglePic = Picture with Id: 2075272710
playerBody.setFillColor(Color(245, 245, 250, 255))
playerBody.setPenColor(black)

val playerRoof = Picture.rectangle(46, 34) //> val playerRoof: net.kogics.kojo.picture.RectanglePic = Picture with Id: 902290382
playerRoof.setFillColor(Color(230, 230, 235, 255))
playerRoof.setPenColor(gray)

val playerGlass = Picture.rectangle(44, 20) //> val playerGlass: net.kogics.kojo.picture.RectanglePic = Picture with Id: 1943102626
playerGlass.setFillColor(Color(20, 40, 60, 255))
playerGlass.setPenColor(noColor)

val playerTailL = Picture.rectangle(8, 6) //> val playerTailL: net.kogics.kojo.picture.RectanglePic = Picture with Id: 1338671747
playerTailL.setFillColor(Color(220, 20, 20, 255))
playerTailL.setPenColor(noColor)

val playerTailR = Picture.rectangle(8, 6) //> val playerTailR: net.kogics.kojo.picture.RectanglePic = Picture with Id: 429393734
playerTailR.setFillColor(Color(220, 20, 20, 255))
playerTailR.setPenColor(noColor)

val playerHeadL = Picture.rectangle(9, 6) //> val playerHeadL: net.kogics.kojo.picture.RectanglePic = Picture with Id: 333110704
playerHeadL.setFillColor(gray)
playerHeadL.setPenColor(noColor)

val playerHeadR = Picture.rectangle(9, 6) //> val playerHeadR: net.kogics.kojo.picture.RectanglePic = Picture with Id: 1162947835
playerHeadR.setFillColor(gray)
playerHeadR.setPenColor(noColor)

val beamL = Picture.fromPath { p =>
  p.moveTo(0, 0)
  p.lineTo(-45, 170)
  p.lineTo(20, 170)
  p.lineTo(9, 0)
} //> val beamL: net.kogics.kojo.picture.PathPic = Picture with Id: 2024287564
beamL.setFillColor(Color(245, 245, 255, 140))
beamL.setPenColor(noColor)

val beamR = Picture.fromPath { p =>
  p.moveTo(0, 0)
  p.lineTo(-20, 170)
  p.lineTo(45, 170)
  p.lineTo(9, 0)
} //> val beamR: net.kogics.kojo.picture.PathPic = Picture with Id: 1977370034
beamR.setFillColor(Color(245, 245, 255, 140))
beamR.setPenColor(noColor)

playerBody.draw()
playerRoof.draw()
playerGlass.draw()
playerTailL.draw()
playerTailR.draw()
playerHeadL.draw()
playerHeadR.draw()

// --- Player Fortuner 2 (Right Car - Companion, Close) ---
val car2Body = Picture.rectangle(52, 95) //> val car2Body: net.kogics.kojo.picture.RectanglePic = Picture with Id: 2060548338
car2Body.setFillColor(Color(50, 150, 250, 255))
car2Body.setPenColor(black)

val car2Roof = Picture.rectangle(46, 34) //> val car2Roof: net.kogics.kojo.picture.RectanglePic = Picture with Id: 1546122645
car2Roof.setFillColor(Color(40, 130, 230, 255))
car2Roof.setPenColor(gray)

val car2Glass = Picture.rectangle(44, 20) //> val car2Glass: net.kogics.kojo.picture.RectanglePic = Picture with Id: 1449427227
car2Glass.setFillColor(Color(20, 40, 60, 255))
car2Glass.setPenColor(noColor)

val car2TailL = Picture.rectangle(8, 6) //> val car2TailL: net.kogics.kojo.picture.RectanglePic = Picture with Id: 1520516773
car2TailL.setFillColor(Color(220, 20, 20, 255))
car2TailL.setPenColor(noColor)

val car2TailR = Picture.rectangle(8, 6) //> val car2TailR: net.kogics.kojo.picture.RectanglePic = Picture with Id: 675971789
car2TailR.setFillColor(Color(220, 20, 20, 255))
car2TailR.setPenColor(noColor)

val car2HeadL = Picture.rectangle(9, 6) //> val car2HeadL: net.kogics.kojo.picture.RectanglePic = Picture with Id: 345334846
car2HeadL.setFillColor(gray)
car2HeadL.setPenColor(noColor)

val car2HeadR = Picture.rectangle(9, 6) //> val car2HeadR: net.kogics.kojo.picture.RectanglePic = Picture with Id: 312375718
car2HeadR.setFillColor(gray)
car2HeadR.setPenColor(noColor)

val beam2L = Picture.fromPath { p =>
  p.moveTo(0, 0)
  p.lineTo(-45, 170)
  p.lineTo(20, 170)
  p.lineTo(9, 0)
} //> val beam2L: net.kogics.kojo.picture.PathPic = Picture with Id: 1850970617
beam2L.setFillColor(Color(245, 245, 255, 140))
beam2L.setPenColor(noColor)

val beam2R = Picture.fromPath { p =>
  p.moveTo(0, 0)
  p.lineTo(-20, 170)
  p.lineTo(45, 170)
  p.lineTo(9, 0)
} //> val beam2R: net.kogics.kojo.picture.PathPic = Picture with Id: 388073728
beam2R.setFillColor(Color(245, 245, 255, 140))
beam2R.setPenColor(noColor)

car2Body.draw()
car2Roof.draw()
car2Glass.draw()
car2TailL.draw()
car2TailR.draw()
car2HeadL.draw()
car2HeadR.draw()

// --- Movement Variables ---
var carX = -55.0 //> var carX: Double = -55.0
var carY = -220.0 //> var carY: Double = -220.0
var truckY = -450.0 //> var truckY: Double = -450.0
var truckSpeed = 4.0 //> var truckSpeed: Double = 4.0
var isCrashed = false //> var isCrashed: Boolean = false
val carSteerSpeed = 12.0 //> val carSteerSpeed: Double = 12.0
var isLightActive = false //> var isLightActive: Boolean = false

def updatePlayerPos(): Unit = {
  // Car 1 (Left)
  playerBody.setPosition(carX, carY)
  playerRoof.setPosition(carX + 3, carY + 28)
  playerGlass.setPosition(carX + 4, carY + 58)
  playerTailL.setPosition(carX + 2, carY + 2)
  playerTailR.setPosition(carX + 42, carY + 2)
  playerHeadL.setPosition(carX + 2, carY + 88)
  playerHeadR.setPosition(carX + 41, carY + 88)

  // Car 2 (Right)
  val car2X = carX + 54
  car2Body.setPosition(car2X, carY)
  car2Roof.setPosition(car2X + 3, carY + 28)
  car2Glass.setPosition(car2X + 4, carY + 58)
  car2TailL.setPosition(car2X + 2, carY + 2)
  car2TailR.setPosition(car2X + 42, carY + 2)
  car2HeadL.setPosition(car2X + 2, carY + 88)
  car2HeadR.setPosition(car2X + 41, carY + 88)

  if (isLightActive && !isCrashed) {
    beamL.setPosition(carX + 2, carY + 94)
    beamR.setPosition(carX + 41, carY + 94)
    beam2L.setPosition(car2X + 2, carY + 94)
    beam2R.setPosition(car2X + 41, carY + 94)
  }
} //> def updatePlayerPos(): Unit

def updateTruckPos(): Unit = {
  val truckX = carX - 3
  truckBody.setPosition(truckX, truckY)
  truckCab.setPosition(truckX + 5, truckY + 105)
} //> def updateTruckPos(): Unit

updatePlayerPos()
updateTruckPos()

// --- HUD / Score Board ---
var score = 0 //> var score: Int = 0
var frameCounter = 0 //> var frameCounter: Int = 0

val scoreText = Picture.text(s"SPEED: 40 KM/H  |  SCORE: $score", 18) //> val scoreText: net.kogics.kojo.picture.TextPic = TextPic (Id: 1575167457)
scoreText.setFillColor(white)
scoreText.setPosition(-canvasWidth / 2 + 20, canvasHeight / 2 - 40)
scoreText.draw()

val helpText = Picture.text("Steer: Left/Right Arrow | Watch out for the Truck!", 15) //> val helpText: net.kogics.kojo.picture.TextPic = TextPic (Id: 1034529341)
helpText.setFillColor(Color(0, 230, 255, 255))
helpText.setPosition(-canvasWidth / 2 + 20, -canvasHeight / 2 + 20)
helpText.draw()

val blastText = Picture.text("nashibaat je asta tech hota,shevti nashib", 20) //> val blastText: net.kogics.kojo.picture.TextPic = TextPic (Id: 1673726383)
blastText.setFillColor(Color(255, 50, 50, 255))
blastText.setPosition(-220, 50)

// --- Game Loop ---
animate {
  if (!isCrashed) {
    updateDashes()

    if (isKeyPressed(Kc.VK_LEFT)) {
      carX -= carSteerSpeed
    }
    if (isKeyPressed(Kc.VK_RIGHT)) {
      carX += carSteerSpeed
    }

    val minX = -roadWidth / 2 + 12
    val maxX = roadWidth / 2 - 106
    if (carX < minX) carX = minX
    if (carX > maxX) carX = maxX

    // Flash Dipper Light on 'A' Press
    if (isKeyPressed(Kc.VK_A)) {
      if (!isLightActive) {
        isLightActive = true
        playerHeadL.setFillColor(Color(245, 245, 255, 255))
        playerHeadR.setFillColor(Color(245, 245, 255, 255))
        car2HeadL.setFillColor(Color(245, 245, 255, 255))
        car2HeadR.setFillColor(Color(245, 245, 255, 255))
        beamL.draw()
        beamR.draw()
        beam2L.draw()
        beam2R.draw()
      }
    } else {
      if (isLightActive) {
        isLightActive = false
        playerHeadL.setFillColor(gray)
        playerHeadR.setFillColor(gray)
        car2HeadL.setFillColor(gray)
        car2HeadR.setFillColor(gray)
        beamL.erase()
        beamR.erase()
        beam2L.erase()
        beam2R.erase()
      }
    }

    // Truck approaches from behind
    truckY += truckSpeed
    
    updatePlayerPos()
    updateTruckPos()

    // Check collision: when truck hits cars, cars fly forward (upwards on screen) and blast
    if (truckY >= carY - 60) {
      isCrashed = true
      
      // Cars fly forward and turn into fiery blast colors
      carY += 120 
      updatePlayerPos()

      playerBody.setFillColor(Color(255, 100, 0, 255))
      car2Body.setFillColor(Color(255, 100, 0, 255))
      playerRoof.setFillColor(Color(200, 50, 0, 255))
      car2Roof.setFillColor(Color(200, 50, 0, 255))
      
      beamL.erase()
      beamR.erase()
      beam2L.erase()
      beam2R.erase()
      
      blastText.draw()
    }

    // Score increments over time
    frameCounter += 1
    if (frameCounter >= 40) {
      score += 1
      scoreText.update(s"SPEED: 40 KM/H  |  SCORE: $score")
      frameCounter = 0
    }
  }
} //> val res142: java.util.concurrent.Future[edu.umd.cs.piccolo.activities.PActivity] = net.kogics.kojo.util.FutureResult@151395bd