天天看點

python寫抽獎轉盤_python實作轉盤效果 python實作輪盤抽獎遊戲

本文執行個體為大家分享了python實作轉盤效果的具體代碼,供大家參考,具體内容如下

#抽獎 面向對象版本

import tkinter

import time

import threading

class choujiang:

#初始化魔術方法

def __init__(self):

#準備好界面

self.root = tkinter.Tk()

self.root.title('lowB版轉盤')

self.root.minsize(300, 300)

# 聲明一個是否按下開始的變量

self.isloop = False

self.newloop = False

#調用設定界面的方法

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='衛', 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):

# 判斷是否開始循環

if self.isloop == True:

return

# 初始化計數 變量

i = 0

# 死循環

while True:

if self.newloop == True:

self.newloop = False

return

# 延時操作

time.sleep(0.1)

# 将所有的元件背景變為白色

for x in self.girlfrends:

x['bg'] = 'white'

# 将目前數值對應的元件變色

self.girlfrends[i]['bg'] = 'red'

# 變量+1

i += 1

# 如果i大于最大索引直接歸零

if i >= len(self.girlfrends):

i = 0

# 建立一個新線程的函數

def newtask(self):

if self.isloop == False:

# 建立線程

t = threading.Thread(target = self.rounds)

# 開啟線程運作

t.start()

# 設定循環開始标志

self.isloop = True

elif self.isloop == True:

self.isloop = False

self.newloop = True

c = choujiang()

小編再為大家分享一款python模拟輪盤抽獎的遊戲

python3.x的版本測試中文的變量名

from random import random

#輪盤賭lpd,獎項分布jxfb,本次轉盤讀數bclpds,中獎情況zjqk,本次戰況bczk,

def lpd(jxfb):

bclpds = random()

for k, v in jxfb.items():

if v[0]<=bclpds

return k

jxfb = {'一等獎':(0, 0.08),

'二等獎':(0.08, 0.3),

'三等獎':(0.3, 1.0)}

zjqk = dict()

#模拟玩10000次,統計中獎情況

for i in range(10000):

bczk = lpd(jxfb)

zjqk[bczk] = zjqk.get(bczk, 0) + 1

for item in zjqk.items():

print(item)

以上就是本文的全部内容,希望對大家的學習有所幫助,也希望大家多多支援腳本之家。