在实际投骰子博弈中,庄家先把三颗骰子放在有盖的器皿内摇晃。当各闲家下注完毕,庄家便打开器皿并派彩。因为最常见的赌注是买骰子点数的大小 编写一个简单的猜骰子点数的游戏,让程序随机输出一个骰子数,用户有两次竞猜机会。猜中提示用户 前面编写程序中的骰子为数字,比较单调。能不能动态模拟显示骰子的随机生成过程呢?编写一个程序,单击 上一个任务动态模拟显示骰子的随机生成的过程。修改上个程序,实现三个骰子同时投掷的过程,单击 掷骰子高手可以控制掷出骰子的点数。编程能否控制输出骰子的点数呢?当然可以,其实就是给程序添加一个后门程序。为上面的程序 我国民间掷骰子游戏以博弈点数猜大小为主,每次下注前,庄家先把三颗骰子放在有盖的器皿内摇晃,参与者下注猜骰子点数大小,下注结束后庄家打开器皿,计算三个骰子点数的和,小于等于10为小,大于10为大,猜中者获胜。编写一个人机对话的掷骰子游戏,玩者选择大或者小,计算机会根据已出现的大小的概率来选择大或小。单击 至此今天的案例就到此结束了,这个案例同样本身的逻辑并不复杂,主要还是对1. 2行代码实现掷骰子(数字版)
骰子(tóu zi)
,有些地方也叫色子(shǎi zi)
。是我国传统的游戏博弈道具,一般用于聚会娱乐时,笔者呢也是在工作的时候,才接触到这个游戏,未有败绩,嘿嘿。在生活中呢,我们最常见的骰子是六面骰,它是一颗正立方体,上面分别有一到六个孔(或数字)。请编写一个程序,使用2
行代码,随机输出一次骰子的投掷结果。运行效果如下图所示。
示例代码如下:import random print(f"你掷出的骰子点数为: {random.choice(range(1, 7))}")
2. 掷多个骰子(数字版)
(总点数为3至10称作小,11至18为大)
,下面就模拟投掷三颗骰子,运行效果如下图所示。
示例代码如下:import random point_list = [random.choice(range(1, 7)) for i in range(3)] msg = "骰子点数和为小" if 3 <= sum(point_list) <= 10 else "骰子点数和为大" print(f"你掷出的骰子点数为: {','.join(map(str, point_list))}=>{msg}")
3. 猜骰子游戏
恭喜你,猜对了,竞猜的点数为
,猜错提示用户运气不佳,猜错了!
。若两次竞猜均失败,则提示竞猜失败,竞猜的点数为
。运行效果如下图所示。
示例代码如下:import random if __name__ == '__main__': print("==========猜骰子游戏==========") print("*" * 29) num = 2 point = random.choice(range(1, 7)) # 生成骰子点数 for i in range(num): print(f"请猜猜骰子的点数,有{num}次机会哦!") input_point = int(input("输入点数:").strip()) if point == input_point: print(f"恭喜你,猜对了!竞猜的点数为: {point}") break print("运气不佳,猜错了!") num -= 1 else: print(f"竞猜失败,竞猜的点数为: {point}")
4. 掷单个骰子(图形版)
开始
按钮,实现骰子在点数1~点数6
的快速转换,最后停留在某一个点数上,运行效果如下图所示。
示例代码如下:from tkinter import * from tkinter.messagebox import * import random def call(): global image1 global count num = random.choice(range(1, 7)) # 随机产生骰子数量 img = "touzi/" + str(num) + "t.png" image1 = PhotoImage(file=img) Label(root, image=image1).grid(row=1, column=1) count += 1 if count >= 20: showwarning(title="掷骰子游戏", message="你骰子的点数为:" + str(num)) else: root.after(100, call) def start(): global count count = 0 call() if __name__ == '__main__': count = 1 root = Tk() # 创建根窗体 root.wm_attributes("-topmost", 1) # 置顶 root.title("掷骰子游戏") # 窗体标题 screenWidth = root.winfo_screenwidth() # 屏幕宽度 screenHeight = root.winfo_screenheight() # 屏幕高度 width = 330 # 窗体宽度 height = 180 # 窗体高度 x = (screenWidth - width) / 2 # 偏移量x y = (screenHeight - height) / 2 # 偏移量y root.geometry("%dx%d+%d+%d" % (width, height, x, y)) # 窗体居中显示 root.resizable(width=False, height=False) # 决定框体大小是否能够调整 msg = Label(root, text='请单击"开始"进行游戏').grid(row=0, column=0) image1 = PhotoImage(file="./touzi/6t.png") label = Label(root, image=image1).grid(row=1, column=1) bank = Label(root, text=" ").grid(row=2, column=1) start_button = Button(root, text="开始", command=start, width=8) start_button.grid(column=1, row=3) root.mainloop()
5. 掷多个骰子(图形版)
开始
按钮,实现3个骰子同时在点数1~点数6
的快速转换,最后停留在某一个点数上,运行效果如下图所示。
示例代码如下:from tkinter import * from tkinter.messagebox import * import random def call(): global image1 global image2 global image3 global count num1 = random.choice(range(1, 7)) num2 = random.choice(range(1, 7)) num3 = random.choice(range(1, 7)) img = "touzi/" + str(num1) + "t.png" image1 = PhotoImage(file=img) Label(root, image=image1).grid(row=1, column=0) img = "touzi/" + str(num2) + "t.png" image2 = PhotoImage(file=img) Label(root, image=image2).grid(row=1, column=1) img = "touzi/" + str(num3) + "t.png" image3 = PhotoImage(file=img) Label(root, image=image3).grid(row=1, column=2) count += 1 if count >= 20: showwarning(title="掷骰子游戏", message="你骰子的点数为:" + str(num1 + num2 + num3)) else: root.after(100, call) def start(): global count count = 0 call() if __name__ == '__main__': count = 1 root = Tk() # 创建根窗体 root.wm_attributes("-topmost", 1) # 置顶 root.title("掷骰子游戏") # 窗体标题 screenWidth = root.winfo_screenwidth() # 屏幕宽度 screenHeight = root.winfo_screenheight() # 屏幕高度 width = 330 # 窗体宽度 height = 180 # 窗体高度 x = (screenWidth - width) / 2 # 偏移量x y = (screenHeight - height) / 2 # 偏移量y root.geometry("%dx%d+%d+%d" % (width, height, x, y)) # 窗体居中显示 root.resizable(width=False, height=False) # 决定框体大小是否能够调整 msg = Label(root, text='请单击"开始"进行游戏').grid(row=0, column=0) image1 = PhotoImage(file="./touzi/6t.png") label1 = Label(root, image=image1).grid(row=1, column=0) image2 = PhotoImage(file="./touzi/6t.png") label2 = Label(root, image=image1).grid(row=1, column=1) image3 = PhotoImage(file="./touzi/6t.png") label3 = Label(root, image=image1).grid(row=1, column=2) bank = Label(root, text=" ").grid(row=2, column=1) start_button = Button(root, text="开始", command=start, width=8) start_button.grid(column=1, row=3) root.mainloop()
6. 掷骰子与后门程序
掷多个骰子(图形版)
添加一个后门程序,让程序按要求投出指定的点数,例如投出点数为4,6,3
。提示:在开始按钮旁边加一个按钮,设置边框为无,点击这个按钮后,再点击开始
按钮,设置投出骰子点数为4,6,3
如下图所示。
示例代码如下:from tkinter import * import random def call(): global image1 global image2 global image3 global count global flag num1 = random.choice(range(1, 7)) num2 = random.choice(range(1, 7)) num3 = random.choice(range(1, 7)) if flag == 0 and count == 19: num1 = 4 num2 = 6 num3 = 3 img = "touzi/" + str(num1) + "t.png" image1 = PhotoImage(file=img) Label(root, image=image1).grid(row=1, column=0) img = "touzi/" + str(num2) + "t.png" image2 = PhotoImage(file=img) Label(root, image=image2).grid(row=1, column=1) img = "touzi/" + str(num3) + "t.png" image3 = PhotoImage(file=img) Label(root, image=image3).grid(row=1, column=2) count += 1 if count < 20: root.after(100, call) else: flag = 1 def start(): global count count = 0 call() def gono(): global flag flag = 0 if __name__ == '__main__': count = 1 flag = 1 root = Tk() # 创建根窗体 root.wm_attributes("-topmost", 1) # 置顶 root.title("掷骰子游戏") # 窗体标题 screenWidth = root.winfo_screenwidth() # 屏幕宽度 screenHeight = root.winfo_screenheight() # 屏幕高度 width = 330 # 窗体宽度 height = 180 # 窗体高度 x = (screenWidth - width) / 2 # 偏移量x y = (screenHeight - height) / 2 # 偏移量y root.geometry("%dx%d+%d+%d" % (width, height, x, y)) # 窗体居中显示 root.resizable(width=False, height=False) # 决定框体大小是否能够调整 msg = Label(root, text='请单击"开始"进行游戏').grid(row=0, column=0) image1 = PhotoImage(file="./touzi/6t.png") label1 = Label(root, image=image1).grid(row=1, column=0) image2 = PhotoImage(file="./touzi/6t.png") label2 = Label(root, image=image1).grid(row=1, column=1) image3 = PhotoImage(file="./touzi/6t.png") label3 = Label(root, image=image1).grid(row=1, column=2) bank = Label(root, text=" ").grid(row=2, column=1) start_button = Button(root, text="开始", command=start, width=8) start_button.grid(column=1, row=3) Button(root, text=" ", command=gono, relief=FLAT, state="normal", width=8).grid(row=3, column=2) root.mainloop()
7. 人机对话: 掷骰子游戏
开始
按钮,开始掷骰子,根据三个骰子点数的和来判断玩家和电脑谁是赢家。运行效果如下动图所示。
示例代码如下:from tkinter import * import random def call(): global image1 global image2 global image3 global count global point_sum # 点数之和 num1 = random.choice(range(1, 7)) img = "touzi/" + str(num1) + "t.png" image1 = PhotoImage(file=img) Label(root, image=image1).grid(row=2, column=0) point_sum += num1 num2 = random.choice(range(1, 7)) img = "touzi/" + str(num2) + "t.png" image2 = PhotoImage(file=img) Label(root, image=image2).grid(row=2, column=1) point_sum += num2 num3 = random.choice(range(1, 7)) img = "touzi/" + str(num3) + "t.png" image3 = PhotoImage(file=img) Label(root, image=image3).grid(row=2, column=2) point_sum += num3 count += 1 if count < 20: root.after(100, call) else: judge() point_sum = 0 def judge(): global big global little global s_sel global y_sel global count global point_sum if point_sum >= 11: big += 1 if y_sel == "大": if s_sel == "小": Label(root, text="恭喜,你赢了电脑!").grid(row=3, column=2) else: Label(root, text="哦,你和电脑打平了!").grid(row=3, column=2) else: if s_sel == "大": Label(root, text="哦耶,电脑赢了!").grid(row=3, column=2) else: Label(root, text="哦,你和电脑都输了!").grid(row=3, column=2) else: little += 1 if y_sel == "大": if s_sel == "小": Label(root, text="哦耶,电脑赢了!").grid(row=3, column=2) else: Label(root, text="哦,你和电脑都输了!").grid(row=3, column=2) else: if s_sel == "大": Label(root, text="恭喜,你赢了电脑!").grid(row=3, column=2) else: Label(root, text="哦,你和电脑打平了!").grid(row=3, column=2) def start(): global count global y_sel count = 0 y_sel = option_value() Label(root, text="你选的是: " + y_sel).grid(column=0, row=3) call() def option_value(): global value global big global little global s_sel value = "大" if var.get() == 1 else "小" Label(root, text="你选的是: " + value).grid(row=3, column=0) s_sel = sys_value(big, little) return value def sys_value(value1, value2): if value1 <= value2: Label(root, text="电脑选的是: 大").grid(row=3, column=1) return "大" else: Label(root, text="电脑选的是: 小").grid(row=3, column=1) return "小" if __name__ == '__main__': count = 1 flag = 1 value = "" # 用来存储用户选择是大 还是小 point_sum = 0 # 用来记录点数之和 big = little = 0 y_sel = "" # 用户选择 s_sel = "" # 系统选择 root = Tk() # 创建根窗体 root.wm_attributes("-topmost", 1) # 置顶 root.title("掷骰子游戏") # 窗体标题 screenWidth = root.winfo_screenwidth() # 屏幕宽度 screenHeight = root.winfo_screenheight() # 屏幕高度 width = 350 # 窗体宽度 height = 240 # 窗体高度 x = (screenWidth - width) / 2 # 偏移量x y = (screenHeight - height) / 2 # 偏移量y root.geometry("%dx%d+%d+%d" % (width, height, x, y)) # 窗体居中显示 root.resizable(width=False, height=False) # 决定框体大小是否能够调整 msg = Label(root, text="选择骰子点数大小").grid(row=0, column=0) var = IntVar() # 选项按钮绑定的变量 var.set(1) # 默认选项是大 # 大选项按钮 rb_big = Radiobutton(root, text="大", value=1, variable=var, command=option_value).grid(row=1, column=0) # 小选项按钮 rb_small = Radiobutton(root, text="小", value=0, variable=var, command=option_value).grid(row=1, column=1) image1 = PhotoImage(file="./touzi/6t.png") label1 = Label(root, image=image1).grid(row=2, column=0) image2 = PhotoImage(file="./touzi/6t.png") label2 = Label(root, image=image1).grid(row=2, column=1) image3 = PhotoImage(file="./touzi/6t.png") label3 = Label(root, image=image1).grid(row=2, column=2) label4 = Label(root, text="你选的是: " + option_value()).grid(row=3, column=0) label5 = Label(root, text="电脑选的是: 大").grid(row=3, column=1) bank = Label(root, text="单击'开始'竞猜......").grid(row=4, column=0) Button(root, text="开始", command=start, width=8).grid(row=5, column=1) root.mainloop()
tkinter
的运用,然而笔者也是辛苦的整理了一下午,笔者至此吐槽两句,从今天开始不太想见到tkinter
了,界面布局不咋美观,处理起来还比较麻烦,以前笔者写前端的时候简直不要太享受~,后续如果读者使用tkinter
较多或者是对它比较感兴趣,可以去看一下Python GUI设计——tkinter菜鸟编程
这本书,写得还挺不错。最后笔者在这里声明,笔者写文章只是为了学习交流,以及让更多学习Python基础的读者少走一些弯路,节省时间,并不用做其他用途,如有侵权,联系笔者删除即可。编写不易,请大家手留余香~。
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算