2048 Android Game Project
2048 Android Game, I had downloaded this game because It was trending, all of my friend was playing so I also downloaded 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 PlayStore. You can download here. If you want to buy source code you can buy here.
2048 Android Game
Android game is also one of them 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 it 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 of your both hand and what not. But to play 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
- Hotel Management System
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 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 number.
Features of 2048 Android Game Project
Following are the functionality of this very popular android 2048 game.
- It is very small sized game. Its size is only 230 KB.
- It does not consume 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 record of your high score. It will give you moral boost each time you play a game.
- When you reach to tile 2048, you have won the game. But wait, do you want to play more, no worries; you can continue with same turn and reach to 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 files – 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 C language, the cursor starts from main() function, same thing goes here.
It extends the Activity class. Now moving further, protected void onCreate(Bundle state) method starts the activity. “state” is the variable which will help in recover and save the state of the activity. Consider when you minimize the application and open it after sometime. 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 helps 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 save 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 editor object.
- BlockSetup blockfield and BlockSetup undoBlockField will setup the blocks for the purpose of undo and other 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 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 simplest java code. These methods are to set and get the values of 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 value corresponding to these coordinates.
void positionUpdation(Block block) – It updates the coordinates of the each tile which is passed as an argument to it.
void setMergedFrom(BlockSetup tile) – It takes an array of tiles which 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 purpose.
public BlockSetup getMergedFrom() – This returns an array of blocks stored which contains the information 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 attribute:
blockField – for individual tiles
undoBlockField – to keep the newly generated tile which become valuable to retrieve for next state’s undo process.
bufferBlockField – to keep 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 clear the grid which will contain 16 cell.
ArrayList getAvailableBlocks() and ArrayList getNotAvailableBlocks()
this method will check for available 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 check if the ‘block’ is available to use it for create 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 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, time elapsed etc.
public AnimateBlock (int x, int y, int animateType, long length, long gap, int others) – This constructor initialize the class attrbitues. X and Y are the position of the block, length is animation type, 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 return 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 blocks. If value 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, cancelled animation is when two tiles are merged and the tiles which are merged their animation will be cancelled. So, they become inactive too.
public void checktTickAll(long totalTime)- If an animation time is expired, that block’s animation will be cancelled and will be stored in cancelledProcess array. Be it, an individual cell or 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 grid is null, a 4, 4 grid will be created using invocation. Otherwise undo state related method 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 are 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 picture about some things you may get confused at.
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 first line, there is region.drawBitmap(background, 0, 0, layout), it will reset the transparency of the screen.
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 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 screento 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 set of methods are invoked which are discussed in 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 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 tou can have tiles with numbers. Then it will check for any active animation, if it is, it will create 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 for 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 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 method 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 take to go to next fram so that each move speed is calculated.
public BlockDesign(Context context) – This constructor set the resources into variables so that they can b 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. Reason being the method name or a variable name is stated in such a way that it fulfill its purpose.