Untitled
unknown
plain_text
8 months ago
4.3 kB
2
Indexable
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.ArrayList;
import java.util.Random;
public;
private static final int HEIGHT = 300;
private static final int UNIT_SIZE = 10;
private static final int GAME_UNITS = (WIDTH * HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
private static final int DELAY = 75;
private final ArrayList<Point> snake = new ArrayList<>();
private Point food;
private char direction = 'R';
private boolean running = false;
private Timer timer;
public SnakeGame() {
setTitle("Snake Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
add(new GamePanel());
pack();
setLocationRelativeTo(null);
setVisible(true);
startGame();
}
private void startGame() {
snake.add(new Point(0, 0));
generateFood();
running = true;
timer = new Timer(DELAY, new GameLoop());
timer.start();
}
private void generateFood() {
Random random = new Random();
food = new Point(random.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE, random.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE);
}
private void move() {
Point head = new Point(snake.get(0));
switch (direction) {
case 'U' -> head.y -= UNIT_SIZE;
case 'D' -> head.y += UNIT_SIZE;
case 'L' -> head.x -= UNIT_SIZE;
case 'R' -> head.x += UNIT_SIZE;
}
snake.add(0, head);
if (head.equals(food)) {
generateFood();
} else {
snake.remove(snake.size() - 1);
}
}
private void checkCollisions() {
Point head = snake.get(0);
if (head.x < 0 || head.x >= WIDTH || head.y < 0 || head.y >= HEIGHT) {
running = false;
}
for (int i = 1; i < snake.size(); i++) {
if (head.equals(snake.get(i))) {
running = false;
}
}
}
private class GamePanel extends JPanel implements KeyListener {
public GamePanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (running) {
g.setColor(Color.RED);
g.fillRect(food.x, food.y, UNIT_SIZE, UNIT_SIZE);
for (Point point : snake) {
g.setColor(Color.GREEN);
g.fillRect(point.x, point.y, UNIT_SIZE, UNIT_SIZE);
}
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
g.setColor(Color.RED);
g.setFont(new Font("Helvetica", Font.BOLD, 20));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Game Over", (WIDTH - metrics.stringWidth("Game Over")) / 2, HEIGHT / 2);
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT -> {
if (direction != 'R') direction = 'L';
}
case KeyEvent.VK_RIGHT -> {
if (direction != 'L') direction = 'R';
}
case KeyEvent.VK_UP -> {
if (direction != 'D') direction = 'U';
}
';
}
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
}
private class GameLoop implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkCollisions();
}
repaint();
}
}
public static void main(String[] args) {
new SnakeGame();
}
}
Editor is loading...
Leave a Comment