Code Sketch
naruto 1v3 yoiiii
Category: Programming
import java.awt.{BasicStroke, Color, Font, Graphics, Graphics2D, Polygon, RenderingHints}
import java.awt.event.{ActionEvent, ActionListener, KeyEvent, KeyListener, MouseEvent, MouseListener}
import javax.swing.{JFrame, JPanel, Timer, WindowConstants}
class NarutoBattlegroundPanel extends JPanel with KeyListener with MouseListener {
private var playerX: Double = 430.0
private var playerY: Double = 420.0
private var enemy1X: Double = 760.0
private var enemy1Y: Double = 270.0
private var enemy2X: Double = 820.0
private var enemy2Y: Double = 520.0
private var playerHealth: Int = 100
private var enemy1Health: Int = 100
private var enemy2Health: Int = 100
private var chakra: Int = 100
private var score: Int = 0
private var combo: Int = 0
private var comboTimer: Int = 0
private var attackCooldown: Int = 0
private var dashCooldown: Int = 0
private var specialCooldown: Int = 0
private var hitFlash: Int = 0
private var enemyHitFlash1: Int = 0
private var enemyHitFlash2: Int = 0
private var gameOver: Boolean = false
private var victory: Boolean = false
private var paused: Boolean = false
private var started: Boolean = true
private var upPressed: Boolean = false
private var downPressed: Boolean = false
private var leftPressed: Boolean = false
private var rightPressed: Boolean = false
private var attackPressed: Boolean = false
private var specialPressed: Boolean = false
private var dashPressed: Boolean = false
private val gameTimer: Timer = new Timer(16, new ActionListener {
override def actionPerformed(e: ActionEvent): Unit = {
updateGame()
repaint()
}
})
setFocusable(true)
addKeyListener(this)
addMouseListener(this)
setBackground(new Color(10, 12, 24))
gameTimer.start()
private def clampDouble(value: Double, min: Double, max: Double): Double = {
if (value < min) min
else if (value > max) max
else value
}
private def distance(x1: Double, y1: Double, x2: Double, y2: Double): Double = {
val dx = x1 - x2
val dy = y1 - y2
Math.sqrt(dx * dx + dy * dy)
}
private def updateGame(): Unit = {
if (paused || gameOver || victory) return
if (attackCooldown > 0) attackCooldown -= 1
if (dashCooldown > 0) dashCooldown -= 1
if (specialCooldown > 0) specialCooldown -= 1
if (comboTimer > 0) comboTimer -= 1
if (hitFlash > 0) hitFlash -= 1
if (enemyHitFlash1 > 0) enemyHitFlash1 -= 1
if (enemyHitFlash2 > 0) enemyHitFlash2 -= 1
if (comboTimer == 0) combo = 0
var speed: Double = 4.4
if (dashPressed && dashCooldown == 0) {
speed = 13.0
dashCooldown = 55
dashPressed = false
}
if (upPressed) playerY -= speed
if (downPressed) playerY += speed
if (leftPressed) playerX -= speed
if (rightPressed) playerX += speed
playerX = clampDouble(playerX, 75.0, 925.0)
playerY = clampDouble(playerY, 145.0, 585.0)
if (attackPressed && attackCooldown == 0) {
normalAttack()
attackPressed = false
}
if (specialPressed && specialCooldown == 0) {
specialAttack()
specialPressed = false
}
moveEnemy1()
moveEnemy2()
enemyAttack()
if (playerHealth <= 0) {
playerHealth = 0
gameOver = true
}
if (enemy1Health <= 0 && enemy2Health <= 0) {
victory = true
}
}
private def normalAttack(): Unit = {
attackCooldown = 18
val d1 = distance(playerX, playerY, enemy1X, enemy1Y)
val d2 = distance(playerX, playerY, enemy2X, enemy2Y)
var didHit: Boolean = false
if (enemy1Health > 0 && d1 < 125.0) {
enemy1Health -= 16
if (enemy1Health < 0) enemy1Health = 0
enemyHitFlash1 = 8
score += 100
didHit = true
}
if (enemy2Health > 0 && d2 < 125.0) {
enemy2Health -= 16
if (enemy2Health < 0) enemy2Health = 0
enemyHitFlash2 = 8
score += 100
didHit = true
}
if (didHit) {
combo += 1
comboTimer = 70
chakra = Math.min(100, chakra + 4)
}
}
private def specialAttack(): Unit = {
if (chakra < 35) return
chakra -= 35
specialCooldown = 95
val d1 = distance(playerX, playerY, enemy1X, enemy1Y)
val d2 = distance(playerX, playerY, enemy2X, enemy2Y)
if (enemy1Health > 0 && d1 < 245.0) {
enemy1Health -= 38
if (enemy1Health < 0) enemy1Health = 0
enemyHitFlash1 = 15
score += 300
}
if (enemy2Health > 0 && d2 < 245.0) {
enemy2Health -= 38
if (enemy2Health < 0) enemy2Health = 0
enemyHitFlash2 = 15
score += 300
}
combo += 2
comboTimer = 100
}
private def moveEnemy1(): Unit = {
if (enemy1Health <= 0) return
val d = distance(playerX, playerY, enemy1X, enemy1Y)
if (d > 105.0) {
if (playerX > enemy1X) enemy1X += 1.8
if (playerX < enemy1X) enemy1X -= 1.8
if (playerY > enemy1Y) enemy1Y += 1.8
if (playerY < enemy1Y) enemy1Y -= 1.8
} else {
enemy1X += Math.sin(System.nanoTime() / 220000000.0) * 1.4
}
enemy1X = clampDouble(enemy1X, 80.0, 920.0)
enemy1Y = clampDouble(enemy1Y, 155.0, 575.0)
}
private def moveEnemy2(): Unit = {
if (enemy2Health <= 0) return
val d = distance(playerX, playerY, enemy2X, enemy2Y)
if (d > 135.0) {
if (playerX > enemy2X) enemy2X += 1.55
if (playerX < enemy2X) enemy2X -= 1.55
if (playerY > enemy2Y) enemy2Y += 1.55
if (playerY < enemy2Y) enemy2Y -= 1.55
} else {
enemy2X += Math.cos(System.nanoTime() / 240000000.0) * 1.5
enemy2Y += Math.sin(System.nanoTime() / 250000000.0) * 1.1
}
enemy2X = clampDouble(enemy2X, 80.0, 920.0)
enemy2Y = clampDouble(enemy2Y, 155.0, 575.0)
}
private def enemyAttack(): Unit = {
if (enemy1Health > 0 && distance(playerX, playerY, enemy1X, enemy1Y) < 85.0) {
if ((System.currentTimeMillis() / 420L) % 2L == 0L) {
playerHealth -= 1
hitFlash = 3
}
}
if (enemy2Health > 0 && distance(playerX, playerY, enemy2X, enemy2Y) < 85.0) {
if ((System.currentTimeMillis() / 520L) % 2L == 0L) {
playerHealth -= 1
hitFlash = 3
}
}
}
private def resetGame(): Unit = {
playerX = 430.0
playerY = 420.0
enemy1X = 760.0
enemy1Y = 270.0
enemy2X = 820.0
enemy2Y = 520.0
playerHealth = 100
enemy1Health = 100
enemy2Health = 100
chakra = 100
score = 0
combo = 0
comboTimer = 0
attackCooldown = 0
dashCooldown = 0
specialCooldown = 0
gameOver = false
victory = false
paused = false
repaint()
requestFocusInWindow()
}
private def drawHealthBar(g: Graphics2D, x: Int, y: Int, w: Int, h: Int, value: Int, max: Int, label: String): Unit = {
g.setFont(new Font("SansSerif", Font.BOLD, 13))
g.setColor(Color.WHITE)
g.drawString(label, x, y - 5)
g.setColor(new Color(40, 40, 50))
g.fillRoundRect(x, y, w, h, 10, 10)
val safeMax = Math.max(1, max)
val safeValue = Math.max(0, Math.min(value, safeMax))
val fillWidth = ((w.toDouble * safeValue.toDouble) / safeMax.toDouble).toInt
if (label == "NARUTO") {
g.setColor(new Color(255, 135, 30))
} else {
g.setColor(new Color(220, 45, 60))
}
g.fillRoundRect(x, y, fillWidth, h, 10, 10)
g.setColor(Color.WHITE)
g.drawRoundRect(x, y, w, h, 10, 10)
}
private def drawShadow(g: Graphics2D, x: Int, y: Int, w: Int): Unit = {
g.setColor(new Color(0, 0, 0, 90))
g.fillOval(x - w / 2, y - 8, w, 18)
}
private def drawNaruto(g: Graphics2D, x: Int, y: Int): Unit = {
val bodyX = x - 24
val bodyY = y - 30
drawShadow(g, x, y + 38, 62)
g.setColor(new Color(235, 120, 25))
g.fillRoundRect(bodyX, bodyY + 24, 48, 55, 16, 16)
g.setColor(new Color(35, 35, 45))
g.fillRect(bodyX + 8, bodyY + 37, 32, 16)
g.setColor(new Color(255, 220, 180))
g.fillOval(x - 25, bodyY - 8, 50, 48)
val hair = new Polygon()
hair.addPoint(x - 24, bodyY + 2)
hair.addPoint(x - 16, bodyY - 23)
hair.addPoint(x - 5, bodyY - 7)
hair.addPoint(x + 3, bodyY - 28)
hair.addPoint(x + 12, bodyY - 7)
hair.addPoint(x + 26, bodyY - 20)
hair.addPoint(x + 23, bodyY + 7)
g.setColor(new Color(245, 170, 25))
g.fillPolygon(hair)
g.setColor(new Color(45, 55, 65))
g.fillRoundRect(x - 24, bodyY + 4, 48, 11, 5, 5)
g.setColor(new Color(35, 90, 145))
g.fillRect(x - 9, bodyY + 6, 18, 7)
g.setColor(Color.BLACK)
g.fillOval(x - 14, bodyY + 18, 7, 6)
g.fillOval(x + 7, bodyY + 18, 7, 6)
g.setColor(new Color(185, 45, 45))
g.fillOval(x - 10, bodyY + 30, 7, 3)
g.fillOval(x + 3, bodyY + 30, 7, 3)
g.setStroke(new BasicStroke(5f))
g.setColor(new Color(235, 120, 25))
g.drawLine(x - 17, bodyY + 75, x - 27, bodyY + 101)
g.drawLine(x + 17, bodyY + 75, x + 28, bodyY + 101)
g.setStroke(new BasicStroke(7f))
g.setColor(new Color(45, 70, 110))
g.drawLine(x - 13, bodyY + 102, x - 28, bodyY + 104)
g.drawLine(x + 13, bodyY + 102, x + 28, bodyY + 104)
g.setStroke(new BasicStroke(4f))
g.setColor(new Color(230, 230, 235))
g.drawLine(x + 23, bodyY + 53, x + 43, bodyY + 38)
}
private def drawEnemy(g: Graphics2D, x: Int, y: Int, kind: Int, flash: Int): Unit = {
val bodyX = x - 23
val bodyY = y - 30
drawShadow(g, x, y + 38, 60)
if (flash > 0) {
g.setColor(Color.WHITE)
} else if (kind == 1) {
g.setColor(new Color(75, 85, 105))
} else {
g.setColor(new Color(100, 35, 65))
}
g.fillRoundRect(bodyX, bodyY + 23, 46, 58, 14, 14)
g.setColor(new Color(255, 218, 185))
g.fillOval(x - 24, bodyY - 8, 48, 44)
if (kind == 1) {
g.setColor(new Color(35, 40, 55))
val hair = new Polygon()
hair.addPoint(x - 25, bodyY + 7)
hair.addPoint(x - 15, bodyY - 27)
hair.addPoint(x - 2, bodyY - 10)
hair.addPoint(x + 13, bodyY - 29)
hair.addPoint(x + 24, bodyY + 4)
g.fillPolygon(hair)
} else {
g.setColor(new Color(35, 20, 25))
val hair = new Polygon()
hair.addPoint(x - 25, bodyY + 8)
hair.addPoint(x - 18, bodyY - 27)
hair.addPoint(x - 2, bodyY - 10)
hair.addPoint(x + 8, bodyY - 25)
hair.addPoint(x + 25, bodyY + 8)
g.fillPolygon(hair)
}
g.setColor(new Color(40, 45, 55))
g.fillRect(x - 23, bodyY + 3, 46, 9)
if (kind == 1) {
g.setColor(new Color(55, 110, 190))
} else {
g.setColor(new Color(190, 40, 55))
}
g.fillRect(x - 18, bodyY + 5, 36, 5)
g.setColor(Color.BLACK)
g.fillOval(x - 13, bodyY + 18, 7, 6)
g.fillOval(x + 6, bodyY + 18, 7, 6)
g.setStroke(new BasicStroke(5f))
g.drawLine(x - 16, bodyY + 77, x - 29, bodyY + 102)
g.drawLine(x + 16, bodyY + 77, x + 29, bodyY + 102)
g.setStroke(new BasicStroke(7f))
g.setColor(new Color(35, 40, 55))
g.drawLine(x - 13, bodyY + 102, x - 29, bodyY + 104)
g.drawLine(x + 13, bodyY + 102, x + 29, bodyY + 104)
}
private def drawChakraEffect(g: Graphics2D, x: Int, y: Int): Unit = {
val pulse = (System.currentTimeMillis() / 7L % 22L).toInt
g.setColor(new Color(80, 170, 255, 90))
g.fillOval(x - 48 - pulse, y - 48 - pulse, 96 + pulse * 2, 96 + pulse * 2)
g.setColor(new Color(150, 220, 255, 160))
g.drawOval(x - 55 - pulse, y - 55 - pulse, 110 + pulse * 2, 110 + pulse * 2)
}
private def drawBackground(g: Graphics2D): Unit = {
val w = getWidth()
val h = getHeight()
g.setColor(new Color(18, 24, 43))
g.fillRect(0, 0, w, h)
g.setColor(new Color(27, 40, 67))
g.fillRect(0, 110, w, h - 110)
val mountain = new Polygon()
mountain.addPoint(0, 260)
mountain.addPoint(130, 145)
mountain.addPoint(240, 260)
mountain.addPoint(390, 125)
mountain.addPoint(530, 260)
mountain.addPoint(690, 135)
mountain.addPoint(850, 260)
mountain.addPoint(1000, 150)
mountain.addPoint(1000, 360)
mountain.addPoint(0, 360)
g.setColor(new Color(35, 51, 75))
g.fillPolygon(mountain)
g.setColor(new Color(62, 75, 90))
g.fillRect(0, 360, w, h - 360)
g.setColor(new Color(76, 88, 96))
var gridX = 0
while (gridX < w) {
g.drawLine(gridX, 360, gridX, h)
gridX += 50
}
var gridY = 380
while (gridY < h) {
g.drawLine(0, gridY, w, gridY)
gridY += 40
}
g.setColor(new Color(230, 230, 230, 25))
g.fillOval(720, 40, 120, 120)
g.setColor(new Color(255, 255, 255, 120))
g.fillOval(735, 55, 90, 90)
g.setColor(new Color(30, 30, 38))
g.fillRect(75, 280, 35, 80)
g.fillRect(60, 300, 65, 20)
g.fillRect(180, 245, 42, 115)
g.fillRect(165, 270, 72, 20)
g.fillRect(880, 275, 35, 85)
g.fillRect(865, 300, 65, 20)
g.setColor(new Color(18, 22, 32, 210))
g.fillRect(0, 0, w, 110)
}
override def paintComponent(graphics: Graphics): Unit = {
super.paintComponent(graphics)
val g = graphics.asInstanceOf[Graphics2D]
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
drawBackground(g)
drawHealthBar(g, 28, 35, 280, 20, playerHealth, 100, "NARUTO")
drawHealthBar(g, 692, 35, 130, 16, enemy1Health, 100, "RIVAL 1")
drawHealthBar(g, 842, 35, 130, 16, enemy2Health, 100, "RIVAL 2")
g.setFont(new Font("SansSerif", Font.BOLD, 26))
g.setColor(Color.WHITE)
g.drawString("NARUTO BATTLEGROUND", 350, 36)
g.setFont(new Font("SansSerif", Font.BOLD, 13))
g.setColor(new Color(175, 195, 220))
g.drawString("1 V 2 ? SURVIVAL ARENA", 430, 58)
g.setColor(new Color(35, 40, 52))
g.fillRoundRect(28, 72, 280, 18, 9, 9)
g.setColor(new Color(70, 160, 255))
g.setColor(Color.WHITE)
g.drawRoundRect(28, 72, 280, 18, 9, 9)
g.setFont(new Font("SansSerif", Font.BOLD, 12))
g.drawString("CHAKRA " + chakra + " / 100", 120, 86)
if (enemy1Health > 0) drawEnemy(g, enemy1X.toInt, enemy1Y.toInt, 1, enemyHitFlash1)
if (enemy2Health > 0) drawEnemy(g, enemy2X.toInt, enemy2Y.toInt, 2, enemyHitFlash2)
if (specialCooldown == 0 && chakra >= 35) {
drawChakraEffect(g, playerX.toInt, playerY.toInt)
}
drawNaruto(g, playerX.toInt, playerY.toInt)
if (hitFlash > 0) {
g.setColor(new Color(255, 60, 60, 45))
g.fillRect(0, 110, getWidth(), getHeight() - 110)
}
if (combo > 0) {
g.setFont(new Font("SansSerif", Font.BOLD, 30))
g.setColor(new Color(255, 205, 60))
g.drawString("COMBO x" + combo, 390, 145)
}
g.setFont(new Font("SansSerif", Font.BOLD, 15))
g.setColor(Color.WHITE)
g.drawString("SCORE " + score, 28, 125)
g.setColor(new Color(15, 18, 28, 220))
g.fillRoundRect(20, getHeight() - 62, 960, 48, 12, 12)
g.setFont(new Font("SansSerif", Font.BOLD, 13))
g.setColor(new Color(225, 230, 240))
g.drawString("MOVE W A S D", 40, getHeight() - 34)
g.drawString("ATTACK J", 190, getHeight() - 34)
g.drawString("RASENGAN K", 310, getHeight() - 34)
g.drawString("DASH SPACE", 475, getHeight() - 34)
g.drawString("PAUSE P", 635, getHeight() - 34)
g.drawString("RESTART R", 760, getHeight() - 34)
if (paused) {
drawOverlay(g, "PAUSED", "Press P to continue")
} else if (victory) {
drawOverlay(g, "VICTORY!", "Both rivals defeated ? Press R to fight again")
} else if (gameOver) {
drawOverlay(g, "DEFEATED", "Press R to restart the battleground")
}
}
private def drawOverlay(g: Graphics2D, title: String, subtitle: String): Unit = {
g.setColor(new Color(5, 8, 16, 205))
g.fillRect(0, 0, getWidth(), getHeight())
g.setFont(new Font("SansSerif", Font.BOLD, 56))
g.setColor(Color.WHITE)
val titleWidth = g.getFontMetrics.stringWidth(title)
g.drawString(title, (getWidth() - titleWidth) / 2, 290)
g.setFont(new Font("SansSerif", Font.BOLD, 18))
g.setColor(new Color(210, 220, 235))
val subWidth = g.getFontMetrics.stringWidth(subtitle)
g.drawString(subtitle, (getWidth() - subWidth) / 2, 330)
g.setFont(new Font("SansSerif", Font.BOLD, 22))
g.setColor(new Color(255, 190, 50))
val scoreText = "SCORE " + score
val scoreWidth = g.getFontMetrics.stringWidth(scoreText)
g.drawString(scoreText, (getWidth() - scoreWidth) / 2, 370)
}
override def keyPressed(e: KeyEvent): Unit = {
e.getKeyCode match {
case KeyEvent.VK_W => upPressed = true
case KeyEvent.VK_S => downPressed = true
case KeyEvent.VK_A => leftPressed = true
case KeyEvent.VK_D => rightPressed = true
case KeyEvent.VK_J => attackPressed = true
case KeyEvent.VK_K => specialPressed = true
case KeyEvent.VK_SPACE => dashPressed = true
case KeyEvent.VK_P => paused = !paused
case KeyEvent.VK_R => resetGame()
case _ =>
}
}
override def keyReleased(e: KeyEvent): Unit = {
e.getKeyCode match {
case KeyEvent.VK_W => upPressed = false
case KeyEvent.VK_S => downPressed = false
case KeyEvent.VK_A => leftPressed = false
case KeyEvent.VK_D => rightPressed = false
case _ =>
}
}
override def keyTyped(e: KeyEvent): Unit = {}
override def mouseClicked(e: MouseEvent): Unit = {
requestFocusInWindow()
}
override def mousePressed(e: MouseEvent): Unit = {}
override def mouseReleased(e: MouseEvent): Unit = {}
override def mouseEntered(e: MouseEvent): Unit = {}
override def mouseExited(e: MouseEvent): Unit = {}
}
val narutoGameFrame = new JFrame("Naruto Battleground - 1V2")
narutoGameFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
narutoGameFrame.setResizable(false)
val narutoGamePanel = new NarutoBattlegroundPanel()
narutoGameFrame.setContentPane(narutoGamePanel)
narutoGameFrame.setSize(1000, 650)
narutoGameFrame.setLocationRelativeTo(null)
narutoGameFrame.setVisible(true)
narutoGamePanel.requestFocusInWindow()