Ligh7_of_H3av3n

SnakeGameByNB

Feb 12th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. package MoreExercise;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.util.LinkedList;
  10. import java.util.Random;
  11.  
  12. public class SnakeGame_BestGame extends JFrame implements ActionListener, KeyListener {
  13.     private static final int GRID_SIZE = 20;
  14.     private static final int TILE_SIZE = 20;
  15.     private static final int GAME_SPEED = 150;
  16.  
  17.     private LinkedList<Point> snake;
  18.     private Point food;
  19.     private int direction; // 0: up, 1: right, 2: down, 3: left
  20.     private boolean running;
  21.  
  22.     public SnakeGame_BestGame() {
  23.         setTitle("Better Snake Game");
  24.         setSize(GRID_SIZE * TILE_SIZE, GRID_SIZE * TILE_SIZE);
  25.         setResizable(false);
  26.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.  
  28.         snake = new LinkedList<>();
  29.         snake.add(new Point(GRID_SIZE / 2, GRID_SIZE / 2));
  30.         direction = 1; // Start moving to the right
  31.         running = true;
  32.  
  33.         generateFood();
  34.  
  35.         Timer timer = new Timer(GAME_SPEED, this);
  36.         timer.start();
  37.  
  38.         addKeyListener(this);
  39.         setFocusable(true);
  40.     }
  41.  
  42.     private void generateFood() {
  43.         Random rand = new Random();
  44.         int x, y;
  45.         do {
  46.             x = rand.nextInt(GRID_SIZE);
  47.             y = rand.nextInt(GRID_SIZE);
  48.         } while (snake.contains(new Point(x, y)));
  49.  
  50.         food = new Point(x, y);
  51.     }
  52.  
  53.     private void move() {
  54.         Point head = snake.getFirst();
  55.         Point newHead;
  56.  
  57.         switch (direction) {
  58.             case 0: // up
  59.                 newHead = new Point(head.x, (head.y - 1 + GRID_SIZE) % GRID_SIZE);
  60.                 break;
  61.             case 1: // right
  62.                 newHead = new Point((head.x + 1) % GRID_SIZE, head.y);
  63.                 break;
  64.             case 2: // down
  65.                 newHead = new Point(head.x, (head.y + 1) % GRID_SIZE);
  66.                 break;
  67.             case 3: // left
  68.                 newHead = new Point((head.x - 1 + GRID_SIZE) % GRID_SIZE, head.y);
  69.                 break;
  70.             default:
  71.                 throw new IllegalStateException("Unexpected value: " + direction);
  72.         }
  73.  
  74.         if (snake.contains(newHead) || !isValidPosition(newHead)) {
  75.             running = false;
  76.             return;
  77.         }
  78.  
  79.         snake.addFirst(newHead);
  80.  
  81.         if (newHead.equals(food)) {
  82.             generateFood();
  83.         } else {
  84.             snake.removeLast();
  85.         }
  86.     }
  87.  
  88.     private boolean isValidPosition(Point point) {
  89.         return point.x >= 0 && point.x < GRID_SIZE && point.y >= 0 && point.y < GRID_SIZE;
  90.     }
  91.  
  92.     private void drawTile(Graphics g, Point point, Color color) {
  93.         g.setColor(color);
  94.         g.fillRect(point.x * TILE_SIZE, point.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
  95.     }
  96.  
  97.     @Override
  98.     public void paint(Graphics g) {
  99.         super.paint(g);
  100.  
  101.         if (running) {
  102.             // Double buffering to reduce flickering
  103.             Image buffer = createImage(getWidth(), getHeight());
  104.             Graphics bufferGraphics = buffer.getGraphics();
  105.  
  106.             // Draw the snake
  107.             for (Point point : snake) {
  108.                 drawTile(bufferGraphics, point, Color.GREEN);
  109.             }
  110.  
  111.             // Draw the food
  112.             drawTile(bufferGraphics, food, Color.RED);
  113.  
  114.             // Draw the buffer
  115.             g.drawImage(buffer, 0, 0, this);
  116.         } else {
  117.             // Game over screen
  118.             g.setColor(Color.BLACK);
  119.             g.fillRect(0, 0, getWidth(), getHeight());
  120.  
  121.             g.setColor(Color.RED);
  122.             g.setFont(new Font("Arial", Font.BOLD, 30));
  123.             String gameOverMessage = "Game Over";
  124.             g.drawString(gameOverMessage, getWidth() / 2 - g.getFontMetrics().stringWidth(gameOverMessage) / 2, getHeight() / 2);
  125.         }
  126.     }
  127.  
  128.     @Override
  129.     public void actionPerformed(ActionEvent e) {
  130.         if (running) {
  131.             move();
  132.             repaint();
  133.         }
  134.     }
  135.  
  136.     @Override
  137.     public void keyPressed(KeyEvent e) {
  138.         if (e.getKeyCode() == KeyEvent.VK_UP && direction != 2) {
  139.             direction = 0;
  140.         } else if (e.getKeyCode() == KeyEvent.VK_RIGHT && direction != 3) {
  141.             direction = 1;
  142.         } else if (e.getKeyCode() == KeyEvent.VK_DOWN && direction != 0) {
  143.             direction = 2;
  144.         } else if (e.getKeyCode() == KeyEvent.VK_LEFT && direction != 1) {
  145.             direction = 3;
  146.         }
  147.     }
  148.  
  149.     @Override
  150.     public void keyTyped(KeyEvent e) {
  151.     }
  152.  
  153.     @Override
  154.     public void keyReleased(KeyEvent e) {
  155.     }
  156.  
  157.     public static void main(String[] args) {
  158.         SwingUtilities.invokeLater(() -> {
  159.             new SnakeGame_BestGame().setVisible(true);
  160.         });
  161.     }
  162. }
Add Comment
Please, Sign In to add comment