1, the Chinese New Year on the mobile phone downloaded 2048 played a few days, the whim decided to write with a py, at the beginning of the time to use Qt to realize, found to rely a bit large. Just see graphics.py is based on the Tkinter do the package to take practiced hand, and borrowed csdn a friend encapsulated model.py (2048 logical parts)
2, because it is practiced hand so there is no place to write bad places please spray the light point.
First look at the demo picture
Attached source code:
2048 main Path
Copy the Code code as follows:
#-*-coding:utf-8-*-
#python3.3.5
From Graphics import*
From Tkinter.messagebox import askquestion
From Tkinter.messagebox import Showinfo
Import Time,random,model,configparser
Import gui_2048 as G
Class Application ():
'''
Initializing the application
'''
def __init__ (self):
Self.matrix = Model.init ()
Self.win = G.init ()
self.create_r_2048 (Self.win)
Self.show_matrix (Self.matrix)
Self.win.master.bind (" ", Self.bind_key)
While 1:
Update ()
'''
Create 16 squares on a grid, best scores, current score
'''
def create_r_2048 (Self,win):
p = Point (10, 190)
n = 4
Self.rt = [0 for row in range (N*n)]
For I in range (n):
For a in range (n):
_p = Point (p.x + 60*i, p.y + 60*a)
Self.rt[i+4*a] = g.rectangle_2048 (win,_p)
#最佳成绩
SELF.ZJCJ = G._text (Win,point (135, + +), point (135 + +, + +), Self.getmaxscore ())
#当前分数
SELF.DQJF = G._text (Win,point (135, + +), point (135 + 115, 120 + 30 + 30), ' 0 ')
'''
Get the best results from the configuration file
'''
def getmaxscore (self):
Config = Configparser. Configparser ()
Config.read (' Config.ini ')
Maxscore = Config.get ("Score", "Maxscore")
Return Maxscore
'''
Write the best results to the configuration file
'''
def setmaxscore (Self,score):
Config = Configparser. Configparser ()
Config.optionxform = str
Config.read (' Config.ini ')
Config.set ("Score", "Maxscore", str (score))
Config.write (Open ("Config.ini", "w"))
'''
Initialize the data and interface, and call it at the end of the game
'''
def my_init (self):
Maxscore = Self.getmaxscore ()
if int (Maxscore) < Model.getscore ():
Self.setmaxscore (Model.getscore ())
Self.zjcj.setText (Model.getscore ())
Matrix = Model.init ()
Self.dqjf.setText (Model.getscore ())
Return matrix
'''
Bind keyboard event capture up and down and Q keys
'''
def bind_key (self, event):
'''
Key Event
'''
If Model.is_over (Self.matrix):
If Askquestion ("Game Over", "game Over!\ndo do want to init it?") = = ' Yes ':
Self.matrix = Self.my_init ()
Self.show_matrix (Self.matrix)
Return
Else
Self.win.close ()
Else
If event.keysym.lower () = = "Q":
Self.win.close ()
elif Event.keysym = = "Left":
Self.matrix = Model.move_left (Self.matrix)
elif Event.keysym = = "Right":
Self.matrix = Model.move_right (Self.matrix)
elif Event.keysym = = "Up":
Self.matrix = model.move_up (Self.matrix)
elif Event.keysym = = "Down":
Self.matrix = Model.move_down (Self.matrix)
If event.keysym in [' Q ', ' left ', ' right ', ' up ', ' Down ']:
Try
Self.matrix = Model.insert (Self.matrix)
Self.dqjf.setText (Model.getscore ())
Self.show_matrix (Self.matrix)
Except
Pass
If Model.is_win (Self.matrix):
If Askquestion ("Win", "you win the Game!\ndo do want to init it?") = = ' Yes ':
Self.matrix = Self.my_init ()
Self.show_matrix (Self.matrix)
Return
Else
Self.win.close ()
'''
Get the result data from a two-dimensional array and show it in 16 squares
'''
def show_matrix (self, Matrix):
For I in range (16):
num = matrix[i//4][i%4]
Print (num)
If num = = 0:
num = "
self.rectangle_2048 (I,num)
'''
Make color and number changes to 16 squares
'''
def rectangle_2048 (Self,i,num):
c = Color_rgb (200, 190, 180)
if num = = 2:
c = Color_rgb (240, 230, 220)
elif num = = 4:
c = Color_rgb (240, 220, 200)
elif num = = 8:
c = Color_rgb (240, 180, 120)
elif num = = 16:
c = Color_rgb (240, 140, 90)
elif num = = 32:
c = Color_rgb (240, 120, 90)
elif num = = 64:
c = Color_rgb (240, 90, 60)
elif num = = 128:
c = Color_rgb (240, 90, 50)
elif num = = 256:
c = Color_rgb (240, 200, 70)
elif num = = 512:
c = Color_rgb (240, 200, 70)
elif num = = 1024:
c = Color_rgb (0, 130, 0)
elif num = = 2048:
c = Color_rgb (0, 130, 0)
'''
Loop to set colors and numbers
'''
Self.rt[i][0].setfill (c)
Self.rt[i][1].settext (num)
#main
Application ()
2048gui part
Copy CodeThe code is as follows:
#-*-coding:utf-8-*-
#python3.3.5
From Graphics import*
#初始化并构建2048界面
DEF init ():
Win = Graphwin ("2048", 260, 450)
Win.master.geometry (' +400+150 ') #屏幕位置
c = Color_rgb (206, 194, 180)
Win.setbackground (c)
Hint (win)
_title (Win)
_grid (Win)
Maxscore (Win)
Curscore (Win)
Return win
#2048方格
Def rectangle_2048 (win, p1 = Point (Ten), txt= ", C = Color_rgb (206, 194, 180)):
P2 = point (p1.x +, P1.Y + 60)
R = _rectangle (win,p1,p2,c)
t = _text (win,p1,p2,txt)
Return r,t
#挂牌
def hint (win,p1 = point (10, 10)):
P2 = point (p1.x + P1.Y + 40)
c = Color_rgb (187, 173, 164)
_rectangle (WIN,P1,P2,C)
t = _text (WIN,P1,P2, ' true hero Challenge 2048~ ')
T.settextcolor (Color_rgb (238, 231, 221))
Return T
#标题logo
def _title (win,p1 = Point (10, 60)):
P2 = point (p1.x + p1.y + 120)
c = Color_rgb (228, 184, 0)
_rectangle (WIN,P1,P2,C)
t = Text (Point (p2.x + p1.x)/2, (P2.Y + p1.y)/2), ' 2048 ')
T.setsize (35)
T.setstyle (' bold ')
T.settextcolor (' white ')
T.draw (Win)
#画正方形
def _rectangle (win,p1,p2,c):
r = Rectangle (P1, p2)
R.setfill (c)
R.setoutline (Color_rgb (198, 186, 174))
R.draw (Win)
Return r
#写文字
def _text (win,p1,p2,txt):
t = Text (Point (p2.x + p1.x)/2, (P2.Y + p1.y)/2), TXT)
T.draw (Win)
Return T
#网格
def _grid (win,p1 = Point (10, 190)):
#上面
P_u_1 = Point (p1.x +, P1.Y)
P_u_2 = Point (p1.x + p1.y)
P_u_3 = Point (p1.x + p1.y)
P_u_4 = Point (p1.x +, P1.Y)
#左面
P_l_1 = Point (p1.x, P1.Y + 60)
P_l_2 = Point (p1.x, P1.Y + 120)
P_l_3 = Point (p1.x, P1.Y + 180)
P_l_4 = Point (p1.x, P1.Y + 240)
#右面
p_r_1 = Point (p1.x + p1.y + 60)
p_r_2 = Point (p1.x + p1.y + 120)
P_r_3 = Point (p1.x + P1.Y + 180)
P_r_4 = Point (p1.x + p1.y + 240)
#下面
P_d_1 = Point (p1.x +, P1.Y + 240)
P_d_2 = Point (p1.x + p1.y + 240)
P_d_3 = Point (p1.x + p1.y + 240)
P_d_4 = Point (p1.x + p1.y + 240)
c = Color_rgb (198, 186, 174)
#画横线
L_w_1 = Line (P1, P_u_4)
L_w_2 = line (P_l_1, p_r_1)
L_w_3 = line (p_l_2, p_r_2)
L_w_4 = line (P_l_3, p_r_3)
L_w_5 = line (P_l_4, P_r_4)
L_w_1.setfill (c)
L_w_2.setfill (c)
L_w_3.setfill (c)
L_w_4.setfill (c)
L_w_5.setfill (c)
L_w_1.draw (Win)
L_w_2.draw (Win)
L_w_3.draw (Win)
L_w_4.draw (Win)
L_w_5.draw (Win)
#画竖线
l_h_1 = Line (P1, P_l_4)
l_h_2 = line (p_u_1, P_d_1)
L_h_3 = line (p_u_2, P_d_2)
L_h_4 = line (P_u_3, P_d_3)
L_h_5 = line (P_u_4, P_d_4)
L_h_1.setfill (c)
L_h_2.setfill (c)
L_h_3.setfill (c)
L_h_4.setfill (c)
L_h_5.setfill (c)
L_h_1.draw (Win)
L_h_2.draw (Win)
L_h_3.draw (Win)
L_h_4.draw (Win)
L_h_5.draw (Win)
#最佳成绩
def maxscore (win,p1 = point (135, 60)):
P2 = point (p1.x + p1.y + 30)
c = Color_rgb (187, 173, 164)
_rectangle (WIN,P1,P2,C)
_text (WIN,P1,P2, ' best result: ')
#当前分数
def curscore (win,p1 = point (135, 120)):
P2 = point (p1.x + p1.y + 30)
c = Color_rgb (187, 173, 164)
_rectangle (WIN,P1,P2,C)
_text (WIN,P1,P2, ' current score: ')
The above is the whole content of this article, I hope you can enjoy.