Class Snake

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

public class Snake extends JPanel implements ActionListener
A classic Snake game implementation with a Mapacho theme.

Features grid-based movement, randomized "cigar" spawning, and a score tracking system. Optimized for execution as a standalone easter egg from the support panel.

Author:
anahata
See Also:
  • Field Details

    • TILE_SIZE

      private final int TILE_SIZE
      The size of a single grid square in pixels.
      See Also:
    • WIDTH

      private final int WIDTH
      The width of the game board in pixels.
      See Also:
    • HEIGHT

      private final int HEIGHT
      The height of the game board in pixels.
      See Also:
    • ALL_TILES

      private final int ALL_TILES
      The maximum possible tiles fitting onto the board.
      See Also:
    • x

      private final int[] x
      Array tracking the horizontal coordinates of all snake segments.
    • y

      private final int[] y
      Array tracking the vertical coordinates of all snake segments.
    • bodyParts

      private int bodyParts
      The current length of the snake.
    • cigarsEaten

      private int cigarsEaten
      The number of cigars eaten during this round.
    • cigarX

      private int cigarX
      The current grid X coordinate of the cigar target.
    • cigarY

      private int cigarY
      The current grid Y coordinate of the cigar target.
    • direction

      private char direction
      The active snake movement direction (U, D, L, R).
    • running

      private boolean running
      Flag indicating if the game loop is active.
    • timer

      private Timer timer
      The animation and tick update timer.
    • random

      private Random random
      Random generator used to spawn new cigars.
  • Constructor Details

    • Snake

      public Snake()
      Constructs a new Snake game panel, initializing keyboard events and starting the game.
  • Method Details

    • startGame

      public void startGame()
      Initializes the game state and starts the animation timer.
    • paintComponent

      public void paintComponent(Graphics g)

      Handles the visual rendering of the mapacho-snake, the cigars, and the HUD (score and status messages).

      Overrides:
      paintComponent in class JComponent
    • draw

      public void draw(Graphics g)
      Renders the game elements.
      Parameters:
      g - the graphics context.
    • newCigar

      public void newCigar()
      Randomly positions a new cigar on the grid.
    • move

      public void move()
      Updates the snake's position based on the current direction.
    • checkCigar

      public void checkCigar()
      Checks if the head has collided with a cigar.
    • checkCollisions

      public void checkCollisions()
      Checks for collisions with walls or the snake's own body.
    • gameOver

      public void gameOver(Graphics g)
      Renders the game over screen.
      Parameters:
      g - the graphics context.
    • actionPerformed

      public void actionPerformed(ActionEvent e)

      The primary game loop triggered by the timer.

      Specified by:
      actionPerformed in interface ActionListener
    • main

      public static void main(String[] args)
      Launcher for the Mapacho Snake game.
      Parameters:
      args - command line arguments (ignored).