Code Sketch


rudraaaaaaaaa
By: Mhalsakant School
Category: Art
cleari()
drawStage(black)

// Game variables
val cb = canvasBounds
var px = -180.0
var py = -120.0
var gx = 170.0
var gy = 120.0
var hasKey = false
var gameOver = false
var won = false
var tick = 0

// Dark room
val room = Picture.rectangle(500, 400)
room.setFillColor(black)
room.setPenColor(gray)
draw(room)

// Blood-red door
val door = Picture.rectangle(45, 75)
door.setPosition(210, 145)
door.setFillColor(red)
door.setPenColor(red)
draw(door)

// Key
val key = Picture.circle(9)
key.setPosition(-120, 100)
key.setFillColor(yellow)
key.setPenColor(yellow)
draw(key)

// Player
val player = Picture.circle(13)
player.setPosition(px, py)
player.setFillColor(cyan)
player.setPenColor(white)
draw(player)

// Ghost
val ghost = Picture.circle(20)
ghost.setPosition(gx, gy)
ghost.setFillColor(red)
ghost.setPenColor(white)
draw(ghost)

// Ghost eyes
val eye1 = Picture.circle(3)
eye1.setPosition(gx - 6, gy + 5)
eye1.setFillColor(white)
eye1.setPenColor(white)
draw(eye1)

val eye2 = Picture.circle(3)
eye2.setPosition(gx + 6, gy + 5)
eye2.setFillColor(white)
eye2.setPenColor(white)
draw(eye2)

// Instructions
val title = Picture.text("HORROR ESCAPE")
title.setPosition(0, 220)
title.setPenColor(red)
draw(title)

val message = Picture.text("Find the yellow key!")
message.setPosition(0, -220)
message.setPenColor(white)
draw(message)

// Game loop
animate {
  if (!gameOver && !won) {
    val speed = 4.0

    // Player movement
    if (isKeyPressed(Kc.VK_LEFT) && px > -235) {
      px -= speed
    }
    if (isKeyPressed(Kc.VK_RIGHT) && px < 235) {
      px += speed
    }
    if (isKeyPressed(Kc.VK_UP) && py < 185) {
      py += speed
    }
    if (isKeyPressed(Kc.VK_DOWN) && py > -185) {
      py -= speed
    }

    player.setPosition(px, py)

    // Collect key
    if (!hasKey &&
        Math.abs(px + 120) < 18 &&
        Math.abs(py - 100) < 18) {
      hasKey = true
      key.setPosition(-1000, -1000)
      message.setText("Key found! Reach the red door!")
    }

    // Ghost follows the player
    val dx = px - gx
    val dy = py - gy
    val distance = Math.sqrt(dx * dx + dy * dy)

    if (distance > 1) {
      gx += dx / distance * 1.4
      gy += dy / distance * 1.4
    }

    ghost.setPosition(gx, gy)
    eye1.setPosition(gx - 6, gy + 5)
    eye2.setPosition(gx + 6, gy + 5)

    // Ghost catches player
    if (distance < 22) {
      gameOver = true
      message.setText("GAME OVER! The ghost caught you!")
      message.setPenColor(red)
      println("GAME OVER! The ghost caught you!")
      stopAnimation()
    }

    // Escape through the door
    if (hasKey &&
        Math.abs(px - 210) < 30 &&
        Math.abs(py - 145) < 45) {
      won = true
      message.setText("YOU ESCAPED! YOU WIN!")
      message.setPenColor(green)
      println("Congratulations! You escaped!")
      stopAnimation()
    }

    // Flickering ghost effect
    tick += 1
    if (tick % 20 < 10) {
      ghost.setFillColor(red)
    } else {
      ghost.setFillColor(magenta)
    }
  }
}