DDR爱好者之家 Design By 杰米

TKinter

Python 的 GUI 库非常多,之所以选择 Tkinter,一是最为简单,二是自带库,不需下载安装,随时使用,跨平台兼容性非常好,三则是从需求出发的,Python 在实际应用中极少用于开发复杂的桌面应用,毕竟,Python 的各种 GUI 工具包都“一般得很”,不具备优势。

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

#!usr/bin/env python
# -*- coding: utf-8 -*-

# author: ***
# date: 2020/06/10

from tkinter import *
import tkinter as tk
import random
import time
from threading import Thread

root = Tk()
root.geometry('620x660')
root.title('摇骰子')
sigpic = PhotoImage(file='signature.png')
shake_cup = PhotoImage(file='touzi/box.png')
crown = PhotoImage(file='touzi/win.png')
readystate = 3
playing = False
used_name = set()
result_dict = dict()
remain = 3
esv_A = StringVar()
esv_B = StringVar()
esv_C = StringVar()

pi_list = list()
for i in range(1, 7):
 pi = PhotoImage(file=r'touzi/t%s.png' % i)
 pi_list.append(pi)


def set_name(cw, ew, nw):
 global readystate
 entryV = ew.get()
 if entryV:
  if entryV in used_name:
   default = nw['text']
   nw.config(text="名称已存在!")
   cw.config(state=tk.DISABLED)
   font_shake(nw, default)
   cw.config(state=tk.ACTIVE)
  else:
   used_name.add(entryV)
   nw.config(text=entryV)
   ew.config(state=tk.DISABLED)
   cw.config(state=tk.DISABLED)
   readystate -= 1
 if readystate == 0:
  for i in "ABC":
   eval('play_btn_%s.config(state=tk.ACTIVE, text="第一次机会")' % i)


def throw_touzi(pw, rw, nw):
 global playing
 global remain
 if pw['text']=="第一次机会":
  playing = True
  pbStack.remove(pw)
  for widget in pbStack:
   widget.config(state=tk.DISABLED)
  pw.config(state=tk.DISABLED, text='第二次机会')
 elif pw['text']=="第二次机会":
  pw.config(state=tk.DISABLED, text='第三次机会')
 else:
  playing = False
  remain -= 1
  pw.config(state=tk.DISABLED, text='play')
 thread = Thread(target=change_img, args=[pw, rw, nw])
 thread.start()


def change_img(pw, rw, nw):
 result_number = random.randint(1, 6)
 ranum_list = list()
 times = 5
 while times:
  ranum = random.randint(1, 6)
  if ranum not in ranum_list:
   ranum_list.append(ranum)
   times = times - 1
 for i in ranum_list:
  time.sleep(0.3)
  throw_label.config(image=pi_list[i-1])
 time.sleep(0.3)
 throw_label.config(image=pi_list[result_number-1])
 time.sleep(0.5)
 if rw['text'] == "结果":
  rw['text'] = str(result_number)
 else:
  rw['text'] = str(rw['text']) + "+%s" % result_number
 time.sleep(0.5)
 rw['text'] = eval(rw['text'])
 if pw['text'] != "play":
  pw.config(state=tk.ACTIVE)
 if playing == False:
  result_dict[nw['text']] = rw['text']
  for widget in pbStack:
   widget.config(state=tk.ACTIVE)
 if not remain:
  result_list = sorted(result_dict.items(), reverse=True, key=lambda rt: rt[1])
  if result_list[0][1] == result_list[1][1]:
   if result_list[1][1] == result_list[2][1]:
    throw_winner['text'] = " 平局 <<"
   else:
    winner = result_list[0][0] + ", " + result_list[1][0]
    throw_winner['text'] = "Winner: %s" % winner
  else:
   winner = result_list[0][0]
   throw_winner['text'] = "Winner: %s" % winner
  throw_label['image'] = crown
  reset_btn.config(state=tk.ACTIVE, text='重新开始', relief='raised')


def font_shake(nw, default):
 nw.config(foreground='red')
 for i in range(5):
  if i%2 == 0:
   time.sleep(0.05)
   nw.config(anchor='n')
  else:
   time.sleep(0.05)
   nw.config(anchor='s')
  root.update()
 nw.config(anchor='c')
 root.update()
 time.sleep(0.5)
 nw.config(text=default, foreground='black')


def restart():
 global readystate, used_name, result_dict, remain, pbStack
 throw_label['image'] = shake_cup
 readystate = 3
 used_name = set()
 result_dict = dict()
 remain = 3
 pbStack = {play_btn_A, play_btn_B, play_btn_C}
 reset_btn.config(state=tk.DISABLED, relief='flat', text='')
 for i in "ABC":
  eval('name_%s.config(text="player %s")' % (i, i))
  eval('entry_%s.config(state=tk.NORMAL)' % i)
  eval('esv_%s.set("")' % i)
  eval('confirm_btn_%s.config(state=tk.ACTIVE)' % i)
  eval('result_%s.config(text="结果")' % i)
 throw_winner['text'] = ""


box_frame_A = Frame(root)
box_frame_A.grid(column=1, ipadx=3)
name_A = Label(box_frame_A, text='player A', height=2)
name_A.pack()
signature_A = Label(box_frame_A, image=sigpic)
signature_A.pack()
play_btn_A = Button(box_frame_A, text='play', command=lambda : throw_touzi(play_btn_A, result_A, name_A))
play_btn_A.pack(side=BOTTOM, pady=3)
result_A = Label(box_frame_A, text='结果')
result_A.pack(side=BOTTOM)
entry_A = Entry(box_frame_A, textvariable=esv_A)
entry_A.pack(side=LEFT, padx=5)
confirm_btn_A = Button(box_frame_A, text='确定', command=lambda : set_name(confirm_btn_A, entry_A, name_A))
confirm_btn_A.pack(side=LEFT)

center_frame = Frame(root)
center_frame.grid(row=1, column=1, pady=20)
throw_label = Label(center_frame, image=shake_cup)
throw_label.pack()
throw_winner = Label(center_frame)
throw_winner.pack()
reset_btn = Button(center_frame, state=tk.DISABLED, relief='flat', command=restart)
reset_btn.pack()

box_frame_B = Frame(root)
box_frame_B.grid(row=3, padx=10)
name_B = Label(box_frame_B, text='player B', height=2)
name_B.pack()
signature_B = Label(box_frame_B, image=sigpic)
signature_B.pack()
play_btn_B = Button(box_frame_B, text='play', command=lambda : throw_touzi(play_btn_B, result_B, name_B))
play_btn_B.pack(side=BOTTOM)
result_B = Label(box_frame_B, text='结果')
result_B.pack(side=BOTTOM)
entry_B = Entry(box_frame_B, textvariable=esv_B)
entry_B.pack(side=LEFT, padx=5)
confirm_btn_B = Button(box_frame_B, text='确定', command=lambda : set_name(confirm_btn_B, entry_B, name_B))
confirm_btn_B.pack(side=LEFT)


box_frame_C = Frame(root)
box_frame_C.grid(row=3, column=2, padx=10)
name_C = Label(box_frame_C, text='player C', height=2)
name_C.pack()
signature_C = Label(box_frame_C, image=sigpic)
signature_C.pack()
play_btn_C = Button(box_frame_C, text='play', command=lambda : throw_touzi(play_btn_C, result_C, name_C))
play_btn_C.pack(side=BOTTOM)
result_C = Label(box_frame_C, text='结果')
result_C.pack(side=BOTTOM)
entry_C = Entry(box_frame_C, textvariable=esv_C)
entry_C.pack(side=LEFT, padx=5)
confirm_btn_C = Button(box_frame_C, text='确定', command=lambda : set_name(confirm_btn_C, entry_C, name_C))
confirm_btn_C.pack(side=LEFT)

if __name__ == '__main__':
 pbStack = {play_btn_A, play_btn_B, play_btn_C}
 for i in "ABC":
  eval('play_btn_%s.config(state=tk.DISABLED)' % i)
 root.mainloop()

程序界面截图:

1.初始界面

Python使用tkinter实现摇骰子小游戏功能的代码

2.就绪界面

Python使用tkinter实现摇骰子小游戏功能的代码

3.结果界面

Python使用tkinter实现摇骰子小游戏功能的代码

总结

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。