5
06/11/2024 7:13 am
Topic starter
I want to create snake game using Java with Intelli J Idea (not Eclipse);
Like on the old Nokia phones 🙂 on the picture:

If you can provide code it will be great! Thanks
2 Answers
4
06/11/2024 7:15 am
First you need to create 2 packages: snakegame - there you can put the main Class: SnakeGame;
The other package is objects - there you can put 2 Classes: Snake and Apple (the stuff snake is eating)
Here is the screenshot:

Here is the SnakeGame Class (the main one):
package snakegame;
import objects.Apple;
import objects.Snake;
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;
public class SnakeGame extends JPanel implements ActionListener {
public static final int SCALE = 32;
public static final int WIDTH = 20;
public static final int HEIGHT = 20;
public static final int SPEED = 4;
Apple a = new Apple((int) (Math.random() * WIDTH), (int) (Math.random() * HEIGHT));
Snake s = new Snake(10, 10, 9, 10);
Timer t = new Timer(1000 / SPEED, this);
public SnakeGame() {
t.start();
addKeyListener(new Keyboard());
setFocusable(true);
}
public void paint(Graphics g) {
g.setColor(color(5, 50, 10));
g.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE);
g.setColor(color(255, 216, 0));
for (int xx = 0; xx <= WIDTH * SCALE; xx += SCALE) {
g.drawLine(xx, 0, xx, HEIGHT * SCALE);
}
for (int yy = 0; yy <= HEIGHT * SCALE; yy += SCALE) {
g.drawLine(0, yy, WIDTH * SCALE, yy);
}
for (int d = 0; d < s.length; d++) {
g.setColor(color(0, 0, 255));
g.fillRect(s.snakeX[d] * SCALE + 1, s.snakeY[d] * SCALE + 1, SCALE - 1, SCALE - 1);
}
g.setColor(color(255, 0, 0));
g.fillRect(a.posX * SCALE + 1, a.posY * SCALE + 1, SCALE - 1, SCALE - 1);
}
public Color color(int red, int green, int blue) {
return new Color(red, green, blue);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setSize(WIDTH * SCALE + 7, HEIGHT * SCALE + 29);
f.setLocationRelativeTo(null);
f.add(new SnakeGame());
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
s.move();
if ((s.snakeX[0] == a.posX) & (s.snakeY[0] == a.posY)) {
a.setRandomPosition();
s.length++;
}
for (int k = 1; k < s.length; k++) {
if ((s.snakeX[k] == a.posX) & (s.snakeY[k] == a.posY)) {
a.setRandomPosition();
}
}
repaint();
}
private class Keyboard extends KeyAdapter {
public void keyPressed(KeyEvent kEve) {
int key = kEve.getKeyCode();
if ((key == KeyEvent.VK_RIGHT) & s.direction != 2) s.direction = 0;
if ((key == KeyEvent.VK_DOWN) & s.direction != 3) s.direction = 1;
if ((key == KeyEvent.VK_LEFT) & s.direction != 0) s.direction = 2;
if ((key == KeyEvent.VK_UP) & s.direction != 1) s.direction = 3;
}
}
}
Class Snake:
package objects;
import snakegame.SnakeGame;
public class Snake {
SnakeGame main;
public int direction = 0;
public int length = 2;
public int snakeX[] = new int[main.WIDTH * main.HEIGHT];
public int snakeY[] = new int[main.WIDTH * main.HEIGHT];
public Snake(int x0, int y0, int x1, int y1) {
snakeX[0] = x0;
snakeY[0] = y0;
snakeX[1] = x1;
snakeY[1] = y1;
}
@SuppressWarnings("static-access")
public void move() {
for (int d = length; d > 0; d--) {
snakeX[d] = snakeX[d - 1];
snakeY[d] = snakeY[d - 1];
}
if (direction == 0) snakeX[0]++;
if (direction == 1) snakeY[0]++;
if (direction == 2) snakeX[0]--;
if (direction == 3) snakeY[0]--;
for (int d = length - 1; d > 0; d--) {
if ((snakeX[0] == snakeX[d]) & (snakeX[0] == snakeY[d])) length = d - 2;
}
if (snakeX[0] > main.WIDTH) snakeX[0] = 0;
if (snakeX[0] < 0) snakeX[0] = main.WIDTH - 1;
if (snakeY[0] > main.HEIGHT - 1) snakeY[0] = 0;
if (snakeY[0] < 0) snakeY[0] = main.HEIGHT - 1;
if (length < 2) length = 2;
}
}
Class Apple:
package objects;
import snakegame.SnakeGame;
public class Apple {
SnakeGame main;
public int posX;
public int posY;
public Apple(int startX, int startY) {
posX = startX;
posY = startY;
}
@SuppressWarnings("static-access")
public void setRandomPosition() {
posX = (int) (Math.random() * main.WIDTH);
posY = (int) (Math.random() * main.HEIGHT);
}
}
3
06/11/2024 7:16 am
Another code for snake game in Java with video (although the game is made on Eclipse, not IntelliJ IDEA):
Cool game! Thanks! Just what was needed for my Java project! 🙂
