How to make a simple version of 2048 Mini Game with Python+Pygame
This article mainly introduces how to use Python+Pygame to make a simple version of 2048 Mini Game related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this how to use Python+Pygame to make a simple version of 2048 Mini Game article will have a harvest, let's take a look.
Text
In order to understand the rules of the game, the editor directly downloaded a 2048 Mini Game, started to play a wave!
And then. I can't stop at all! 23333 ~
How to play: slide up and down with your fingers or keyboard to merge two identical numbers, for example: 2 + 2 = 4, 4 + 4 = 8. Until
1024 + 1024 = 2048!
Main code # exercise 1: define the function and move the 0 element in the list to the end. # [2]-- > [2] # [0] # [4] > [4] # suitable for zero basic students def zero_to_end (list_target): # select non-zero elements to form a new list # [2]-> [2] 2] new_list = [] for item in list_target: if item! = 0: new_list.append (item) # add zero element [2Mague 2]-- > [2Jing 2] 0] # judge the number of zero elements in the original list: list_target.count (0) for i in range (list_target.count (0)): new_list.append (0) # return a new list return new_list # def zero_to_end (list_target): # # select non-zero elements to form a new list # # [2,0,2,0]-> [2 2] # new_list = [item for item in list_target if item! = 0] # # repeatedly generate zero element [0] * list_target.count (0) # new_list + = [0] * list_target.count (0) # # return a new list # return new_list # classmate method # def zero_to_end (list_target): # # delete zero element in Append # for item in list_target:# if item = = 0item # list_target.remove (0) # list_target.append (item) # # return the new list # return list_target # Test # print (zero_to_end ([1) ) # print (zero_to_end ([0meme 4,2,4]) # exercise 2: define a function that merges the same (or not adjacent) list elements # [2]-- > [4-0] # [2-0]-- > [4-2-2-0]-- > [4-2-2-0-0]-- > [4-2-2-0-0]-- > [4-2-0] 4 list 0] #-- > [2 record4] # hint: # 0 element moves to the end # adjacent same merge # list [0] = = list [1] def merge (list_target): # 1. Move the zero element to the end [2 list_target 0]-- > list_target = zero_to_end (list_target) # 2. Merge for i in range (len (list_target)-1): # if non-zero elements are adjacent and the same if list_ target [I]! = 0 and list_ target [I] = = list_ target [I + 1]: # accumulate the latter element to the previous element list_ target [I] + = list_ target [I + 1] # the latter element is cleared list_ target [I + 1] = 0 # 3. Move the zero element to the end [2 list_target 2]-- > [4] > [4] > [4] list_target = zero_to_end (list_target) return list_target # print ( 2list_ 0]) # exercise 3: define the function 11:33def print_atlas (list_atlas): # 00 01 02 03 for rin range (len (list_atlas)): for c in range (len (list_ Atlas [r]): print (list_ Atlas [r] [c], end= ") print () atlas01 = [2 Magneol 0,0,2] [8,0,4,4], [2,2,0,4], [0,2,4,0],] print_atlas (atlas01) # exercise 4: print the second line in the console With the fourth line of elements. # the first column, and the third column elements. # second line # for c in range (4): # print (atlas01 [1] [c], end= ") # print () # # fourth line # for c in range (4): # print (atlas01 [3] [c]) End= ") # print () # # first column # for rin range (4): # print (atlas01 [r] [0]) # # third column # for rin range (4): # print (atlas01 [r] [2]) # exercise 5 Define the function to move up # hint: form an one-dimensional list of each column element of a two-dimensional list, give it to the merge merge function, and then return it to the two-dimensional list def move_up (atlas): # 15:30 # form the first column element of the two-dimensional list into an one-dimensional list # 00 10 20 30 for c in range (4): list_merge = [] for r in range (4): list_merge.append (Atlas [r] [c]) # hand over the merged merge function list_merge = merge (list_merge) # and return it to the 2D list for r in range (4): atlas [r] [c] = list_ merge [r] return atlas # resutl = move_up (atlas01) # print_atlas (resutl) # extension Job 1: define the function to move to the left def move_left (atlas): for rin range (4): # get the line list_merge = [] for c in range (4): # 00 01 02 03 from left to right List_merge.append (Atlas [r] [c]) list_merge = merge (list_merge) for c in range (4): atlas [r] [c] = list_ Merge [c] return atlas # resutl = move_left (atlas01) # print_atlas (resutl) # extension Job 2: define the function to move downward # 30 20 10 00def move_down (atlas): for c in Range (4): list_merge = [] # get two-dimensional list column element for r in range (3) from bottom to top -1 list_merge.append [r] [c]) list_merge = merge (list_merge) # get one-dimensional list elements from left to right # return the two-dimensional list for r in range (3,-1) from bottom to top -1): atlas [r] [c] = list_merge [3-r] # 0 123 return atlas def move_right (atlas): for r in range (4): list_merge= [] for c in range (3,-1,-1): list_merge.append (Atlas [r] [c]) list_merge=merge (list_merge) for c in range (3,-1) -1): atlas [r] [c] = list_merge [3-c] return atlas# resutl = move_down (atlas01) # print_atlas (resutl) # extension Job 3: define the function to move to the right while True: shell= input ("Please enter player instructions wsad:") if shell== "w": move_up (atlas01) print_atlas (atlas01) elif shell== "s ": move_down (atlas01) print_atlas (atlas01) elif shell==" a ": move_left (atlas01) print_atlas (atlas01) elif shell==" d ": move_right (atlas01) print_atlas (atlas01) else: print (" input error ")
This is the end of the article on "how to make a simple version of 2048 Mini Game with Python+Pygame". Thank you for reading! I believe that everyone has a certain understanding of "how to use Python+Pygame to make a simple version of 2048 Mini Game". If you want to learn more, you are welcome to follow the industry information channel.