We will use one horizontal and one vertical white line image for our borders: Note: Size is pretty much the zoom factor. If we select the Main Camera in the Hierarchy then we can set the Background Color to black, adjust the Size and the Position like shown in the following image:
We will name it Snake, select any location like C:\GameDev, select 2D in the Template section, and click Create: Let's get started! Assuming you are using a recent version of the Unity Hub, we'll boot the Hub interface and select New, then fill out the dialogue accordingly: These versions are alpha and beta respectively, and more often than not things will break! Troubleshooting broken things isn't fun and causes frustration which you want to avoid when starting out in game development.
Protip: Never update to any version of Unity that has an 'a' or 'b' suffix. Newer versions such as 2019.2 should work as well, however to prevent any confusion it's always recommended to keep to the version of Unity the tutorial was written for. Our Snake Tutorial will use Unity 2018.4 LTS.
Even if you don't know those concepts yet, the Tutorial should still be doable, however we recommend you brush up on your knowledge of the mentioned subjects before attempting this tutorial.įeel free to read our easier Unity Tutorials like Unity 2D Pong Game to get used to this amazing game engine. This tutorial does not require any special skills except some knowledge about the Unity basics like GameObjects and Transforms.
In this tutorial, we will be explaining how to make a simple yet functional clone of Snake. It's been ported to almost all systems out there, even on classic old Nokia phones! Like most arcade games it's still easy and lots of fun to develop your own Snake game. The most basic element, we only need private data members as discussed and public methods to set and retrieve them.Snake is an arcade game that was created back in the 1970's.
Now that we are clear with the what and what not of the game, lets start coding! Code
A constructor to initialise te board with number of rows and columns.While food is an attribute of the board, movement is an attribute of the snake! Hence it is wise to let the Snake class contain method pertaining to the Snake's movement. This is answered by the Snake class we described above. Whether it is a part of the snake or not? This is attributed to the board, as the cell doesn't choose whether it shall contain food, the board is acquainted with the cells it contains and hence should empowered to assign a cell to contain food. We need to assign food to some random cell as well. While the cells revert to the questions, being the collection and home of cells, the board needs to ensure the cells get satisfiable answers It is the set of valid points for the game to function on, qualitatively a matrix of cells. A function that specifies its type (contains food etc.).> Whether it is a part of the snake or not? The identify of food is random, but identity of cell is meaningful, so I would rather make a class Cell than food.Ĭells are also important to kep track of position of the snake!Įach cell is thereby contains the information about Our boards, consistes of various cells, A cell at random will be proclaimed as the cell containing food. While food seemed intuitive to me while thinking, cell feels intuitive to me for working! Why so? A function to set the head of the snake.A function that returns the head of the snake.A function to check if the snake has crashed.A function to grow the size of the snake.A constructor to initialise the snake with its head.So it shall contain the following functions: > The ability to control movement of the Snake > Know and maintain the size of the snake.
Now that we're clear with what makes a Snake Game, lets look at it with an object oriented approach. So lets get this straight, What do we need in our snake game? The classic game that we are all acquainted with is actually surprisingly simple to implement and understand. Let us start immersing ourselves into it by fueling ourselves with some Nostalgia. In this article, we have explored how to design the classical Snake Game using Object Oriented Programming (OOP) concepts and implement it using Java.