Kojo Wiki

docs for Kojo

View source on GitHub

Hunted 2 - increasing the number of hunters without any code change

This activity has the following desired goals:

  • Further practice with using ArrayBuffers and HashMaps in a game (M, T).
  • Learning to use ArrayBuffers and HashMaps in a game (M, T).

Step 1

Type in the following code and run it:

cleari()
drawStage(cm.darkGreen)
val cb = canvasBounds

val player = Picture.rectangle(40, 40)
player.setFillColor(cm.yellow)
player.setPenColor(black)
player.setPosition(cb.x + cb.width / 2, cb.y + 20)
draw(player)

val nh = 20
val hunters = ArrayBuffer.empty[Picture]
val huntersVel = HashMap.empty[Picture, Vector2D]
repeatFor(1 to nh) { n =>
    val pic = Picture.rectangle(40, 40)
    pic.setFillColor(cm.lightBlue)
    pic.setPenColor(black)
    pic.setPosition(cb.x + cb.width / (nh + 2) * n, cb.y + randomDouble(100, cb.height - 200))
    hunters.append(pic)
    val hv = Vector2D(random(1, 4), random(1, 4))
    huntersVel(pic) = hv
    draw(pic)
}

def gameLost() {
    stopAnimation()
    drawCenteredMessage("You Lost", red, 30)
}

val speed = 5
animate {
    repeatFor(hunters) { h =>
        var hv = huntersVel(h)
        h.translate(hv)
        if (h.collidesWith(stageBorder)) {
            hv = bouncePicOffStage(h, hv)
            huntersVel(h) = hv
        }

        if (h.collidesWith(player)) {
            gameLost()
        }
    }

    if (isKeyPressed(Kc.VK_RIGHT)) {
        player.translate(speed, 0)
    }
    if (isKeyPressed(Kc.VK_LEFT)) {
        player.translate(-speed, 0)
    }
    if (isKeyPressed(Kc.VK_UP)) {
        player.translate(0, speed)
    }
    if (isKeyPressed(Kc.VK_DOWN)) {
        player.translate(0, -speed)
    }

    if (player.collidesWith(stageBorder)) {
        gameLost()
    }
}
showGameTime(10, "You Win", black, 25)
activateCanvas()

Q1a. How does the above code move all the hunters around on the stage? How is every hunter translated in the animation loop?

Q1b. How does the above code make all the hunters buonce off the stage border? How is a hunters velocity updated after colliding with the stage border?


Exercise

Extend the above game to make the hunters bounce off each other.


Contribute Content
Copyright © Kogics Foundation