Code Sketch
rudraaaaaa
Category: Art
import java.awt.Color
cleari()
originBottomLeft()
setBackground(black)
// 3D Horror Corridor - First Person Style
class HorrorGame {
var playerX = 0.0
var progress = 0.0
var ghostDistance = 100.0
var hasKey = false
var gameOver = false
var won = false
var frame = 0
def setup(surface: CanvasDraw) {
import surface._
}
def drawLoop(surface: CanvasDraw) {
import surface._
val w = cwidth.toDouble
val h = cheight.toDouble
val cx = w / 2.0
val cy = h / 2.0
// Dark background
background(5, 5, 10)
// Ceiling and floor
noStroke()
fill(22, 20, 28)
rect(0, cy, w, h / 2)
fill(35, 28, 32)
rect(0, 0, w, cy)
// Perspective corridor
stroke(90, 20, 25)
strokeWeight(3)
line(0, h, cx, cy)
line(w, h, cx, cy)
line(0, 0, cx, cy)
line(w, 0, cx, cy)
// Corridor lines
stroke(65, 35, 40)
strokeWeight(2)
var i = 1
while (i <= 7) {
val t = i.toDouble / 8.0
val y = cy + (h / 2.0) * t
line(0, y, w, y)
i += 1
}
// Door
val doorSize = 35 + progress * 2
noStroke()
fill(75, 0, 0)
rect(
cx - doorSize / 2,
cy - doorSize,
doorSize,
doorSize * 2
)
fill(180, 20, 10)
ellipse(cx + doorSize / 4, cy, 5, 5)
// Key
if (!hasKey) {
fill(255, 210, 0)
ellipse(cx + 50 - playerX, cy - 25, 16, 16)
fill(255, 240, 80)
rect(cx + 47 - playerX, cy - 40, 6, 20)
}
// Ghost movement
if (!gameOver && !won) {
ghostDistance -= 0.015
}
val ghostScale =
math.max(0.1, 1.0 - ghostDistance / 100.0)
val gx = cx + math.sin(frame * 0.04) * 65 * ghostScale
val gy = cy + 20 * ghostScale
val gr = 15 + 50 * ghostScale
// Ghost body
noStroke()
fill(130, 0, 15)
ellipse(gx, gy, gr, gr * 1.3)
// Ghost eyes
fill(230, 220, 220)
ellipse(gx - gr / 4, gy + gr / 5, gr / 7, gr / 7)
ellipse(gx + gr / 4, gy + gr / 5, gr / 7, gr / 7)
fill(0, 0, 0)
ellipse(gx - gr / 4, gy + gr / 5, gr / 14, gr / 14)
ellipse(gx + gr / 4, gy + gr / 5, gr / 14, gr / 14)
// Player movement
if (!gameOver && !won) {
if (isKeyPressed(Kc.VK_UP)) {
progress += 0.02
ghostDistance -= 0.05
}
if (isKeyPressed(Kc.VK_DOWN)) {
progress = math.max(0.0, progress - 0.02)
}
if (isKeyPressed(Kc.VK_LEFT)) {
playerX = math.max(-100.0, playerX - 3)
}
if (isKeyPressed(Kc.VK_RIGHT)) {
playerX = math.min(100.0, playerX + 3)
}
// Collect key
if (progress > 1.0) {
hasKey = true
}
// Escape
if (progress > 3.0 && hasKey) {
won = true
}
// Ghost catches player
if (ghostDistance < 0.0) {
gameOver = true
}
}
// HUD - default text size
fill(255, 255, 255)
text("DARK CORRIDOR", 20, h - 30)
fill(255, 210, 0)
if (hasKey) {
text("KEY: FOUND", 20, h - 55)
} else {
text("KEY: SEARCHING", 20, h - 55)
}
fill(220, 40, 40)
text("ESCAPE PROGRESS: " + progress.toInt, 20, h - 80)
// Ending screen
if (gameOver || won) {
fill(Color.black)
rect(cx - 180, cy - 50, 360, 100)
if (won) {
fill(50, 255, 80)
text("YOU ESCAPED!", cx - 100, cy + 10)
} else {
fill(255, 0, 0)
text("GAME OVER!", cx - 95, cy + 10)
}
}
frame += 1
}
}
val game = new HorrorGame
canvasSketch(game)