Python每日一练(12) 您所在的位置:网站首页 猜骰子点数的游戏怎么叫出来 Python每日一练(12)

Python每日一练(12)

2024-07-16 14:27| 来源: 网络整理| 查看: 265

目录 1. 2行代码实现掷骰子(数字版) 2. 掷多个骰子(数字版) 3. 猜骰子游戏 4. 掷单个骰子(图形版) 5. 掷多个骰子(图形版) 6. 掷骰子与后门程序 7. 人机对话: 掷骰子游戏

1. 2行代码实现掷骰子(数字版)

骰子(tóu zi),有些地方也叫色子(shǎi zi)。是我国传统的游戏博弈道具,一般用于聚会娱乐时,笔者呢也是在工作的时候,才接触到这个游戏,未有败绩,嘿嘿。在生活中呢,我们最常见的骰子是六面骰,它是一颗正立方体,上面分别有一到六个孔(或数字)。请编写一个程序,使用2行代码,随机输出一次骰子的投掷结果。运行效果如下图所示。 示例代码如下:

123import random print(f"你掷出的骰子点数为: {random.choice(range(1, 7))}")

2. 掷多个骰子(数字版)

在实际投骰子博弈中,庄家先把三颗骰子放在有盖的器皿内摇晃。当各闲家下注完毕,庄家便打开器皿并派彩。因为最常见的赌注是买骰子点数的大小(总点数为3至10称作小,11至18为大),下面就模拟投掷三颗骰子,运行效果如下图所示。 示例代码如下:

12345import random point_list = [random.choice(range(1, 7)) for i in range(3)] msg = "骰子点数和为小" if 3 = 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的快速转换,最后停留在某一个点数上,运行效果如下图所示。 示例代码如下:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859from 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 如下图所示。 示例代码如下:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071from 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. 人机对话: 掷骰子游戏

我国民间掷骰子游戏以博弈点数猜大小为主,每次下注前,庄家先把三颗骰子放在有盖的器皿内摇晃,参与者下注猜骰子点数大小,下注结束后庄家打开器皿,计算三个骰子点数的和,小于等于10为小,大于10为大,猜中者获胜。编写一个人机对话的掷骰子游戏,玩者选择大或者小,计算机会根据已出现的大小的概率来选择大或小。单击开始按钮,开始掷骰子,根据三个骰子点数的和来判断玩家和电脑谁是赢家。运行效果如下动图所示。 示例代码如下:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135from 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


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有