Code Sketch


alok
By: Mhalsakant School
Category: Programming
import scala.collection.mutable.ArrayBuffer

cleari()
originBottomLeft()

// ============================================================
// SOLAR SYSTEM & PLANET ADVENTURE GAMES V1.0
// ============================================================

var globalTime = 0.0
var currentStage = 0 // 0: Solar System View, 1: Planet Game View
var selectedPlanet = ""

// ???? ??? ????????? ????? (Orbits)
val planets = ArrayBuffer(
  ("Mercury", 60.0, 0.04, Color(169, 169, 169), "Catch the Space Asteroids!"),
  ("Venus", 90.0, 0.03, Color(238, 203, 173), "Collect Morning Star Glow!"),
  ("Earth", 130.0, 0.025, Color(30, 144, 255), "Save Earth from Space Debris!"),
  ("Mars", 170.0, 0.02, Color(178, 34, 34), "Red Planet Rover Jump Game!"),
  ("Jupiter", 220.0, 0.015, Color(218, 165, 32), "Giant Storm Dodge Game!"),
  ("Saturn", 270.0, 0.01, Color(238, 221, 130), "Catch the Ring Crystals!")
)

// ??? ??????????? (Game Play Variables)
var playerX = cwidth / 2.0
var playerY = 100.0
var score = 0
var targetX = randomDouble(50, cwidth - 50)
var targetY = randomDouble(150, cheight - 100)

// ????? (Obstacles / Items)
case class GameObject(var x: Double, var y: Double, var vx: Double, var vy: Double, var isGood: Boolean)
val gameObjects = ArrayBuffer.empty[GameObject]

def initMiniGame(planetName: String): Unit = {
  selectedPlanet = planetName
  score = 0
  gameObjects.clear()
  var i = 0
  while (i < 5) {
    gameObjects.append(GameObject(randomDouble(50, cwidth - 50), randomDouble(100, cheight - 100), randomDouble(-2, 2), randomDouble(-2, 2), randomBoolean))
    i += 1
  }
}

// ????? ????????? ??? ????? ???
animateWithState(0.0) { angle =>
  erasePictures()
  globalTime += 0.03

  if (currentStage == 0) {
    // --- ???? ?????? ?????? (SOLAR SYSTEM VIEW) ---
    setBackground(Color(10, 15, 30))

    // ????? (Sun)
    val sun = Picture.circle(30.0)
    sun.setFillColor(Color(255, 140, 0))
    sun.setPenColor(Color(255, 215, 0))
    sun.setPosition(cwidth / 2.0, cheight / 2.0)
    draw(sun)

    val sunText = Picture.text("SUN (Click any Planet to Play!)", 16)
    sunText.setPenColor(Color(255, 255, 0))
    sunText.setPosition(cwidth / 2.0 - 120, cheight / 2.0 - 50)
    draw(sunText)

    // ???? ???? ?????? ??? ??????
    var pIndex = 0
    while (pIndex < planets.length) {
      val (name, dist, speed, col, desc) = planets(pIndex)
      val pAngle = globalTime * speed * 10.0
      val px = (cwidth / 2.0) + math.cos(pAngle) * dist
      val py = (cheight / 2.0) + math.sin(pAngle) * dist

      // ??????? ????? (Orbit line)
      val orbit = Picture.circle(dist)
      orbit.setPenColor(Color(50, 60, 80))
      orbit.setPosition(cwidth / 2.0, cheight / 2.0)
      // draw(orbit) // ??? ??????? ????? ?????? ????

      // ???? (Planet Circle)
      val planetPic = Picture.circle(12.0)
      planetPic.setFillColor(col)
      planetPic.setPenColor(Color(255, 255, 255))
      planetPic.setPosition(px, py)
      draw(planetPic)

      // ??????? ???
      val namePic = Picture.text(name, 12)
      namePic.setPenColor(Color(200, 220, 255))
      namePic.setPosition(px - 20, py - 25)
      draw(namePic)

      pIndex += 1
    }

  } else {
    // --- ???????? ??????? ????-??? (PLANET MINI-GAME) ---
    setBackground(Color(20, 25, 45))

    // ???????? ????? (Player Movement using W, A, S, D)
    // 

    // ??????????? ????? ????
    gameObjects.foreach { obj =>
      obj.x += obj.vx
      obj.y += obj.vy
      if (obj.x < 0 || obj.x > cwidth) obj.vx *= -1
      if (obj.y < 0 || obj.y > cheight) obj.vy *= -1

      val objPic = Picture.circle(10.0)
      objPic.setFillColor(if (obj.isGood) Color(0, 255, 0) else Color(255, 0, 0))
      objPic.setPosition(obj.x, obj.y)
      draw(objPic)

      // ?????? ?????? (Collision Check)
      val dist = math.hypot(playerX - obj.x, playerY - obj.y)
      if (dist < 20.0) {
        if (obj.isGood) score += 1 else score -= 1
        obj.x = randomDouble(50, cwidth - 50)
        obj.y = randomDouble(100, cheight - 100)
      }
    }

    // ?????? ????? (Player Avatar)
    val playerPic = Picture.circle(15.0)
    playerPic.setFillColor(Color(255, 255, 0))
    playerPic.setPenColor(Color(255, 69, 0))
    playerPic.setPosition(playerX, playerY)
    draw(playerPic)

    // ?????? ??? ??????
    val infoPic = Picture.text(s"Playing on: $selectedPlanet | Score: $score (Press 'B' to go back)", 16)
    infoPic.setPenColor(Color(255, 220, 100))
    infoPic.setPosition(50, cheight - 40)
    draw(infoPic)
    
    val hintPic = Picture.text("Use W, A, S, D to Move | Green = +1, Red = -1", 14)
    hintPic.setPenColor(Color(200, 230, 255))
    hintPic.setPosition(50, cheight - 70)
    draw(hintPic)
  }

  angle + 1.0
}

// ???? ????? ???? ???? ?????? (Planet Click Selection)
onMouseClick { (x, y) =>
  if (currentStage == 0) {
    // ????? ???????? ??????????? ????? ???? ??? ??? ?? ?????
    var pIndex = 0
    while (pIndex < planets.length) {
      val (name, dist, speed, _, _) = planets(pIndex)
      val pAngle = globalTime * speed * 10.0
      val px = (cwidth / 2.0) + math.cos(pAngle) * dist
      val py = (cheight / 2.0) + math.sin(pAngle) * dist

      val distanceToClick = math.hypot(x - px, y - py)
      if (distanceToClick < 25.0) {
        currentStage = 1
        initMiniGame(name)
      }
      pIndex += 1password()
    }
  }
}

// ??????? ??????? (Keyboard Controls)
onKeyPress { k =>
  if (currentStage == 1) {
    if (k == 'd' || k == 'D') playerX += 15.0
    if (k == 'a' || k == 'A') playerX -= 15.0
    if (k == 'w' || k == 'W') playerY += 15.0
    if (k == 's' || k == 'S') playerY -= 15.0
    if (k == 'b' || k == 'B') currentStage = 0 // ????? ???? ???????? ??? ??????????
  }
}

println("=== SOLAR SYSTEM & PLANET GAMES LOADED SUCCESSFULLY ===")