Code Sketch


Bixuu AncientDragonDominionEvent5
By: Mhalsakant School
Category: Programming
import java.awt.{BorderLayout, Color, Dimension, Font, GradientPaint, Graphics, Graphics2D, GridLayout, RenderingHints}
import java.awt.event.{ActionEvent, ActionListener, KeyEvent, KeyListener, MouseEvent, MouseListener, WindowAdapter, WindowEvent}
import javax.swing.{BorderFactory, JButton, JFrame, JLabel, JPanel, JTextField, SwingConstants, SwingUtilities, Timer, WindowConstants}
import scala.collection.mutable.ArrayBuffer

object AncientDragonDominionEvent5 {
  def main(args: Array[String]): Unit = {
    SwingUtilities.invokeLater(new Runnable {
      override def run(): Unit = {
        val window = new AncientDragonDominionFrame()
        window.setVisible(true)
      }
    })
  }
}

class AncientDragonDominionFrame extends JFrame("INFINITY REALMS SEASON 05 - ANCIENT DRAGON DOMINION") {
  private val cards = new java.awt.CardLayout()
  private val root = new JPanel(cards)
  private var playerName = "DRAGON RIDER"
  private val loginPanel = new JPanel(new BorderLayout(12, 12))
  private val nameField = new JTextField(18)
  private val loginStatus = new JLabel("ENTER YOUR NAME TO ENTER THE ANCIENT REALM", SwingConstants.CENTER)
  private val menuPanel = new JPanel(new BorderLayout(12, 12))
  private val welcomeLabel = new JLabel("WELCOME, DRAGON RIDER", SwingConstants.CENTER)
  private val gamePanel = new AncientDragonGamePanel(new Runnable {
    override def run(): Unit = showMenu()
  })

  buildLoginPanel()
  buildMenuPanel()
  root.add(loginPanel, "LOGIN")
  root.add(menuPanel, "MENU")
  root.add(gamePanel, "GAME")
  setContentPane(root)
  setSize(1020, 720)
  setMinimumSize(new Dimension(900, 650))
  setLocationRelativeTo(null)
  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
  addWindowListener(new WindowAdapter {
    override def windowClosing(e: WindowEvent): Unit = gamePanel.stopGame()
  })
  cards.show(root, "LOGIN")

  private def styleButton(button: JButton): Unit = {
    button.setFont(new Font("SansSerif", Font.BOLD, 17))
    button.setForeground(new Color(245, 250, 239))
    button.setBackground(new Color(35, 83, 67))
    button.setFocusPainted(false)
    button.setBorder(BorderFactory.createCompoundBorder(
      BorderFactory.createLineBorder(new Color(153, 218, 141), 2),
      BorderFactory.createEmptyBorder(10, 18, 10, 18)
    ))
  }

  private def buildLoginPanel(): Unit = {
    loginPanel.setBackground(new Color(13, 28, 28))
    val title = new JLabel("ANCIENT DRAGON DOMINION", SwingConstants.CENTER)
    title.setFont(new Font("Serif", Font.BOLD, 34))
    title.setForeground(new Color(222, 190, 105))
    val subtitle = new JLabel("ANCIENT MAGIC AWAKENS", SwingConstants.CENTER)
    subtitle.setFont(new Font("SansSerif", Font.BOLD, 17))
    subtitle.setForeground(new Color(151, 220, 160))
    val center = new JPanel(new GridLayout(0, 1, 8, 8))
    center.setOpaque(false)
    val prompt = new JLabel("DRAGON RIDER NAME", SwingConstants.CENTER)
    prompt.setForeground(Color.WHITE)
    prompt.setFont(new Font("SansSerif", Font.BOLD, 15))
    nameField.setFont(new Font("SansSerif", Font.PLAIN, 20))
    nameField.setHorizontalAlignment(SwingConstants.CENTER)
    nameField.setBackground(new Color(230, 241, 221))
    val enterButton = new JButton("ENTER THE REALM")
    styleButton(enterButton)
    enterButton.addActionListener(new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = {
        val typed = nameField.getText.trim
        if (typed.length > 0) {
          playerName = typed.take(18)
          welcomeLabel.setText("WELCOME, " + playerName.toUpperCase)
          loginStatus.setText("THE DRAGON GATES ARE OPEN")
          cards.show(root, "MENU")
        } else {
          loginStatus.setText("PLEASE ENTER A NAME FIRST")
        }
      }
    })
    center.add(prompt)
    center.add(nameField)
    center.add(enterButton)
    loginStatus.setForeground(new Color(242, 209, 126))
    loginStatus.setFont(new Font("SansSerif", Font.BOLD, 14))
    loginPanel.add(title, BorderLayout.NORTH)
    loginPanel.add(center, BorderLayout.CENTER)
    loginPanel.add(loginStatus, BorderLayout.SOUTH)
    loginPanel.setBorder(BorderFactory.createEmptyBorder(70, 150, 70, 150))
  }

  private def buildMenuPanel(): Unit = {
    menuPanel.setBackground(new Color(13, 28, 28))
    welcomeLabel.setFont(new Font("Serif", Font.BOLD, 28))
    welcomeLabel.setForeground(new Color(222, 190, 105))
    val story = new JLabel("Collect five Ancient Runes, awaken your Dragon, and open the Dominion Gate.", SwingConstants.CENTER)
    story.setForeground(new Color(202, 226, 190))
    story.setFont(new Font("SansSerif", Font.PLAIN, 16))
    val startButton = new JButton("START ADVENTURE")
    styleButton(startButton)
    startButton.addActionListener(new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = {
        cards.show(root, "GAME")
        gamePanel.startGame(playerName)
      }
    })
    val controls = new JLabel("WASD / ARROWS: MOVE     SPACE: DRAGON PULSE     Q: AURA SHIELD     E: OPEN GATE     P: PAUSE", SwingConstants.CENTER)
    controls.setForeground(new Color(166, 202, 177))
    controls.setFont(new Font("SansSerif", Font.BOLD, 13))
    val center = new JPanel(new GridLayout(0, 1, 20, 20))
    center.setOpaque(false)
    center.add(story)
    center.add(startButton)
    center.add(controls)
    menuPanel.add(welcomeLabel, BorderLayout.NORTH)
    menuPanel.add(center, BorderLayout.CENTER)
    menuPanel.setBorder(BorderFactory.createEmptyBorder(100, 45, 100, 45))
  }

  private def showMenu(): Unit = {
    gamePanel.stopGame()
    cards.show(root, "MENU")
  }
}

class AncientDragonGamePanel(onReturn: Runnable) extends JPanel with KeyListener with MouseListener {
  private class Rune(val x: Double, val y: Double) {
    var collected: Boolean = false
  }

  private val random = new scala.util.Random()
  private val runes = ArrayBuffer[Rune]()
  private val keys = scala.collection.mutable.Set[Int]()
  private val realmNames = Array("EMBERWOOD FOREST", "MOONSTONE MOUNTAINS", "CELESTIAL DRAGON SANCTUM")
  private val realmStories = Array(
    "Ancient roots hide the first runes.",
    "Cold winds guard the moonstone secrets.",
    "The final dragon seal waits beyond the clouds."
  )
  private val runeSpots = Array(
    Array((160, 170), (320, 490), (470, 205), (650, 480), (810, 175)),
    Array((170, 460), (300, 180), (500, 480), (650, 190), (810, 430)),
    Array((160, 190), (340, 470), (500, 170), (680, 470), (820, 180))
  )

  private var playerName = "DRAGON RIDER"
  private var realmIndex = 0
  private var playerX = 90.0
  private var playerY = 340.0
  private var targetX = -1.0
  private var targetY = -1.0
  private var facingX = 1.0
  private var facingY = 0.0
  private var runeCount = 0
  private var totalRunes = 0
  private var coins = 0
  private var health = 5
  private var shieldTicks = 0
  private var shieldCooldown = 0
  private var pulseCooldown = 0
  private var invulnerableTicks = 0
  private var dashTicks = 0
  private var guardianX = 820.0
  private var guardianY = 340.0
  private var guardianHealth = 3
  private var guardianActive = false
  private var guardianDefeated = false
  private var paused = false
  private var finished = false
  private var message = "Find the five Ancient Runes."
  private var dragonAwakened = false
  private var frameCount = 0
  private val timer = new Timer(16, new ActionListener {
    override def actionPerformed(e: ActionEvent): Unit = updateGame()
  })

  setBackground(new Color(10, 24, 30))
  setFocusable(true)
  addKeyListener(this)
  addMouseListener(this)
  setToolTipText("WASD to move, SPACE for Dragon Pulse, Q for Shield, E at the gate")
  loadRealm()

  def startGame(name: String): Unit = {
    playerName = name
    paused = false
    finished = false
    requestFocusInWindow()
    timer.start()
  }

  def stopGame(): Unit = {
    timer.stop()
    keys.clear()
  }

  private def loadRealm(): Unit = {
    runes.clear()
    var i = 0
    while (i < 5) {
      val point = runeSpots(realmIndex)(i)
      runes += new Rune(point._1.toDouble, point._2.toDouble)
      i += 1
    }
    playerX = 85.0
    playerY = 340.0
    targetX = -1.0
    targetY = -1.0
    runeCount = 0
    guardianX = 820.0
    guardianY = 340.0
    guardianHealth = 3
    guardianActive = false
    guardianDefeated = false
    health = 5
    shieldTicks = 0
    message = realmStories(realmIndex)
    dragonAwakened = realmIndex > 0 || totalRunes >= 5
    repaint()
  }

  private def distance(ax: Double, ay: Double, bx: Double, by: Double): Double = {
    val dx = ax - bx
    val dy = ay - by
    math.sqrt(dx * dx + dy * dy)
  }

  private def updateGame(): Unit = {
    if (!paused && !finished) {
      frameCount += 1
      if (shieldTicks > 0) shieldTicks -= 1
      if (shieldCooldown > 0) shieldCooldown -= 1
      if (pulseCooldown > 0) pulseCooldown -= 1
      if (invulnerableTicks > 0) invulnerableTicks -= 1
      if (dashTicks > 0) dashTicks -= 1

      var dx = 0.0
      var dy = 0.0
      if (keys.contains(KeyEvent.VK_W) || keys.contains(KeyEvent.VK_UP)) dy -= 1.0
      if (keys.contains(KeyEvent.VK_S) || keys.contains(KeyEvent.VK_DOWN)) dy += 1.0
      if (keys.contains(KeyEvent.VK_A) || keys.contains(KeyEvent.VK_LEFT)) dx -= 1.0
      if (keys.contains(KeyEvent.VK_D) || keys.contains(KeyEvent.VK_RIGHT)) dx += 1.0

      if (dx == 0.0 && dy == 0.0 && targetX >= 0.0) {
        dx = targetX - playerX
        dy = targetY - playerY
        if (math.sqrt(dx * dx + dy * dy) < 8.0) {
          targetX = -1.0
          targetY = -1.0
          dx = 0.0
          dy = 0.0
        }
      }

      val magnitude = math.sqrt(dx * dx + dy * dy)
      if (magnitude > 0.0) {
        dx = dx / magnitude
        dy = dy / magnitude
        facingX = dx
        facingY = dy
        var speed = 3.4
        if (dashTicks > 0) speed = 7.4
        playerX += dx * speed
        playerY += dy * speed
        targetX = -1.0
        targetY = -1.0
      }

      playerX = math.max(35.0, math.min(965.0, playerX))
      playerY = math.max(105.0, math.min(590.0, playerY))

      runes.foreach { rune =>
        if (!rune.collected && distance(playerX, playerY, rune.x, rune.y) < 31.0) {
          rune.collected = true
          runeCount += 1
          totalRunes += 1
          coins += 25
          if (runeCount >= 3) dragonAwakened = true
          message = "Ancient Rune collected! +25 Dragon Coins"
          if (runeCount >= 5) {
            guardianActive = true
            message = "THE SHADOW GUARDIAN HAS AWAKENED! Use SPACE."
          }
        }
      }

      if (guardianActive && !guardianDefeated) {
        val gx = playerX - guardianX
        val gy = playerY - guardianY
        val gd = math.sqrt(gx * gx + gy * gy)
        if (gd > 1.0) {
          guardianX += gx / gd * 1.15
          guardianY += gy / gd * 1.15
        }
        if (gd < 35.0 && invulnerableTicks == 0 && shieldTicks == 0) {
          health -= 1
          invulnerableTicks = 75
          message = "Guardian aura hit! Use Q to shield and SPACE to pulse."
          if (health <= 0) {
            health = 5
            playerX = 85.0
            playerY = 340.0
            message = "The Grove Spirit restored you at the entrance."
          }
        }
      }
      repaint()
    }
  }

  private def useDragonPulse(): Unit = {
    if (!paused && !finished && pulseCooldown == 0) {
      pulseCooldown = 35
      if (guardianActive && !guardianDefeated && distance(playerX, playerY, guardianX, guardianY) < 270.0) {
        guardianHealth -= 1
        message = "DRAGON PULSE HIT! Guardian energy weakened."
        if (guardianHealth <= 0) {
          guardianDefeated = true
          guardianActive = false
          message = "SHADOW GUARDIAN DEFEATED! The Dominion Gate is open!"
        }
      } else {
        message = "Ancient Dragon Pulse released! Get closer to the Guardian."
      }
      repaint()
    }
  }

  private def useShield(): Unit = {
    if (!paused && !finished && shieldCooldown == 0) {
      shieldTicks = 100
      shieldCooldown = 260
      message = "ANCIENT AURA SHIELD ACTIVATED!"
      repaint()
    }
  }

  private def interact(): Unit = {
    if (!paused && !finished && guardianDefeated && distance(playerX, playerY, 925.0, 340.0) < 75.0) {
      if (realmIndex < 2) {
        realmIndex += 1
        loadRealm()
        message = "New realm discovered! Find five more runes."
      } else {
        finished = true
        message = "ETERNAL DOMINION RESTORED! YOU ARE THE DRAGON SOVEREIGN!"
      }
    } else if (!guardianDefeated && runeCount < 5) {
      message = "Collect all five runes to summon the Shadow Guardian."
    } else if (guardianActive) {
      message = "Defeat the Shadow Guardian with SPACE before opening the gate."
    }
    repaint()
  }

  override def paintComponent(graphics: Graphics): Unit = {
    super.paintComponent(graphics)
    val g = graphics.create().asInstanceOf[Graphics2D]
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
    val w = getWidth
    val h = getHeight
    val skyTop = if (realmIndex == 0) new Color(15, 45, 39) else if (realmIndex == 1) new Color(20, 35, 67) else new Color(37, 25, 63)
    val skyBottom = if (realmIndex == 0) new Color(62, 93, 55) else if (realmIndex == 1) new Color(60, 78, 112) else new Color(94, 55, 105)
    g.setPaint(new GradientPaint(0, 0, skyTop, 0, h, skyBottom))
    g.fillRect(0, 0, w, h)

    var star = 0
    while (star < 75) {
      val sx = (star * 137 + 29) % math.max(1, w)
      val sy = (star * 71 + 22) % 330
      g.setColor(new Color(220, 235, 202, 120))
      g.fillOval(sx, sy, 2 + (star % 3), 2 + (star % 3))
      star += 1
    }

    g.setColor(new Color(20, 55, 43, 180))
    g.fillOval(-90, 270, w / 2, 390)
    g.fillOval(w / 3, 300, w / 2, 340)
    g.fillOval(w - 300, 275, 430, 380)
    drawAncientTree(g, 105, 330)
    drawAncientTree(g, 900, 245)
    drawGate(g, 925, 340)

    runes.foreach { rune =>
      if (!rune.collected) drawRune(g, rune.x.toInt, rune.y.toInt)
    }
    if (guardianActive && !guardianDefeated) drawGuardian(g, guardianX.toInt, guardianY.toInt)
    if (dragonAwakened) drawDragon(g, (playerX - 34).toInt, (playerY - 30).toInt)
    drawPlayer(g, playerX.toInt, playerY.toInt)

    g.setColor(new Color(7, 16, 22, 205))
    g.fillRoundRect(15, 12, w - 30, 75, 18, 18)
    g.setFont(new Font("Serif", Font.BOLD, 22))
    g.setColor(new Color(235, 211, 139))
    g.drawString("ANCIENT DRAGON DOMINION", 32, 42)
    g.setFont(new Font("SansSerif", Font.BOLD, 13))
    g.setColor(new Color(201, 228, 196))
    g.drawString("RIDER: " + playerName, 34, 68)
    g.drawString("REALM: " + realmNames(realmIndex), 260, 42)
    g.drawString("RUNES: " + runeCount + "/5", 260, 68)
    g.drawString("COINS: " + coins, 410, 42)
    g.drawString("HEARTS: " + health, 410, 68)
    g.drawString("SHIELD: " + (if (shieldTicks > 0) "ACTIVE" else if (shieldCooldown == 0) "READY" else "CHARGING"), 535, 42)
    g.drawString("DRAGON: " + (if (dragonAwakened) "AWAKENED" else "SLEEPING"), 535, 68)

    g.setColor(new Color(7, 16, 22, 190))
    g.fillRoundRect(20, h - 68, w - 40, 48, 14, 14)
    g.setFont(new Font("SansSerif", Font.BOLD, 14))
    g.setColor(new Color(242, 226, 184))
    g.drawString(message, 35, h - 39)
    g.setFont(new Font("SansSerif", Font.PLAIN, 12))
    g.setColor(new Color(213, 228, 205))
    g.drawString("WASD/ARROWS MOVE   SPACE DRAGON PULSE   Q AURA SHIELD   E OPEN GATE   P PAUSE   B MENU", 30, h - 10)

    if (paused) drawOverlay(g, "REALM PAUSED", "Press P to continue")
    if (finished) drawOverlay(g, "ETERNAL DOMINION RESTORED!", "You awakened the Ancient Dragon. Press B to return to the menu.")
    g.dispose()
  }

  private def drawAncientTree(g: Graphics2D, x: Int, y: Int): Unit = {
    g.setColor(new Color(77, 55, 35))
    g.fillRoundRect(x - 12, y - 2, 25, 120, 10, 10)
    g.setColor(new Color(43, 105, 68, 220))
    g.fillOval(x - 65, y - 90, 125, 110)
    g.fillOval(x - 95, y - 40, 110, 92)
    g.fillOval(x - 10, y - 44, 112, 92)
    g.setColor(new Color(178, 215, 115, 170))
    g.fillOval(x - 15, y - 62, 25, 25)
    g.fillOval(x + 30, y - 15, 18, 18)
  }

  private def drawGate(g: Graphics2D, x: Int, y: Int): Unit = {
    val open = guardianDefeated
    g.setColor(if (open) new Color(238, 197, 99, 220) else new Color(90, 112, 105, 210))
    g.drawOval(x - 43, y - 82, 86, 164)
    g.drawOval(x - 34, y - 73, 68, 146)
    g.setColor(if (open) new Color(255, 231, 153, 115) else new Color(30, 55, 55, 180))
    g.fillOval(x - 28, y - 67, 56, 134)
    g.setFont(new Font("SansSerif", Font.BOLD, 11))
    g.setColor(Color.WHITE)
    g.drawString(if (open) "OPEN" else "SEALED", x - 18, y + 100)
  }

  private def drawRune(g: Graphics2D, x: Int, y: Int): Unit = {
    g.setColor(new Color(91, 210, 151, 65))
    g.fillOval(x - 24, y - 24, 48, 48)
    val pointsX = Array(x, x + 15, x, x - 15)
    val pointsY = Array(y - 19, y, y + 19, y)
    g.setColor(new Color(205, 245, 172))
    g.fillPolygon(pointsX, pointsY, 4)
    g.setColor(new Color(255, 245, 202))
    g.drawLine(x, y - 12, x, y + 12)
  }

  private def drawPlayer(g: Graphics2D, x: Int, y: Int): Unit = {
    if (invulnerableTicks == 0 || frameCount % 8 < 4) {
      g.setColor(new Color(10, 22, 20, 110))
      g.fillOval(x - 17, y + 14, 34, 12)
      g.setColor(new Color(180, 82, 51))
      g.fillOval(x - 13, y - 26, 26, 26)
      g.setColor(new Color(225, 201, 140))
      g.fillOval(x - 8, y - 22, 16, 16)
      g.setColor(new Color(44, 91, 68))
      g.fillPolygon(Array(x, x - 18, x + 18), Array(y - 7, y + 24, y + 24), 3)
      g.setColor(new Color(232, 213, 150))
      g.fillOval(x + 5, y - 12, 5, 5)
      if (shieldTicks > 0) {
        g.setColor(new Color(140, 223, 255, 110))
        g.drawOval(x - 25, y - 34, 50, 62)
        g.drawOval(x - 29, y - 38, 58, 70)
      }
    }
  }

  private def drawDragon(g: Graphics2D, x: Int, y: Int): Unit = {
    g.setColor(new Color(53, 173, 120))
    g.fillOval(x - 10, y - 4, 28, 18)
    g.setColor(new Color(128, 230, 165))
    g.fillPolygon(Array(x - 3, x - 20, x - 12), Array(y + 2, y - 18, y + 8), 3)
    g.fillPolygon(Array(x + 8, x + 22, x + 14), Array(y + 2, y - 18, y + 8), 3)
    g.setColor(new Color(245, 225, 155))
    g.fillOval(x + 11, y - 1, 4, 4)
  }

  private def drawGuardian(g: Graphics2D, x: Int, y: Int): Unit = {
    g.setColor(new Color(92, 28, 92, 100))
    g.fillOval(x - 35, y - 36, 70, 75)
    g.setColor(new Color(82, 51, 117))
    g.fillOval(x - 22, y - 27, 44, 53)
    g.setColor(new Color(244, 201, 111))
    g.fillOval(x - 12, y - 11, 7, 7)
    g.fillOval(x + 5, y - 11, 7, 7)
    g.setColor(new Color(214, 152, 243, 170))
    g.drawOval(x - 29, y - 33, 58, 66)
    g.setFont(new Font("SansSerif", Font.BOLD, 11))
    g.setColor(Color.WHITE)
    g.drawString("GUARDIAN " + guardianHealth + "/3", x - 35, y - 43)
  }

  private def drawOverlay(g: Graphics2D, title: String, subtitle: String): Unit = {
    g.setColor(new Color(5, 12, 18, 205))
    g.fillRect(0, 0, getWidth, getHeight)
    g.setFont(new Font("Serif", Font.BOLD, 34))
    g.setColor(new Color(244, 211, 135))
    val fm = g.getFontMetrics
    g.drawString(title, (getWidth - fm.stringWidth(title)) / 2, getHeight / 2 - 12)
    g.setFont(new Font("SansSerif", Font.BOLD, 15))
    g.setColor(Color.WHITE)
    val fm2 = g.getFontMetrics
    g.drawString(subtitle, (getWidth - fm2.stringWidth(subtitle)) / 2, getHeight / 2 + 25)
  }

  override def keyPressed(e: KeyEvent): Unit = {
    val code = e.getKeyCode
    keys.add(code)
    if (code == KeyEvent.VK_SPACE) useDragonPulse()
    if (code == KeyEvent.VK_Q) useShield()
    if (code == KeyEvent.VK_E) interact()
    if (code == KeyEvent.VK_P && !finished) {
      paused = !paused
      if (paused) message = "Realm paused. Press P to continue."
      else message = "The ancient journey continues."
      repaint()
    }
    if (code == KeyEvent.VK_SHIFT && !paused && !finished) dashTicks = 14
    if (code == KeyEvent.VK_B) {
      stopGame()
      onReturn.run()
    }
    if (code == KeyEvent.VK_ENTER && finished) {
      stopGame()
      onReturn.run()
    }
  }

  override def keyReleased(e: KeyEvent): Unit = { keys.remove(e.getKeyCode) }
  override def keyTyped(e: KeyEvent): Unit = {}
  override def mousePressed(e: MouseEvent): Unit = {
    if (!paused && !finished && e.getY > 95 && e.getY < getHeight - 75) {
      targetX = e.getX.toDouble
      targetY = e.getY.toDouble
      requestFocusInWindow()
    }
  }
  override def mouseClicked(e: MouseEvent): Unit = {}
  override def mouseReleased(e: MouseEvent): Unit = {}
  override def mouseEntered(e: MouseEvent): Unit = {}
  override def mouseExited(e: MouseEvent): Unit = {}
}

AncientDragonDominionEvent5.main(Array[String]())