Skip to content

About

Classic Snake game modeled in Java with object oriented classes and AWT/Swing rendering

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Snake Game (Java)

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.

Tech stack

  • Java (standard library, no external dependencies)
  • Java AWT and Swing for graphics, colors and keyboard input
  • Compiled and run with the JDK (javac / java)

What it does

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 a KeyAdapter. 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).

Project structure

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

Running locally

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.

Status

Personal learning project and a work in progress. The class names and comments are in Portuguese.

License

Released under the MIT License. See the LICENSE file.

About

Classic Snake game modeled in Java with object oriented classes and AWT/Swing rendering

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages