Class GamePanel

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

public class GamePanel extends JPanel implements ActionListener
The GamePanel class handles the gameplay logic and rendering for the Snake Game. It manages:
  • Snake movement and collision detection
  • Apple generation and score tracking
  • Game rendering and updating

The game can run in two modes:

  • EASY mode: Slower snake speed
  • HARD mode: Faster snake speed
See Also:
  • Field Details

    • gameMode

      private static GamePanel.Mode gameMode
      Current game mode (default: EASY).
    • unitSize

      private static final int unitSize
      Size of each unit (square) on the game board.
      See Also:
    • boardWidth

      private static final int boardWidth
      Width of the game board (rounded to fit units).
      See Also:
    • boardHeight

      private static final int boardHeight
      Height of the game board (rounded to fit units).
      See Also:
    • maxUnits

      private static final int maxUnits
      Maximum number of units (board area divided by unit size).
      See Also:
    • bodyParts

      private int bodyParts
      Initial length of the snake.
    • applesEaten

      private int applesEaten
      Score - number of apples eaten.
    • appleX

      private int appleX
      X and Y positions of the apple.
    • appleY

      private int appleY
      X and Y positions of the apple.
    • direction

      private char direction
      Current direction of the snake's movement.
    • isRunning

      private boolean isRunning
      Game running state.
    • x

      private final int[] x
      Arrays storing the snake's X and Y coordinates.
    • y

      private final int[] y
    • timer

      private Timer timer
      Timer to control the game speed and refresh rate.
    • random

      private Random random
      Random number generator for apple positioning.
  • Constructor Details

    • GamePanel

      GamePanel()
      Constructs the GamePanel, initializes the game settings, and starts the game.
  • Method Details

    • setMode

      protected static void setMode(GamePanel.Mode mode)
      Sets the game mode (difficulty).
      Parameters:
      mode - The selected game mode (EASY or HARD).
    • setGameSpeed

      private void setGameSpeed()
      Configures the game speed based on the selected mode.
      • EASY mode: 300ms delay
      • HARD mode: 150ms delay
    • startGame

      protected void startGame()
      Starts the game by:
      • Resetting snake length and score
      • Setting the initial direction to 'R'
      • Spawning the first apple
      • Starting the game timer
    • paintComponent

      protected void paintComponent(Graphics g)
      Handles the painting of the game board.
      Overrides:
      paintComponent in class JComponent
      Parameters:
      g - The Graphics object used for rendering.
    • draw

      protected void draw(Graphics g)
      Draws the game elements:
      • Apple
      • Snake body
      • Game over message (if the game ends)
      Parameters:
      g - The Graphics object used for rendering.
    • newApple

      protected void newApple()
      Spawns a new apple at a random location, ensuring it doesn't overlap with the snake's body.
    • move

      protected void move()
      Moves the snake in the current direction.
    • checkApple

      protected void checkApple()
      Checks if the snake has eaten an apple. Increases the snake's length and score if true.
    • checkCollisions

      private void checkCollisions()
      Checks for collisions with the snake itself or the game boundaries. Stops the game if a collision occurs.
    • gameOver

      private void gameOver(Graphics g)
      Displays the "Game Over" message.
      Parameters:
      g - The Graphics object used for rendering.
    • actionPerformed

      public void actionPerformed(ActionEvent e)
      Handles the game loop by updating the snake's position, checking for collisions, and determining if the snake has eaten an apple. The game is repainted after each tick.
      Specified by:
      actionPerformed in interface ActionListener
      Parameters:
      e - The ActionEvent triggered by the timer.