Code Sketch
bixuu
Category: Programming
import java.awt._
import java.awt.event._
import javax.swing._
import scala.collection.mutable
import scala.util.Random
object NeonDash {
class Obstacle(
val x: Double,
val kind: Int,
val width: Double,
val height: Double,
val y: Double = 0.0
)
class Particle(
var x: Double,
var y: Double,
var vx: Double,
var vy: Double,
var life: Double,
val maxLife: Double,
val color: Color
)
class Star(
var x: Double,
var y: Double,
val size: Double
)
def main(args: Array[String]): Unit = {
SwingUtilities.invokeLater(new Runnable {
override def run(): Unit = {
val frame = new JFrame("NEON DASH - CUBE TO SHIP")
val game = new GamePanel
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
frame.setContentPane(game)
frame.setSize(1150, 720)
frame.setMinimumSize(new Dimension(850, 550))
frame.setLocationRelativeTo(null)
frame.setVisible(true)
game.requestFocusInWindow()
}
})
}
class GamePanel extends JPanel {
private val MENU = 0
private val PLAYING = 1
private val PAUSED = 2
private val DEAD = 3
private val COMPLETE = 4
private var state = MENU
private val rng = new Random()
private val keys = mutable.Set[Int]()
private val playerX = 200.0
private val playerSize = 38.0
private val halfDistance = 6000.0
private val totalDistance = 12000.0
private var cameraX = 0.0
private var jumpHeight = 0.0
private var jumpVelocity = 0.0
private var grounded = true
private var rotation = 0.0
private var shipMode = false
private var shipY = 320.0
private var shipVelocity = 0.0
private var score = 0
private var bestScore = 0
private var elapsed = 0.0
private var animation = 0.0
private var shake = 0.0
private var invincible = 0.0
private var message = "READY TO DASH!"
private var messageTime = 2.0
private var lastTime = System.nanoTime()
private val gravity = 1900.0
private val jumpPower = 720.0
private val obstacles = mutable.ArrayBuffer[Obstacle]()
private val particles = mutable.ArrayBuffer[Particle]()
private val stars = mutable.ArrayBuffer[Star]()
setPreferredSize(new Dimension(1150, 720))
setBackground(Color.BLACK)
setFocusable(true)
setDoubleBuffered(true)
createWorld()
createStars()
// ==================================
// WORLD
// ==================================
private def createWorld(): Unit = {
obstacles.clear()
// CUBE SECTION: 0% - 50%
obstacles ++= Vector(
new Obstacle(550, 0, 43, 43),
new Obstacle(960, 1, 88, 43),
new Obstacle(1420, 2, 48, 57),
new Obstacle(1830, 0, 43, 43),
new Obstacle(2180, 1, 88, 43),
new Obstacle(2660, 2, 48, 57),
new Obstacle(3090, 0, 43, 43),
new Obstacle(3470, 1, 88, 43),
new Obstacle(3950, 2, 48, 57),
new Obstacle(4390, 0, 43, 43),
new Obstacle(4720, 1, 88, 43),
new Obstacle(5190, 2, 48, 57),
new Obstacle(5570, 0, 43, 43),
new Obstacle(5800, 0, 43, 43)
)
// SHIP SECTION: 50% - 100%
// Kind 3 = ceiling obstacle
// Kind 4 = floor obstacle
// Kind 5 = middle obstacle
def shipPair(
x: Double,
ceilingHeight: Double,
floorStart: Double
): Unit = {
obstacles += new Obstacle(
x, 3, 72, ceilingHeight,
75
)
obstacles += new Obstacle(
x, 4, 72, 550 - floorStart,
floorStart
)
}
shipPair(6350, 175, 415)
shipPair(6780, 220, 420)
shipPair(7210, 165, 365)
shipPair(7650, 215, 410)
shipPair(8090, 155, 360)
shipPair(8520, 205, 405)
shipPair(8950, 170, 370)
shipPair(9380, 225, 420)
shipPair(9810, 165, 365)
shipPair(10240, 210, 410)
shipPair(10670, 160, 355)
shipPair(11100, 205, 405)
shipPair(11530, 175, 375)
// Extra floating barriers
obstacles += new Obstacle(7900, 5, 55, 55, 285)
obstacles += new Obstacle(9380, 5, 55, 55, 305)
obstacles += new Obstacle(10880, 5, 55, 55, 290)
}
private def createStars(): Unit = {
stars.clear()
for (i <- 0 until 130) {
stars += new Star(
rng.nextDouble() * 1200,
rng.nextDouble() * 720,
1 + rng.nextDouble() * 2
)
}
}
// ==================================
// TIMER
// ==================================
private val timer = new Timer(16, new ActionListener {
override def actionPerformed(e: ActionEvent): Unit = {
updateGame()
repaint()
}
})
timer.start()
// ==================================
// INPUT
// ==================================
addKeyListener(new KeyAdapter {
override def keyPressed(e: KeyEvent): Unit = {
val k = e.getKeyCode
val fresh = !keys.contains(k)
keys += k
if (!fresh) return
if (state == MENU) {
if (k == KeyEvent.VK_ENTER) startGame()
} else if (state == PLAYING) {
if (!shipMode &&
(k == KeyEvent.VK_SPACE ||
k == KeyEvent.VK_UP ||
k == KeyEvent.VK_W)) {
jump()
}
if (k == KeyEvent.VK_ESCAPE ||
k == KeyEvent.VK_P) {
state = PAUSED
}
} else if (state == PAUSED) {
if (k == KeyEvent.VK_ENTER ||
k == KeyEvent.VK_ESCAPE ||
k == KeyEvent.VK_P) {
state = PLAYING
lastTime = System.nanoTime()
}
} else if (state == DEAD) {
if (k == KeyEvent.VK_ENTER ||
k == KeyEvent.VK_R) {
startGame()
}
if (k == KeyEvent.VK_ESCAPE) state = MENU
} else if (state == COMPLETE) {
if (k == KeyEvent.VK_ENTER ||
k == KeyEvent.VK_R) {
startGame()
}
}
}
override def keyReleased(e: KeyEvent): Unit = {
keys -= e.getKeyCode
}
})
addFocusListener(new FocusAdapter {
override def focusLost(e: FocusEvent): Unit = {
keys.clear()
if (state == PLAYING) state = PAUSED
}
})
addMouseListener(new MouseAdapter {
override def mousePressed(e: MouseEvent): Unit = {
requestFocusInWindow()
if (state == MENU) {
startGame()
} else if (state == PLAYING && !shipMode) {
jump()
} else if (state == PAUSED) {
state = PLAYING
lastTime = System.nanoTime()
}
}
})
// ==================================
// START
// ==================================
private def startGame(): Unit = {
cameraX = 0
jumpHeight = 0
jumpVelocity = 0
grounded = true
rotation = 0
shipMode = false
shipY = 320
shipVelocity = 0
score = 0
elapsed = 0
shake = 0
invincible = 0
message = "CUBE MODE! LET'S DASH!"
messageTime = 2.5
particles.clear()
keys.clear()
lastTime = System.nanoTime()
state = PLAYING
requestFocusInWindow()
}
// ==================================
// JUMP
// ==================================
private def jump(): Unit = {
if (state != PLAYING || shipMode || !grounded) return
jumpVelocity = jumpPower
grounded = false
emit(
playerX + 18,
groundY,
7,
currentColor
)
}
private def groundY: Double = getHeight - 105.0
private def currentColor: Color = {
if (shipMode) new Color(190, 80, 255)
else new Color(30, 215, 255)
}
// ==================================
// PARTICLES
// ==================================
private def emit(
x: Double,
y: Double,
count: Int,
color: Color
): Unit = {
for (i <- 0 until count) {
val a = rng.nextDouble() * math.Pi * 2
val speed = 35 + rng.nextDouble() * 180
particles += new Particle(
x, y,
math.cos(a) * speed,
math.sin(a) * speed,
0.4 + rng.nextDouble() * 0.55,
0.95,
color
)
}
}
private def die(): Unit = {
if (state != PLAYING) return
state = DEAD
shake = 0.3
emit(
playerX + playerSize / 2,
if (shipMode) shipY else groundY - playerSize / 2 - jumpHeight,
38,
currentColor
)
bestScore = math.max(bestScore, score)
message = "CRASH!"
messageTime = 2
}
// ==================================
// UPDATE
// ==================================
private def updateGame(): Unit = {
val now = System.nanoTime()
val dt = math.min(
(now - lastTime) / 1000000000.0,
0.035
)
lastTime = now
animation += dt
stars.foreach { s =>
s.y += 17 * dt
if (s.y > getHeight) {
s.y = -5
s.x = rng.nextDouble() * getWidth
}
}
var i = particles.length - 1
while (i >= 0) {
val p = particles(i)
p.x += p.vx * dt
p.y += p.vy * dt
p.vx *= 0.96
p.vy *= 0.96
p.life -= dt
if (p.life <= 0) particles.remove(i)
i -= 1
}
if (shake > 0) shake -= dt
if (messageTime > 0) messageTime -= dt
if (invincible > 0) invincible -= dt
if (state != PLAYING) return
elapsed += dt
val speed = 355.0
cameraX += speed * dt
score = (cameraX / 10).toInt
// CUBE TO SHIP TRANSFORMATION
if (!shipMode && cameraX >= halfDistance) {
shipMode = true
jumpHeight = 0
jumpVelocity = 0
grounded = true
shipY = groundY - 245
shipVelocity = 0
message = "AIRPLANE MODE! HOLD SPACE TO FLY!"
messageTime = 3.5
emit(
playerX + 20,
shipY,
35,
new Color(175, 80, 255)
)
}
// ==================================
// CUBE PHYSICS
// ==================================
if (!shipMode) {
if (!grounded) {
jumpHeight += jumpVelocity * dt
jumpVelocity -= gravity * dt
if (jumpHeight <= 0) {
jumpHeight = 0
jumpVelocity = 0
grounded = true
rotation =
math.round(rotation / (math.Pi / 2)) * (math.Pi / 2)
} else {
rotation += dt * 8.0
}
}
} else {
// ==================================
// AIRPLANE PHYSICS
// Hold SPACE = UP
// Release SPACE = DOWN
// ==================================
val flying = keys.contains(KeyEvent.VK_SPACE) ||
keys.contains(KeyEvent.VK_UP)
if (flying) {
shipVelocity -= 1450.0 * dt
} else {
shipVelocity += 850.0 * dt
}
shipVelocity = math.max(
-520,
math.min(520, shipVelocity)
)
shipY += shipVelocity * dt
if (shipY < 95 || shipY > groundY - 25) {
die()
return
}
}
// ==================================
// COLLISION
// ==================================
val playerLeft = playerX - 13
val playerRight = playerX + 28
var collision = false
obstacles.foreach { o =>
if (!collision) {
val screenX = playerX + o.x - cameraX
val overlapsX =
screenX + o.width > playerLeft &&
screenX < playerRight
if (overlapsX) {
if (!shipMode && o.kind <= 2) {
val playerTop =
groundY - playerSize - jumpHeight
val playerBottom =
playerTop + playerSize
val obstacleTop = groundY - o.height
val hitVertical =
playerBottom > obstacleTop +
(if (o.kind == 0 || o.kind == 1) 9 else 1) &&
playerTop < groundY
if (hitVertical) collision = true
} else if (shipMode && o.kind >= 3) {
val playerTop = shipY - 13
val playerBottom = shipY + 13
val obstacleTop = o.y
val obstacleBottom = o.y + o.height
if (
playerBottom > obstacleTop &&
playerTop < obstacleBottom
) {
collision = true
}
}
}
}
}
if (collision) {
die()
return
}
// ==================================
// LEVEL FINISH
// ==================================
if (cameraX >= totalDistance) {
state = COMPLETE
bestScore = math.max(bestScore, score)
emit(
getWidth / 2,
getHeight / 2,
90,
new Color(100, 255, 180)
)
}
}
// ==================================
// BACKGROUND
// ==================================
private def drawBackground(g: Graphics2D): Unit = {
val c = currentColor
g.setPaint(new GradientPaint(
0, 0, new Color(5, 8, 28),
0, getHeight, new Color(21, 8, 43)
))
g.fillRect(0, 0, getWidth, getHeight)
g.setColor(new Color(
c.getRed,
c.getGreen,
c.getBlue,
27
))
g.fillOval(
getWidth / 3,
getHeight / 4,
getWidth / 2,
getHeight / 2
)
stars.foreach { s =>
g.setColor(new Color(180, 220, 255, 175))
g.fillOval(
s.x.toInt,
s.y.toInt,
s.size.toInt.max(1),
s.size.toInt.max(1)
)
}
// Background neon bars
g.setColor(new Color(
c.getRed,
c.getGreen,
c.getBlue,
34
))
var x = -((cameraX * 0.2) % 90).toInt
while (x < getWidth) {
g.drawLine(x, 0, x, groundY.toInt)
x += 90
}
}
// ==================================
// GROUND
// ==================================
private def drawGround(g: Graphics2D): Unit = {
val gy = groundY.toInt
g.setPaint(new GradientPaint(
0, gy, new Color(20, 26, 49),
0, getHeight, new Color(4, 7, 24)
))
g.fillRect(0, gy, getWidth, getHeight - gy)
g.setColor(currentColor)
g.setStroke(new BasicStroke(3f))
g.drawLine(0, gy, getWidth, gy)
g.setColor(new Color(100, 160, 220, 65))
val gap = 55
var px = -((cameraX * 0.8) % gap).toInt
while (px < getWidth) {
g.drawLine(
px, gy + 5,
px - 25, getHeight
)
px += gap
}
g.setStroke(new BasicStroke(1f))
}
// ==================================
// CUBE OBSTACLES
// ==================================
private def drawSpike(
g: Graphics2D,
x: Int,
width: Int,
height: Int
): Unit = {
val gy = groundY.toInt
val poly = new Polygon(
Array(x, x + width / 2, x + width),
Array(gy, gy - height, gy),
3
)
g.setColor(new Color(
currentColor.getRed,
currentColor.getGreen,
currentColor.getBlue,
50
))
g.fillOval(
x - 5,
gy - height,
width + 10,
height + 12
)
g.setPaint(new GradientPaint(
x.toFloat,
(gy - height).toFloat,
Color.WHITE,
(x + width).toFloat,
gy.toFloat,
currentColor.darker()
))
g.fillPolygon(poly)
g.setColor(Color.WHITE)
g.drawPolygon(poly)
}
private def drawCubeObstacles(g: Graphics2D): Unit = {
obstacles.foreach { o =>
if (o.kind <= 2) {
val sx = playerX + o.x - cameraX
if (sx > -110 && sx < getWidth + 110) {
val x = sx.toInt
if (o.kind == 0) {
drawSpike(g, x, 43, 43)
} else if (o.kind == 1) {
drawSpike(g, x, 43, 43)
drawSpike(g, x + 43, 43, 43)
} else {
val gy = groundY.toInt
val h = o.height.toInt
val w = o.width.toInt
g.setColor(new Color(
currentColor.getRed,
currentColor.getGreen,
currentColor.getBlue,
48
))
g.fillRoundRect(
x - 7, gy - h - 7,
w + 14, h + 14,
12, 12
)
g.setPaint(new GradientPaint(
x.toFloat, (gy - h).toFloat,
currentColor.brighter(),
(x + w).toFloat, gy.toFloat,
currentColor.darker()
))
g.fillRect(x, gy - h, w, h)
g.setColor(Color.WHITE)
g.drawRect(x, gy - h, w, h)
}
}
}
}
}
// ==================================
// PLANE OBSTACLES
// ==================================
private def drawShipObstacles(g: Graphics2D): Unit = {
obstacles.foreach { o =>
if (o.kind >= 3) {
val sx = playerX + o.x - cameraX
if (sx > -100 && sx < getWidth + 100) {
val x = sx.toInt
val y = o.y.toInt
val w = o.width.toInt
val h = o.height.toInt
g.setColor(new Color(
currentColor.getRed,
currentColor.getGreen,
currentColor.getBlue,
48
))
g.fillRoundRect(
x - 7,
y - 5,
w + 14,
h + 10,
10, 10
)
g.setPaint(new GradientPaint(
x.toFloat,
y.toFloat,
currentColor.brighter(),
(x + w).toFloat,
(y + h).toFloat,
currentColor.darker()
))
g.fillRect(x, y, w, h)
g.setColor(Color.WHITE)
g.drawRect(x, y, w, h)
// Neon line detail
g.setColor(new Color(255, 255, 255, 95))
for (i <- 1 until 4) {
val yy = y + h * i / 4
g.drawLine(x + 8, yy, x + w - 8, yy)
}
}
}
}
}
// ==================================
// CUBE
// ==================================
private def drawCube(g: Graphics2D): Unit = {
if (state == DEAD) return
if (invincible > 0 &&
(animation * 12).toInt % 2 == 0) return
val x = playerX
val y = groundY - playerSize - jumpHeight
// Shadow
g.setColor(new Color(0, 0, 0, 110))
val shadowW = math.max(
12,
(40 - jumpHeight * 0.14).toInt
)
g.fillOval(
(x + 19 - shadowW / 2).toInt,
groundY.toInt - 6,
shadowW,
10
)
val old = g.getTransform
g.translate(x + 19, y + 19)
g.rotate(rotation)
g.setColor(new Color(
25, 215, 255, 60
))
g.fillRoundRect(-26, -26, 52, 52, 10, 10)
g.setPaint(new GradientPaint(
-18, -18,
Color.WHITE,
18, 18,
new Color(10, 105, 210)
))
g.fillRoundRect(
-19, -19,
38, 38,
5, 5
)
g.setStroke(new BasicStroke(2.4f))
g.setColor(Color.WHITE)
g.drawRoundRect(-19, -19, 38, 38, 5, 5)
// Eyes
g.setColor(new Color(10, 30, 70))
g.fillRect(-10, -9, 6, 7)
g.fillRect(5, -9, 6, 7)
g.setColor(new Color(180, 250, 255))
g.drawLine(-9, 6, 9, 6)
g.setStroke(new BasicStroke(1f))
g.setTransform(old)
}
// ==================================
// SHIP
// ==================================
private def drawPlane(g: Graphics2D): Unit = {
if (state == DEAD) return
val x = playerX.toInt
val y = shipY.toInt
val tilt = math.max(
-0.55,
math.min(0.55, shipVelocity / 950.0)
)
val old = g.getTransform
g.translate(x, y)
g.rotate(tilt)
// Engine flame
g.setPaint(new GradientPaint(
-25, 0,
Color.WHITE,
-55, 0,
new Color(120, 50, 255, 25)
))
g.fillPolygon(
Array(-18, -56, -18),
Array(-7, 0, 7),
3
)
// Aura
g.setColor(new Color(180, 80, 255, 55))
g.fillOval(-30, -26, 70, 52)
// Plane body
val body = new Polygon(
Array(32, 3, -24, -10, 21),
Array(0, -12, -9, 0, 10),
5
)
g.setPaint(new GradientPaint(
0, -15,
Color.WHITE,
0, 16,
new Color(125, 40, 235)
))
g.fillPolygon(body)
g.setColor(Color.WHITE)
g.setStroke(new BasicStroke(2f))
g.drawPolygon(body)
// Wing
val wing = new Polygon(
Array(5, -15, -8, 13),
Array(-3, -24, -4, 1),
4
)
g.setColor(new Color(60, 190, 255))
g.fillPolygon(wing)
g.setColor(Color.WHITE)
g.drawPolygon(wing)
// Cockpit
g.setColor(new Color(40, 225, 255))
g.fillOval(5, -5, 12, 9)
g.setColor(Color.WHITE)
g.drawOval(5, -5, 12, 9)
g.setStroke(new BasicStroke(1f))
g.setTransform(old)
}
// ==================================
// PARTICLES
// ==================================
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(
p.color.getRed,
p.color.getGreen,
p.color.getBlue,
alpha
))
g.fillOval(
p.x.toInt,
p.y.toInt,
5, 5
)
}
}
// ==================================
// HUD
// ==================================
private def panel(
g: Graphics2D,
x: Int, y: Int,
w: Int, h: Int
): Unit = {
g.setColor(new Color(5, 10, 27, 215))
g.fillRoundRect(x, y, w, h, 17, 17)
g.setColor(new Color(
currentColor.getRed,
currentColor.getGreen,
currentColor.getBlue,
145
))
g.drawRoundRect(x, y, w, h, 17, 17)
}
private def centerText(
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 = {
panel(g, 18, 18, 210, 85)
g.setFont(new Font("Dialog", Font.BOLD, 19))
g.setColor(currentColor)
g.drawString("NEON DASH", 35, 47)
g.setFont(new Font("Dialog", Font.BOLD, 14))
g.setColor(Color.WHITE)
g.drawString("SCORE: " + score, 35, 78)
// Progress bar
val progress = math.max(
0.0,
math.min(1.0, cameraX / totalDistance)
)
val barW = 340
val bx = getWidth / 2 - barW / 2
val by = 23
g.setColor(new Color(40, 42, 65))
g.fillRoundRect(bx, by, barW, 12, 10, 10)
g.setColor(currentColor)
g.fillRoundRect(
bx, by,
(barW * progress).toInt,
12, 10, 10
)
centerText(
g,
(progress * 100).toInt + "%",
57,
new Font("Dialog", Font.BOLD, 12),
Color.WHITE
)
// Mode status
panel(g, getWidth - 185, 18, 165, 65)
g.setFont(new Font("Dialog", Font.BOLD, 14))
g.setColor(currentColor)
g.drawString(
if (shipMode) "SHIP MODE" else "CUBE MODE",
getWidth - 168,
55
)
// Controls
g.setFont(new Font("Dialog", Font.PLAIN, 12))
g.setColor(new Color(210, 224, 245))
g.drawString(
if (shipMode)
"HOLD SPACE: FLY UP | RELEASE: FLY DOWN | ESC: PAUSE"
else
"SPACE / UP / CLICK: JUMP | ESC: PAUSE",
22,
getHeight - 17
)
if (messageTime > 0 && message.nonEmpty) {
panel(
g,
getWidth / 2 - 240,
getHeight - 75,
480,
40
)
centerText(
g,
message,
getHeight - 50,
new Font("Dialog", Font.BOLD, 14),
currentColor
)
}
}
// ==================================
// BUTTON
// ==================================
private def button(
g: Graphics2D,
text: String,
y: Int
): Unit = {
val x = getWidth / 2 - 155
g.setPaint(new GradientPaint(
x.toFloat, y.toFloat,
new Color(30, 210, 255),
(x + 310).toFloat, (y + 62).toFloat,
new Color(120, 55, 230)
))
g.fillRoundRect(x, y, 310, 62, 19, 19)
g.setColor(Color.WHITE)
g.drawRoundRect(x, y, 310, 62, 19, 19)
centerText(
g,
text,
y + 39,
new Font("Dialog", Font.BOLD, 20),
Color.WHITE
)
}
// ==================================
// MAIN MENU
// ==================================
private def drawMenu(g: Graphics2D): Unit = {
g.setColor(new Color(0, 0, 15, 150))
g.fillRect(0, 0, getWidth, getHeight)
centerText(
g,
"NEON DASH",
getHeight / 2 - 115,
new Font("Dialog", Font.BOLD, 65),
new Color(60, 220, 255)
)
centerText(
g,
"CUBE TO AIRPLANE ADVENTURE",
getHeight / 2 - 68,
new Font("Dialog", Font.BOLD, 17),
Color.WHITE
)
centerText(
g,
"First half: Cube Run",
getHeight / 2 - 13,
new Font("Dialog", Font.BOLD, 15),
new Color(60, 220, 255)
)
centerText(
g,
"Second half: Airplane Flight",
getHeight / 2 + 16,
new Font("Dialog", Font.BOLD, 15),
new Color(195, 105, 255)
)
button(
g,
"START GAME",
getHeight / 2 + 65
)
}
// ==================================
// OVERLAY
// ==================================
private def overlay(
g: Graphics2D,
title: String,
subtitle: String,
buttonText: String,
color: Color
): Unit = {
g.setColor(new Color(0, 0, 15, 195))
g.fillRect(0, 0, getWidth, getHeight)
centerText(
g,
title,
getHeight / 2 - 55,
new Font("Dialog", Font.BOLD, 46),
color
)
centerText(
g,
subtitle,
getHeight / 2 - 10,
new Font("Dialog", Font.PLAIN, 15),
Color.WHITE
)
button(
g,
buttonText,
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
)
val old = g.getTransform
if (shake > 0) {
g.translate(
rng.nextInt(7) - 3,
rng.nextInt(7) - 3
)
}
drawBackground(g)
drawGround(g)
if (!shipMode) {
drawCubeObstacles(g)
} else {
drawShipObstacles(g)
}
drawParticles(g)
if (state != MENU && state != DEAD) {
if (shipMode) drawPlane(g)
else drawCube(g)
}
if (state == MENU) {
drawMenu(g)
}
if (state == PLAYING) {
drawHUD(g)
}
if (state == PAUSED) {
drawHUD(g)
overlay(
g,
"GAME PAUSED",
"Press ENTER to continue",
"RESUME",
currentColor
)
}
if (state == DEAD) {
overlay(
g,
"CRASHED!",
"FINAL SCORE: " + score,
"RESTART",
new Color(255, 80, 110)
)
centerText(
g,
"PRESS ENTER OR R",
getHeight - 25,
new Font("Dialog", Font.BOLD, 12),
Color.WHITE
)
}
if (state == COMPLETE) {
overlay(
g,
"LEVEL COMPLETE!",
"YOU MASTERED CUBE AND AIRPLANE MODE!",
"PLAY AGAIN",
new Color(100, 255, 180)
)
}
g.setTransform(old)
g.dispose()
}
}
}
// LAUNCH
NeonDash.main(Array.empty[String])