Python 入門指南 5.0
exercise4402.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 | # 從 Brython 引入 document 並以 doc 為別名
from browser import document as doc
# 從 Brython 引入 window
from browser import window
# 從 random 引入 randint
from random import randint
# 從 exercise2303 引入 Dice
from exercise2303 import Dice
# 計算秒數
t = 10
# 擲骰子動畫
def animate_dice(event):
d1 = Dice()
d2 = Dice()
total = d1.point + d2.point
def show_rolling():
doc["dice1"].text = f"{randint(1, 6)}"
doc["dice2"].text = f"{randint(1, 6)}"
doc["run"].disabled = True
doc["display"].text = f"擲骰子中"
global t
t -= 1
if t < 0:
window.clearInterval(timer_id)
doc["dice1"].text = f"{d1}"
doc["dice2"].text = f"{d2}"
doc["display"].text = f"擲出點數為 {total}"
t = 10
doc["run"].disabled = False
timer_id = window.setInterval(show_rolling, 600)
# 按鈕註冊事件
doc["run"].bind("click", animate_dice)
# 檔名: exercise4402.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 12 月
|