Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
3.7 kB
1
Indexable
Never
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.Random;

public class SnakeGame extends JFrame implements ActionListener, KeyListener {

    private Timer timer;
    private int gridSize = 20;
    private int screenWidth = 400;
    private int screenHeight = 400;
    private int[] x, y;
    private int length, applesEaten;
    private int appleX, appleY;
    private char direction;

    public SnakeGame() {
        setTitle("Snake Game");
        setSize(screenWidth, screenHeight);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);

        x = new int[screenWidth / gridSize * screenHeight / gridSize];
        y = new int[screenWidth / gridSize * screenHeight / gridSize];
        length = 1;
        direction = 'R';

        addKeyListener(this);
        setFocusable(true);

        timer = new Timer(100, this);
        timer.start();

        spawnApple();
    }

    public void spawnApple() {
        Random rand = new Random();
        appleX = rand.nextInt(screenWidth / gridSize) * gridSize;
        appleY = rand.nextInt(screenHeight / gridSize) * gridSize;
    }

    public void move() {
        for (int i = length; i > 0; i--) {
            x[i] = x[i - 1];
            y[i] = y[i - 1];
        }

        switch (direction) {
            case 'U':
                y[0] -= gridSize;
                break;
            case 'D':
                y[0] += gridSize;
                break;
            case 'L':
                x[0] -= gridSize;
                break;
            case 'R':
                x[0] += gridSize;
                break;
        }
    }

    public void checkCollision() {
        if (x[0] < 0 || x[0] >= screenWidth || y[0] < 0 || y[0] >= screenHeight) {
            gameOver();
        }

        for (int i = length; i > 0; i--) {
            if (x[0] == x[i] && y[0] == y[i]) {
                gameOver();
            }
        }

        if (x[0] == appleX && y[0] == appleY) {
            length++;
            applesEaten++;
            spawnApple();
        }
    }

    public void gameOver() {
        timer.stop();
        JOptionPane.showMessageDialog(this, "Game Over! Apples Eaten: " + applesEaten);
        System.exit(0);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (int i = 0; i < length; i++) {
            g.setColor(i == 0 ? Color.GREEN : Color.RED);
            g.fillRect(x[i], y[i], gridSize, gridSize);
        }

        g.setColor(Color.BLUE);
        g.fillRect(appleX, appleY, gridSize, gridSize);

        Toolkit.getDefaultToolkit().sync();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        move();
        checkCollision();
        repaint();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                if (direction != 'D') direction = 'U';
                break;
            case KeyEvent.VK_DOWN:
                if (direction != 'U') direction = 'D';
                break;
            case KeyEvent.VK_LEFT:
                if (direction != 'R') direction = 'L';
                break;
            case KeyEvent.VK_RIGHT:
                if (direction != 'L') direction = 'R';
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    public static void main(String[] args) {
        new SnakeGame().setVisible(true);
    }
}
Leave a Comment