Code Sketch
Bixuu
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("MARKET DEALER - CASH CHASE 3D")
frame.setSize(1100, 760)
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
frame.setLocationRelativeTo(null)
frame.setResizable(false)
// GAME STATES
var screen = 0
var playerMode = 1
var paused = false
var won = false
// PLAYER DATA
var p1Lane = 0
var p2Lane = 1
var p1Lives = 3
var p2Lives = 3
var p1Cash = 0
var p2Cash = 0
var totalScore = 0
var nitro = 100.0
var dealerChase = 0.0
var escapeProgress = 0.0
var gameTime = 1800
var elapsed = 0
var gameTicks = 0
var worldSpeed = 0.006
var roadOffset = 0.0
var animation = 0.0
var p1Boost = false
var p2Boost = false
var p1Hit = 0
var p2Hit = 0
var p1Flash = 0
var p2Flash = 0
var lastSecond = System.currentTimeMillis()
// GAME OBJECT
case class WorldItem(
var lane: Int,
var z: Double,
kind: Int,
var done: Boolean = false
)
val items = ArrayBuffer[WorldItem]()
val stars = Array.fill(130)(
(
random.nextInt(1100),
random.nextInt(250),
random.nextInt(3) + 1
)
)
// RESET GAME
def startGame(mode: Int): Unit = {
playerMode = mode
screen = 1
paused = false
won = false
p1Lane = 0
p2Lane = 1
p1Lives = 3
p2Lives = 3
p1Cash = 0
p2Cash = 0
totalScore = 0
nitro = 100
dealerChase = 0
escapeProgress = 0
gameTime = 1800
elapsed = 0
gameTicks = 0
worldSpeed = 0.006
roadOffset = 0
p1Boost = false
p2Boost = false
p1Hit = 0
p2Hit = 0
p1Flash = 0
p2Flash = 0
items.clear()
lastSecond = System.currentTimeMillis()
}
def backToMenu(): Unit = {
screen = 0
paused = false
p1Boost = false
p2Boost = false
items.clear()
}
def totalCash: Int = p1Cash + p2Cash
def alive: Boolean = {
if (playerMode == 1) p1Lives > 0
else p1Lives > 0 || p2Lives > 0
}
// DRAW HELPERS
def centerText(
g: Graphics2D,
text: String,
x: Int,
y: Int
): Unit = {
val fm = g.getFontMetrics
g.drawString(
text,
x - fm.stringWidth(text) / 2,
y
)
}
def drawButton(
g: Graphics2D,
x: Int,
y: Int,
w: Int,
h: Int,
title: String,
color: Color
): Unit = {
g.setColor(new Color(0, 0, 0, 100))
g.fillRoundRect(
x + 4, y + 5, w, h, 20, 20
)
g.setPaint(new GradientPaint(
x, y, color,
x, y + h, color.darker()
))
g.fillRoundRect(
x, y, w, h, 20, 20
)
g.setColor(Color.WHITE)
g.setStroke(new BasicStroke(2f))
g.drawRoundRect(
x, y, w, h, 20, 20
)
g.setFont(new Font("Arial", Font.BOLD, 19))
centerText(
g, title, x + w / 2, y + h / 2 + 7
)
}
def drawDealer(
g: Graphics2D,
x: Int,
y: Int,
scale: Double
): Unit = {
val head = (65 * scale).toInt
val bodyW = (115 * scale).toInt
val bodyH = (110 * scale).toInt
g.setColor(new Color(0, 0, 0, 120))
g.fillOval(
x - bodyW / 2,
y + bodyH,
bodyW, 25
)
// JACKET
g.setColor(new Color(20, 20, 30))
g.fillRoundRect(
x - bodyW / 2,
y + head / 2,
bodyW,
bodyH,
25, 25
)
// FACE
g.setColor(new Color(185, 130, 95))
g.fillOval(
x - head / 2,
y - head,
head,
head
)
// HAIR
g.setColor(new Color(25, 20, 20))
g.fillArc(
x - head / 2,
y - head,
head,
head / 2,
0, 180
)
// SUNGLASSES
g.setColor(Color.BLACK)
g.fillRoundRect(
x - head / 2 + 7,
y - head / 2,
head / 2 - 5,
13,
5, 5
)
g.fillRoundRect(
x + 3,
y - head / 2,
head / 2 - 5,
13,
5, 5
)
g.setColor(Color.WHITE)
g.drawLine(
x - 3,
y - head / 2 + 5,
x + 4,
y - head / 2 + 5
)
// SHIRT
g.setColor(new Color(160, 25, 45))
g.fillPolygon({
val p = new Polygon()
p.addPoint(x, y + head / 2)
p.addPoint(x - 22, y + head)
p.addPoint(x, y + head + 15)
p.addPoint(x + 22, y + head)
p
})
}
def drawCar(
g: Graphics2D,
x: Int,
y: Int,
scale: Double,
color: Color,
tag: String
): Unit = {
val cw = (82 * scale).toInt
val ch = (110 * scale).toInt
// SHADOW
g.setColor(new Color(0, 0, 0, 130))
g.fillOval(
x - cw / 2 - 10,
y + ch / 2 - 5,
cw + 20,
25
)
// CAR BODY
val body = new Polygon()
body.addPoint(x, y - ch / 2)
body.addPoint(x + cw / 2, y - ch / 5)
body.addPoint(x + cw / 2 - 5, y + ch / 2)
body.addPoint(x - cw / 2 + 5, y + ch / 2)
body.addPoint(x - cw / 2, y - ch / 5)
g.setPaint(new GradientPaint(
x.toFloat, (y - ch / 2).toFloat,
color.brighter(),
x.toFloat, (y + ch / 2).toFloat,
color.darker()
))
g.fillPolygon(body)
g.setColor(Color.WHITE)
g.setStroke(new BasicStroke(2f))
g.drawPolygon(body)
// ROOF
val roof = new Polygon()
roof.addPoint(x - cw / 3, y - ch / 5)
roof.addPoint(x + cw / 3, y - ch / 5)
roof.addPoint(x + cw / 4, y + ch / 5)
roof.addPoint(x - cw / 4, y + ch / 5)
g.setColor(new Color(25, 35, 65))
g.fillPolygon(roof)
g.setColor(new Color(130, 220, 255))
g.drawPolygon(roof)
// WINDSHIELD
g.setColor(new Color(125, 220, 255))
g.fillPolygon({
val p = new Polygon()
p.addPoint(x - cw / 4, y - ch / 7)
p.addPoint(x + cw / 4, y - ch / 7)
p.addPoint(x + cw / 5, y + ch / 14)
p.addPoint(x - cw / 5, y + ch / 14)
p
})
// HEADLIGHTS
g.setColor(new Color(255, 240, 160))
g.fillOval(
x - cw / 2 + 5,
y + ch / 3,
(cw / 5).max(3),
(ch / 10).max(3)
)
g.fillOval(
x + cw / 2 - cw / 5 - 5,
y + ch / 3,
(cw / 5).max(3),
(ch / 10).max(3)
)
// LABEL
if (scale >= 0.8) {
g.setColor(Color.WHITE)
g.setFont(new Font("Arial", Font.BOLD, 12))
centerText(
g, tag, x, y + ch / 2 + 18
)
}
}
// MAIN GAME PANEL
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 / 3
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
)
// SKY
g2.setPaint(new GradientPaint(
0, 0, new Color(5, 8, 35),
0, horizon, new Color(90, 35, 95)
))
g2.fillRect(0, 0, w, h)
// MOON
g2.setColor(new Color(255, 210, 160, 180))
g2.fillOval(835, 65, 100, 100)
// STARS
for (i <- stars.indices) {
val s = stars(i)
val b = (
150 + 100 * math.abs(
math.sin(animation + i)
)
).toInt.min(255)
g2.setColor(new Color(b, b, 255))
g2.fillOval(
s._1, s._2,
2 + i % 2, 2 + i % 2
)
}
if (screen == 0) {
// MENU BACKGROUND CITY
for (i <- 0 until 12) {
val bw = 40 + (i * 17) % 60
val bh = 100 + (i * 29) % 150
val x = i * 100 - 20
g2.setColor(new Color(15, 22, 48))
g2.fillRect(
x, horizon - bh,
bw, bh + 80
)
g2.setColor(new Color(0, 200, 255, 130))
for (j <- 0 until 3) {
g2.fillRect(
x + 8 + j * 14,
horizon - bh + 20,
6, 10
)
}
}
// MENU TITLE
g2.setFont(
new Font("Arial", Font.BOLD, 53)
)
g2.setColor(new Color(0, 220, 255))
centerText(
g2,
"MARKET DEALER",
cx, 95
)
g2.setFont(
new Font("Arial", Font.BOLD, 33)
)
g2.setColor(new Color(255, 45, 100))
centerText(
g2,
"CASH CHASE 3D",
cx, 140
)
// DEALER
drawDealer(
g2, 210, 370, 1.5
)
// DIALOGUE BOX
g2.setColor(new Color(8, 15, 35, 235))
g2.fillRoundRect(
320, 205,
700, 170,
22, 22
)
g2.setColor(new Color(255, 185, 40))
g2.setStroke(new BasicStroke(3f))
g2.drawRoundRect(
320, 205,
700, 170,
22, 22
)
g2.setFont(
new Font("Arial", Font.BOLD, 23)
)
g2.setColor(new Color(255, 205, 70))
g2.drawString(
"MARKET DEALER:",
350, 245
)
g2.setFont(
new Font("Arial", Font.BOLD, 21)
)
g2.setColor(Color.WHITE)
g2.drawString(
"\"You can trust me... I can give you rupees!\"",
350, 285
)
g2.setFont(
new Font("Arial", Font.BOLD, 19)
)
g2.setColor(new Color(255, 70, 90))
g2.drawString(
"HEY! HE TOOK YOUR Rs 5,000!",
350, 325
)
g2.setColor(Color.WHITE)
g2.setFont(
new Font("Arial", Font.PLAIN, 15)
)
g2.drawString(
"GET YOUR MONEY BACK AND ESCAPE!",
350, 350
)
// MENU BUTTONS
drawButton(
g2, 305, 440,
225, 65,
"SOLO MODE",
new Color(25, 100, 210)
)
drawButton(
g2, 570, 440,
225, 65,
"2 PLAYER MODE",
new Color(180, 35, 110)
)
g2.setColor(Color.WHITE)
g2.setFont(new Font("Arial", Font.BOLD, 15))
centerText(
g2,
"COLLECT Rs 5,000 | ESCAPE THE DEALER",
cx, 550
)
centerText(
g2,
"P1: A / D / W BOOST P2: ARROWS / UP BOOST",
cx, 585
)
centerText(
g2,
"MISSION LENGTH: UP TO 30 MINUTES",
cx, 620
)
}
if (screen == 1) {
// CITY BUILDINGS
for (i <- 0 until 18) {
val z = (
(i / 18.0) + roadOffset * 0.65
) % 1.0
val y = horizon + (h - horizon) * z
val half = 48 + z * 390
val spread = half + 25 + z * 90
val bh = 20 + z * 130
val bw = 10 + z * 55
for (side <- Array(-1, 1)) {
val x = cx + side * spread
g2.setColor(new Color(13, 20, 44))
g2.fillRect(
(x - bw / 2).toInt,
(y - bh).toInt,
bw.toInt,
bh.toInt
)
g2.setColor(new Color(0, 200, 255, 140))
for (j <- 0 until 3) {
g2.fillRect(
(x - bw / 2 + 4 + j * 12).toInt,
(y - bh + 10 + j * 13).toInt,
math.max(2, (bw * 0.14).toInt),
math.max(2, (bh * 0.08).toInt)
)
}
g2.setColor(new Color(255, 40, 120, 100))
g2.drawLine(
x.toInt,
(y - bh).toInt,
x.toInt,
y.toInt
)
}
}
// MARKET SIGN
g2.setColor(new Color(15, 20, 45, 220))
g2.fillRoundRect(
cx - 100, horizon - 85,
200, 45,
12, 12
)
g2.setColor(new Color(0, 225, 255))
g2.setFont(
new Font("Arial", Font.BOLD, 20)
)
centerText(
g2, "NIGHT MARKET",
cx, horizon - 55
)
// ROAD
val road = new Polygon()
road.addPoint(cx - 48, horizon)
road.addPoint(cx + 48, horizon)
road.addPoint(cx + 445, h)
road.addPoint(cx - 445, h)
g2.setColor(new Color(29, 30, 43))
g2.fillPolygon(road)
// ROAD GLOW
g2.setStroke(new BasicStroke(5f))
g2.setColor(new Color(0, 210, 255))
g2.drawLine(
cx - 48, horizon,
cx - 445, h
)
g2.drawLine(
cx + 48, horizon,
cx + 445, h
)
// ROAD LANES
for (lane <- 1 to 2) {
for (i <- 0 until 13) {
val z = (
i / 13.0 + roadOffset
) % 1.0
val z2 = math.min(1.0, z + 0.04)
val topX = cx - 48 + lane * 32
val bottomX = cx - 445 + lane * 296
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(210, 225, 255))
g2.setStroke(
new BasicStroke(
math.max(1.0, z * 7).toFloat
)
)
g2.drawLine(
x1.toInt, y1.toInt,
x2.toInt, y2.toInt
)
}
}
// 3D PROJECTOR
def itemX(lane: Int, z: Double): Int = {
val half = 48 + z * 395
(
cx + lane * half * 0.58
).toInt
}
def itemY(z: Double): Int = {
(
horizon + (h - horizon) * z
).toInt
}
// DRAW ITEMS
items.toList.sortBy(_.z).foreach { o =>
val z = o.z
if (z >= 0 && z <= 1.10) {
val x = itemX(o.lane, z)
val y = itemY(z)
val size = 12 + z * 65
if (o.kind == 0) {
// 3D OBSTACLE CRATE
val crate = size.toInt
val front = new Polygon()
front.addPoint(
x - crate / 2, y - crate / 2
)
front.addPoint(
x + crate / 2, y - crate / 2
)
front.addPoint(
x + crate / 2, y + crate / 2
)
front.addPoint(
x - crate / 2, y + crate / 2
)
g2.setColor(new Color(110, 55, 30))
g2.fillPolygon(front)
val side = new Polygon()
side.addPoint(
x + crate / 2, y - crate / 2
)
side.addPoint(
x + crate / 2 + crate / 5,
y - crate / 2 - crate / 5
)
side.addPoint(
x + crate / 2 + crate / 5,
y + crate / 2 - crate / 5
)
side.addPoint(
x + crate / 2,
y + crate / 2
)
g2.setColor(new Color(160, 85, 40))
g2.fillPolygon(side)
g2.setColor(Color.ORANGE)
g2.drawPolygon(front)
} else if (o.kind == 1) {
// GOLD MONEY BAG
g2.setColor(
new Color(255, 190, 20, 70)
)
g2.fillOval(
(x - size).toInt,
(y - size).toInt,
(size * 2).toInt,
(size * 2).toInt
)
g2.setColor(new Color(255, 180, 20))
g2.fillOval(
(x - size / 2).toInt,
(y - size / 2).toInt,
size.toInt,
size.toInt
)
g2.setColor(new Color(100, 45, 5))
g2.setFont(
new Font("Arial", Font.BOLD,
math.max(8, size.toInt / 3))
)
centerText(
g2, "Rs",
x, y + 5
)
} else {
// NITRO BOOST
val diamond = new Polygon()
diamond.addPoint(
x, (y - size / 2).toInt
)
diamond.addPoint(
(x + size / 2).toInt, y
)
diamond.addPoint(
x, (y + size / 2).toInt
)
diamond.addPoint(
(x - size / 2).toInt, y
)
g2.setColor(
new Color(150, 40, 255, 80)
)
g2.fillOval(
(x - size).toInt,
(y - size).toInt,
(size * 2).toInt,
(size * 2).toInt
)
g2.setColor(new Color(180, 80, 255))
g2.fillPolygon(diamond)
g2.setColor(Color.WHITE)
g2.drawPolygon(diamond)
}
}
}
// PLAYER CARS
if (p1Lives > 0) {
val px = itemX(p1Lane, 0.96)
val py = h - 135
drawCar(
g2,
px,
py,
1.0,
new Color(0, 130, 255),
"P1"
)
if (p1Flash > 0) {
g2.setColor(new Color(255, 30, 30, 90))
g2.fillOval(px - 65, py - 70, 130, 170)
}
}
if (playerMode == 2 && p2Lives > 0) {
val px = itemX(p2Lane, 0.75)
val py = h - 255
drawCar(
g2,
px,
py,
0.80,
new Color(240, 50, 100),
"P2"
)
if (p2Flash > 0) {
g2.setColor(new Color(255, 30, 30, 90))
g2.fillOval(px - 55, py - 55, 110, 140)
}
}
// DEALER PURSUIT MIRROR
g2.setColor(new Color(5, 10, 28, 230))
g2.fillRoundRect(
cx - 120, 18,
240, 95,
18, 18
)
g2.setColor(new Color(255, 45, 65))
g2.drawRoundRect(
cx - 120, 18,
240, 95,
18, 18
)
g2.setColor(Color.WHITE)
g2.setFont(
new Font("Arial", Font.BOLD, 13)
)
centerText(
g2,
"DEALER PURSUIT",
cx, 39
)
// PURSUING CAR IN MIRROR
drawCar(
g2,
cx,
77,
0.32,
new Color(200, 25, 45),
""
)
// CHASE BAR
g2.setColor(Color.DARK_GRAY)
g2.fillRoundRect(
cx - 115, 120,
230, 13,
8, 8
)
g2.setColor(new Color(255, 45, 65))
g2.fillRoundRect(
cx - 115, 120,
(230 * dealerChase / 100).toInt,
13,
8, 8
)
// HUD
g2.setColor(new Color(5, 10, 30, 225))
g2.fillRoundRect(
16, 16,
255, 190,
18, 18
)
g2.setColor(new Color(0, 220, 255))
g2.drawRoundRect(
16, 16,
255, 190,
18, 18
)
g2.setFont(
new Font("Consolas", Font.BOLD, 18)
)
g2.drawString(
"CASH CHASE",
32, 47
)
g2.setColor(Color.WHITE)
g2.setFont(
new Font("Consolas", Font.BOLD, 15)
)
g2.drawString(
"TIME : " +
(gameTime / 60) + ":" +
(if (gameTime % 60 < 10) "0" else "") +
(gameTime % 60),
32, 80
)
g2.drawString(
"P1 CASH : Rs " + p1Cash,
32, 108
)
g2.drawString(
"P1 LIVES : " + p1Lives,
32, 136
)
if (playerMode == 2) {
g2.drawString(
"P2 CASH : Rs " + p2Cash,
32, 164
)
g2.drawString(
"P2 LIVES : " + p2Lives,
32, 190
)
} else {
g2.drawString(
"TOTAL : Rs " + totalCash,
32, 164
)
}
// NITRO HUD
g2.setColor(new Color(5, 10, 30, 220))
g2.fillRoundRect(
w - 220, 20,
195, 80,
15, 15
)
g2.setColor(Color.WHITE)
g2.setFont(
new Font("Arial", Font.BOLD, 16)
)
g2.drawString(
"NITRO",
w - 200, 46
)
g2.setColor(Color.DARK_GRAY)
g2.fillRoundRect(
w - 200, 57,
150, 16,
8, 8
)
g2.setColor(new Color(0, 210, 255))
g2.fillRoundRect(
w - 200, 57,
(150 * nitro / 100).toInt,
16, 8, 8
)
// OBJECTIVE HUD
g2.setColor(new Color(5, 10, 30, 210))
g2.fillRoundRect(
w - 225, 122,
205, 85,
15, 15
)
g2.setColor(Color.WHITE)
g2.setFont(
new Font("Arial", Font.BOLD, 15)
)
g2.drawString(
"TARGET Rs 5,000",
w - 208, 150
)
g2.drawString(
"COLLECTED " + totalCash,
w - 208, 177
)
// ESCAPE BAR
if (totalCash >= 5000) {
g2.setColor(new Color(10, 20, 30, 220))
g2.fillRoundRect(
cx - 125, 155,
250, 42,
10, 10
)
g2.setColor(new Color(0, 255, 150))
g2.setFont(
new Font("Arial", Font.BOLD, 15)
)
centerText(
g2,
"ESCAPE READY!",
cx, 174
)
g2.setColor(Color.DARK_GRAY)
g2.fillRoundRect(
cx - 110, 180,
220, 9,
6, 6
)
g2.setColor(new Color(0, 255, 150))
g2.fillRoundRect(
cx - 110, 180,
(220 * escapeProgress / 100).toInt,
9, 6, 6
)
}
// BOTTOM CONTROLS
g2.setFont(
new Font("Arial", Font.BOLD, 14)
)
g2.setColor(new Color(140, 230, 255))
g2.drawString(
"P1: A/D MOVE | W NITRO",
20, h - 50
)
if (playerMode == 2) {
g2.drawString(
"P2: LEFT/RIGHT MOVE | UP NITRO",
310, h - 50
)
}
g2.drawString(
"P: PAUSE R: RESTART",
725, h - 50
)
// PAUSE
if (paused) {
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)
)
centerText(
g2, "GAME PAUSED",
cx, h / 2
)
g2.setFont(
new Font("Arial", Font.BOLD, 20)
)
centerText(
g2, "PRESS P TO CONTINUE",
cx, h / 2 + 45
)
}
}
// END SCREEN
if (screen == 2) {
g2.setColor(new Color(0, 0, 0, 210))
g2.fillRect(0, 0, w, h)
g2.setFont(
new Font("Arial", Font.BOLD, 58)
)
g2.setColor(
if (won) new Color(0, 255, 160)
else new Color(255, 55, 85)
)
centerText(
g2,
if (won) "ESCAPE SUCCESS!" else "GAME OVER",
cx, 230
)
g2.setColor(Color.WHITE)
g2.setFont(
new Font("Arial", Font.BOLD, 25)
)
centerText(
g2,
"TOTAL CASH : Rs " + totalCash,
cx, 300
)
centerText(
g2,
"SCORE : " + totalScore,
cx, 345
)
g2.setColor(new Color(255, 205, 50))
g2.setFont(
new Font("Arial", Font.BOLD, 21)
)
centerText(
g2,
if (won)
"YOU GOT YOUR MONEY BACK!"
else
"THE MARKET DEALER CAUGHT YOU!",
cx, 405
)
drawButton(
g2,
cx - 135, 460,
270, 60,
"PLAY AGAIN",
new Color(25, 110, 210)
)
g2.setFont(
new Font("Arial", Font.BOLD, 15)
)
g2.setColor(Color.WHITE)
centerText(
g2,
"PRESS R TO RESTART | PRESS M FOR MENU",
cx, 560
)
}
g2.dispose()
}
}
panel.setBackground(Color.BLACK)
panel.setFocusable(true)
frame.setContentPane(panel)
// MOUSE CONTROLS
panel.addMouseListener(new MouseAdapter {
override def mousePressed(e: MouseEvent): Unit = {
val x = e.getX
val y = e.getY
if (screen == 0) {
if (y >= 430 && y <= 520) {
if (x >= 280 && x <= 540) {
startGame(1)
}
if (x >= 550 && x <= 820) {
startGame(2)
}
}
}
if (screen == 2) {
if (y >= 450 && y <= 535) {
startGame(playerMode)
}
}
panel.repaint()
}
})
// KEYBOARD BIND SYSTEM
def bind(
key: String,
name: String
)(action: => Unit): Unit = {
panel.getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW
).put(
KeyStroke.getKeyStroke(key),
name
)
panel.getActionMap.put(
name,
new AbstractAction {
override def actionPerformed(
e: ActionEvent
): Unit = {
action
panel.repaint()
}
}
)
}
// PLAYER 1 MOVEMENT
bind("pressed A", "p1left") {
if (screen == 1 && !paused && p1Lives > 0) {
p1Lane = math.max(-1, p1Lane - 1)
}
}
bind("pressed D", "p1right") {
if (screen == 1 && !paused && p1Lives > 0) {
p1Lane = math.min(1, p1Lane + 1)
}
}
bind("pressed W", "p1boost") {
p1Boost = true
}
bind("released W", "p1boostoff") {
p1Boost = false
}
// PLAYER 2 MOVEMENT
bind("pressed LEFT", "p2left") {
if (screen == 1 && !paused &&
playerMode == 2 && p2Lives > 0) {
p2Lane = math.max(-1, p2Lane - 1)
}
}
bind("pressed RIGHT", "p2right") {
if (screen == 1 && !paused &&
playerMode == 2 && p2Lives > 0) {
p2Lane = math.min(1, p2Lane + 1)
}
}
bind("pressed UP", "p2boost") {
p2Boost = true
}
bind("released UP", "p2boostoff") {
p2Boost = false
}
// PAUSE
bind("pressed P", "pause") {
if (screen == 1) {
paused = !paused
p1Boost = false
p2Boost = false
}
}
// RESTART
bind("pressed R", "restart") {
if (screen == 1 || screen == 2) {
startGame(playerMode)
}
}
// MENU
bind("pressed M", "menu") {
backToMenu()
}
// GAME ENGINE
val timer = new Timer(16, new ActionListener {
override def actionPerformed(e: ActionEvent): Unit = {
animation += 0.04
if (screen == 1 && !paused) {
gameTicks += 1
// TIMER
val now = System.currentTimeMillis()
if (now - lastSecond >= 1000) {
if (gameTime > 0) {
gameTime -= 1
elapsed += 1
}
lastSecond = now
}
// SPEED
worldSpeed = 0.006 +
math.min(elapsed / 120000.0, 0.003)
// NITRO
val usingNitro =
(p1Boost || (playerMode == 2 && p2Boost)) &&
nitro > 0
if (usingNitro) {
nitro = math.max(0.0, nitro - 0.18)
} else {
nitro = math.min(100.0, nitro + 0.035)
}
val currentSpeed =
if (usingNitro) worldSpeed * 1.7
else worldSpeed
// ROAD
roadOffset = (
roadOffset + currentSpeed * 2.0
) % 1.0
// DEALER CHASE
dealerChase = math.min(
100.0,
dealerChase + 0.0015 +
(if (totalCash >= 5000) 0.001 else 0.0)
)
if (usingNitro) {
dealerChase = math.max(
0.0,
dealerChase - 0.004
)
}
// SPAWN ITEMS
val spawnRate = math.max(
32,
65 - elapsed / 180
)
if (gameTicks % spawnRate == 0) {
val lane = random.nextInt(3) - 1
val roll = random.nextInt(100)
val kind =
if (roll < 42) 0
else if (roll < 86) 1
else 2
items += WorldItem(
lane,
0.015,
kind
)
}
// MOVE AND COLLECT ITEMS
items.foreach { o =>
val oldZ = o.z
o.z += currentSpeed
if (!o.done &&
oldZ < 0.86 &&
o.z >= 0.86) {
val hitP1 =
p1Lives > 0 && p1Lane == o.lane
val hitP2 =
playerMode == 2 &&
p2Lives > 0 && p2Lane == o.lane
if (o.kind == 0) {
// OBSTACLE
if (hitP1) {
p1Lives -= 1
p1Flash = 25
dealerChase = math.min(
100.0, dealerChase + 15
)
}
if (hitP2) {
p2Lives -= 1
p2Flash = 25
dealerChase = math.min(
100.0, dealerChase + 15
)
}
} else if (o.kind == 1) {
// MONEY BAG
if (hitP1) {
p1Cash += 1000
totalScore += 100
}
if (hitP2) {
p2Cash += 1000
totalScore += 100
}
} else {
// NITRO PICKUP
if (hitP1 || hitP2) {
nitro = math.min(
100.0,
nitro + 30.0
)
totalScore += 50
}
}
o.done = true
}
}
// REMOVE OLD ITEMS
val remaining = items.filter(
_.z <= 1.08
)
items.clear()
items ++= remaining
// ESCAPE MISSION
if (totalCash >= 5000) {
escapeProgress = math.min(
100.0,
escapeProgress + 0.12
)
}
// HEALTH EFFECTS
if (p1Flash > 0) p1Flash -= 1
if (p2Flash > 0) p2Flash -= 1
// WIN / LOSE CONDITIONS
if (escapeProgress >= 100) {
won = true
screen = 2
} else if (
!alive ||
dealerChase >= 100 ||
gameTime <= 0
) {
won = false
screen = 2
}
}
panel.repaint()
}
})
// WINDOW
frame.setVisible(true)
timer.start()
panel.requestFocusInWindow()
}
})