Code Sketch


bhimraj_editor
By: Mhalsakant School
Category: Art
cleari()
disablePanAndZoom()
setBackground(darkGray)

// Game Configuration & State Variables
var score = 0 //> var score: Int = 0
var gameRunning = true //> var gameRunning: Boolean = true
val roadWidth = 300.0 //> val roadWidth: Double = 300.0
val speed = 6.0 //> val speed: Double = 6.0

// Get Canvas Dimensions
val cb = canvasBounds //> val cb: edu.umd.cs.piccolo.util.PBounds = PBounds[x=-387.5,y=-263.5,width=775.0,height=527.0]

// Draw the Road Track
val road = Picture.rectangle(roadWidth, cb.height) //> val road: net.kogics.kojo.picture.RectanglePic = Picture with Id: 337679307
road.setFillColor(lightGray)
road.setPosition(-roadWidth / 2, cb.y)
draw(road)

// Player's Car
val car = Picture.rectangle(36, 60) //> val car: net.kogics.kojo.picture.RectanglePic = Picture with Id: 551223447
car.setFillColor(red)
var carX = 0.0 //> var carX: Double = 0.0
var carY = -150.0 //> var carY: Double = -150.0
car.setPosition(carX, carY)
draw(car)

// Incoming Obstacle Car
val obstacle = Picture.rectangle(40, 30) //> val obstacle: net.kogics.kojo.picture.RectanglePic = Picture with Id: 155396832
obstacle.setFillColor(green)
var obsX = (math.random * (roadWidth - 60)) - ((roadWidth - 60) / 2) //> var obsX: Double = 83.75928381109009
var obsY = 250.0 //> var obsY: Double = 250.0
obstacle.setPosition(obsX, obsY)
draw(obstacle)

// Scoreboard Text
var scoreText = Picture.text(s"Score: $score") //> var scoreText: net.kogics.kojo.picture.TextPic = TextPic (Id: 1888941798)
scoreText.setPenColor(white)
scoreText.setPosition(-180, 200)
draw(scoreText)

// Main Animation Loop and Keyboard Controls
animate {
  if (gameRunning) {
    // Steer Left / Right using Arrow Keys
    if (isKeyPressed(Kc.VK_LEFT) && carX > -roadWidth / 2 + 18) {
      carX -= 6.0
    }
    if (isKeyPressed(Kc.VK_RIGHT) && carX < roadWidth / 2 - 18) {
      carX += 6.0
    }
    car.setPosition(carX, carY)

    // Move Obstacle Downwards to Simulate Motion
    obsY -= speed
    if (obsY < cb.y - 50) {
      obsY = cb.y + cb.height + 50
      obsX = (math.random * (roadWidth - 60)) - ((roadWidth - 60) / 2)
      score += 1
      
      // Update Score Display
      scoreText.erase()
      scoreText = Picture.text(s"Score: $score")
      scoreText.setPenColor(white)
      scoreText.setPosition(-180, 200)
      draw(scoreText)
    }
    obstacle.setPosition(obsX, obsY)

    // Collision Detection between Player Car and Obstacle
    if (car.collidesWith(obstacle)) {
      gameRunning = false
      val gameOverText = Picture.text(s"GAME OUT! Final Score: $score")
      gameOverText.setPenColor(yellow)
      gameOverText.setPosition(-140, 0)
      draw(gameOverText)
      stopAnimation()
    }
  }
} //> val res15: java.util.concurrent.Future[edu.umd.cs.piccolo.activities.PActivity] = net.kogics.kojo.util.FutureResult@711b2359

// Focus the canvas so keyboard events register instantly
activateCanvas()
println("Game Ready! Use your Left and Right Arrow Keys to drive.")