Code Sketch


yo plzne fihghter
By: Mhalsakant School
Category: Programming

import java.awt._
import java.awt.event._
import javax.swing._
import scala.collection.mutable
import scala.util.Random

object StarfallNexus {

  def main(args: Array[String]): Unit = {
    SwingUtilities.invokeLater(new Runnable {
      override def run(): Unit = {
        val frame = new JFrame("STARFALL NEXUS - SPACE SURVIVAL")
        val game = new GamePanel
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
        frame.setContentPane(game)
        frame.setSize(1250, 820)
        frame.setMinimumSize(new Dimension(850, 600))
        frame.setLocationRelativeTo(null)
        frame.setVisible(true)
        game.requestFocusInWindow()
      }
    })
  }

  private class Star(var x: Double, var y: Double, var z: Double)

  private class Asteroid(
    var x: Double,
    var y: Double,
    val radius: Double,
    var speed: Double,
    val seed: Int
  ) {
    var angle = 0.0
    var spin = 0.5
  }

  private class Bullet(var x: Double, var y: Double)

  private class Particle(
    var x: Double,
    var y: Double,
    var vx: Double,
    var vy: Double,
    var life: Double,
    val maxLife: Double,
    val size: Double
  )

  private class GamePanel extends JPanel {

    private val MENU = 0
    private val PLAYING = 1
    private val PAUSED = 2
    private val GAMEOVER = 3

    private var state = MENU

    private val random = new Random()

    private val keys = mutable.Set[Int]()

    private val stars = mutable.ArrayBuffer[Star]()
    private val asteroids = mutable.ArrayBuffer[Asteroid]()
    private val bullets = mutable.ArrayBuffer[Bullet]()
    private val particles = mutable.ArrayBuffer[Particle]()

    private var playerX = 0.0
    private var playerY = 0.0

    private var health = 3
    private var score = 0
    private var bestScore = 0

    private var animation = 0.0
    private var spawnClock = 0.0
    private var fireClock = 0.0
    private var invincible = 0.0
    private var boostTime = 0.0
    private var boostCooldown = 0.0

    private var lastTime = System.nanoTime()

    setPreferredSize(new Dimension(1250, 820))
    setBackground(Color.BLACK)
    setFocusable(true)
    setDoubleBuffered(true)

    createStars(210)

    private def createStars(count: Int): Unit = {
      stars.clear()
      var i = 0
      while (i < count) {
        stars += new Star(
          random.nextDouble() * 2.0 - 1.0,
          random.nextDouble() * 2.0 - 1.0,
          0.12 + random.nextDouble() * 0.88
        )
        i += 1
      }
    }

    private val timer = new Timer(16, new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = {
        updateGame()
        repaint()
      }
    })

    timer.start()

    addKeyListener(new KeyAdapter {

      override def keyPressed(e: KeyEvent): Unit = {

        val code = e.getKeyCode
        val firstPress = !keys.contains(code)
        keys += code

        if (!firstPress) return

        if (code == KeyEvent.VK_ENTER) {
          if (state == MENU || state == GAMEOVER) startGame()
          else if (state == PAUSED) state = PLAYING
        }

        if (code == KeyEvent.VK_ESCAPE || code == KeyEvent.VK_P) {
          if (state == PLAYING) state = PAUSED
          else if (state == PAUSED) state = PLAYING
        }

        if (code == KeyEvent.VK_SHIFT &&
            state == PLAYING &&
            boostCooldown <= 0.0) {
          boostTime = 0.45
          boostCooldown = 2.0
        }

        if (code == KeyEvent.VK_R && state == GAMEOVER) {
          startGame()
        }
      }

      override def keyReleased(e: KeyEvent): Unit = {
        keys -= e.getKeyCode
      }
    })

    addFocusListener(new FocusAdapter {
      override def focusLost(e: FocusEvent): Unit = keys.clear()
    })

    addMouseListener(new MouseAdapter {
      override def mousePressed(e: MouseEvent): Unit = {
        requestFocusInWindow()

        val x = e.getX
        val y = e.getY

        val bx = getWidth / 2 - 145
        val by = getHeight / 2 + 65

        if (x >= bx && x <= bx + 290 &&
            y >= by && y <= by + 62) {

          if (state == MENU || state == GAMEOVER) startGame()
          else if (state == PAUSED) state = PLAYING
        }
      }
    })

    private def startGame(): Unit = {
      playerX = getWidth / 2.0
      playerY = getHeight - 115.0

      health = 3
      score = 0

      asteroids.clear()
      bullets.clear()
      particles.clear()

      spawnClock = 0.5
      fireClock = 0.0
      invincible = 1.5
      boostTime = 0.0
      boostCooldown = 0.0

      keys.clear()
      lastTime = System.nanoTime()
      state = PLAYING

      requestFocusInWindow()
    }

    private def distance(
      x1: Double,
      y1: Double,
      x2: Double,
      y2: Double
    ): Double = {
      math.hypot(x2 - x1, y2 - y1)
    }

    // ---------------- BACKGROUND ----------------

    private def updateStars(dt: Double): Unit = {

      stars.foreach { s =>

        val oldZ = s.z

        s.z -= dt * 0.30

        if (s.z <= 0.06) {
          s.z = 1.0
          s.x = random.nextDouble() * 2.0 - 1.0
          s.y = random.nextDouble() * 2.0 - 1.0
        }
      }
    }

    private def drawBackground(g: Graphics2D): Unit = {

      g.setPaint(new GradientPaint(
        0, 0, new Color(5, 8, 27),
        getWidth, getHeight, new Color(19, 8, 39)
      ))

      g.fillRect(0, 0, getWidth, getHeight)

      drawNebula(g)

      stars.foreach { s =>

        val cx = getWidth / 2.0
        val cy = getHeight / 2.0

        val sx = cx + s.x * getWidth * 0.48 / s.z
        val sy = cy + s.y * getHeight * 0.48 / s.z

        val size = math.max(1, (3.0 * (1.05 - s.z)).toInt)

        if (sx >= 0 && sx < getWidth &&
            sy >= 0 && sy < getHeight) {

          val brightness = (255 * (1.0 - s.z) + 45).toInt.min(255)

          g.setColor(new Color(
            brightness,
            brightness,
            255,
            220
          ))

          g.fillOval(
            sx.toInt,
            sy.toInt,
            size,
            size
          )

          val tailX = cx + s.x * getWidth * 0.48 / (s.z + 0.035)
          val tailY = cy + s.y * getHeight * 0.48 / (s.z + 0.035)

          if (state == PLAYING) {
            g.setColor(new Color(150, 180, 255, 75))
            g.drawLine(
              tailX.toInt,
              tailY.toInt,
              sx.toInt,
              sy.toInt
            )
          }
        }
      }
    }

    private def drawNebula(g: Graphics2D): Unit = {

      g.setColor(new Color(90, 25, 155, 28))
      g.fillOval(
        -100,
        120,
        getWidth / 2,
        getHeight / 2
      )

      g.setColor(new Color(15, 90, 190, 25))
      g.fillOval(
        getWidth / 2,
        180,
        getWidth / 2,
        getHeight / 2
      )

      g.setColor(new Color(190, 40, 110, 15))
      g.fillOval(
        getWidth / 4,
        getHeight / 3,
        getWidth / 2,
        getHeight / 2
      )
    }

    // ---------------- ASTEROIDS ----------------

    private def spawnAsteroid(): Unit = {

      val r = 19.0 + random.nextDouble() * 38.0

      asteroids += new Asteroid(
        random.nextDouble() * (getWidth - 100) + 50,
        -r - 5,
        r,
        155.0 + random.nextDouble() * 165.0 + score * 0.025,
        random.nextInt(10000)
      )
    }

    private def drawAsteroid(g: Graphics2D, a: Asteroid): Unit = {

      val n = 10
      val xs = new Array[Int](n)
      val ys = new Array[Int](n)

      var i = 0

      while (i < n) {

        val angle = a.angle + i * math.Pi * 2.0 / n

        val variation =
          0.78 + math.abs(math.sin(i * 12.7 + a.seed)) * 0.24

        val r = a.radius * variation

        xs(i) = (a.x + math.cos(angle) * r).toInt
        ys(i) = (a.y + math.sin(angle) * r).toInt

        i += 1
      }

      g.setColor(new Color(0, 0, 0, 95))
      g.fillOval(
        (a.x - a.radius).toInt,
        (a.y - a.radius + 8).toInt,
        (a.radius * 2).toInt,
        (a.radius * 2).toInt
      )

      g.setPaint(new GradientPaint(
        (a.x - a.radius).toFloat,
        (a.y - a.radius).toFloat,
        new Color(160, 157, 171),
        (a.x + a.radius).toFloat,
        (a.y + a.radius).toFloat,
        new Color(48, 45, 69)
      ))

      g.fillPolygon(xs, ys, n)

      g.setColor(new Color(203, 194, 207, 130))
      g.drawPolygon(xs, ys, n)

      g.setColor(new Color(33, 34, 57, 140))
      g.fillOval(
        (a.x - a.radius * 0.35).toInt,
        (a.y - a.radius * 0.1).toInt,
        (a.radius * 0.45).toInt,
        (a.radius * 0.38).toInt
      )
    }

    // ---------------- PLAYER SHIP ----------------

    private def drawShip(g: Graphics2D): Unit = {

      val x = playerX.toInt
      val y = playerY.toInt

      if (invincible > 0 && (animation * 10).toInt % 2 == 0) {
        return
      }

      val flame = 20 + math.sin(animation * 22) * 9

      // Engine glow
      g.setColor(new Color(0, 155, 255, 40))
      g.fillOval(x - 40, y + 8, 80, 65)

      // Engine fire
      val fire = new Polygon(
        Array(x - 11, x, x + 11),
        Array(y + 27, y + 27 + flame.toInt, y + 27),
        3
      )

      g.setPaint(new GradientPaint(
        x.toFloat,
        y.toFloat + 20,
        Color.WHITE,
        x.toFloat,
        y.toFloat + 60,
        new Color(30, 130, 255, 40)
      ))

      g.fillPolygon(fire)

      // Wings
      intPolygon(g,
        Array(x, x - 42, x - 26, x - 16),
        Array(y - 35, y + 22, y + 35, y + 16),
        new Color(42, 89, 157)
      )

      intPolygon(g,
        Array(x, x + 42, x + 26, x + 16),
        Array(y - 35, y + 22, y + 35, y + 16),
        new Color(42, 89, 157)
      )

      // Main ship
      val ship = new Polygon(
        Array(
          x,
          x + 20,
          x + 17,
          x + 10,
          x,
          x - 10,
          x - 17,
          x - 20
        ),
        Array(
          y - 48,
          y - 3,
          y + 26,
          y + 33,
          y + 24,
          y + 33,
          y + 26,
          y - 3
        ),
        8
      )

      g.setPaint(new GradientPaint(
        x.toFloat,
        y.toFloat - 48,
        new Color(216, 241, 255),
        x.toFloat,
        y.toFloat + 33,
        new Color(30, 86, 163)
      ))

      g.fillPolygon(ship)

      g.setColor(new Color(220, 250, 255))
      g.drawPolygon(ship)

      // Cockpit
      g.setPaint(new GradientPaint(
        x.toFloat - 8,
        y.toFloat - 29,
        new Color(150, 238, 255),
        x.toFloat + 8,
        y.toFloat,
        new Color(15, 90, 178)
      ))

      g.fillOval(x - 9, y - 28, 18, 29)

      // Wing lights
      g.setColor(new Color(50, 234, 255))
      g.fillOval(x - 30, y + 14, 6, 6)
      g.fillOval(x + 24, y + 14, 6, 6)

      // Shield
      if (invincible > 0) {
        g.setColor(new Color(90, 215, 255, 100))
        g.setStroke(new BasicStroke(2f))
        g.drawOval(x - 47, y - 57, 94, 116)
        g.setStroke(new BasicStroke(1f))
      }
    }

    private def intPolygon(
      g: Graphics2D,
      xs: Array[Int],
      ys: Array[Int],
      color: Color
    ): Unit = {
      g.setColor(color)
      g.fillPolygon(xs, ys, xs.length)
    }

    // ---------------- BULLETS ----------------

    private def shoot(): Unit = {

      bullets += new Bullet(playerX - 10, playerY - 38)
      bullets += new Bullet(playerX + 10, playerY - 38)

      addParticles(playerX, playerY - 45, 3,
        new Color(70, 220, 255))
    }

    private def drawBullets(g: Graphics2D): Unit = {

      bullets.foreach { b =>

        g.setColor(new Color(40, 170, 255, 70))
        g.fillOval(b.x.toInt - 8, b.y.toInt - 12, 16, 28)

        g.setColor(new Color(130, 243, 255))
        g.fillRoundRect(
          b.x.toInt - 2,
          b.y.toInt - 12,
          4,
          23,
          4,
          4
        )
      }
    }

    // ---------------- PARTICLES ----------------

    private def addParticles(
      x: Double,
      y: Double,
      amount: Int,
      color: Color
    ): Unit = {

      var i = 0

      while (i < amount) {

        val angle = random.nextDouble() * math.Pi * 2
        val speed = 40 + random.nextDouble() * 260

        particles += new Particle(
          x,
          y,
          math.cos(angle) * speed,
          math.sin(angle) * speed,
          0.35 + random.nextDouble() * 0.7,
          1.05,
          2 + random.nextDouble() * 6
        )

        i += 1
      }
    }

    private def drawParticles(g: Graphics2D): Unit = {

      particles.foreach { p =>

        val alpha = math.max(
          0,
          math.min(255, (255 * p.life / p.maxLife).toInt)
        )

        g.setColor(new Color(90, 210, 255, alpha))

        val size = math.max(1, p.size.toInt)

        g.fillOval(
          p.x.toInt,
          p.y.toInt,
          size,
          size
        )
      }
    }

    // ---------------- UPDATE ----------------

    private def updateGame(): Unit = {

      val now = System.nanoTime()

      val dt = math.min(
        (now - lastTime) / 1000000000.0,
        0.035
      )

      lastTime = now
      animation += dt

      updateStars(dt)

      if (state != PLAYING) return

      invincible = math.max(0.0, invincible - dt)
      boostTime = math.max(0.0, boostTime - dt)
      boostCooldown = math.max(0.0, boostCooldown - dt)

      // Player movement
      var dx = 0.0
      var dy = 0.0

      if (keys.contains(KeyEvent.VK_A) ||
          keys.contains(KeyEvent.VK_LEFT)) dx -= 1

      if (keys.contains(KeyEvent.VK_D) ||
          keys.contains(KeyEvent.VK_RIGHT)) dx += 1

      if (keys.contains(KeyEvent.VK_W) ||
          keys.contains(KeyEvent.VK_UP)) dy -= 1

      if (keys.contains(KeyEvent.VK_S) ||
          keys.contains(KeyEvent.VK_DOWN)) dy += 1

      val length = math.hypot(dx, dy)

      if (length > 0) {

        val speed = if (boostTime > 0) 800.0 else 370.0

        playerX += dx / length * speed * dt
        playerY += dy / length * speed * dt

      }

      playerX = math.max(48, math.min(getWidth - 48, playerX))
      playerY = math.max(75, math.min(getHeight - 60, playerY))

      // Shooting
      fireClock -= dt

      if (keys.contains(KeyEvent.VK_SPACE) && fireClock <= 0) {
        shoot()
        fireClock = 0.17
      }

      // Spawn
      spawnClock -= dt

      if (spawnClock <= 0) {
        spawnAsteroid()
        spawnClock = math.max(0.22, 0.90 - score * 0.0004) +
          random.nextDouble() * 0.35
      }

      // Update bullets
      var i = bullets.length - 1

      while (i >= 0) {

        val b = bullets(i)
        b.y -= 760 * dt

        if (b.y < -30) {
          bullets.remove(i)
        }

        i -= 1
      }

      // Update particles
      i = particles.length - 1

      while (i >= 0) {

        val p = particles(i)

        p.x += p.vx * dt
        p.y += p.vy * dt

        p.vx *= math.pow(0.5, dt)
        p.vy *= math.pow(0.5, dt)

        p.life -= dt

        if (p.life <= 0) particles.remove(i)

        i -= 1
      }

      // Asteroids and collision
      i = asteroids.length - 1

      while (i >= 0) {

        val a = asteroids(i)

        a.y += a.speed * dt
        a.angle += a.spin * dt

        // Bullet collision
        var hit = false
        var j = bullets.length - 1

        while (j >= 0 && !hit) {

          val b = bullets(j)

          if (distance(a.x, a.y, b.x, b.y) <
              a.radius + 7) {

            bullets.remove(j)
            hit = true

            score += 25
            addParticles(
              a.x,
              a.y,
              24,
              new Color(255, 145, 65)
            )
          }

          j -= 1
        }

        if (hit) {

          asteroids.remove(i)

        } else if (
          distance(a.x, a.y, playerX, playerY) <
          a.radius + 28
        ) {

          if (invincible <= 0) {

            health -= 1
            invincible = 1.6

            addParticles(
              playerX,
              playerY,
              35,
              new Color(100, 205, 255)
            )
          }

          asteroids.remove(i)

          if (health <= 0) {
            health = 0
            state = GAMEOVER
            bestScore = math.max(bestScore, score)
          }

        } else if (a.y > getHeight + a.radius + 30) {

          asteroids.remove(i)

        }

        i -= 1
      }

      // Survival score
      score += (dt * 2).toInt
      bestScore = math.max(bestScore, score)
    }

    // ---------------- HUD ----------------

    private def drawPanel(
      g: Graphics2D,
      x: Int,
      y: Int,
      w: Int,
      h: Int
    ): Unit = {

      g.setColor(new Color(5, 12, 31, 205))
      g.fillRoundRect(x, y, w, h, 20, 20)

      g.setColor(new Color(90, 190, 255, 110))
      g.drawRoundRect(x, y, w, h, 20, 20)
    }

    private def centered(
      g: Graphics2D,
      text: String,
      y: Int,
      font: Font,
      color: Color
    ): Unit = {

      g.setFont(font)
      g.setColor(color)

      val width = g.getFontMetrics.stringWidth(text)

      g.drawString(
        text,
        getWidth / 2 - width / 2,
        y
      )
    }

    private def drawHUD(g: Graphics2D): Unit = {

      drawPanel(g, 20, 20, 250, 125)

      g.setFont(new Font("Dialog", Font.BOLD, 19))
      g.setColor(new Color(135, 223, 255))
      g.drawString("STARFALL NEXUS", 39, 49)

      g.setFont(new Font("Dialog", Font.BOLD, 15))
      g.setColor(Color.WHITE)
      g.drawString("SCORE  " + score, 39, 82)

      g.setColor(new Color(255, 110, 122))
      g.drawString("LIVES", 39, 113)

      var i = 0

      while (i < health) {
        g.setColor(new Color(255, 80, 110))
        g.fillOval(103 + i * 25, 98, 18, 18)
        i += 1
      }

      drawPanel(g, getWidth - 210, 20, 190, 85)

      g.setFont(new Font("Dialog", Font.BOLD, 13))
      g.setColor(Color.WHITE)
      g.drawString("BOOST SYSTEM", getWidth - 190, 49)

      val ready = boostCooldown <= 0

      g.setColor(new Color(46, 45, 65))
      g.fillRoundRect(getWidth - 190, 62, 150, 13, 8, 8)

      val progress =
        if (ready) 150
        else (150.0 * (1.0 - boostCooldown / 2.0)).toInt

      g.setColor(
        if (ready) new Color(30, 220, 255)
        else new Color(128, 92, 225)
      )

      g.fillRoundRect(getWidth - 190, 62, progress, 13, 8, 8)

      if (boostTime > 0) {
        centered(
          g,
          "BOOST ACTIVE!",
          getHeight - 95,
          new Font("Dialog", Font.BOLD, 18),
          new Color(100, 235, 255)
        )
      }

      g.setFont(new Font("Dialog", Font.PLAIN, 12))
      g.setColor(new Color(211, 226, 255))
      g.drawString(
        "WASD: MOVE   SPACE: SHOOT   SHIFT: BOOST   ESC: PAUSE",
        24,
        getHeight - 18
      )
    }

    // ---------------- MENUS ----------------

    private def drawButton(
      g: Graphics2D,
      text: String,
      y: Int
    ): Unit = {

      val x = getWidth / 2 - 145

      g.setPaint(new GradientPaint(
        x.toFloat,
        y.toFloat,
        new Color(24, 197, 255),
        (x + 290).toFloat,
        (y + 62).toFloat,
        new Color(104, 63, 217)
      ))

      g.fillRoundRect(x, y, 290, 62, 22, 22)

      g.setColor(new Color(225, 250, 255, 190))
      g.drawRoundRect(x, y, 290, 62, 22, 22)

      centered(
        g,
        text,
        y + 39,
        new Font("Dialog", Font.BOLD, 19),
        Color.WHITE
      )
    }

    private def drawMenu(g: Graphics2D): Unit = {

      g.setColor(new Color(0, 0, 12, 150))
      g.fillRect(0, 0, getWidth, getHeight)

      centered(
        g,
        "STARFALL",
        getHeight / 2 - 115,
        new Font("Dialog", Font.BOLD, 68),
        new Color(95, 222, 255)
      )

      centered(
        g,
        "N E X U S",
        getHeight / 2 - 73,
        new Font("Dialog", Font.BOLD, 22),
        new Color(213, 153, 255)
      )

      centered(
        g,
        "THE GALAXY NEEDS A HERO",
        getHeight / 2 - 25,
        new Font("Dialog", Font.BOLD, 15),
        Color.WHITE
      )

      centered(
        g,
        "Survive the asteroid storm. Protect your spaceship.",
        getHeight / 2 + 13,
        new Font("Dialog", Font.PLAIN, 16),
        new Color(203, 224, 250)
      )

      drawButton(
        g,
        "START MISSION",
        getHeight / 2 + 65
      )

      centered(
        g,
        "BEST SCORE: " + bestScore,
        getHeight - 35,
        new Font("Dialog", Font.BOLD, 13),
        new Color(145, 208, 255)
      )
    }

    private def drawOverlay(
      g: Graphics2D,
      title: String,
      subtitle: String,
      button: String,
      color: Color
    ): Unit = {

      g.setColor(new Color(0, 0, 13, 190))
      g.fillRect(0, 0, getWidth, getHeight)

      centered(
        g,
        title,
        getHeight / 2 - 55,
        new Font("Dialog", Font.BOLD, 50),
        color
      )

      centered(
        g,
        subtitle,
        getHeight / 2 - 12,
        new Font("Dialog", Font.PLAIN, 16),
        Color.WHITE
      )

      drawButton(
        g,
        button,
        getHeight / 2 + 65
      )
    }

    // ---------------- PAINT ----------------

    override protected def paintComponent(graphics: Graphics): Unit = {

      super.paintComponent(graphics)

      val g = graphics.create().asInstanceOf[Graphics2D]

      g.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON
      )

      g.setRenderingHint(
        RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_QUALITY
      )

      g.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON
      )

      drawBackground(g)

      asteroids.foreach(a => drawAsteroid(g, a))

      drawBullets(g)
      drawParticles(g)

      if (state == PLAYING || state == PAUSED) {
        drawShip(g)
        drawHUD(g)
      }

      if (state == MENU) {
        drawMenu(g)
      }

      if (state == PAUSED) {
        drawOverlay(
          g,
          "MISSION PAUSED",
          "Your spaceship is safe for now.",
          "RESUME MISSION",
          new Color(105, 220, 255)
        )
      }

      if (state == GAMEOVER) {
        drawOverlay(
          g,
          "MISSION FAILED",
          "FINAL SCORE: " + score,
          "RESTART MISSION",
          new Color(255, 110, 130)
        )
      }

      g.dispose()
    }
  }
}

// Launch the game
StarfallNexus.main(Array.empty[String])