Unlike the traditional game, where two chips with the same numbers collide to form one with a number twice as large, here the math is different. Although you can run through the first couple of paragraphs for general development. It’s great fun to develop a game rather than playing other’s games, now we will play the game which we developed.Those who are familiar with the standard 2048 can immediately start playing. We have successfully developed popular 2048 game in python. If any cell value has reached 2048, then the player is won and a message box is flashed on the screen announcing the winner. the state where no movement can change the matrix, then the game is over. Similarly, right is same as moving left+reverse.Īfter every operation, we need to check the game status, if all cells are occupied and we cannot even merge any two cells i.e.Moving down is same as moving up but we need to reverse the matrix.For moving up, we will take transpose then swipe left and again take transpose to return to the original order.For left swipe, we will just compress and then merge the gridCell matrix and then if compress or merge is true (indicating the values of the matrix is affected by previous two functions), then we need to compress the grid again.Link_keys: First of all it checks if the game is already won or lost, and if it is, it executes a return statement without doing anything.start: It calls random_cell twice to assign ‘2’ to gridCell value of two random cells and then it paints the grid and after that, it calls link_keys to link up, down, left, and right keys. It initializes all the variables with appropriate default values. _init_(self): It is the constructor function.This class doesn’t have many variables, it only has some Boolean variables indicating game status. paintGrid: It assigns foreground and background color to each cell of the 4×4 grid corresponding to its gridCell value.We can merge two cells if and only if they hold the same gridCell value. Can_merge: It returns a boolean value denoting we can merge any two cells or not.Random_cell: It first stores all the empty cells in a list and then picks a random cell from the created list and make its gridCell value 2.mergeGrid: It adds the gridCell value of two adjacent cells if they have same gridCell values.CompressGrid: It moves all not empty cells to the left, so that merging can be done easily.Transpose: It uses zip function and takes transpose of the gridCell matrix.Reverse: It reverse the gridCell matrix. It initializes all the variables with appropriate default values like ‘0’ for gridCell, False for moved, merge and so on. Score: It stores the current score of the player.It is also used to configure the background and foreground of the cell according to its gridCell value. Board: It is a 4×4 grid of tkinter label widget which displays the value of the cell on tkinter window.gridCell: It is a 4×4 integer matrix which stores the actual integer value of all the cells.gameArea: It is a tkinter frame widget.Color: It is a dictionary that stores foreground color for every cell.Bg_color: It is a dictionary that stores background color for every cell.Self.gameArea=Frame(self.window,bg= 'azure3') Please download source code of 2048 game in Python: 2048 Game Code Creating main.py Tkinter Download Python 2048 Project Code We will develop this game using python and tkinter. It is a really addictive game and the main operation performed in this game is addition which makes it easy for all of us. Get Ready to become a Python professional with 70+ Python ProjectsĢ048 is a simple mathematics puzzle game.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |