Kojo Wiki

docs for Kojo

View source on GitHub

Game refinement project 2 - Car Ride

This activity has the following desired goals:

  • Learning to understand a given game and refine it using images and sounds (M, T).
  • Practice with exporting a game as a web-app (M, T).

Steps to be followed:

  1. Understand the game below.
  2. Refine the game-play (make the other cars obstruct myCar more, allow game to continue after a collision with reduced energy, etc).
  3. Add background and character images to the game to make it more attractive.
  4. Add sounds to the game to make it more attractive.
  5. Load all assets from the web.
  6. Test the game in iKojo.
  7. Export the game as a web-app.
  8. Test on PC and mobile.

Game starter code

The code for the game is shown below. Study it to fully understand how the game works.

cleari()
originBottomLeft()
drawStage(black)

def rectanglePic(w: Int, h: Int) = Picture {
    repeat(2) {
        forward(h)
        right()
        forward(w)
        right()
    }
}

val myCar = rectanglePic(40, 80)
myCar.setPenColor(cm.darkGray)
myCar.setFillColor(cm.lightBlue)
myCar.setPosition(cwidth / 2, cheight / 2)
draw(myCar)

def otherCarMaker(x: Double) = {
    val car = rectanglePic(40, 80)
    car.setPenColor(cm.darkGray)
    car.setFillColor(cm.lightCoral)
    car.setPosition(x, cheight)
    car
}

val cars = HashSet.empty[Picture]

val otherSpeed = 8
val mySpeed = 6

timer(700) {
    val otherCar = otherCarMaker(randomDouble(0, cwidth - 40))
    draw(otherCar)
    cars.add(otherCar)
}

animate {
    repeatFor(cars) { car =>
        car.translate(0, -otherSpeed)
        if (car.collidesWith(myCar)) {
            stopAnimation()
            drawCenteredMessage("You Lose", red, 30)
        }
    }

    if (isKeyPressed(Kc.VK_UP)) {
        myCar.translate(0, mySpeed)
    }
    if (isKeyPressed(Kc.VK_DOWN)) {
        myCar.translate(0, -mySpeed)
    }
    if (isKeyPressed(Kc.VK_RIGHT)) {
        myCar.translate(mySpeed, 0)
    }
    if (isKeyPressed(Kc.VK_LEFT)) {
        myCar.translate(-mySpeed, 0)
    }
}
showGameTime(60, "You Win", green, 15)
activateCanvas()

Copyright © 2010–2021. Licensed as per Terms of Use.