Content

Introduction

The Snake game is a well-known game for mobile devices. The first time Snake made its debut was in 1997 and people usually know the game from Nokia 6110. Since then, this game changed many times and now it is time to adjust it for modern devices with touch display. Also, do not worry about different mobile platforms, because we are going to show you how it can be developed for multiple platforms with only one code.

The game overview

We all know what the goal of this game is but just to make sure that we are on the same page; the purpose is to feed the snake with food that appears randomly on the screen. To make this game slightly different from other snake games we added two types of food. The first smaller type of food adds ten points to your score, and the second larger type adds twenty points. The snake also becomes longer with every piece of food eaten.

small food - increase score by 10 points

large food - increase score by 20 points

System requirements

Nowadays, the majority of mobile devices uses Andorid and iOS platform but there are still platforms like Bada OS or Windows Phone growing their market share. To cover the most of the devices available on the market, we used Moscrif SDK. Using Moscrif SDK, developers can create applications for iOS, Android and Bada OS with only one code base.

Our game has very small hardware requirements; and therefore, it even runs on devices with only 420MHz processor. It means that our game can run on low-end devices with low performing capabilities as well.

Graphic proposal

Every game design starts with ideas transformed into graphics proposal. It defines how the game should look like and what game objects it should contain. It also indicates the functionality of game. The game is friendly for all ages, because it does not use any controversial design but the colours work together in nice, eye-appealing fashion.

Image: graphics proposal of our game

User interface

As you can see on the pictures above, the graphics show two parts of the game - introduction part, which is the first scene with menu and then the second scene, which is the actual game.

Menu - this is the introduction scene. It is displayed at the start or the end of the application and when the game is paused. Users can start a new game, continue in paused game or quit the application.

Game - this is the main scene where the game is played. It consists of sprite objects, which are spread on the game scene. Simply said, the snake moves on the screen and eats the food. Users can control snake by tapping the screen towards the direction where they want the snake to go.

Menu

The menu is the introduction scene. It is displayed when application starts and it enables the user to start new game, continue game (if it was paused) or quit the game. Due to Apple’s application policy of using quit button in applications, the button is not added to iOS version.

The menu is created by MenuScene class. It is extended from Scene class. When the instance of the class is created, init function is called. Menu scene class is overlaid by the parent’s init function. However, the parent init function is not called automatically, it is needed to call it by super.init() command to ensure correct object creation. Menu consist of three buttons which provide operation functionality: continue, new game or quit.

Image: menu elements

All buttons are created by separate functions. However, as we mentioned before, make sure that quit button is not added to your iOS version. Apple will not accept it.

The buttons are created as an instance of GameButton which has two states: pressed and „normal“. Both are placed in one source file as two frames. By default, the first frame is shown. When the button is pressed, the next frame is shown. The coordinates of one frame are set by frameWidth and frameHeight properties. GameButton also calls onClick event when tapped.

The added buttons are drawn automatically by Scene class. However, the background has to be drawn manually in draw function. Background image has been already loaded in init function. Also, parent’s draw function has to be called in draw function, because it draws added buttons and other elements.

Game

Game consists of many parts like snake, food, various buttons etc. It also has many functionalities like contact detection or food distribution. The base for all objects and functionalities is Game Scene class.

Function _createDrops distributes drops to the playground. The drops are distributed into random positions. However, we have to ensure that two foods will not overlap.

Firstly, we create new Drop object. Then - in while cycle - we try to find a location on the screen where our object isn’t in collision with any others. We get random position by function rand(Integer). We must detect, if there isn’t a collision with other objects. If everything is ok, we can jump from while cycle by using Stop variable. At the end, we add the drop to the our array this.food and to the scene.

Game scene has only one button which allows the users to return to the game menu. It is also created by GameButton class. Another UI element is score indicator.

Score

Score is an important feature, which makes the game more challenging. In this game, the score depends on number and type of foods eaten and the target is to achieve the highest score without coming to contact with wall or part of the snake.

How to calculate the score?

Score is recalculated each time the snake eats the food. We can take into consideration that the snake's head is the part eating the food. All the food distributed on the screen was pushed to one array called food. Having access to all food objects and also to the snake head allows us to check whether the food was eaten or not. We need to check if there is any food under the head by intersectsBounds function.

Once we are able to check if objects overlap, it is easy to call this function for all food objects. The best place to do it is in onProcess event, where movement is managed as well. If the head overlaps with some food, we increase the value of score property (containing current score) by the value of food eaten.

Snake

Snake is the main game character and its movement is controlled by tapping on the screen.

Snake consists of sprite objects Head, Body and Tail, which have common parent Part. Class Part extends class Sprite with property direction, which is used to describe every snake’s part. It is a very important property, because it defines the movement direction. Snake class synchronises these objects with the help of Delay and Switch objects.

Snake consists of body parts (and there can be plenty of body parts), which means that the snake turns gradually. First it turns its head, then it turns the first body part, second body part etc., and at last it turns the tail. These parts, including the head and then tail can turn on more than one place.

Every time the user taps on the screen (and the game is active) the turnTouch(x,y) function is called. This function first checks whether the snake (its head) is currently moving horizontally or vertically. If it is moving horizontally, it checks whether the user tapped above or below the snake’s head. If user taps above the snake head, it goes up. If user taps under the snake head, it goes down. The same process takes place if the snake moves horizontally.

Edges are set in place where the snake should turn. Turning is managed by Refresh function of Switch object. This function is called every time when snake moves from snake object onProcess function. This function applies all the edges and also removes the edges if they are not needed anymore. Its behavior is shown in next image.

Summary

Now you know how to create one of the most popular games in mobile industry by using Moscrif SDK. This will ensure that you decrease the amount of work and you will achieve the best results. Our project is focused on mobile phones and tablets with iOS, Andorid or Bada. The wide range of supported devices and resolution independence of this game opens huge market placement opportunities. Do not waste your time on developing applications separately for every mobile platform and try Moscrif cross platform development tool (available for free here).

More resources

More samples, demos and information about Moscrif can be found on www.moscrif.com

License

This article, along with any associated source code and files, is licensed under The BSD License