Code Sketch
yoi neo play
Category: Programming
import java.awt._
import java.awt.event._
import javax.swing._
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
SwingUtilities.invokeLater(new Runnable {
def run(): Unit = {
val random = new Random()
val frame = new JFrame("NEON PORTAL RUNNER")
frame.setSize(1100, 720)
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
frame.setLocationRelativeTo(null)
frame.setResizable(false)
var playerLane = 0
var score = 0
var crystals = 0
var lives = 3
var speed = 0.010
var energy = 100.0
var jumpHeight = 0.0
var jumpVelocity = 0.0
var shieldFrames = 0
var hitFlash = 0
var roadOffset = 0.0
var animation = 0.0
var frameCount = 0
var gameOver = false
var paused = false
var boost = false
case class Thing(
var lane: Int,
var z: Double,
kind: Int,
var resolved: Boolean = false
)
val things = ArrayBuffer[Thing]()
val stars = Array.fill(180)(
(
random.nextInt(1100),
random.nextInt(220),
random.nextInt(150) + 80
)
)
def resetGame(): Unit = {
playerLane = 0
score = 0
crystals = 0
lives = 3
speed = 0.010
energy = 100.0
jumpHeight = 0.0
jumpVelocity = 0.0
shieldFrames = 0
hitFlash = 0
roadOffset = 0.0
animation = 0.0
frameCount = 0
gameOver = false
paused = false
boost = false
things.clear()
}
val panel = new JPanel() {
override def paintComponent(g: Graphics): Unit = {
super.paintComponent(g)
val g2 = g.create().asInstanceOf[Graphics2D]
val w = getWidth
val h = getHeight
val cx = w / 2
val horizon = h * 30 / 100
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
)
// BACKGROUND
g2.setPaint(new GradientPaint(
0f, 0f, new Color(3, 4, 25),
0f, h.toFloat, new Color(20, 8, 55)
))
g2.fillRect(0, 0, w, h)
// NEBULA
g2.setColor(new Color(80, 20, 160, 35))
g2.fillOval(-100, 30, 450, 300)
g2.setColor(new Color(0, 150, 220, 30))
g2.fillOval(w - 400, 40, 500, 300)
// STARS
for (i <- stars.indices) {
val s = stars(i)
val brightness = (
130 + 100 * math.abs(math.sin(animation + i))
).toInt.min(255)
g2.setColor(
new Color(brightness, brightness, 255)
)
g2.fillOval(s._1, s._2, 2, 2)
}
// PORTAL
g2.setColor(new Color(130, 40, 255, 30))
g2.fillOval(cx - 115, horizon - 120, 230, 230)
g2.setColor(new Color(70, 200, 255, 150))
g2.setStroke(new BasicStroke(3f))
g2.drawOval(cx - 90, horizon - 95, 180, 180)
g2.setColor(new Color(200, 100, 255, 140))
g2.drawOval(cx - 72, horizon - 77, 144, 144)
// ROAD
val road = new Polygon()
road.addPoint(cx - 48, horizon)
road.addPoint(cx + 48, horizon)
road.addPoint(cx + 440, h)
road.addPoint(cx - 440, h)
g2.setColor(new Color(15, 20, 40))
g2.fillPolygon(road)
// ROAD EDGES
g2.setStroke(new BasicStroke(5f))
g2.setColor(new Color(0, 220, 255))
g2.drawLine(cx - 48, horizon, cx - 440, h)
g2.drawLine(cx + 48, horizon, cx + 440, h)
// LANE MARKINGS
for (lane <- 1 to 2) {
for (i <- 0 until 12) {
val z = ((i / 12.0) + roadOffset) % 1.0
val z2 = math.min(1.0, z + 0.035)
val topX = cx - 48 + lane * 32
val bottomX = cx - 440 + lane * 293
val x1 = cx + (topX - cx) * (1 - z) +
(bottomX - cx) * z
val x2 = cx + (topX - cx) * (1 - z2) +
(bottomX - cx) * z2
val y1 = horizon + (h - horizon) * z
val y2 = horizon + (h - horizon) * z2
g2.setColor(new Color(100, 210, 255, 180))
g2.setStroke(
new BasicStroke(math.max(1.0, z * 8).toFloat)
)
g2.drawLine(
x1.toInt, y1.toInt,
x2.toInt, y2.toInt
)
}
}
// PORTAL RINGS
for (i <- 0 until 9) {
val z = ((i / 9.0) + roadOffset) % 1.0
val y = horizon + (h - horizon) * z
val halfWidth = 50 + z * 430
val ringHeight = 12 + z * 125
g2.setColor(new Color(50, 150, 255, 90))
g2.setStroke(
new BasicStroke(math.max(1.0, z * 4).toFloat)
)
g2.drawOval(
(cx - halfWidth).toInt,
(y - ringHeight / 2).toInt,
(halfWidth * 2).toInt,
ringHeight.toInt
)
}
// SIDE LIGHTS
for (i <- 0 until 15) {
val z = ((i / 15.0) + roadOffset) % 1.0
val y = horizon + (h - horizon) * z
val spread = 55 + z * 490
val pillarHeight = 10 + z * 75
for (side <- Array(-1, 1)) {
val x = cx + side * spread
g2.setColor(new Color(0, 180, 255, 140))
g2.fillRect(
x.toInt,
(y - pillarHeight).toInt,
(2 + z * 5).toInt,
pillarHeight.toInt
)
}
}
// PROJECTION FUNCTIONS
def objectX(lane: Int, z: Double): Int = {
val roadHalf = 48 + z * 392
(cx + lane * roadHalf * 0.58).toInt
}
def objectY(z: Double): Int = {
(horizon + (h - horizon) * z).toInt
}
// OBJECTS
things.toList.sortBy(_.z).foreach { o =>
val z = o.z
if (z >= 0.0 && z <= 1.10) {
val x = objectX(o.lane, z)
val y = objectY(z)
val objectSize = 10 + z * 65
if (o.kind == 0) {
// OBSTACLE
val ow = objectSize
val oh = objectSize * 0.75
g2.setColor(new Color(255, 30, 75, 65))
g2.fillRoundRect(
(x - ow / 2 - 8).toInt,
(y - oh / 2 - 8).toInt,
(ow + 16).toInt,
(oh + 16).toInt,
12, 12
)
g2.setColor(new Color(180, 20, 55))
g2.fillRoundRect(
(x - ow / 2).toInt,
(y - oh / 2).toInt,
ow.toInt,
oh.toInt,
10, 10
)
g2.setColor(Color.WHITE)
g2.drawRoundRect(
(x - ow / 2).toInt,
(y - oh / 2).toInt,
ow.toInt,
oh.toInt,
10, 10
)
g2.setColor(Color.YELLOW)
g2.fillRect(
(x - ow / 2).toInt,
(y - 3).toInt,
ow.toInt,
math.max(3, (oh * 0.15).toInt)
)
} else if (o.kind == 1) {
// CRYSTAL
val r = objectSize / 2
val diamond = new Polygon()
diamond.addPoint(x, y - r.toInt)
diamond.addPoint((x + r * 0.7).toInt, y)
diamond.addPoint(x, (y + r).toInt)
diamond.addPoint((x - r * 0.7).toInt, y)
g2.setColor(new Color(0, 230, 255, 70))
g2.fillOval(
(x - r).toInt - 10,
(y - r).toInt - 10,
(r * 2 + 20).toInt,
(r * 2 + 20).toInt
)
g2.setColor(new Color(20, 210, 255))
g2.fillPolygon(diamond)
g2.setColor(Color.WHITE)
g2.drawPolygon(diamond)
} else {
// ENERGY BATTERY
val bw = objectSize * 0.75
val bh = objectSize
g2.setColor(new Color(160, 40, 255, 60))
g2.fillOval(
(x - bw).toInt,
(y - bh).toInt,
(bw * 2).toInt,
(bh * 2).toInt
)
g2.setColor(new Color(150, 60, 255))
g2.fillRoundRect(
(x - bw / 2).toInt,
(y - bh / 2).toInt,
bw.toInt,
bh.toInt,
8, 8
)
g2.setColor(Color.WHITE)
g2.drawString("+", x - 5, y + 6)
}
}
}
// PLAYER ROBOT
val playerX = objectX(playerLane, 1.0)
val playerBaseY = h - 145
val lift = (jumpHeight * 125).toInt
val playerY = playerBaseY - lift
// SHADOW
g2.setColor(new Color(0, 0, 0, 130))
g2.fillOval(
playerX - 55,
playerBaseY + 55,
110, 24
)
// SHIELD
if (shieldFrames > 0) {
val pulse = (5 * math.sin(animation * 8)).toInt
g2.setColor(new Color(0, 210, 255, 65))
g2.fillOval(
playerX - 65 - pulse,
playerY - 70 - pulse,
130 + pulse * 2,
155 + pulse * 2
)
g2.setColor(new Color(80, 230, 255, 200))
g2.setStroke(new BasicStroke(3f))
g2.drawOval(
playerX - 65 - pulse,
playerY - 70 - pulse,
130 + pulse * 2,
155 + pulse * 2
)
}
// BOARD
val board = new Polygon()
board.addPoint(playerX - 43, playerY + 47)
board.addPoint(playerX + 43, playerY + 47)
board.addPoint(playerX + 33, playerY + 68)
board.addPoint(playerX - 33, playerY + 68)
g2.setColor(new Color(0, 200, 255))
g2.fillPolygon(board)
g2.setColor(Color.WHITE)
g2.drawPolygon(board)
// ROBOT LEGS
g2.setColor(new Color(80, 170, 255))
g2.fillRoundRect(
playerX - 20, playerY + 20,
13, 32, 8, 8
)
g2.fillRoundRect(
playerX + 7, playerY + 20,
13, 32, 8, 8
)
// ROBOT BODY
g2.setColor(new Color(30, 90, 200))
g2.fillRoundRect(
playerX - 25, playerY - 28,
50, 57, 14, 14
)
g2.setColor(new Color(100, 220, 255))
g2.setStroke(new BasicStroke(3f))
g2.drawRoundRect(
playerX - 25, playerY - 28,
50, 57, 14, 14
)
// ARMS
g2.setColor(new Color(80, 190, 255))
g2.setStroke(new BasicStroke(8f))
g2.drawLine(
playerX - 23, playerY - 15,
playerX - 48, playerY + 8
)
g2.drawLine(
playerX + 23, playerY - 15,
playerX + 48, playerY + 8
)
// HEAD
g2.setColor(new Color(130, 210, 255))
g2.fillOval(
playerX - 24, playerY - 68,
48, 43
)
g2.setColor(new Color(10, 20, 60))
g2.fillRoundRect(
playerX - 17, playerY - 55,
34, 15, 8, 8
)
// EYES
g2.setColor(new Color(0, 240, 255))
g2.fillOval(
playerX - 12, playerY - 51,
8, 7
)
g2.fillOval(
playerX + 4, playerY - 51,
8, 7
)
// HUD
g2.setColor(new Color(3, 12, 35, 225))
g2.fillRoundRect(18, 18, 275, 160, 18, 18)
g2.setColor(new Color(0, 210, 255))
g2.setStroke(new BasicStroke(2f))
g2.drawRoundRect(18, 18, 275, 160, 18, 18)
g2.setFont(new Font("Consolas", Font.BOLD, 21))
g2.drawString("NEON PORTAL RUNNER", 33, 49)
g2.setFont(new Font("Consolas", Font.BOLD, 15))
g2.setColor(Color.WHITE)
g2.drawString("SCORE : " + score, 35, 82)
g2.drawString("CRYSTALS : " + crystals, 35, 108)
g2.drawString("LIVES : " + lives, 35, 134)
g2.drawString(
"SPEED : " + math.round(speed * 3000),
35, 160
)
// ENERGY PANEL
g2.setColor(new Color(3, 12, 35, 225))
g2.fillRoundRect(
w - 235, 18, 215, 95, 15, 15
)
g2.setColor(new Color(180, 80, 255))
g2.drawRoundRect(
w - 235, 18, 215, 95, 15, 15
)
g2.setFont(new Font("Consolas", Font.BOLD, 16))
g2.setColor(Color.WHITE)
g2.drawString("ENERGY", w - 215, 48)
g2.setColor(Color.DARK_GRAY)
g2.fillRoundRect(w - 215, 63, 175, 18, 8, 8)
g2.setColor(new Color(150, 70, 255))
g2.fillRoundRect(
w - 215, 63,
(175 * energy / 100).toInt,
18, 8, 8
)
g2.setColor(Color.WHITE)
g2.drawString(
math.round(energy).toString + "%",
w - 215, 101
)
if (shieldFrames > 0) {
g2.setColor(new Color(0, 220, 255))
g2.drawString("SHIELD ACTIVE", w - 215, 139)
}
// HIT EFFECT
if (hitFlash > 0) {
g2.setColor(new Color(255, 20, 50, 45))
g2.fillRect(0, 0, w, h)
}
// INSTRUCTIONS
g2.setFont(new Font("Arial", Font.BOLD, 14))
g2.setColor(new Color(130, 220, 255))
g2.drawString("LEFT / RIGHT = MOVE", 22, h - 24)
g2.drawString("SPACE = JUMP", 235, h - 24)
g2.drawString("E = SHIELD", 395, h - 24)
g2.drawString("P = PAUSE", 525, h - 24)
g2.drawString("R = RESTART", 650, h - 24)
// PAUSE SCREEN
if (paused && !gameOver) {
g2.setColor(new Color(0, 0, 0, 190))
g2.fillRect(0, 0, w, h)
g2.setColor(Color.WHITE)
g2.setFont(new Font("Arial", Font.BOLD, 55))
g2.drawString(
"GAME PAUSED",
cx - 190, h / 2
)
g2.setFont(new Font("Arial", Font.BOLD, 20))
g2.drawString(
"PRESS P TO CONTINUE",
cx - 130, h / 2 + 50
)
}
// GAME OVER
if (gameOver) {
g2.setColor(new Color(0, 0, 0, 210))
g2.fillRect(0, 0, w, h)
g2.setFont(new Font("Arial", Font.BOLD, 58))
g2.setColor(new Color(255, 55, 90))
val title = "GAME OVER"
val fm = g2.getFontMetrics
g2.drawString(
title,
(w - fm.stringWidth(title)) / 2,
h / 2 - 45
)
g2.setColor(Color.WHITE)
g2.setFont(new Font("Arial", Font.BOLD, 24))
g2.drawString(
"FINAL SCORE : " + score,
cx - 125, h / 2 + 10
)
g2.drawString(
"CRYSTALS COLLECTED : " + crystals,
cx - 170, h / 2 + 47
)
g2.setColor(new Color(0, 230, 255))
g2.drawString(
"PRESS R TO PLAY AGAIN",
cx - 160, h / 2 + 100
)
}
g2.dispose()
}
}
// PANEL SETUP
panel.setFocusable(true)
panel.setBackground(Color.BLACK)
frame.setContentPane(panel)
// KEYBOARD SYSTEM
def bind(
stroke: String,
name: String
)(action: => Unit): Unit = {
panel.getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW
).put(
KeyStroke.getKeyStroke(stroke),
name
)
panel.getActionMap.put(
name,
new AbstractAction {
override def actionPerformed(
e: ActionEvent
): Unit = action
}
)
}
// MOVEMENT
bind("pressed LEFT", "left") {
if (!gameOver && !paused) {
playerLane = math.max(-1, playerLane - 1)
}
}
bind("pressed A", "leftA") {
if (!gameOver && !paused) {
playerLane = math.max(-1, playerLane - 1)
}
}
bind("pressed RIGHT", "right") {
if (!gameOver && !paused) {
playerLane = math.min(1, playerLane + 1)
}
}
bind("pressed D", "rightD") {
if (!gameOver && !paused) {
playerLane = math.min(1, playerLane + 1)
}
}
// JUMP
bind("pressed SPACE", "jump") {
if (!gameOver && !paused &&
jumpHeight <= 0.0 && jumpVelocity == 0.0) {
jumpVelocity = 0.18
}
}
// SHIELD
bind("pressed E", "shield") {
if (!gameOver && !paused &&
energy >= 25.0 && shieldFrames <= 0) {
energy -= 25.0
shieldFrames = 180
}
}
// NITRO
bind("pressed SHIFT", "boostOn") {
boost = true
}
bind("released SHIFT", "boostOff") {
boost = false
}
// PAUSE
bind("pressed P", "pause") {
if (!gameOver) {
paused = !paused
}
}
// RESTART
bind("pressed R", "restart") {
resetGame()
}
// TIMER
val timer = new Timer(16, new ActionListener {
override def actionPerformed(e: ActionEvent): Unit = {
animation += 0.045
if (!paused && !gameOver) {
frameCount += 1
score += 1
// SPEED
if (boost && energy > 0) {
speed = 0.025
energy = math.max(0.0, energy - 0.55)
} else {
speed = 0.010 +
math.min(score / 300000.0, 0.015)
energy = math.min(100.0, energy + 0.025)
}
// ROAD
roadOffset = (
roadOffset + speed * 2.5
) % 1.0
// JUMP PHYSICS
if (jumpHeight > 0.0 || jumpVelocity > 0.0) {
jumpHeight += jumpVelocity
jumpVelocity -= 0.012
if (jumpHeight <= 0.0) {
jumpHeight = 0.0
jumpVelocity = 0.0
}
}
// SHIELD
if (shieldFrames > 0) {
shieldFrames -= 1
}
if (hitFlash > 0) {
hitFlash -= 1
}
// SPAWNING
val spawnEvery = math.max(
30,
55 - score / 1500
)
if (frameCount % spawnEvery == 0) {
val lane = random.nextInt(3) - 1
val roll = random.nextInt(100)
val kind =
if (roll < 55) 0
else if (roll < 88) 1
else 2
things += Thing(lane, 0.015, kind)
}
// MOVE OBJECTS
things.foreach { o =>
val oldZ = o.z
o.z += speed
if (!o.resolved &&
oldZ < 0.87 &&
o.z >= 0.87) {
o.resolved = true
if (o.lane == playerLane) {
if (o.kind == 0) {
if (jumpHeight < 0.45 &&
shieldFrames <= 0) {
lives -= 1
hitFlash = 25
if (lives <= 0) {
gameOver = true
}
} else {
score += 30
}
} else if (o.kind == 1) {
crystals += 1
score += 100
energy = math.min(
100.0,
energy + 12.0
)
} else {
energy = math.min(
100.0,
energy + 30.0
)
score += 50
}
}
}
}
// REMOVE OLD OBJECTS
val remaining = things.filter(_.z <= 1.10)
things.clear()
things ++= remaining
}
panel.repaint()
}
})
// SHOW ACTUAL GAME WINDOW
frame.setVisible(true)
timer.start()
panel.requestFocusInWindow()
}
})