main.java:
package com.company;
import javax.swing.*;
public class Main extends JFrame {
public Main() {
setTitle("Змейка");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(320,345);
setLocation(400, 400);
setVisible(true);
add(new GameField());
}
public static void main(String[] args) {
Main mw = new Main();
}
}
GameField.java:
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
public class GameField extends JPanel implements ActionListener {
private final int size = 320;
private final int dotSize = 16;
private final int allDots = 400;
private Image dot;
private Image apple;
private int appleX;
private int appleY;
private int[] x = new int[allDots];
private int[] y = new int[allDots];
private int dots;
private Timer timer;
private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;
private boolean inGame = true;
public GameField() {
setBackground(Color.black);
loadImages();
initGame();
addKeyListener(new FieldKeyListener());
setFocusable(true);
}
public void initGame() {
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 48 - i * dotSize;
y[i] = 48;
}
timer = new Timer(250, this);
timer.start();
createApple();
}
public void createApple() {
appleX = new Random().nextInt(20) * dotSize;
appleY = new Random().nextInt(20) * dotSize;
}
public void loadImages() {
ImageIcon iia = new ImageIcon("fruit.png");
apple = iia.getImage();
ImageIcon iid = new ImageIcon("dot.png");
dot = iid.getImage();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (inGame) {
g.drawImage(apple, appleX, appleY, this);
for (int i = 0; i < dots; i++) {
g.drawImage(dot, x[i], y[i], this);
}
} else {
String str = "Game Over";
Font font = new Font("Arial", 14, Font.BOLD);
g.setColor(Color.red);
g.setFont(font);
g.drawString(str, 125, size / 2);
}
}
public void move() {
for (int i = dots; i > 0; i--) {
x[i] = x[i-1];
y[i] = y[i-1];
}
if (left) {
x[0] -= dotSize;
} else if (right) {
x[0] += dotSize;
} else if (up) {
y[0] -= dotSize;
} else if (down) {
y[0] += dotSize;
}
}
public void checkApple() {
if (x[0] == appleX && y[0] == appleY) {
dots++;
createApple();
}
}
public void checkCollisions() {
for (int i = dots; i > 0; i--) {
if (i > 4 && x[0] == x[i] && y[0] == y[i]) {
inGame = false;
}
}
if (x[0] > size) {
inGame = false;
} else if (x[0] < 0) {
inGame = false;
} else if (y[0] > size) {
inGame = false;
} else if (y[0] < 0) {
inGame = false;
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollisions();
move();
}
repaint();
}
class FieldKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && !right) {
left = true;
up = false;
down = false;
} else if (key == KeyEvent.VK_RIGHT && !left) {
right = true;
up = false;
down = false;
} else if (key == KeyEvent.VK_UP && !down) {
right = false;
up = true;
left = false;
} else if (key == KeyEvent.VK_DOWN && !up) {
right = false;
left = false;
down = true;
}
}
}
}