Use graphics.py to implement 2048 games _python

Source: Internet
Author: User

1, the New Year's time in the mobile phone up and down 2048 played a few days, the whim of the decision to write a py, just at the beginning of the time to use QT to achieve, found a little dependent. Just see graphics.py is based on Tkinter do the encapsulation to take to practicing, and borrowed csdn a friend encapsulated model.py (2048 logical part)
2, because it is practicing, so unavoidably have write the bad place please everybody spurts lightly.

First look at the demo picture

Attached source code:

2048 Master Process

Copy 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 an 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 ("<Key>", Self.bind_key)
While 1:
Update ()
'''
Create 16 squares on the grid, best scores, current points
'''
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"))
'''
Initializes the data and interface to call after 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 key
'''
def bind_key (self, event):
'''
Key Event
'''
If Model.is_over (Self.matrix):
If Askquestion ("GAME over", "GAME Over!\ndo your 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 your want to init it?") = = ' Yes ':
Self.matrix = Self.my_init ()
Self.show_matrix (Self.matrix)
Return
Else
Self.win.close ()
'''
Gets the result data from the two-dimensional array and displays 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 set color and number
'''
Self.rt[i][0].setfill (c)
Self.rt[i][1].settext (num)
#main
Application ()

2048gui part
Copy Code code 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, ' Real 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 + 180, 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 + 180, 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 results: ')
#当前分数
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 entire content of this article, I hope you can enjoy.

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.