Class AgiKart

All Implemented Interfaces:
ActionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible

public class AgiKart extends JPanel implements ActionListener
AgiKart is a high-performance pseudo-3D retro kart racing game in Java Swing.

Utilizing a Mode 7 style perspective ground projection algorithm, depth-sorted 3D billboard rendering for sprites (trees, item boxes, karts, and obstacles), responsive drift-and-slide kart physics, and waypoint-following AI opponents, it provides a highly polished, authentic classic 16-bit racing experience. It also features procedural audio synthesis using Java's built-in MIDI Synthesizer.

Author:
anahata
See Also:
  • Field Details

    • SCREEN_WIDTH

      private static final int SCREEN_WIDTH
      The logical screen width for the Mode 7 low-res rendering buffer.
      See Also:
    • SCREEN_HEIGHT

      private static final int SCREEN_HEIGHT
      The logical screen height for the Mode 7 low-res rendering buffer.
      See Also:
    • WINDOW_WIDTH

      private static final int WINDOW_WIDTH
      The actual display width of the game window.
      See Also:
    • WINDOW_HEIGHT

      private static final int WINDOW_HEIGHT
      The actual display height of the game window.
      See Also:
    • TRACK_POINTS

      private static final double[][] TRACK_POINTS
      Coordinates representing the center loop of the race track.
    • ITEM_BOX_LOCATIONS

      private static final double[][] ITEM_BOX_LOCATIONS
      Center coordinates for item box spawners.
    • trees

      private final List<AgiKart.PineTree> trees
      List of static tree coordinates placed alongside the road borders.
    • itemBoxes

      private final List<AgiKart.ItemBox> itemBoxes
      List of active item boxes in the world.
    • bananas

      private final List<AgiKart.PlacedBanana> bananas
      List of bananas placed on the track.
    • shells

      private final List<AgiKart.ThrownShell> shells
      List of active red shells traveling on the track.
    • trackMushrooms

      private final List<AgiKart.TrackMushroom> trackMushrooms
      List of active roadside collectible Super Mushrooms.
    • player

      private AgiKart.KartEntity player
      The human player entity.
    • opponents

      private final List<AgiKart.KartEntity> opponents
      The list of AI competitor entities.
    • cameraHeight

      private final double cameraHeight
      Height of the camera above the ground plane.
      See Also:
    • fov

      private double fov
      Field of View perspective scaling factor.
    • horizon

      private final int horizon
      Horizon row index on the low-res screen buffer.
      See Also:
    • screenBuffer

      private BufferedImage screenBuffer
      Low-resolution rendering canvas.
    • screenPixels

      private int[] screenPixels
      Direct pointer to the low-resolution canvas pixels.
    • trackTexture

      private BufferedImage trackTexture
      High-resolution, off-screen track texture map.
    • trackPixels

      private int[] trackPixels
      Direct pointer to the track texture map pixels.
    • timer

      private Timer timer
      High-frequency game loop timer.
    • keyLeft

      private boolean keyLeft
      Steering flag (turning left).
    • keyRight

      private boolean keyRight
      Steering flag (turning right).
    • keyUp

      private boolean keyUp
      Acceleration flag.
    • keyDown

      private boolean keyDown
      Braking / Reverse flag.
    • raceStarted

      private boolean raceStarted
      Flag indicating if the race has started (after countdown).
    • countdownFrames

      private int countdownFrames
      The starting countdown timer frames.
    • gameOver

      private boolean gameOver
      Flag indicating if the player has finished all laps.
    • gameTicks

      private int gameTicks
      Global frame tick counter.
    • shootCooldown

      private int shootCooldown
      Direct blaster shell fire cooldown.
    • finalLapWarningPlayed

      private boolean finalLapWarningPlayed
      Warning triggered when the player enters the final lap.
    • screenShake

      private int screenShake
      Camera screen shake intensity timer.
    • OIL_SLICKS

      private static final double[][] OIL_SLICKS
      Coordinates for stationary roadside Oil Slicks that trigger spinouts.
    • soundPool

      private final ExecutorService soundPool
      Single-threaded pool for non-blocking procedural sound playbacks.
    • synth

      private Synthesizer synth
      MIDI Synthesizer interface.
    • midiChannel

      private MidiChannel midiChannel
      The main MIDI channel for lead synthesizers.
    • engineChannel

      private MidiChannel engineChannel
      Continuous MIDI channel for engine humming sound.
    • raceStartTime

      private long raceStartTime
    • currentLapStartTime

      private long currentLapStartTime
    • bestLapTime

      private double bestLapTime
    • lastLapTime

      private double lastLapTime
    • particles

      private final List<AgiKart.Particle> particles
  • Constructor Details

    • AgiKart

      public AgiKart()
      Constructs a new AgiKart game panel and triggers resources initialization.
  • Method Details

    • playNoteAsync

      private void playNoteAsync(int note, int velocity, int durationMs)
      Dispatches a musical note command asynchronously to prevent blocking the game thread.
    • playItemPickupSound

      private void playItemPickupSound()
      Plays a high-pitched ding sound when picking up items.
    • playBoostSound

      private void playBoostSound()
      Plays a sweeping pitch sound during a mushroom boost.
    • playCrashSound

      private void playCrashSound()
      Plays a descending pitch sound when a kart spins out.
    • playFinalLapSound

      private void playFinalLapSound()
      Plays a high-intensity warning fanfare when entering the final lap.
    • playLapCompletedSound

      private void playLapCompletedSound()
      Plays a grand championship lap completed fanfare.
    • initSound

      private void initSound()
      Initializes the Java MIDI Synthesizer and configures instrument patching.
    • generateTrackTexture

      private void generateTrackTexture()
      Generates a beautiful 1024x1024 track texture procedurally in memory.

      Renders a forest green background grid, draws a custom winding race course, overlays red-and-white striped curb borders, and stamps a checkered finish line.

    • initEnvironment

      private void initEnvironment()
      Programmatically populates environment details, including curbside pine trees and item boxes.
    • initKarts

      private void initKarts()
      Initializes karts, setting up a starting grid behind the start-finish line.
    • initControls

      private void initControls()
      Binds keyboard controls to manage player movement and item triggers.
    • triggerPlayerItem

      private void triggerPlayerItem()
      Triggers the player's active item and manages world placements.
    • shootGreenShellDirect

      private void shootGreenShellDirect()
      Fires a green shell directly in front of the player's kart with a cooldown.
    • resetGame

      private void resetGame()
      Resets the entire race standings and parameters to begin anew.
    • cleanup

      public void cleanup()
      Shuts down background threads and releases MIDI Synthesizer resources.
    • paintComponent

      protected void paintComponent(Graphics g)

      Processes the 3D projection, depth-sorted blitting, and HUD rendering overlays.

      Overrides:
      paintComponent in class JComponent
    • renderMode7

      private void renderMode7()
      Implements a fast, hardware-friendly Mode 7 ground perspective floor caster.
    • renderBackgroundSceneries

      private void renderBackgroundSceneries(Graphics2D g)
      Renders background mountain sceneries scrolling smoothly as the player turns.
    • render3DSprites

      private void render3DSprites(Graphics2D g)
      Gathers, depth-sorts, and draws all 3D projected billboard sprites on the screen.
    • projectWorldPoint

      private AgiKart.SpriteProjection projectWorldPoint(double wx, double wy, double wz, double camX, double camY, double cos, double sin)
      Projects a 3D world coordinate onto the low-resolution screen canvas relative to the camera.
    • drawPineTree

      private void drawPineTree(Graphics2D g, double sx, double sy, double scale)
      Vector draws a beautiful pine tree billboard sprite.
    • drawItemBox

      private void drawItemBox(Graphics2D g, double sx, double sy, double scale)
      Vector draws a hovering, glowing item box spawner.
    • drawPlacedBanana

      private void drawPlacedBanana(Graphics2D g, double sx, double sy, double scale)
      Vector draws a placed banana peel on the road.
    • drawTrackMushroom

      private void drawTrackMushroom(Graphics2D g, double sx, double sy, double scale)
      Vector draws a gorgeous 3D billboard mushroom standing on the ground.
    • drawThrownShell

      private void drawThrownShell(Graphics2D g, double sx, double sy, double scale)
      Vector draws a traveling homing red shell on the road.
    • drawWorldKart

      private void drawWorldKart(Graphics2D g, double sx, double sy, double scale, Color color, String name, double angle, int spinTimer)
      Vector draws a gorgeous detailed kart competitor with chassis, wheels, driver, and nametag.
    • renderHUD

      private void renderHUD(Graphics2D g)
      Renders a highly-polished high-resolution HUD, featuring real-time minimaps and race positions.
    • drawItemIcon

      private void drawItemIcon(Graphics2D g, int cx, int cy, int size, String itemType)
      Vector draws stunning retro-quality HUD item icons.
    • calculatePlayerPosition

      private int calculatePlayerPosition()
      Calculates the player's real-time position rank compared to other karts.
    • sortCompetitors

      private void sortCompetitors(List<AgiKart.KartEntity> list)
      Sorts the racing competitors based on lap counts and next waypoint progress.
    • actionPerformed

      public void actionPerformed(ActionEvent e)

      Executes the primary high-frequency physics tick, collisions, and state transitions.

      Specified by:
      actionPerformed in interface ActionListener
    • updatePlayerPhysics

      private void updatePlayerPhysics()
      Updates the player's kart speed, angles, drift sliding, and coordinate bounds.
    • updateAICompetitor

      private void updateAICompetitor(AgiKart.KartEntity ai)
      Updates an AI competitor's intelligence, steering vectors, and waypoint progress.
    • triggerAIItem

      private void triggerAIItem(AgiKart.KartEntity ai)
      Executes AI item activation decisions based on current item held.
    • updateKartWaypoint

      private void updateKartWaypoint(AgiKart.KartEntity kart)
      Updates waypoints checklist and lap completions for karts.
    • updateHomingShells

      private void updateHomingShells()
      Updates active traveling red shells on track checkpoints towards targets.
    • checkBumpingCollisions

      private void checkBumpingCollisions()
      Calculates bumping/colliding bouncing interactions between karts.
    • checkItemOverlaps

      private void checkItemOverlaps()
      Evaluates item pickup boxes and obstacle collisions on the track.
    • main

      public static void main(String[] args)
      Standalone executable entry-point.
    • playBarsaAnthem

      private void playBarsaAnthem()
      Plays an 8-bit retro melodic fanfare of the classic Barça Anthem.