一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 使用graphics.py實現2048小游戲

使用graphics.py實現2048小游戲

2019-11-25 22:34hebedich Python

本文給大家分享的是使用Python實現2048小游戲的源碼,非QT實現的哦,推薦給大家,有需要的小伙伴參考下吧。

1、過年的時候在手機上下載了2048玩了幾天,心血來潮決定用py寫一個,剛開始的時候想用QT實現,發現依賴有點大。正好看到graphics.py是基于tkinter做的封裝就拿來練手,并借用了CSDN一位朋友封裝的model.py(2048邏輯部分)
2、由于是練手的所以不免有寫的不好的地方請大家噴的輕點。

先看看演示圖片

使用graphics.py實現2048小游戲

附上源碼:

2048主程

 

復制代碼代碼如下:

#-*-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():
    '''
    初始化應用程序
    '''
    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()
    '''
    創建網格上的16個方格、最佳成績、當前分數
    '''
    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, 60 + 30),Point(135 + 115, 60 + 30 + 30),self.getMaxScore())
        #當前分數
        self.dqjf = g._text(win,Point(135, 120 + 30),Point(135 + 115, 120 + 30 + 30),'0')
    '''
    從配置文件中獲取最佳成績
    '''     
    def getMaxScore(self):
        config = configparser.ConfigParser() 
        config.read('config.ini')  
        maxScore = config.get("Score", "maxScore") 
        return maxScore
    '''
    把最佳成績寫入配置文件
    ''' 
    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"))
    '''
    初始化數據和界面,在游戲結束后調用
    '''
    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
    '''
    綁定鍵盤事件 捕獲上下左右和Q鍵
    '''   
    def bind_key(self, event):
        '''
        key event
        '''
        if model.is_over(self.matrix):
            if askquestion("GAME OVER","GAME OVER!\nDo you 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 you want to init it?") == 'yes':
                self.matrix = self.my_init() 
                self.show_matrix(self.matrix)
                return
            else:
                self.win.close() 
    '''
    從二維數組中獲取結果數據并展示在16方格中
    '''
    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)
    '''
    對16個方格做顏色和數字變更
    '''
    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)
        '''
        循環設置顏色和數字
        '''
        self.rt[i][0].setFill(c)
        self.rt[i][1].setText(num)
#main
Application()


2048gui部分

復制代碼代碼如下:

#-*-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(10, 10),txt='',c = color_rgb(206, 194, 180)): 
    p2 = Point(p1.x + 60, 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 + 240, p1.y + 40)
    c = color_rgb(187, 173, 164)
    _rectangle(win,p1,p2,c)
    t = _text(win,p1,p2,'真英雄 挑戰2048~') 
    t.setTextColor(color_rgb(238, 231, 221))
    return t
#標題logo
def _title(win,p1 = Point(10, 60)): 
    p2 = Point(p1.x + 120, 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 + 60, p1.y) 
    p_u_2 = Point(p1.x + 120, p1.y)
    p_u_3 = Point(p1.x + 180, p1.y)
    p_u_4 = Point(p1.x + 240, 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 + 240, p1.y + 60)
    p_r_2 = Point(p1.x + 240, p1.y + 120)
    p_r_3 = Point(p1.x + 240, p1.y + 180)
    p_r_4 = Point(p1.x + 240, p1.y + 240)
    #下面
    p_d_1 = Point(p1.x + 60 , p1.y + 240)
    p_d_2 = Point(p1.x + 120 , p1.y + 240)
    p_d_3 = Point(p1.x + 180 , p1.y + 240)
    p_d_4 = Point(p1.x + 240 , 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 + 115, p1.y + 30)
    c = color_rgb(187, 173, 164)
    _rectangle(win,p1,p2,c)
    _text(win,p1,p2,'最佳成績:')
#當前分數
def curScore(win,p1 = Point(135, 120)): 
    p2 = Point(p1.x + 115, p1.y + 30)
    c = color_rgb(187, 173, 164)
    _rectangle(win,p1,p2,c)
    _text(win,p1,p2,'當前分數:')

 

 

以上就是本文的全部內容了,希望大家能夠喜歡。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲男人的天堂成人 | 欧美黑人换爱交换乱理伦片 | 色中文网| 亚洲一区二区三区深夜天堂 | 国产成人欧美 | 欧美亚洲国产综合在线 | 国产激情视频 | 久久精品在现线观看免费15 | 火影忍者小南裸羞羞漫画 | 韩国最新理论片奇忧影院 | 日韩成人精品在线 | 欧美一区精品二区三区 | 青草国内精品视频在线观看 | 被夫上司侵犯了中文字幕 | 麻豆在线md0087免费 | 欧美同性猛男videos | 欧美18一19性高清hd4k | 九九九九在线视频播放 | 欧美日韩国产在线人成dvd | 亚洲视频免费 | 男人午夜剧场 | 91手机看片国产永久免费 | 亚洲热在线视频 | 日本高清视频一区二区 | 风间由美理论片在线观看 | 成人免费福利网站在线看 | 成人亚洲欧美日韩在线观看 | 丝瓜茄子绿巨人秋葵榴莲污 | 亚洲国产精品91 | 4455永久在线视频观看 | 精品亚洲一区二区三区在线播放 | 扒开双腿疯狂进出爽爽动态图 | 99这里只有精品66视频 | 毛片影院| 国产成人精品一区二区仙踪林 | hd最新国产人妖ts视频 | 美女福利视频午夜在线 | 婷婷综合久久 | 精品午夜寂寞影院在线观看 | 美女和男人差差 | 国产精品xxxav免费视频 |