In this article, I'll try to make the source code easily readable and understandable by dividing the whole code into small JAVA classes and by using the flexibility that JAVA provides. At the end of this article, the overall appearance of our project will be as follows:
Now, we can start writing our classes. Let's name our first class Cell.java:
public class Cell {
final static int CELL_TYPE_EMPTY = 0, CELL_TYPE_FOOD = 10, CELL_TYPE_SNAKE_NODE = 20;
final int row, col;
int type;
public Cell(int row, int col) {
this.row = row;
this.col = col;
}
}
final static int CELL_TYPE_EMPTY = 0, CELL_TYPE_FOOD = 10, CELL_TYPE_SNAKE_NODE = 20;
final int row, col;
int type;
public Cell(int row, int col) {
this.row = row;
this.col = col;
}
}
This class only keeps row and column numbers. Also some constant integers to determine the types of cells.
Our second class is Board.java. This class will allow us to identify the area where snake navigates (like a chessboard). Our class is as follows:
public class Board {
final int ROW_COUNT, COL_COUNT;
Cell[][] cells;
public Board(int rowCount, int columnCount) {
ROW_COUNT = rowCount;
COL_COUNT = columnCount;
cells = new Cell[ROW_COUNT][COL_COUNT];
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COL_COUNT; column++) {
cells[row][column] = new Cell(row, column);
}
}
}
public void generateFood() {
int row = (int) (Math.random() * ROW_COUNT);
int column = (int) (Math.random() * COL_COUNT);
cells[row][column].type = Cell.CELL_TYPE_FOOD;
}
}
final int ROW_COUNT, COL_COUNT;
Cell[][] cells;
public Board(int rowCount, int columnCount) {
ROW_COUNT = rowCount;
COL_COUNT = columnCount;
cells = new Cell[ROW_COUNT][COL_COUNT];
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COL_COUNT; column++) {
cells[row][column] = new Cell(row, column);
}
}
}
public void generateFood() {
int row = (int) (Math.random() * ROW_COUNT);
int column = (int) (Math.random() * COL_COUNT);
cells[row][column].type = Cell.CELL_TYPE_FOOD;
}
}
In this class, we are creating a two-dimensional array of the class "Cell" sized by rowCount * columnCount.
generatefood() is assigning the type of a random Cell to CELL_TYPE_FOOD.
Our third class is Snake.java.
Finally, our last class Router.java:
Here are the base JAVA classes of our snake game project. If we look into the source code deeply, we realize that, these codes may not fulfill some of the advanced features of the game.
It is up to you to enhance this project to fulfill the requirements of your imagination. I played around the code to make it seem like the screenshot below:
public class Snake {
LinkedList<Cell> snakePartList = new LinkedList<>();
Cell head;
public Snake(Cell initPos) {
head = initPos;
snakePartList.add(head);
}
public void grow() {
snakePartList.add(head);
}
public void move(Cell nextCell) {
Cell tail = snakePartList.removeLast();
tail.type = Cell.CELL_TYPE_EMPTY;
head = nextCell;
snakePartList.addFirst(head);
}
public boolean checkCrash(Cell nextCell) {
for (Cell cell : snakePartList) {
if (cell == nextCell) {
return true;
}
}
return false;
}
}
LinkedList<Cell> snakePartList = new LinkedList<>();
Cell head;
public Snake(Cell initPos) {
head = initPos;
snakePartList.add(head);
}
public void grow() {
snakePartList.add(head);
}
public void move(Cell nextCell) {
Cell tail = snakePartList.removeLast();
tail.type = Cell.CELL_TYPE_EMPTY;
head = nextCell;
snakePartList.addFirst(head);
}
public boolean checkCrash(Cell nextCell) {
for (Cell cell : snakePartList) {
if (cell == nextCell) {
return true;
}
}
return false;
}
}
Finally, our last class Router.java:
public class Router {
public static final int DIRECTION_NONE = 0, DIRECTION_RIGHT = 1, DIRECTION_LEFT = -1, DIRECTION_UP = 2, DIRECTION_DOWN = -2;
private Snake snake;
private Board board;
private int direction;
private boolean gameOver;
public Router(Snake snake, Board board) {
this.snake = snake;
this.board = board;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void update() {
if (!gameOver) {
if (direction != DIRECTION_NONE) {
Cell nextCell = getNextCell(snake.head);
if (snake.checkCrash(nextCell)) {
setDirection(DIRECTION_NONE);
gameOver = true;
} else {
snake.move(nextCell);
if (nextCell.type == Cell.CELL_TYPE_FOOD) {
snake.grow();
board.generateFood();
}
}
}
}
}
private Cell getNextCell(Cell currentPosition) {
int row = currentPosition.row;
int col = currentPosition.col;
if (direction == DIRECTION_RIGHT) {
col++;
} else if (direction == DIRECTION_LEFT) {
col--;
} else if (direction == DIRECTION_UP) {
row--;
} else if (direction == DIRECTION_DOWN) {
row++;
}
Cell nextCell = board.cells[row][col];
return nextCell;
}
}
public static final int DIRECTION_NONE = 0, DIRECTION_RIGHT = 1, DIRECTION_LEFT = -1, DIRECTION_UP = 2, DIRECTION_DOWN = -2;
private Snake snake;
private Board board;
private int direction;
private boolean gameOver;
public Router(Snake snake, Board board) {
this.snake = snake;
this.board = board;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void update() {
if (!gameOver) {
if (direction != DIRECTION_NONE) {
Cell nextCell = getNextCell(snake.head);
if (snake.checkCrash(nextCell)) {
setDirection(DIRECTION_NONE);
gameOver = true;
} else {
snake.move(nextCell);
if (nextCell.type == Cell.CELL_TYPE_FOOD) {
snake.grow();
board.generateFood();
}
}
}
}
}
private Cell getNextCell(Cell currentPosition) {
int row = currentPosition.row;
int col = currentPosition.col;
if (direction == DIRECTION_RIGHT) {
col++;
} else if (direction == DIRECTION_LEFT) {
col--;
} else if (direction == DIRECTION_UP) {
row--;
} else if (direction == DIRECTION_DOWN) {
row++;
}
Cell nextCell = board.cells[row][col];
return nextCell;
}
}
Here are the base JAVA classes of our snake game project. If we look into the source code deeply, we realize that, these codes may not fulfill some of the advanced features of the game.
It is up to you to enhance this project to fulfill the requirements of your imagination. I played around the code to make it seem like the screenshot below:
Hi,
YanıtlaSilthanks dude for your article.
classes for snake game are so simple and useful, but i couldnt find jframe and applet for complete the game please help me. thanks for your attention. see u later.
I have been searching for a guide to start my snake project and no one made it so clear and simple as you! Thanks for the help!
YanıtlaSilhow to run this pls teach me
YanıtlaSilWhich one is the main class of the project?
YanıtlaSilWhich one is the main class of the project?
SilBu yorum bir blog yöneticisi tarafından silindi.
SilHow to run this project? There is no main method or no applet in this project
YanıtlaSilHow to run this project? There is no main method or no applet in this project
SilHOW CAN RUN THIS PROJECT
SilGreat Job !!
YanıtlaSilHii Sir,
YanıtlaSilYour Provided Java projects helped me to get good score in engineering. thank you for providing java projects with source code.
So you basically cheated
Silashok chavan Oh great, so you managed to cheat basically.
YanıtlaSilashok chavan Oh great, so you managed to cheat basically.
YanıtlaSilwhere is the main class of this whole code bro
YanıtlaSilwhat is the error on snake java code... part on LinkedList
YanıtlaSil?
you need to add a line to the top of that class:
Silimport java.util.*;
how to run this project ?
YanıtlaSilI have a project to submit tomorrow I’m quite confused so I’ve decided to use your work I pray I pass ����
YanıtlaSil