2048 Android Game, I had downloaded this game because it was trending, all of my friends were playing so I also downloaded the 2048 Game. Once I finished this game, I release that I can also create this game, Then I created this game. It is live on Play Store. You can download it here. If you want to buy source code you can buy it here.
2048 Android Game
The android game is also one of the features. You are bored with your activities and want to refresh yourself; just a quick android game will suffice. You are waiting for someone in a restaurant, that person is late. What will you do? Just grab your android phone and start playing your favorite game. It is one of the best ways to kill the time.
These games are finished in a very short span of time. If not, you can hit a pause button and resume it from where you finished later on. 2048 android game is based on the very same concept. This is game is very simple to play. This game can be played by even a 4th standard student. There are other android games for which you have to rotate the screen of your smartphone, shake your phone, tap on the screen consistently, using your both hand, and whatnot.
But to play the android game 2048, you just have to do some swiping, that too with only your one finger or thumb of your any hand. There is no other physical activity involved. You can play this game while you are eating, while you are talking to another person (this is not a good habit though). In short, it is the best game every smartphone should have.
You can check these posts:
- Android Assignment Help
- Top 18 Android Project Ideas
- Tiles Management System
- Bidding Management System
- Computer Science Final Year Project Help
How It Is Played?
2048 android game is 16 cells – 1 grid game. Each cell contains one tile of a number. That number is a multiple of 2k, where k = 1,2,3,4… So, the numbers on the tiles will be 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192. When you start it for the first time (as a matter of fact, for every new game), two random tiles of numbers 2 or will occupy the cells.
You just have to swipe up / down / right / left. When two tiles of the same number meet, they are merged and form one tile equal to the two times number of previous tiles. Example – Tile of numbers 2 and 2 will make the tile of numbers.
Features of 2048 Android Game Project
Following are the functionality of this very popular android 2048 game:
- It is a very small-sized game. Its size is only 230 KB.
- It does not consume a very large amount of RAM, so your smartphone will not hang up.
- It does not ask for any personal information, so you do not have to worry about your privacy.
- Very simple and clear user interface.
- There is only one rule, join the numbers and get to the 2048 tile.
- There is no time limitation. You can complete this game when you want.
- You can start it from where you finish it off last time.
- A great undo support is also available. What will you do if you played one wrong move? You can undo it when you want so that it does not spoil your strategy to win the game.
- It also keeps a record of your high score. It will give you a morale boost each time you play a game.
- When you reach tile 2048, you have won the game. But wait, do you want to play more, no worries; you can continue with the same turn and reach higher tiles.
Now let’s talk developing of the game.
The source code of this game contains 9 java files:
- Animate Block
- Animate Block Area
- Block Area
- Block Design
- Block Setup
- Main Activity
- Perform Action
- Play Game
Details of each java file – Behold, you are going to learn very important concepts of android. They might help you to develop any other android game.
MainActivity.java – It is the main file in the system which is necessary for each and every android application. Whenever an android application is run, it starts from here. If you know the C language, the cursor starts from the main () function, the same thing goes here. It extends the Activity class. Now moving further, the protected void onCreate(Bundle state) method starts the activity. “state” is the variable that will help in recovery and save the state of the activity.
Consider when you minimize the application and open it after some time. You don’t want to lose the previous information. Do you? SetView = new BlockDesign(getBaseContext()); instantiate the object of the activity BlockDesign (will discuss this activity later). SharedPreferences help in storing the data in the application. And, setContentView(setView) is used to set the design or UI to the activity.
- Public Boolean onKeyDown() – This method will help in detecting swiping direction for this android game.
- Public void onSaveInstanceState(Bundle state) – As we have enabled the feature of saving the state information of the application, this method saves it for us in key-value pair. Also, it will call saveGame() method to save what information we want to save.
- Void saveGame() – SharedPreferences.Editor will receive the instance in the editor object.
- BlockSetup blockfield and BlockSetup undoBlockField will set up the blocks for the purpose of undo and another tile. edit.putInt(“width”, blockfield.length); will set the width of the tile in the editor object, which will come in handy later on.
- Edit.putInt(xx + ” ” + yy, blockfield[xx][yy].getvalues()); will set the coordinates of tile at position xx, yy.
- Edit.putInt(“undo block” + xx + ” ” + yy, undoBlockField[xx][yy].getvalues()); It will take out the coordinate of the newly created block so that it can be tracked and removed whenever undo is pressed.
- Void loadGame() – As the name signifies, it will load the application into the memory for its use.
- SetView.playGame.animBlockArea.cancelAll(); It will stop all the animation which are running before proceeding. Coming to the next nested for loop, value = configuration.getInt() will create the tiles with the numbers on it, if they are already there, otherwise, a blank block will be created.
- SetView.playGame.score = configuration.getLong(“score”, setView.playGame.score); it will get the score stored into key – value pair. Same goes with next few lines.
- Protected void onPause() – When the game is paused, saveGame() method is invoked to save the game.
This java file contains the information regarding the tiles we are going to create in the game. All the methods are self-explanatory. It involves the simplest java code. These methods are to set and get the values of the X-axis and Y-axis. They will be used for invocation from the other files.
This class inherits the Block class. Public BlockSetup(int x, int y, int values) – This constructor takes x position, y position, and a value corresponding to these coordinates. Void positionUpdation(Block block) – It updates the coordinates of each tile which is passed as an argument to it. Void setMergedFrom(BlockSetup tile) – It takes an array of tiles that are merged and stored into the array.
The main function of this method is to save the tiles for future use e.g., for undo purposes. Public BlockSetup getMergedFrom() – This returns an array of blocks stored which contains the information on which tiles were used to form another tile.
This file contains the methods to get the information of available blocks so that a new tile can be created, a random block tile to be generated, etc.
There are three important attributes:
- BlockField – for individual tiles.
- UndoBlockField – to keep the newly generated tile that becomes valuable to retrieve for the next state’s undo process.
- BufferBlockField – to keep the temporary block.
Public BlockArea(int sizeX, int sizeY) – This constructor is used to create an array of blocks of size passed an argument to it. It also clears the grid which will contain 16 cells. ArrayList getAvailableBlocks() and ArrayList getNotAvailableBlocks(). This method will check for availability and not available cells in the grid and return an array of these blocks.
Public Boolean isBlocksAvailable() – It will check if there are a bunch of blocks available and return the value. Public Boolean isBlockAvailable(Block block) – This method checks if the ‘block’ is available to use for creating a new tile. Public BlockSetup getBlockContent(Block block) – Now after getting information about blocks, this method will return the value it contains.
Public Boolean isBlockWithinBounds(Block block) – This method will check, the blocks we are going to execute, are well within the bounds of the grid. Though, it would not make a significant impact. Public void readySaveBlocks()– It will set the buffer block with the newly created block’s x-axis and y-axis value. Public void backBlocks() – This method is invoked when undo field is pressed.
The value which is in undoBlockField is copied into blockFied.
Note: Rests of the methods are self-explanatory, easy as eating a pie. Now let’s talk about some animation. Animation is just a fancy word for time-related parameters. It will keep the tab on time delaying, the time elapsed, etc.
Public AnimateBlock (int x, int y, int animateType, long length, long gap, int  others) – This constructor initializes the class attributes. X and Y are the positions of the block, length is animation type, the gap is how much time is delayed between two moves. Public void checkTick(long totalTime) – This method keeps track of the time it takes for a complete animation.
Public Boolean processCompleted() – It returns the Boolean as true or false if the animation has been completed or not.
This file adds animation to your application. Public void playRendering() – It will start the animation for each block. If values of x and y are -1 that means not a single block has been animated so it will animate the whole grid at once. Otherwise, it will add for each cell one at a time. Before proceeding further, I should tell you, canceled animation is when two tiles are merged and the tiles which are merged their animation will be canceled.
So, they become inactive too. Public void checktTickAll(long totalTime)- If an animation time is expired, that block’s animation will be canceled and will be stored in cancelledProcess array. Be it, an individual cell or a whole grid. Public Boolean isProcessActive() – It returns true or false depending on if any animation is active or not.
Public void newGameStart() – It will start the new game. If there is the grid is null, a 4, 4 grid will be created using invocation. Otherwise, undo state-related methods will be invoked. Private void addStartingBlocks() – This method will add two tiles at the start of the game. Void createBlock(BlockSetup blockSetup) – It will create a cell and properties of it such as its position, time to process.
Public void move (int direction) – Here are direction values 0 for up, 1 for right, 2 for down, 3 for left. BlockSetup.positionUpdation(positions ); converges the two tiles position. score = score + merged.getvalues() It will update the score each time the tiles are merged.
Now we are done with most of our individual logic to implement. Here comes the designing part. Nothing looks beautiful if an element of graphic design is not added to an entity. There are several attributes to the class which is defined in the beginning. You can understand them easily as I have tried to name them according to their usage.
Still, I will try to draw some pictures about some things you may get confused about. Paint layout = new Paint (); – Paint class holds the style and color information about how to draw shapes, texts, bitmaps. So, an instance of paint class is instantiated. Public void onDraw(Canvas canvas) – In the first line, there is region.drawBitmap(background, 0, 0, layout), it will reset the transparency of the screen.
The next block of code will check whether the game is active or not, also the animation is active or not, if so, a new button field will be created so that when the game is over, a new game can be started. Then all the cells in the method will be created using the function invocation (defined later on in this file).
Next if (playGame.animBlockArea.isProcessActive()) else if (!playGame.isGameActive() && refresh) && refreshLastTime) set of statement will refresh the screen to know if the animation is still running. Protected void onSizeChanged(int width, int height, int oldw, int oldh) – At first android doesn’t know what size would it be.
It needs to calculate so this method calculates using height and width. It is called only once and some sets of methods are invoked which are discussed in a later part. Void createBlockText() and void createScoreText() – It will draw texts for each individual tiles and text for score respectively. Void createHeader(Canvas region) and public void createInstruction(Canvas region) – These methods are to draw header (2048) at the top and instruction at the bottom of the screen.
Void createBackgroundBlock(Canvas region) – It will create the background blocks to hold the tiles. Void createBlocks(Canvas region) – This method will create the tiles. First, it will check if there is a tile with some value, it will get that value and represent it on the tile so you can have tiles with numbers. Then it will check for any active animation, if it is, it will create a new tile, merge two tiles and move the tiles.
If not, it will just create a cell. Void endState(Canvas region) – It will draw the end game state. If the game is won it will ask if you want to continue or not and will create the state according to that. There would be some invocation of the methods. It is a placeholder method. Void createEndGameStates(Canvas region, Boolean win, Boolean showField) – This method is invoked to draw the interface for if the game is won or lost.
Void createBitmapBackground(int width, int height) – It will simply draw a background, color it. It is a placeholder method to call several methods for drawing the interface such as header, instruction, etc. Void timeTrack() and void checkTick() – Methods are for tracking the time of each frame and calculate how much time it takes to go to the next frame so that each moving speed is calculated.
Public BlockDesign(Context context) – This constructor sets the resources into variables so that they can be used. All of these things are self-explanatory. setOnTouchListener(new PerformAction(this)); is implanted to catch the touch of the screen. And, in the last object of PlayGame class is invoked. I have explained most of the source code.
If there is still something remaining that means those points are very easy to understand. The reason being the method name or a variable name is stated in such a way that it fulfills its purpose.