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

python学习网 2020-07-02 16:14:02

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

很多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
QQ群:1097524789

  1 #!usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 
  4 # author: ***
  5 # date: 2020/06/10
  6 
  7 from tkinter import *
  8 import tkinter as tk
  9 import random
 10 import time
 11 from threading import Thread
 12 
 13 root = Tk()
 14 root.geometry('620x660')
 15 root.title('摇骰子')
 16 sigpic = PhotoImage(file='signature.png')
 17 shake_cup = PhotoImage(file='touzi/box.png')
 18 crown = PhotoImage(file='touzi/win.png')
 19 readystate = 3
 20 playing = False
 21 used_name = set()
 22 result_dict = dict()
 23 remain = 3
 24 esv_A = StringVar()
 25 esv_B = StringVar()
 26 esv_C = StringVar()
 27 
 28 pi_list = list()
 29 for i in range(1, 7):
 30     pi = PhotoImage(file=r'touzi/t%s.png' % i)
 31     pi_list.append(pi)
 32 
 33 
 34 def set_name(cw, ew, nw):
 35     global readystate
 36     entryV = ew.get()
 37     if entryV:
 38         if entryV in used_name:
 39             default = nw['text']
 40             nw.config(text="名称已存在!")
 41             cw.config(state=tk.DISABLED)
 42             font_shake(nw, default)
 43             cw.config(state=tk.ACTIVE)
 44         else:
 45             used_name.add(entryV)
 46             nw.config(text=entryV)
 47             ew.config(state=tk.DISABLED)
 48             cw.config(state=tk.DISABLED)
 49             readystate -= 1
 50     if readystate == 0:
 51         for i in "ABC":
 52             eval('play_btn_%s.config(state=tk.ACTIVE, text="第一次机会")' % i)
 53 
 54 
 55 def throw_touzi(pw, rw, nw):
 56     global playing
 57     global remain
 58     if pw['text']=="第一次机会":
 59         playing = True
 60         pbStack.remove(pw)
 61         for widget in pbStack:
 62             widget.config(state=tk.DISABLED)
 63         pw.config(state=tk.DISABLED, text='第二次机会')
 64     elif pw['text']=="第二次机会":
 65         pw.config(state=tk.DISABLED, text='第三次机会')
 66     else:
 67         playing = False
 68         remain -= 1
 69         pw.config(state=tk.DISABLED, text='play')
 70     thread = Thread(target=change_img, args=[pw, rw, nw])
 71     thread.start()
 72 
 73 
 74 def change_img(pw, rw, nw):
 75     result_number = random.randint(1, 6)
 76     ranum_list = list()
 77     times = 5
 78     while times:
 79         ranum = random.randint(1, 6)
 80         if ranum not in ranum_list:
 81             ranum_list.append(ranum)
 82             times = times - 1
 83     for i in ranum_list:
 84         time.sleep(0.3)
 85         throw_label.config(image=pi_list[i-1])
 86     time.sleep(0.3)
 87     throw_label.config(image=pi_list[result_number-1])
 88     time.sleep(0.5)
 89     if rw['text'] == "结果":
 90         rw['text'] = str(result_number)
 91     else:
 92         rw['text'] = str(rw['text']) + "+%s" % result_number
 93     time.sleep(0.5)
 94     rw['text'] = eval(rw['text'])
 95     if pw['text'] != "play":
 96         pw.config(state=tk.ACTIVE)
 97     if playing == False:
 98         result_dict[nw['text']] = rw['text']
 99         for widget in pbStack:
100             widget.config(state=tk.ACTIVE)
101     if not remain:
102         result_list = sorted(result_dict.items(), reverse=True, key=lambda rt: rt[1])
103         if result_list[0][1] == result_list[1][1]:
104             if result_list[1][1] == result_list[2][1]:
105                 throw_winner['text'] = ">> 平局 <<"
106             else:
107                 winner = result_list[0][0] + ", " + result_list[1][0]
108                 throw_winner['text'] = "Winner: %s" % winner
109         else:
110             winner = result_list[0][0]
111             throw_winner['text'] = 
阅读(2352) 评论(0)