亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用graphics.py實(shí)現(xiàn)2048小游戲

 更新時(shí)間:2015年03月10日 10:56:50   投稿:hebedich  
本文給大家分享的是使用Python實(shí)現(xiàn)2048小游戲的源碼,非QT實(shí)現(xiàn)的哦,推薦給大家,有需要的小伙伴參考下吧。

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

先看看演示圖片

附上源碼:

2048主程

復(fù)制代碼 代碼如下:

#-*-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():
    '''
    初始化應(yīng)用程序
    '''
    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()
    '''
    創(chuàng)建網(wǎng)格上的16個(gè)方格、最佳成績(jī)、當(dāng)前分?jǐn)?shù)
    '''
    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)
        #最佳成績(jī)
        self.zjcj = g._text(win,Point(135, 60 + 30),Point(135 + 115, 60 + 30 + 30),self.getMaxScore())
        #當(dāng)前分?jǐn)?shù)
        self.dqjf = g._text(win,Point(135, 120 + 30),Point(135 + 115, 120 + 30 + 30),'0')
    '''
    從配置文件中獲取最佳成績(jī)
    '''    
    def getMaxScore(self):
        config = configparser.ConfigParser()
        config.read('config.ini') 
        maxScore = config.get("Score", "maxScore")
        return maxScore
    '''
    把最佳成績(jī)寫入配置文件
    '''
    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"))
    '''
    初始化數(shù)據(jù)和界面,在游戲結(jié)束后調(diào)用
    '''
    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()
    '''
    從二維數(shù)組中獲取結(jié)果數(shù)據(jù)并展示在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)
    '''
    對(duì)16個(gè)方格做顏色和數(shù)字變更
    '''
    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)
        '''
        循環(huán)設(shè)置顏色和數(shù)字
        '''
        self.rt[i][0].setFill(c)
        self.rt[i][1].setText(num)
#main
Application()

2048gui部分
復(fù)制代碼 代碼如下:

#-*-coding:utf-8-*-
#python3.3.5
from graphics import*
#初始化并構(gòu)建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,'真英雄 挑戰(zhàn)2048~')
    t.setTextColor(color_rgb(238, 231, 221))
    return t
#標(biāo)題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
#網(wǎng)格
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)
#最佳成績(jī)
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,'最佳成績(jī):')
#當(dāng)前分?jǐn)?shù)
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,'當(dāng)前分?jǐn)?shù):')

以上就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

  • Python實(shí)現(xiàn)的擬合二元一次函數(shù)功能示例【基于scipy模塊】

    Python實(shí)現(xiàn)的擬合二元一次函數(shù)功能示例【基于scipy模塊】

    這篇文章主要介紹了Python實(shí)現(xiàn)的擬合二元一次函數(shù)功能,結(jié)合實(shí)例形式分析了Python基于scipy模塊進(jìn)行二元一次函數(shù)擬合相關(guān)科學(xué)運(yùn)算操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-05-05
  • 一文帶你深入理解python中pytest-repeat插件的工作原理

    一文帶你深入理解python中pytest-repeat插件的工作原理

    這篇文章主要和大家一起深入探討到底pytest_repeat插件的具體功能是如何實(shí)現(xiàn)的呢,相信具體了解了該插件,其他三方插件也可以很快了解它內(nèi)部運(yùn)行機(jī)制,所以本文詳細(xì)講解了python pytest-repeat插件的工作原理,需要的朋友可以參考下
    2023-09-09
  • python密碼學(xué)對(duì)稱和非對(duì)稱密碼教程

    python密碼學(xué)對(duì)稱和非對(duì)稱密碼教程

    這篇文章主要為大家介紹了python密碼學(xué)對(duì)稱和非對(duì)稱密碼教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Python中利用aiohttp制作異步爬蟲及簡(jiǎn)單應(yīng)用

    Python中利用aiohttp制作異步爬蟲及簡(jiǎn)單應(yīng)用

    asyncio可以實(shí)現(xiàn)單線程并發(fā)IO操作,是Python中常用的異步處理模塊。這篇文章主要介紹了Python中利用aiohttp制作異步爬蟲的相關(guān)知識(shí),需要的朋友可以參考下
    2018-11-11
  • Python可視化程序調(diào)用流程解析

    Python可視化程序調(diào)用流程解析

    這篇文章主要為大家介紹了可視化Python程序調(diào)用流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • graphviz 最新安裝教程適用初學(xué)者

    graphviz 最新安裝教程適用初學(xué)者

    Graphviz 是一個(gè)自動(dòng)排版的作圖軟件,可以生成 png pdf 等格式,這篇文章主要介紹了graphviz 2022最新安裝教程適用初學(xué)者,需要的朋友可以參考下
    2023-02-02
  • 使用Python腳本來獲取Cisco設(shè)備信息的示例

    使用Python腳本來獲取Cisco設(shè)備信息的示例

    這篇文章主要介紹了編寫Python腳本來獲取Python腳本來獲取Cisco設(shè)備信息的教程,文中的示例是獲取一臺(tái)思科交換機(jī)的腳本,需要的朋友可以參考下
    2015-05-05
  • Alpine安裝Python3依賴出現(xiàn)的問題及解決方法

    Alpine安裝Python3依賴出現(xiàn)的問題及解決方法

    這篇文章主要介紹了Alpine安裝Python3依賴出現(xiàn)的問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • pytorch實(shí)現(xiàn)邏輯回歸

    pytorch實(shí)現(xiàn)邏輯回歸

    這篇文章主要為大家詳細(xì)介紹了pytorch實(shí)現(xiàn)邏輯回歸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 利用?Python?讓圖表動(dòng)起來

    利用?Python?讓圖表動(dòng)起來

    這篇文章主要給大家分享如何利用?Python?讓圖表動(dòng)起來,本文圍繞Python?讓圖表動(dòng)起來的話題舉例matplotlib動(dòng)畫功能的一個(gè)例子展開文章內(nèi)容,需要的朋友可以參考一下
    2021-10-10

最新評(píng)論