标签:blog http io ar os sp for on div
闲来无聊,电脑上的游戏也被删了,自己动手写了个2048,目前只是实现了基本的游戏功能,代码还比较弱:
百度盘:http://pan.baidu.com/s/1eQpJbgQ
gui代码如下:
#!/usr/bin/python # -*- coding: utf-8 -*- #导入tkinter import Tkinter import game2048_Class import sys def 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] #定义的方法 监听键盘事件 def printkey(event): print(‘input: ‘ + event.char) input_chr = event.char 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.end_flag) : sys.exit(0) refreshGui() game = game2048_Class.game2048(4) iwindow = Tkinter.Tk() iwindow.wm_title("2048") button_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‘] = 6 button[‘height‘] = 3 button.grid(row = i,column = j) button_list[i].append(button) iwindow.bind_all(‘<Key>‘, printkey) #显示窗体 iwindow.mainloop()
game_2048的类如下:
#!/usr/bin/python # -*- coding: utf-8 -*- """ 2048 game Version: 2014-11-19 Author: zyh """ import random import sys import os import pythoncom import pyHook class game2048: """docstring for game2048""" n = 0 #维度 nlist = [] #矩阵 numlist = [2,2,2,4] #取值数组 start_flag = 0 #游戏开始标志 end_flag = 0 #游戏结束标志 move_flag = 0 #移动是否有效标志 #2048 初始化函数 def __init__(self, n): self.n = n for i in range(self.n):#初始化2048矩阵 self.nlist.append([]) for j in range(self.n): #self.nlist[i][j] = 0 self.nlist[i].append(0) #########<随机两个位置为2或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) #########</随机两个位置为2或4>############# def nlistModify(self,action): #根据用户输入的上i、下k、左j、右l,图形进行相应的变化 originalMatrix = [] #保存原始矩阵,用于判断矩阵是否发生了变化 for i in range(self.n): #初始化原始矩阵 originalMatrix.append([]) for j in range(self.n): #self.nlist[i][j] = 0 originalMatrix[i].append(self.nlist[i][j]) #print "originalMatrix1:",originalMatrix #self.outPut(originalMatrix) nlistModify = [] nlistModifyBack = [] self.move_flag = 0 #用于判定本次用户输入方向的移动是否有效 if( action == ‘i‘ ):#矩阵向上移动 for i in range(self.n): #print self.n,i nlistModify.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,i nlistModifyBack.append([row[i] for row in nlistModify])#将矩阵的列变成行 self.nlist = nlistModifyBack elif( action == ‘j‘ ):#矩阵向左移动 self.nlist = self.matrixMove(self.nlist) #print "nlistModify_self.nlist:",self.nlist elif( action == ‘k‘ ):#矩阵向下移动 for i in range(self.n): #print self.n,i nlistModify.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,i nlistModifyBack.append([row[i] for row in nlistModify])#将矩阵的列变成行 self.nlist = nlistModifyBack elif( action == ‘l‘ ):#矩阵向右移动 nlistModify = self.nlist for i in range(self.n): nlistModify[i].reverse()#将矩阵的顺序左右颠倒 nlistModify = self.matrixMove(nlistModify)#矩阵向左移动 nlistModifyBack = nlistModify for i in range(self.n): nlistModifyBack[i].reverse()#将矩阵的顺序左右颠倒 self.nlist = nlistModifyBack else: 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 = 1 break if (self.move_flag): break def matrixMove(self,matrix):#矩阵从右向左移动,相同的数字合并,空余位置补0 m = matrix #print "matrix:",matrix length = 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.nlist def listMoveOnce(self,single_list):#每次合并一个数字 l = single_list len_list = len(single_list)#列表的长度 #print "list:" #print l #print "listlength:" #print len_list flag = 0 zero_flag = 0 for i in range(len_list-1): if (l[i] == 0): zero_flag = zero_flag + 1 continue else: j = i + 1 if (j<len_list): while (l[j] == 0): j = j + 1 if j > len_list -1 : break #print "j:",j #print "len_list:",len_list pass if (j<len_list): if (l[i] == l[j]): l[i] = l[i] + l[j] t_i = i + 1 t_j = j + 1 while (t_i < len_list)&(t_j < len_list): l[t_i] = l[t_j] t_i = t_i + 1 t_j = t_j + 1 while (t_i < len_list): l[t_i] = 0 t_i = t_i + 1 flag = flag + 1 break #print "l:",l #print "j:",j #print "zero_flag:",zero_flag #print "zero_flag:",zero_flag for 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-k l[len_list-1-t_k] = 0 if flag == 0: flag = 1 return flag , l def listMove(self,single_list):#列表从后向前移动,相同的数字合并,空余位置补0 p = 0 len_list = len(single_list)#列表的长度 p,l = self.listMoveOnce(single_list) while (p < len_list): #print "p:" #print p q,l[p:] = self.listMoveOnce(single_list[p:]) #print "q:" #print q if( q==0 ): break p = p + q return l def randomNum(self): #根据用户输入,图形进行相应的变化后,剩余为0的位置随机出现2或4 zero_list = [] random_ij = [] for i in range(self.n): for j in range(self.n): if (self.nlist[i][j] == 0): temp_ij = [i,j] zero_list.append(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 #保存原始矩阵,用于判断完成后,恢复原始矩阵 self.end_flag = 1 for i in range(self.n): for j in range(self.n): if (self.nlist[i][j] == 0): self.end_flag = 0 break if (self.end_flag == 0) : break for m in [‘i‘,‘j‘,‘k‘,‘l‘]: self.nlistModify(m) if self.move_flag == 1: self.end_flag = 0 break self.nlist = originalMatrix #2048 输出函数 def outPut(self,matrix): #print "self.nlist:",self.nlist os.system(‘cls‘) n = len(matrix) for i in range(n): #for j in range(n): # print matrix[i][j],"\t" # pass print 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.outPut(game.nlist) #game.userInput() # game.matrixMove(‘i‘) #print game.listMoveOnce([0,0,0,0,2])
标签:blog http io ar os sp for on div
原文地址:http://www.cnblogs.com/finalsatan/p/4116500.html