欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)帶圖形界面的炸金花游戲(升級(jí)版)

 更新時(shí)間:2022年12月07日 08:36:49   作者:Hann Yang  
詐金花又叫三張牌,是在全國(guó)廣泛流傳的一種民間多人紙牌游戲,它具有獨(dú)特的比牌規(guī)則。本文將通過Python語(yǔ)言實(shí)現(xiàn)升級(jí)版的帶圖形界面的詐金花游戲,需要的可以參考一下

舊版本的代碼請(qǐng)見上一篇博文: 

Python實(shí)現(xiàn)帶圖形界面的炸金花游戲

本文嘗試在舊版本的基礎(chǔ)上,“升級(jí)”以下幾個(gè)部分:

一、圖形的旋轉(zhuǎn),模擬四個(gè)玩家兩兩面對(duì)圍坐在牌桌上

旋轉(zhuǎn)方法 rotate(angle) 本文只用到轉(zhuǎn)動(dòng)角度這一個(gè)參數(shù),角度正值表示逆時(shí)針轉(zhuǎn)動(dòng);負(fù)值表示順時(shí)針轉(zhuǎn)動(dòng)。

method rotate in module PIL.Image:
rotate(angle, resample=0, expand=0, center=None, translate=None, fillcolor=None) method of PIL.Image.Image instance
    Returns a rotated copy of this image.  This method returns a copy of this image, rotated the given number of degrees counter clockwise around its centre.
    :param angle: In degrees counter clockwise.
    :param resample: An optional resampling filter.  This can be one of :py:data: `PIL.Image.NEAREST`  (use nearest neighbour), :py:data:`PIL.Image.BILINEAR` (linear interpolation in a 2x2 environment), or :py:data:`PIL.Image.BICUBIC`
       (cubic spline interpolation in a 4x4 environment).
       If omitted, or if the image has mode "1" or "P", it is set to :py:data: `PIL.Image.NEAREST`. See :ref:`concept-filters`.
    :param expand: Optional expansion flag.  If true, expands the output image to make it large enough to hold the entire rotated image.
       If false or omitted, make the output image the same size as the input image.  Note that the expand flag assumes rotation around the center and no translation.
    :param center: Optional center of rotation (a 2-tuple).  Origin is the upper left corner.  Default is the center of the image.
    :param translate: An optional post-rotate translation (a 2-tuple).
    :param fillcolor: An optional color for area outside the rotated image.
    :returns: An :py:class:`~PIL.Image.Image` object.

如不是正方形圖片,轉(zhuǎn)動(dòng)角度不是180度的話,就會(huì)被截掉一部分。效果如下: 

演示代碼:

import tkinter as tk
from PIL import Image,ImageTk
 
def load(i=0):
    img = Image.open("pokers.png").resize((375,150))
    box = img.rotate(90*i)
    res = ImageTk.PhotoImage(image=box)
    img.close()
    return res
 
if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('800x480')
    root.title('圖片旋轉(zhuǎn)')
    cv = tk.Canvas(root, width=1600, height=800, bg='darkgreen')
    cv.pack()
 
    png = [None]*4
    coord = ((i,j) for j in (120,345) for i in (200,600))
    for i,xy in enumerate(coord):
        png[i] = load(i)
        cv.create_image(xy, image=png[i])
        cv.create_text(xy[0],xy[1]+95, text=f'逆時(shí)針轉(zhuǎn)動(dòng){i*90}度',fill='white')
    
    root.mainloop()

為保存全圖在轉(zhuǎn)動(dòng)之前,設(shè)置一個(gè)正方形框 box = img.crop((0,0,375,375)).rotate(-90*i),順時(shí)針轉(zhuǎn)動(dòng)的效果如下:

演示代碼:

import tkinter as tk
from PIL import Image,ImageTk
 
def load(i=0):
    img = Image.open("pokers.png").resize((375,150))
    box = img.crop((0,0,375,375)).rotate(-90*i)
    res = ImageTk.PhotoImage(image=box)
    img.close()
    return res
 
if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('800x800')
    root.title('圖片旋轉(zhuǎn)')
    cv = tk.Canvas(root, width=1600, height=800, bg='darkgreen')
    cv.pack()
 
    png = []
    coord = ((i,j) for j in (200,600) for i in (200,600))
    for i,xy in enumerate(coord):
        png.append(load(i))
        cv.create_image(xy, image=png[i])
    
    root.mainloop()

然后再用crop()方法來截取出黑色背景除外的部分,就是所需的轉(zhuǎn)動(dòng)四個(gè)方向上的圖像;最后把這些圖片再次分割成一張張小紙牌,存入一個(gè)三維列表備用。 

二、增加變量,使得比大小游戲有累積輸贏過程

在玩家文本框后各添加一個(gè)文本框,動(dòng)態(tài)顯示每一局的輸贏情況;各玩家的值存放于全局變量Money列表中,主要代碼如下:

    ALL, ONE = 1000, 200 #初始值、單次輸贏值
    Money = [ALL]*4 #設(shè)置各方初始值
    ...
    ...
    cv.create_text(tx,ty, text=f'Player{x+1}', fill='white') #玩家1-4顯示文本框
    txt.append(cv.create_text(tx+60,ty, fill='gold',text=Money[x])) #顯示框
    ...
    ...
    Money[idx] += ONE*4 #每次贏ONE*3,多加自己的一份
    for i in range(4):
        Money[i] -= ONE #多加的在此扣減
        cv.itemconfig(txt[i], text=str(Money[i])) #修改各方的值
    cv.update()

三、界面增加下拉式菜單,菜單項(xiàng)調(diào)用的綁定函數(shù)

顯示效果見題圖左上角,主要代碼如下:

    btnCmd = '發(fā)牌',dealCards,'開牌',playCards,'洗牌',Shuffle
    Menu = tk.Menu(root)
    menu = tk.Menu(Menu, tearoff = False)
    for t,cmd in zip(btnCmd[::2],btnCmd[1::2]):
        menu.add_radiobutton(label = t, command = cmd)
    menu.add_separator() #菜單分割線
    menu.add_command(label = "退出", command = ExitApp)
    Menu.add_cascade(label="菜單",menu = menu)
    root.config(menu = Menu)

四、導(dǎo)入信息框庫(kù),增加提示信息框的使用

使用了2種信息框類型:提示showinfo()和確認(rèn)選擇askokcancel()

tkinter.messagebox庫(kù)共有8種信息框類型,其使用方法基本相同,只是顯示的圖標(biāo)有區(qū)別:

Help on module tkinter.messagebox in tkinter:
NAME
    tkinter.messagebox
FUNCTIONS
    askokcancel(title=None, message=None, **options)
        Ask if operation should proceed; return true if the answer is ok
    
    askquestion(title=None, message=None, **options)
        Ask a question
    
    askretrycancel(title=None, message=None, **options)
        Ask if operation should be retried; return true if the answer is yes
    
    askyesno(title=None, message=None, **options)
        Ask a question; return true if the answer is yes
    
    askyesnocancel(title=None, message=None, **options)
        Ask a question; return true if the answer is yes, None if cancelled.
    
    showerror(title=None, message=None, **options)
        Show an error message
    
    showinfo(title=None, message=None, **options)
        Show an info message
    
    showwarning(title=None, message=None, **options)
        Show a warning message
DATA
    ABORT = 'abort'
    ABORTRETRYIGNORE = 'abortretryignore'
    CANCEL = 'cancel'
    ERROR = 'error'
    IGNORE = 'ignore'
    INFO = 'info'
    NO = 'no'
    OK = 'ok'
    OKCANCEL = 'okcancel'
    QUESTION = 'question'
    RETRY = 'retry'
    RETRYCANCEL = 'retrycancel'
    WARNING = 'warning'
    YES = 'yes'
    YESNO = 'yesno'
    YESNOCANCEL = 'yesnocancel'

另:發(fā)牌、開牌、洗牌按鈕可否點(diǎn)擊,由兩個(gè)全局變量控制,當(dāng)不能使用時(shí)彈出提示信息框。但更好方式通常是設(shè)置按鈕的state狀態(tài),在 tk.DISABLED 和 tk.NORMAL 之間切換,用以下代碼:

if btn[0]['state'] == tk.DISABLED:
    btn[0]['state'] = tk.NORMAL
else:
    btn[0]['state'] = tk.DISABLED  #使得按鈕灰化,無法被按下
 
#或者在初始按鈕時(shí)使用:
tk.Button(root,text="點(diǎn)不了",command=test,width=10,state=tk.DISABLED)

“詐金花”完整源代碼

運(yùn)行結(jié)果:

到此這篇關(guān)于Python實(shí)現(xiàn)帶圖形界面的炸金花游戲(升級(jí)版)的文章就介紹到這了,更多相關(guān)Python炸金花內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python opencv minAreaRect 生成最小外接矩形的方法

    python opencv minAreaRect 生成最小外接矩形的方法

    這篇文章主要介紹了python opencv minAreaRect 生成最小外接矩形的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-07-07
  • python opencv捕獲攝像頭并顯示內(nèi)容的實(shí)現(xiàn)

    python opencv捕獲攝像頭并顯示內(nèi)容的實(shí)現(xiàn)

    這篇文章主要介紹了python opencv捕獲攝像頭并顯示內(nèi)容的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python的Flask框架中實(shí)現(xiàn)分頁(yè)功能的教程

    Python的Flask框架中實(shí)現(xiàn)分頁(yè)功能的教程

    這篇文章主要介紹了Python的Flask框架中實(shí)現(xiàn)分頁(yè)功能的教程,文中的示例基于一個(gè)博客來實(shí)現(xiàn),需要的朋友可以參考下
    2015-04-04
  • python的移位操作實(shí)現(xiàn)詳解

    python的移位操作實(shí)現(xiàn)詳解

    這篇文章主要介紹了ppython的移位操作實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 四個(gè)Python操作Excel的常用腳本分享

    四個(gè)Python操作Excel的常用腳本分享

    在數(shù)字化時(shí)代,很多人工作中經(jīng)常和excel打交道。本文和大家分享四個(gè)Python操作excel的腳本,讓你工作效率更高,需要的小伙伴可以參考一下
    2022-11-11
  • 詳解python深淺拷貝區(qū)別

    詳解python深淺拷貝區(qū)別

    在本篇文章里小編給大家整理了關(guān)于python深淺拷貝區(qū)別的相關(guān)知識(shí)點(diǎn)總結(jié),有興趣的朋友們可以參考下。
    2019-06-06
  • 詳解python實(shí)現(xiàn)簡(jiǎn)單區(qū)塊鏈結(jié)構(gòu)

    詳解python實(shí)現(xiàn)簡(jiǎn)單區(qū)塊鏈結(jié)構(gòu)

    區(qū)塊鏈?zhǔn)且环N數(shù)據(jù)結(jié)構(gòu),也是一個(gè)分布式數(shù)據(jù)庫(kù)。這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單區(qū)塊鏈結(jié)構(gòu)的相關(guān)知識(shí),需要的朋友可以參考下
    2021-04-04
  • 簡(jiǎn)單了解Java Netty Reactor三種線程模型

    簡(jiǎn)單了解Java Netty Reactor三種線程模型

    這篇文章主要介紹了簡(jiǎn)單了解Java Netty Reactor三種線程模型,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 詳解appium自動(dòng)化測(cè)試工具(monitor、uiautomatorviewer)

    詳解appium自動(dòng)化測(cè)試工具(monitor、uiautomatorviewer)

    這篇文章主要介紹了詳解appium自動(dòng)化測(cè)試工具(monitor、uiautomatorviewer),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 用Python做一個(gè)久坐提醒小助手的示例代碼

    用Python做一個(gè)久坐提醒小助手的示例代碼

    這篇文章主要介紹了用Python做一個(gè)久坐提醒小助手的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02

最新評(píng)論