A classic Snake game written in Java as a study of object oriented programming. The game state is modeled with separate classes for the snake, the food and the playing field border, and rendering is done with the Java AWT and Swing graphics API.
- Java (standard library, no external dependencies)
- Java AWT and Swing for graphics, colors and keyboard input
- Compiled and run with the JDK (
javac/java)
The source under JavaSnake/src/com/model contains the core game model:
Cobra(snake): keeps the list of body segments, moves one cell per turn, grows when it eats, tracks the current direction and reads arrow key input through aKeyAdapter. It also detects collisions with its own body.Comida(food): places a food item at a random cell inside the field and checks whether that cell overlaps the snake or the border, so a new position can be generated when needed.Borda(border): builds the wall around the 30 by 30 cell field and draws it.
Each model class renders itself on a Graphics context using a fixed cell size, so the field is drawn as a grid of colored blocks (green snake, red food, white border).
JavaSnake/
src/com/model/
Cobra.java snake model, movement, growth, input and self collision
Comida.java food placement and overlap checks
Borda.java field border construction and drawing
.vscode/ Visual Studio Code Java project settings
Clone the repository:
git clone https://github.com/nicolasfvp/snake-game.git
cd snake-game/JavaSnake
Compile the model sources:
javac -d bin src/com/model/*.java
The repository ships the game model classes. A runnable main entry point and the game window loop are not included yet, so this compiles the model rather than launching a full game window. The classes are designed to be wired into a Swing JPanel that runs the turn loop and calls each object's desenhar (draw) and mover (move) methods.
Personal learning project and a work in progress. The class names and comments are in Portuguese.
Released under the MIT License. See the LICENSE file.