首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python-使用tkinter实现的摇骰子小游戏

贴吧看到的一个求助题,大致需求是:3个人摇骰子,每人摇3次,点数之和最大的获胜,支持玩家名称输入。我觉得这个题目挺有意思的,做了个界面程序,欢迎大家交流指正~

1#!usr/bin/env python

2# -*- coding: utf-8 -*-

3

4# author: ***

5# date: 2020/06/10

6

7fromtkinterimport*

8importtkinterastk

9importrandom

10importtime

11fromthreadingimportThread

12

13root = Tk()

14root.geometry('620x660')

15root.title('摇骰子')

16sigpic = PhotoImage(file='signature.png')

17shake_cup = PhotoImage(file='touzi/box.png')

18crown = PhotoImage(file='touzi/win.png')

19readystate =3

20playing =False

21used_name = set()

22result_dict = dict()

23remain =3

24esv_A = StringVar()

25esv_B = StringVar()

26esv_C = StringVar()

27

28pi_list = list()

29foriinrange(1,7):

30pi = PhotoImage(file=r'touzi/t%s.png'% i)

31pi_list.append(pi)

32

33

34defset_name(cw, ew, nw):

35globalreadystate

36entryV = ew.get()

37ifentryV:

38ifentryVinused_name:

39default = nw['text']

40nw.config(text="名称已存在!")

41cw.config(state=tk.DISABLED)

42font_shake(nw, default)

43cw.config(state=tk.ACTIVE)

44else:

45used_name.add(entryV)

46nw.config(text=entryV)

47ew.config(state=tk.DISABLED)

48cw.config(state=tk.DISABLED)

49readystate -=1

50ifreadystate ==:

51foriin"ABC":

52eval('play_btn_%s.config(state=tk.ACTIVE, text="第一次机会")'% i)

53

54

55defthrow_touzi(pw, rw, nw):

56globalplaying

57globalremain

58ifpw['text']=="第一次机会":

59playing =True

60pbStack.remove(pw)

61forwidgetinpbStack:

62widget.config(state=tk.DISABLED)

63pw.config(state=tk.DISABLED, text='第二次机会')

64elifpw['text']=="第二次机会":

65pw.config(state=tk.DISABLED, text='第三次机会')

66else:

67playing =False

68remain -=1

69pw.config(state=tk.DISABLED, text='play')

70thread = Thread(target=change_img, args=[pw, rw, nw])

71thread.start()

72

73

74defchange_img(pw, rw, nw):

75result_number = random.randint(1,6)

76ranum_list = list()

77times =5

78whiletimes:

79ranum = random.randint(1,6)

80ifranumnotinranum_list:

81ranum_list.append(ranum)

82times = times -1

83foriinranum_list:

84time.sleep(0.3)

85throw_label.config(image=pi_list[i-1])

86time.sleep(0.3)

87throw_label.config(image=pi_list[result_number-1])

88time.sleep(0.5)

89ifrw['text'] =="结果":

90rw['text'] = str(result_number)

91else:

92rw['text'] = str(rw['text']) +"+%s"% result_number

93time.sleep(0.5)

94rw['text'] = eval(rw['text'])

95ifpw['text'] !="play":

96pw.config(state=tk.ACTIVE)

97ifplaying ==False:

98result_dict[nw['text']] = rw['text']

99forwidgetinpbStack:

100widget.config(state=tk.ACTIVE)

101ifnotremain:

102result_list = sorted(result_dict.items(), reverse=True, key=lambdart: rt[1])

103ifresult_list[][1] == result_list[1][1]:

104ifresult_list[1][1] == result_list[2][1]:

105throw_winner['text'] =">> 平局

106else:

107winner = result_list[][] +", "+ result_list[1][]

108throw_winner['text'] ="Winner: %s"% winner

109else:

110winner = result_list[][]

111throw_winner['text'] ="Winner: %s"% winner

112throw_label['image'] = crown

113reset_btn.config(state=tk.ACTIVE, text='重新开始', relief='raised')

114

115

116deffont_shake(nw, default):

117nw.config(foreground='red')

118foriinrange(5):

119ifi%2==:

120time.sleep(0.05)

121nw.config(anchor='n')

122else:

123time.sleep(0.05)

124nw.config(anchor='s')

125root.update()

126nw.config(anchor='c')

127root.update()

128time.sleep(0.5)

129nw.config(text=default, foreground='black')

130

131

132defrestart():

133globalreadystate, used_name, result_dict, remain, pbStack

134throw_label['image'] = shake_cup

135readystate =3

136used_name = set()

137result_dict = dict()

138remain =3

139pbStack =

140reset_btn.config(state=tk.DISABLED, relief='flat', text='')

141foriin"ABC":

142eval('name_%s.config(text="player %s")'% (i, i))

143eval('entry_%s.config(state=tk.NORMAL)'% i)

144eval('esv_%s.set("")'% i)

145eval('confirm_btn_%s.config(state=tk.ACTIVE)'% i)

146eval('result_%s.config(text="结果")'% i)

147throw_winner['text'] =""

148

149

150box_frame_A = Frame(root)

151box_frame_A.grid(column=1, ipadx=3)

152name_A = Label(box_frame_A, text='player A', height=2)

153name_A.pack()

154signature_A = Label(box_frame_A, image=sigpic)

155signature_A.pack()

156play_btn_A = Button(box_frame_A, text='play', command=lambda: throw_touzi(play_btn_A, result_A, name_A))

157play_btn_A.pack(side=BOTTOM, pady=3)

158result_A = Label(box_frame_A, text='结果')

159result_A.pack(side=BOTTOM)

160entry_A = Entry(box_frame_A, textvariable=esv_A)

161entry_A.pack(side=LEFT, padx=5)

162confirm_btn_A = Button(box_frame_A, text='确定', command=lambda: set_name(confirm_btn_A, entry_A, name_A))

163confirm_btn_A.pack(side=LEFT)

164

165center_frame = Frame(root)

166center_frame.grid(row=1, column=1, pady=20)

167throw_label = Label(center_frame, image=shake_cup)

168throw_label.pack()

169throw_winner = Label(center_frame)

170throw_winner.pack()

171reset_btn = Button(center_frame, state=tk.DISABLED, relief='flat', command=restart)

172reset_btn.pack()

173

174box_frame_B = Frame(root)

175box_frame_B.grid(row=3, padx=10)

176name_B = Label(box_frame_B, text='player B', height=2)

177name_B.pack()

178signature_B = Label(box_frame_B, image=sigpic)

179signature_B.pack()

180play_btn_B = Button(box_frame_B, text='play', command=lambda: throw_touzi(play_btn_B, result_B, name_B))

181play_btn_B.pack(side=BOTTOM)

182result_B = Label(box_frame_B, text='结果')

183result_B.pack(side=BOTTOM)

184entry_B = Entry(box_frame_B, textvariable=esv_B)

185entry_B.pack(side=LEFT, padx=5)

186confirm_btn_B = Button(box_frame_B, text='确定', command=lambda: set_name(confirm_btn_B, entry_B, name_B))

187confirm_btn_B.pack(side=LEFT)

188

189

190box_frame_C = Frame(root)

191box_frame_C.grid(row=3, column=2, padx=10)

192name_C = Label(box_frame_C, text='player C', height=2)

193name_C.pack()

194signature_C = Label(box_frame_C, image=sigpic)

195signature_C.pack()

196play_btn_C = Button(box_frame_C, text='play', command=lambda: throw_touzi(play_btn_C, result_C, name_C))

197play_btn_C.pack(side=BOTTOM)

198result_C = Label(box_frame_C, text='结果')

199result_C.pack(side=BOTTOM)

200entry_C = Entry(box_frame_C, textvariable=esv_C)

201entry_C.pack(side=LEFT, padx=5)

202confirm_btn_C = Button(box_frame_C, text='确定', command=lambda: set_name(confirm_btn_C, entry_C, name_C))

203confirm_btn_C.pack(side=LEFT)

204

205

206if__name__ =='__main__':

207pbStack =

208foriin"ABC":

209eval('play_btn_%s.config(state=tk.DISABLED)'% i)

210root.mainloop()

程序界面截图:

1.初始界面

2.就绪界面

3.结果界面

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20200702A0DCL500?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券