python實現(xiàn)轉(zhuǎn)盤效果 python實現(xiàn)輪盤抽獎游戲
更新時間:2019年01月22日 09:47:15 作者:CAPF_CS
這篇文章主要為大家詳細介紹了python實現(xiàn)轉(zhuǎn)盤效果,python實現(xiàn)輪盤抽獎游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python實現(xiàn)轉(zhuǎn)盤效果的具體代碼,供大家參考,具體內(nèi)容如下
#抽獎 面向?qū)ο蟀姹? import tkinter import time import threading class choujiang: #初始化魔術(shù)方法 def __init__(self): #準(zhǔn)備好界面 self.root = tkinter.Tk() self.root.title('lowB版轉(zhuǎn)盤') self.root.minsize(300, 300) # 聲明一個是否按下開始的變量 self.isloop = False self.newloop = False #調(diào)用設(shè)置界面的方法 self.setwindow() self.root.mainloop() #界面布局方法 def setwindow(self): #開始停止按鈕 self.btn_start = tkinter.Button(self.root, text = 'start/stop',command = self.newtask) self.btn_start.place(x=90, y=125, width=50, height=50) self.btn1 = tkinter.Button(self.root, text='趙', bg='red') self.btn1.place(x=20, y=20, width=50, height=50) self.btn2 = tkinter.Button(self.root, text='錢', bg='white') self.btn2.place(x=90, y=20, width=50, height=50) self.btn3 = tkinter.Button(self.root, text='孫', bg='white') self.btn3.place(x=160, y=20, width=50, height=50) self.btn4 = tkinter.Button(self.root, text='李', bg='white') self.btn4.place(x=230, y=20, width=50, height=50) self.btn5 = tkinter.Button(self.root, text='周', bg='white') self.btn5.place(x=230, y=90, width=50, height=50) self.btn6 = tkinter.Button(self.root, text='吳', bg='white') self.btn6.place(x=230, y=160, width=50, height=50) self.btn7 = tkinter.Button(self.root, text='鄭', bg='white') self.btn7.place(x=230, y=230, width=50, height=50) self.btn8 = tkinter.Button(self.root, text='王', bg='white') self.btn8.place(x=160, y=230, width=50, height=50) self.btn9 = tkinter.Button(self.root, text='馮', bg='white') self.btn9.place(x=90, y=230, width=50, height=50) self.btn10 = tkinter.Button(self.root, text='陳', bg='white') self.btn10.place(x=20, y=230, width=50, height=50) self.btn11 = tkinter.Button(self.root, text='褚', bg='white') self.btn11.place(x=20, y=160, width=50, height=50) self.btn12 = tkinter.Button(self.root, text='衛(wèi)', bg='white') self.btn12.place(x=20, y=90, width=50, height=50) # 將所有選項組成列表 self.girlfrends = [self.btn1,self.btn2,self.btn3,self.btn4,self.btn5,self.btn6,self.btn7,self.btn8,self.btn9,self.btn10,self.btn11,self.btn12] def rounds(self): # 判斷是否開始循環(huán) if self.isloop == True: return # 初始化計數(shù) 變量 i = 0 # 死循環(huán) while True: if self.newloop == True: self.newloop = False return # 延時操作 time.sleep(0.1) # 將所有的組件背景變?yōu)榘咨? for x in self.girlfrends: x['bg'] = 'white' # 將當(dāng)前數(shù)值對應(yīng)的組件變色 self.girlfrends[i]['bg'] = 'red' # 變量+1 i += 1 # 如果i大于最大索引直接歸零 if i >= len(self.girlfrends): i = 0 # 建立一個新線程的函數(shù) def newtask(self): if self.isloop == False: # 建立線程 t = threading.Thread(target = self.rounds) # 開啟線程運行 t.start() # 設(shè)置循環(huán)開始標(biāo)志 self.isloop = True elif self.isloop == True: self.isloop = False self.newloop = True c = choujiang()
小編再為大家分享一款python模擬輪盤抽獎的游戲
python3.x的版本測試中文的變量名
from random import random #獎項分布jxfb,本次轉(zhuǎn)盤讀數(shù)bclpds,中獎情況zjqk,本次戰(zhàn)況bczk, def lpd(jxfb): bclpds = random() for k, v in jxfb.items(): if v[0]<=bclpds<v[1]: return k jxfb = {'一等獎':(0, 0.08), '二等獎':(0.08, 0.3), '三等獎':(0.3, 1.0)} zjqk = dict() #模擬玩10000次,統(tǒng)計中獎情況 for i in range(10000): bczk = lpd(jxfb) zjqk[bczk] = zjqk.get(bczk, 0) + 1 for item in zjqk.items(): print(item)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中的Json模塊dumps、dump、loads、load函數(shù)用法詳解
這篇文章主要介紹了python中的Json模塊dumps、dump、loads、load函數(shù)用法講解,本文逐一介紹結(jié)合實例代碼給大家講解的非常詳細,需要的朋友可以參考下2022-11-11