import tkinter as tk
import random
def play_once():
global curmoney
if curmoney < 30:
result_label.config(text='金額不足,請結束遊戲!')
return
# 清除舊的元件(除了按鈕)
for widget in frame.winfo_children():
if widget not in (btn_play, btn_play10, btn_play100):
widget.destroy()
# 隨機產生三個數字
ns = [random.randint(0, 9) for _ in range(3)]
# 顯示數字
colors = ['#f90', '#09c', '#fc0']
for i, n in enumerate(ns):
label = tk.Label(frame, text=n, bg=colors[i], font=('Arial', 100, 'bold'))
label.grid(column=i, row=0)
# 判斷中幾個 7
c7 = ns.count(7)
money = [0, 50, 500, 5000][c7]
# 更新金額
curmoney = curmoney - 30 + money
# 顯示說明文字
tk.Label(frame, text='多少個7: ' + str(c7), background='#ccc').grid(column=0, row=1, columnspan=3)
tk.Label(frame, text='獎金:' + str(money), background='#ccc').grid(column=0, row=2, columnspan=3)
tk.Label(frame, text='可用金額:'+str(curmoney), font=('Arial', 20)).grid(column=0, row=3, columnspan=3)
def play_10_times(i=0):
if i < 10 and curmoney >= 30:
play_once()
frame.after(10, play_10_times, i+1)
elif curmoney < 30:
result_label.config(text='金額不足,中止連續遊戲')
def play_100_times(i=0):
if i < 100 and curmoney >= 30:
play_once()
frame.after(100, play_100_times, i+1)
elif curmoney < 30:
result_label.config(text='金額不足,中止連續遊戲')
def create_ui():
global frame, btn_play, btn_play10, btn_play100, result_label
root = tk.Tk()
root.title('777 拉霸機')
root.geometry('360x480')
frame = tk.Frame(root)
frame.pack(pady=20)
# 單次遊戲按鈕
btn_play = tk.Button(frame, text='PLAY', font=('Arial', 20, 'bold'), command=play_once)
btn_play.grid(column=1, row=8)
# 連續 10 次按鈕
btn_play10 = tk.Button(frame, text='PLAY10', font=('Arial', 20, 'bold'), command=play_10_times)
btn_play10.grid(column=1, row=9)
# 連續 100 次按鈕
btn_play100 = tk.Button(frame, text='PLAY100', font=('Arial', 20, 'bold'), command=play_100_times)
btn_play100.grid(column=1, row=10)
# 提示文字
result_label = tk.Label(frame, text='', font=('Arial', 14))
result_label.grid(column=0, row=11, columnspan=3)
root.mainloop()
curmoney = 1000
create_ui()
沒有留言:
張貼留言