Python实现带图形界面的炸金花游戏(升级版)
作者:Hann 发布时间:2023-06-27 08:35:20
旧版本的代码请见上一篇博文:
Python实现带图形界面的炸金花游戏
本文尝试在旧版本的基础上,“升级”以下几个部分:
一、图形的旋转,模拟四个玩家两两面对围坐在牌桌上
旋转方法 rotate(angle) 本文只用到转动角度这一个参数,角度正值表示逆时针转动;负值表示顺时针转动。
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.
如不是正方形图片,转动角度不是180度的话,就会被截掉一部分。效果如下:
演示代码:
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('图片旋转')
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'逆时针转动{i*90}度',fill='white')
root.mainloop()
为保存全图在转动之前,设置一个正方形框 box = img.crop((0,0,375,375)).rotate(-90*i),顺时针转动的效果如下:
演示代码:
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('图片旋转')
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()方法来截取出黑色背景除外的部分,就是所需的转动四个方向上的图像;最后把这些图片再次分割成一张张小纸牌,存入一个三维列表备用。
二、增加变量,使得比大小游戏有累积输赢过程
在玩家文本框后各添加一个文本框,动态显示每一局的输赢情况;各玩家的值存放于全局变量Money列表中,主要代码如下:
ALL, ONE = 1000, 200 #初始值、单次输赢值
Money = [ALL]*4 #设置各方初始值
...
...
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()
三、界面增加下拉式菜单,菜单项调用的绑定函数
显示效果见题图左上角,主要代码如下:
btnCmd = '发牌',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)
四、导入信息框库,增加提示信息框的使用
使用了2种信息框类型:提示showinfo()和确认选择askokcancel()
tkinter.messagebox库共有8种信息框类型,其使用方法基本相同,只是显示的图标有区别:
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'
另:发牌、开牌、洗牌按钮可否点击,由两个全局变量控制,当不能使用时弹出提示信息框。但更好方式通常是设置按钮的state状态,在 tk.DISABLED 和 tk.NORMAL 之间切换,用以下代码:
if btn[0]['state'] == tk.DISABLED:
btn[0]['state'] = tk.NORMAL
else:
btn[0]['state'] = tk.DISABLED #使得按钮灰化,无法被按下
#或者在初始按钮时使用:
tk.Button(root,text="点不了",command=test,width=10,state=tk.DISABLED)
“诈金花”完整源代码
运行结果:
来源:https://blog.csdn.net/boysoft2002/article/details/128179253
猜你喜欢
- 目录目标为什么操作步骤工程截图运行效果目标在SpringBoot中集成内存数据库Derby.为什么像H2、hsqldb、derby、sqli
- 前言用python编程绘图,其实非常简单。中学生、大学生、研究生都能通过这10篇教程从入门到精通!快速绘制几种简单的柱状图。1垂直柱图(普通
- 1.包: package PaintBrush; /** * * @author lucifer */ public class Paint
- 一个网站的一个页面download.asp通过判断referer来确定是不是从他本站点过来的链接,使用这个功能我们可以用来防止下载盗链,当然
- Oracle中大文本数据类型Clob 长文本类型 (MySQL中不支持,使用的是text)Blob 二进
- 一. valid卷积的梯度我们分两种不同的情况讨论valid卷积的梯度:第一种情况,在已知卷积核的情况下,对未知张量求导(即对张量中每一个变
- MySQL短链接怎么设置1.查看mysql连接数语句命令:2.首先作为超级用户登录到MYSQL,注意必须是超级用户,否则后面会提示没有修改权
- 作为让高中生心脏骤停的四个字,对于高考之后的人来说可谓刻骨铭心,所以定义不再赘述,直接撸图,其标准方程分别为在Python中,绘制动图需要用
- 所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。我们永远不
- baiduclient.pyimport urllib.parseimport gzipimport jsonimport refrom h
- SPI是一种JDK提供的加载插件的灵活机制,分离了接口与实现,就拿常用的数据库驱动来说,我们只需要在spring系统中引入对应的数据库依赖包
- 为什么要引入线程池如果在程序中经常要用到线程,频繁的创建和销毁线程会浪费很多硬件资源,所以需要把线程和任务分离。线程可以反复利用,省去了重复
- 发现问题最近在将mysql升级到mysql 5.7后,进行一些group by 查询时,比如下面的SELECT *, count(id) a
- 字段是Python是字典中唯一的键-值类型,是Python中非常重要的数据结构,因其用哈希的方式存储数据,其复杂度为O(1),速度非常快。下
- python中注释在python中的注释一般分为单行注释、多行注释以及文档注释。注释描述在实际开发过程中,有效的代码注释不仅可以提升个人的工
- Exec 包我们可以使用官方的 os/exec 包来运行外部命令。当我们执行 shell 命令时,我们是在 G
- Burp Suite是什么Burp Suite 是用于攻击web 应用程序的集成平台。它包含了许多Burp工具,这些不同的burp工具通过协
- 网上有许关于固定表格的标题行的文章,但是既要固定标题行又要固定标题列的却几乎没有。现我写下如下代码以供大家参考:<html> &
- Part 1: 简介在PyTorch中,torch.cat()是一个被广泛使用的函数。它可以让我们在某个维度上把多个张量组合在一起。对于那些
- 伴随着自然语言技术和机器学习技术的发展,越来越多的有意思的自然语言小项目呈现在大家的眼前,聊天机器人就是其中最典型的应用,今天小编就带领大家