Code Sketch
alok
Category: Programming
import scala.collection.mutable.ArrayBuffer
clear()
originBottomLeft()
// ============================================================
// STOP THE SECONDS - NAG PANCHAMI EDITION V1.0
// ============================================================
var globalTime = 0.0
var currentStage = 0 // 0: Welcome, 1: Instructions, 2: Game Play
// ??? ???????????
var startTime: Long = 0L
var elapsedTime: Double = 0.0
var isTimerRunning: Boolean = false
var targetTime: Double = 5.0 // ????? ??????? (???. 5.0 ?????)
var score = 0
var bestDiff: Double = 999.0
// ?????????? (????? ??????????)
case class Particle(var x: Double, var y: Double, var vx: Double, var vy: Double, var col: java.awt.Color)
val particles = ArrayBuffer.empty[Particle]
def spawnParticles(x: Double, y: Double, count: Int): Unit = {
var i = 0
while (i < count && particles.length < 200) {
val col = if (randomBoolean) Color(255, 215, 0) else Color(255, 105, 180)
particles.append(Particle(x, y, randomDouble(-2.5, 2.5), randomDouble(-2.5, 2.5), col))
i += 1
}
}
// ????? ??????? ??? ????? ???
animateWithState(0.0) { angle =>
erasePictures()
globalTime += 0.03
// ?????????? ????? ????
particles.filterInPlace(_.col.getAlpha > 0)
particles.foreach { p =>
p.x += p.vx
p.y += p.vy
}
// ?????????? (???????? ????? ???)
setBackground(Color(15, 25, 45))
if (currentStage == 0) {
// --- ????? 0: ????? ??????? ---
val title = Picture.text("STOP THE SECONDS - FESTIVAL CHALLENGE", 22)
title.setPenColor(Color(255, 215, 0))
title.setPosition(cwidth / 2.0 - 220, cheight - 120)
draw(title)
val sub = Picture.text("Stop the timer exactly at the Target Time using SPACEBAR!", 14)
sub.setPenColor(Color(200, 230, 255))
sub.setPosition(cwidth / 2.0 - 210, cheight - 160)
draw(sub)
val prompt = Picture.text("Click anywhere or Press ENTER to Continue", 14)
prompt.setPenColor(Color(255, 180, 50))
prompt.setPosition(cwidth / 2.0 - 160, 80)
draw(prompt)
} else if (currentStage == 1) {
// --- ????? 1: ???? ??? ?????? ---
val heading = Picture.text("=== GAME RULES & TARGET ===", 18)
heading.setPenColor(Color(255, 220, 100))
heading.setPosition(cwidth / 2.0 - 150, cheight - 80)
draw(heading)
val r1 = Picture.text(s"1. Target Time: $targetTime Seconds", 15)
r1.setPenColor(Color(255, 255, 255))
r1.setPosition(cwidth / 2.0 - 150, cheight - 130)
draw(r1)
val r2 = Picture.text("2. Press SPACEBAR to START the timer.", 15)
r2.setPenColor(Color(255, 255, 255))
r2.setPosition(cwidth / 2.0 - 150, cheight - 170)
draw(r2)
val r3 = Picture.text("3. Press SPACEBAR again to STOP near the target!", 15)
r3.setPenColor(Color(255, 255, 255))
r3.setPosition(cwidth / 2.0 - 150, cheight - 210)
draw(r3)
val startPrompt = Picture.text("Press ENTER or Click to START Game!", 16)
startPrompt.setPenColor(Color(50, 255, 100))
startPrompt.setPosition(cwidth / 2.0 - 160, 70)
draw(startPrompt)
} else {
// --- ????? 2: ????? ??? ???? ??????? ---
// ?? ????? ???? ???? ?? ??? ?????
if (isTimerRunning) {
val currentMillis = System.currentTimeMillis()
elapsedTime = (currentMillis - startTime) / 1000.0
}
// ??????? ????? ?????? (??????????)
val outerRing = Picture.circle(80.0)
outerRing.setPenColor(Color(255, 215, 0))
outerRing.setPenThickness(3.0)
outerRing.setPosition(cwidth / 2.0, cheight / 2.0 + 20)
draw(outerRing)
// ??????? ??? ????????? ??????
val timeDisplay = Picture.text(f"$elapsedTime%2.2f s", 32)
timeDisplay.setPenColor(if (isTimerRunning) Color(0, 255, 127) else Color(255, 100, 100))
timeDisplay.setPosition(cwidth / 2.0 - 65, cheight / 2.0 + 10)
draw(timeDisplay)
// ?????? ??? ??????
val targetText = Picture.text(s"Target: $targetTime.00 s | Score: $score", 16)
targetText.setPenColor(Color(255, 220, 100))
targetText.setPosition(50, cheight - 50)
draw(targetText)
val hintText = Picture.text("Press SPACEBAR to Start / Stop! | Press R to Reset", 14)
hintText.setPenColor(Color(200, 230, 255))
hintText.setPosition(50, cheight - 80)
draw(hintText)
}
angle + 1.0
}
// ???? ????? ????????
onMouseClick { (x, y) =>
if (currentStage == 0) {
currentStage = 1
} else if (currentStage == 1) {
currentStage = 2
}
}
// ??????? ??????? (Spacebar ???? ????? ??? ?????)
onKeyPress { k =>
if (currentStage < 2) {
if (k == '\n' || k == '\r') {
currentStage += 1
}
} else {
// ??? ???????? ??-????? ?????????
if (k == ' ') { // ???????? ????????
if (!isTimerRunning) {
// ??? ???? ????
startTime = System.currentTimeMillis()
isTimerRunning = true
} else {
// ??? ???????
isTimerRunning = false
val diff = Math.abs(elapsedTime - targetTime)
if (diff <= 0.15) {
score += 10
spawnParticles(cwidth / 2.0, cheight / 2.0, 35)
}
}
} else if (k == 'r' || k == 'R') {
// ????? ??????????
isTimerRunning = false
elapsedTime = 0.0
}
}
}
println("=== STOP THE SECONDS GAME LOADED SUCCESSFULLY ===")