Link to the Project Repo: https://github.com/Ethan-zc/TheSnakeGame
Video for SetUp and simple demo: Click Here
- Every second one user live, the user would get 10 points.
- When one snake eat the apple, the snake would get 100 points.
- When one snake cause a collision, which means that one snake dead because it head collide on the killer snake's body,
the killer snake would get 1000 point, and the other one would die.
- When the time runs out or all snakes are dead, the game would end and the points for each snake would be demonstrated.
After the game is finished, one user could also check the leader board for the highest scores in the database.
Choose New > Project From Version Control, and paste the address of this project:
https://github.com/Ethan-zc/TheSnakeGame.git , clone the project onto the local computer.
Please choose SDK 17 as the SDK for the project.
For the server, please change the server: address: 192.168.1.156 to the local ip address of the server computer.
About how to find local IP address: Click Here
In the meantime, change the localhostIP in /src/main/java/TheGameClient to the corresponding local IP address of the server for all users in the same local network.
As for the server, please run TheSnakeGameApplication. After seeing that the server is running,
different computers under the same local network could run TheGameClient to as client to login and play the game.
For detailed process, please refer to the video: Video
Following are detailed introduction for different part of the project.
In this project, SpringBoot is used for API construction. Controller, Service and ServiceImplements are implemented for APIs. Followings are the list for important APIs in this project.
- localIP:8080/account/register: Used for new user to be registered.
- localIP:8080/account/login: Used for users to login with their username and password. Notice that the password that stored in the database would be encrypted using md5.
- localIP:8080/account/getLeaderBoard: Used for get the leaderboard of all scores stored in db with username.
- localIP:8080/game/addgame: Used for server to add a record of new game into game table.
- localIP:8080/game/addscore: Used for adding score of certain user in certain game into the database.
MyBatis is used to map java functions into sql query to select, insert data into our database. Mappers are constructed to do this work. AccountMapper and GameMapper are used for AccountService and GameService to do interactions with database.
SQLite is used as the database of the project. Using /resources/DatabaseCreation.sql with SQLite, we get the database file schema.db.
Then we used the application-sqlite.yml to configure that the server would take the schema.db as our database.
The database is created as following:

By default, we have added data into the database when it is created, the added data are:
INSERT INTO acc (accname, pwd) VALUES ('zichenyang', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO acc (accname, pwd) VALUES ('zhangnan', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO acc (accname, pwd) VALUES ('pochita', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO game (starttime) VALUES ('2020-09-14 23:18:17');
INSERT INTO game (starttime) VALUES ('2022-12-14 23:18:17');
INSERT INTO acc_game (score, accid, gameid) VALUES ('123456', '1', '1');
INSERT INTO acc_game (score, accid, gameid) VALUES ('23441', '2', '2');
INSERT INTO acc_game (score, accid, gameid) VALUES ('12314123', '3', '1');
INSERT INTO acc_game (score, accid, gameid) VALUES ('1312', '1', '2');
INSERT INTO acc_game (score, accid, gameid) VALUES ('1312', '3', '2');
Here the password is md5 encrypted, which is 123456 for these three users
Here Socket is mainly used for the information exchange during the waiting room between client and server. Two classes are created to handle the socket.WaitingClient: This one is served for the client, when the user enter the waiting room, the WaitingCLient would be created and the client would send a message with username. If there are already users in the waiting room, the client would then receive a list of users that currently in the waiting room. When the game start, the client who press the button would send a message "GAME START!" to the server, and then create a Client class to handle the game process. WaitingRoom: This class is served as the server of waiting room. It would receive message from user and broadcast the user list to all users in the waiting room dynamically. When it receive the message of "GAME START" from user, it would broadcast "GAME START" to all users in the waiting room, and the close the socket, start the threads for GameHandler to start the game. 1. When each user starts the client after a server started, a Signing Up page would show up:
Users can choose to register a new account (the
account/register API will be called) or Sign In using an existing account: Error handling will be made by the
account/login API. 2. A welcome page would come up after logged in successfully:
User at here could choose to view the leader board or start the game directly.
3. If the user choose to view the leader board, the leader board with 6 highest scores with their players would display:
4. If the user choose to start the game, he/she will be added to the waiting room automatically:
After joining the waiting room, players can see all players joined the game with usernames listed. the server will start to communicate with clients using sockets. All clients would inform the server with their joining (implemented in the
src/main/java/com/seven/zichen/snakegame/socket/WaitingClient.java),
and clients would know others joining (implemented in src/main/java/com/seven/zichen/snakegame/socket/WaitingRoom.java). 5. All UI panels are implemented in the folder
src/main/java/com/seven/zichen/snakegame/models/6. After any one of the waiting player choose to start the game, the game will start and the game frame would be drawn:
which is implemented in
src/main/java/com/seven/zichen/snakegame/client/DrawGame.javaWhen the game starts, the server would start to communicate with clients through UDP transmission. There are 4 types of UDP datagrams: Game Initialization, Direction Change, Grid Handling and Score Sending.
Inside the
~/client folder, there is a ClientListener implements methods listening UDP transmission from the server and a ClientListener
responsible for sending UDP to the server. At the beginning, the GameManager inside the ~/game folder through GameHandlerManager and GameHandlerOutput
would send out a Game Initialization UDP to all clients, ClientListener after receiving the UDP, will initialize the DrawGame through Client. MoveSnakes will
continuously update the whole game grid with different colors for each pixel (Different Colors: Self Snake, Other Snake(s), Apple) and send grids to clients by Grid Handling. HandleReturnedGrid on the client side will handle the grid sent by the server
and update the game frame through DrawGame. If players press keyboard to change the snake direction, HandleInputDirection on the client side will handle the key event and
a Direction Change UDP would be sent to the server. The MoveSnake on the server side will update the direction of the requested snake and update the grid. When the game is over(all snakes are dead or time runs up),
Game Manager on the server side will send Score Sending UDP to all clients, clients after receiving scores will display the game over page: Users could choose to view the leader board with the latest scores updated.
UDP Transmission Code Reference: https://github.com/TheoCabannes/SnakeGame










