2D Voxel Game

voxel2d

I decided to create a small game in the style of "Minecraft" or "Terraria". I've seen these games, and always fascinated myself with the terrain generation. So I sought to try and replicate, or at least create something similar. I was always asked questions like "Why don't you do something original?". That wasn't my intention - my intention was to learn about procedural terrain generation.

voxel2d2

I used Unity's built in Perlin noise function to create the heights. The game is 2D, so the y value was set to 0. This created columns of blocks in a random terrain. I decided to layer them - the first layer was smooth hills, with each layer getting rougher and smaller, which creates a nice random terrain. Trees are generated by placing tree 'seeds' on the top of random grass blocks. Furthermore, caves were generated by feeding the perlin noise the X and Y coordinate, and only creating a block if the resulting value was higher than a given number.

voxel2d3

For creating ores, I followed a similar approach, but placed ore blocks instead of nothing. I adjusted the settings to get a desired amount being generated. The results were nice and now gravel, coal and iron ore can be found amongst the stone.

Blocks can be destroyed, and all take different amounts of time (depending on the type). When destroyed, the block shrinks and 'pops' up. That block is then added the inventory. The inventory is customisable with the code - I can change the number of slots and rows with it. Items are stored in a stack, and tool tips are shown on hover, telling the player what block it is and a small description. Blocks can then be placed on the ground, and the inventory count decreases accordingly.

When certain blocks are destroyed, they leave a 'background' which makes the game look nicer visually. View the code on GitHub.

voxel2d4