A basic implementation of Snake in Java.
I wrote this around July of 2023 while preparing to apply/transfer to a developer position at the company I was currently working for at the time. I've dabbled with simple games learning how to program back in middle school, but I figured this was a good project to review Java, as I was extensively working with JavaScript writing Mirth Connect channels in my current position at the time. Working on this project gave me a decent opportunity to play around with Java's Stream API, and examine its functional programming capabilities.
For this particular implementation, I challenged myself to
not use for
loops if an alternative existed within the Stream
API. This also forced me to get really comfortable with
lambda expressions and
functional interfaces.
-
JDK 8 or higher.
- I personally recommend Eclipse Temurin to keep things simple.
- If you are worried about conflicting JDK versions,
I would recommend employing a strategy that allows you
to have multiple JDKs coexist on the same machine:
- *nix
- Windows
- You could install your JDKs by version to different directories,
and symlink
C:\Java
to the current installation you wish to use. - Use Java exclusively in an environment like
MSYS2 with native symlinks enabled,
and then use jenv and/or
SDKMAN! to install your JDKs in your
MSYS2 environment.
- This technique still allows usage of Java from Windows directly, but is out of the scope of this document.
- You could install your JDKs by version to different directories,
and symlink
-
- If you use SDKMAN!, you can use it to install Maven.
Assuming the requirements above are met:
-
Clone this repository, and change into that directory if you haven't done so already:
# Cloning to ./snake isn't mandatory, but it makes things easier git clone https://github.com/thepeoplescoder/java--snake snake cd snake
-
Use Maven to compile everything:
mvn package
-
Afterwards, you should have an executable
.jar
located at./target/snake-1.0-SNAPSHOT.jar
.
After building, you can run the app:
-
via the
.jar
directly:java -jar target/snake-1.0-SNAPSHOT.jar
- The
.jar
is completely self-contained and can be moved anywhere.
- The
-
via Maven:
mvn exec:java
- Use the arrow keys to move around.
- P pauses/unpauses the game.
- Q/Escape quits the game.
- On game over, press Enter to restart.
- Things you would expect from your standard Snake game:
- If you eat an apple, the snake grows
- If you crash into a wall, game over
- If you crash into yourself, game over
- Snake is randomly positioned initially
- This is done such that the snake doesn't start:
- in an invalid position
- crashing into an obstacle immediately
- This is done such that the snake doesn't start:
- On game over, the messages range from dry humor to
lightly taunting
- When I was working on this game, I was playing a lot of Risk of Rain 2 at the time. Their game over messages do the same, so I drew inspiration from it.
- Body is deliberately slightly darker than head so direction of travel is clear
- Internally, the game is structured such that the
game logic is separate from how it is displayed
to the user.
- Currently, only a Swing implementation exists.
- The game deliberately crashes upon eating 999 apples.
- This can be changed by modifying the value of
com.thepeoplescoder.snake.Shared.Settings.Game.applesPerLevel
.
- This can be changed by modifying the value of
- Show number of apples collected
- Implement multiple lives
- Implement multiple levels
- Implement a cross-platform version that works on the terminal, using text to emulate pixel graphics