Python version 2048 games

Source: Internet
Author: User

Idle to boredom, the game on the computer was also deleted, I wrote a 2048, at present just realized the basic game function, the code is relatively weak:

Baidu Plate: Http://pan.baidu.com/s/1eQpJbgQ

The GUI code is as follows:

#!/usr/bin/python#-*-coding:utf-8-*-#导入tkinterimport Tkinter import game2048_classimport sysdef Refreshgui (): n = len (game.nlist) for I in Range (n): for j in Range (n): if game.nlist[i][j] = = 0:button_list[i][j][' text '] = ' else:button_list[i ][j][' text '] = Game.nlist[i][j] #定义的方法 Monitor keyboard event def printkey (event): Print (' input: ' + Event.char) INPUT_CHR = Event.cha R if ((INPUT_CHR = = "S") & (Game.start_flag = = 0): Game.start_flag = 1 elif (INPUT_CHR = = "Q"): Sys . Exit (0) else:game.nlistModify (INPUT_CHR) if (Game.move_flag): Game.randomnum () Game.isend () if (game.e Nd_flag): Sys.exit (0) Refreshgui () game = game2048_class.game2048 (4) Iwindow = tkinter.tk () iwindow.wm_title ("2048") bu Tton_list = []num_game_list = Len (game.nlist) for I in Range (num_game_list): #初始化2048矩阵button_list. Append ([]) for J in Range (num_game_list): Button = Tkinter.button (Iwindow) button[' text '] = ' button[' width '] = 6button[' height '] = 3button.grid (row = I,column = j) Button_liSt[i].append (Button) iwindow.bind_all (' <Key> ', PrintKey) #显示窗体iwindow. Mainloop () 

The

game_2048 classes are as follows:

#!/usr/bin/python#-*-coding:utf-8-*-"" "2048 Gameversion:2014-11-19author:zyh" "" Import Randomimport Sysimport Osimport pythoncom Import Pyhook class game2048: "" "DocString for game2048" "" n = 0 #维度nlist = [] #矩阵numlist = [2,2,2,4] # Array of values Start_flag = 0 #游戏开始标志end_flag = 0 #游戏结束标志move_flag = 0 #移动是否有效标志 #2048 initialization function def __init__ (self, n): SELF.N = nfor i in R Ange (SELF.N): #初始化2048矩阵self. Nlist.append ([]) for J in Range (SELF.N): #self. Nlist[i][j] = 0self.nlist[i].append (0) # # # # # # # # # # # #####< random two positions of 2 or 4>############ #x_first = Random.randint (0,self.n-1) Y_first = Random.randint (0,self.n-1) Self.nlist[x_first][y_first] = Random.choice (self.numlist) X_second = Random.randint (0,self.n-1) Y_second = Random.randint (0,self.n-1) while ((X_second = = X_first) & (Y_second = y_first): X_second = Random.randint (0,self.n- 1) Y_second = Random.randint (0,self.n-1) Self.nlist[x_second][y_second] = Random.choice (self.numlist) #########</ Random two positions are 2 or 4>############ #def nlistmodify (self,action): #根据用户输入的上i, down K, leftJ, right L, the graph changes accordingly Originalmatrix = [] #保存原始矩阵, used to determine if the matrix has changed for the I in Range (SELF.N): #初始化原始矩阵originalMatrix. Append ([]) for J In range (SELF.N): #self. Nlist[i][j] = 0originalmatrix[i].append (self.nlist[i][j]) #print "originalMatrix1:", Originalmatrix#self.output (originalmatrix) nlistmodify = []nlistmodifyback = []self.move_flag = 0 #  Used to determine if the movement of this user input direction is valid if (action = = ' I '): #矩阵向上移动for i in range (SELF.N): #print self.n,inlistmodify.append ([row[i] for row in Self.nlist]) #将矩阵的列变成行 #print "Nlistmodify_matrix:" #self. OutPut (nlistmodify) nlistmodify = Self.matrixmove ( nlistmodify) #矩阵向左移动 #print "Nlistmodify_matrix_move:" #self. OutPut (nlistmodify) for I in Range (SELF.N): #print SELF.N, Inlistmodifyback.append ([row[i] for row in nlistmodify]) #将矩阵的列变成行self. nlist = Nlistmodifybackelif (Action = = ' J '): # The matrix moves left self.nlist = Self.matrixmove (self.nlist) #print "nlistmodify_self.nlist:", self.nlistelif (action = = ' K '): # The matrix moves down for I in a range (SELF.N): #print self.n,inlistmodify.append ([row[i] for row in self.nlist]) #将矩阵的列变成行for i IN Range (SELF.N): Nlistmodify[i].reverse () #将矩阵的顺序左右颠倒nlistModify = Self.matrixmove (nlistmodify) #矩阵向左移动for I in range ( SELF.N): Nlistmodify[i].reverse () #将矩阵的顺序左右颠倒for I in Range (SELF.N): #print self.n,inlistmodifyback.append ([row[i] for Row in nlistmodify]) #将矩阵的列变成行self. nlist = Nlistmodifybackelif (action = = ' L '): #矩阵向右移动nlistModify = Self.nlistfor I in ran GE (SELF.N): Nlistmodify[i].reverse () #将矩阵的顺序左右颠倒nlistModify = Self.matrixmove (nlistmodify) #矩阵向左移动nlistModifyBack = Nlistmodifyfor i in range (SELF.N): Nlistmodifyback[i].reverse () #将矩阵的顺序左右颠倒self. nlist = nlistmodifybackelse:pass# Print "originalMatrix2:" #self. Output (Originalmatrix) #print "Movedmatrix:" #self. Output (self.nlist) for I in range ( SELF.N): For J in Range (SELF.N): if (originalmatrix[i][j]! = Self.nlist[i][j]): Self.move_flag = 1breakif (Self.move_flag) : Breakdef matrixmove (Self,matrix): #矩阵从右向左移动, same number merge, spare position complement 0m = Matrix#print "Matrix:", matrixlength = Len (matrix) for I In range (length): m[i] = Self.listmove (M[i]) #print "m[i]:", M[i]return M#PRint "Matrixmove_self.nlist:", Self.nlistdef listmoveonce (self,single_list): #每次合并一个数字l = Single_listlen_list = Len ( single_list) #列表的长度 #print "list:" #print l#print "listlength:" #print len_listflag = 0zero_flag = 0for i in range (len_list-1 ): if (l[i] = = 0): Zero_flag = zero_flag + 1continueelse:j = i + 1if (j<len_list): while (l[j] = = 0): j = j + 1if J > L En_list-1: Break#print "J:", J#print "Len_list:", Len_listpassif (j<len_list): if (l[i] = = L[j]): l[i] = l[i] + l[j]t_i =  i + 1t_j = j + 1while (T_i < Len_list) & (T_j < Len_list): l[t_i] = L[t_j]t_i = t_i + 1t_j = t_j + 1while (T_i < len_list): l[t_i] = 0t_i = t_i + 1flag = flag + 1break#print "L:", L#print "J:", J#print "Zero_flag:", Zero_flag#print "Zero_ Flag: ", Zero_flagfor K in range (Len_list-zero_flag): l[k] = l[k+zero_flag]for t_k in range (Zero_flag): #print" K: ", K#print Len_list-1-kl[len_list-1-t_k] = 0if Flag = = 0:flag = 1return flag, Ldef listmove (self,single_list): #列表从后向前移动, same number combined, spare bit Offset 0p = 0len_list = Len (single_list) #列The length of the table p,l = Self.listmoveonce (single_list) while (P < len_list): #print "P:" #print pq,l[p:] = self.listmoveonce (single_ List[p:]) #print "Q:" #print qif (q==0): BREAKP = p + qreturn ldef randomnum (self): #根据用户输入, the position of the remaining 0 randomly appears 2 or 4zero_ after the graphic changes accordingly List = []random_ij = []for i in Range (SELF.N): to J in range (SELF.N): if (self.nlist[i][j] = = 0): Temp_ij = [i,j]zero_list.a Ppend (temp_ij) Random_ij = Random.choice (zero_list) self.nlist[random_ij[0]][random_ij[1]] = Random.choice ( self.numlist) def isend (self): #判断游戏是否结束originalMatrix = Self.nlist #保存原始矩阵, used to determine when the original matrix was restored Self.end_flag = 1for i in range (SELF.N): for J in Range (SELF.N): if (self.nlist[i][j] = = 0): Self.end_flag = 0breakif (Self.end_flag = = 0): breakfor m in [' I ', ' j ', ' K ', ' L ']:self.nlistmodify (m) if Self.move_flag = = 1:self.end_flag = 0breakself.nlist = originalmatrix#2048 Output function def output (Self,matrix): #print "self.nlist:", Self.nlistos.system (' cls ') n = len (matrix) for I in Range (n): #for J in     Range (n): #print matrix[i][j], "\ t" #passprint matrix[i]print "                                 "If __name__==" __main__ ": Game = game2048 (4) #game. Userinput () #print game.nlist #game. Output (game.nlist) #game. Randomnum () #game. Output (game.nlist) game.nlist = [8,16,8,2],[32,2,8,4],[4,32 , 8,2],[32,2,4,16]] #game. nlist = [[0,0,0,0],[0,0,0,0],[0,0,0,4],[0,0,0,16]] #game. nlist = [[0,0,0,0],[0,0,0,2],[0,0,    0,4],[0,0,2,8]] #左移右移都无效 #game. OutPut (game.nlist) #print "Game.move_flag:", Game.move_flag #game. Nlistmodify ("J")    #print "Game.move_flag:", Game.move_flag #game. OutPut (game.nlist) #f, L = game.listmoveonce ([2,0,0,0]) #print F #print l #print game.nlist print "" #game. Nlistmodify (' l ') #game. OUTP UT (game.nlist) #game. Userinput () # game.matrixmove (' i ') #print game.listmoveonce ([0,0,0,0,2])

Python version 2048 games

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.