Monday, February 20, 2017

jSnake!

This weekend I finished another game, this time a Snake game. As far as gameplay goes it's a very basic snake game, there's only 1 objective and that's to eat as many coins as you can without eating yourself or a wall. Arrow keys or WASD for controls, M toggles the music on and off and Enter will reset you when you die. There is a secret way to make the game go faster, which also awards you more points every time you eat a coin ;)

As far as the technical side of things the game itself was actually super simple. I made it in jMonkey again, with zay-es and lemur gui like last time. The game itself is actually just a grid of numbers with each number representing either empty space, a wall, a coin or a part of the dragon. Every time the game world updates it advances the head of the dragon by the currently chosen direction (just hold the direction you wanna go) and it will remove the tail, all the other numbers stay the same. If a coin is eaten it doesn't remove the tail this round, so it will grow.

The hardest part was getting the game to be visualized in 3d. I accomplished this by breaking my map down into several parts.

First, the Dragon (or snake as it's usually referred to internally) is a list of coordinates which are written to the map every time it updates. This list is ordered so I always know where the head and tail are, and I can keep tabs on each part of the body. The head and tail always exist and they just update their positions, a new body needs to be created every time the coin is eaten though. At first I thought the entity component system would make this hard, but in practice it made it super easy to update the body without much work.

The coin is rather simple. The 3d model is actually always present, it just gets hidden when the coin is eaten. Then the game will attempt to place the coin somewhere on the map, once a frame until it's successful. The larger your snake the more attempts this will take. I suppose theoretically you might get to a point where it takes several of the games turns before a new coin actually appears, but in practice you're more likely to get bored and bite your own tail before then. (editors note: 1 game turn = alot of frames)

The maps 3d model is updated on a reset. This is to allow the possibility of a map re-size, but for the purposes of this demo I just left the size fixed.

Once again you can download the finished game by using the url below. I posted a win32 build and the JAR so if you have Java installed it should run on most operating systems.

https://drive.google.com/drive/folders/0Bz4nByoX2Ks7NGd5X0QzWjVFSjg?usp=sharing

No comments:

Post a Comment