Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MoreExercise;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.LinkedList;
- import java.util.Random;
- public class SnakeGame_BestGame extends JFrame implements ActionListener, KeyListener {
- private static final int GRID_SIZE = 20;
- private static final int TILE_SIZE = 20;
- private static final int GAME_SPEED = 150;
- private LinkedList<Point> snake;
- private Point food;
- private int direction; // 0: up, 1: right, 2: down, 3: left
- private boolean running;
- public SnakeGame_BestGame() {
- setTitle("Better Snake Game");
- setSize(GRID_SIZE * TILE_SIZE, GRID_SIZE * TILE_SIZE);
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- snake = new LinkedList<>();
- snake.add(new Point(GRID_SIZE / 2, GRID_SIZE / 2));
- direction = 1; // Start moving to the right
- running = true;
- generateFood();
- Timer timer = new Timer(GAME_SPEED, this);
- timer.start();
- addKeyListener(this);
- setFocusable(true);
- }
- private void generateFood() {
- Random rand = new Random();
- int x, y;
- do {
- x = rand.nextInt(GRID_SIZE);
- y = rand.nextInt(GRID_SIZE);
- } while (snake.contains(new Point(x, y)));
- food = new Point(x, y);
- }
- private void move() {
- Point head = snake.getFirst();
- Point newHead;
- switch (direction) {
- case 0: // up
- newHead = new Point(head.x, (head.y - 1 + GRID_SIZE) % GRID_SIZE);
- break;
- case 1: // right
- newHead = new Point((head.x + 1) % GRID_SIZE, head.y);
- break;
- case 2: // down
- newHead = new Point(head.x, (head.y + 1) % GRID_SIZE);
- break;
- case 3: // left
- newHead = new Point((head.x - 1 + GRID_SIZE) % GRID_SIZE, head.y);
- break;
- default:
- throw new IllegalStateException("Unexpected value: " + direction);
- }
- if (snake.contains(newHead) || !isValidPosition(newHead)) {
- running = false;
- return;
- }
- snake.addFirst(newHead);
- if (newHead.equals(food)) {
- generateFood();
- } else {
- snake.removeLast();
- }
- }
- private boolean isValidPosition(Point point) {
- return point.x >= 0 && point.x < GRID_SIZE && point.y >= 0 && point.y < GRID_SIZE;
- }
- private void drawTile(Graphics g, Point point, Color color) {
- g.setColor(color);
- g.fillRect(point.x * TILE_SIZE, point.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- if (running) {
- // Double buffering to reduce flickering
- Image buffer = createImage(getWidth(), getHeight());
- Graphics bufferGraphics = buffer.getGraphics();
- // Draw the snake
- for (Point point : snake) {
- drawTile(bufferGraphics, point, Color.GREEN);
- }
- // Draw the food
- drawTile(bufferGraphics, food, Color.RED);
- // Draw the buffer
- g.drawImage(buffer, 0, 0, this);
- } else {
- // Game over screen
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(Color.RED);
- g.setFont(new Font("Arial", Font.BOLD, 30));
- String gameOverMessage = "Game Over";
- g.drawString(gameOverMessage, getWidth() / 2 - g.getFontMetrics().stringWidth(gameOverMessage) / 2, getHeight() / 2);
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (running) {
- move();
- repaint();
- }
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_UP && direction != 2) {
- direction = 0;
- } else if (e.getKeyCode() == KeyEvent.VK_RIGHT && direction != 3) {
- direction = 1;
- } else if (e.getKeyCode() == KeyEvent.VK_DOWN && direction != 0) {
- direction = 2;
- } else if (e.getKeyCode() == KeyEvent.VK_LEFT && direction != 1) {
- direction = 3;
- }
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> {
- new SnakeGame_BestGame().setVisible(true);
- });
- }
- }
Add Comment
Please, Sign In to add comment