用模块编程呢
时间: 2023-09-15 19:24:28 浏览: 24
好的,以下是使用模块编程的 Micro:bit 时钟代码:
`clock.py` 模块:
```python
from microbit import *
import images
# 获取当前时间
def current_time():
return (int(str(hour()).zfill(2)[:1]), int(str(hour()).zfill(2)[1:]),
int(str(minute()).zfill(2)[:1]), int(str(minute()).zfill(2)[1:]))
# 显示数字
def show_digit(digit):
display.show(images.DIGITS[digit])
# 显示时间
def show_time():
h1, h2, m1, m2 = current_time()
show_digit(h1)
sleep(500)
show_digit(h2)
sleep(500)
show_digit(m1)
sleep(500)
show_digit(m2)
sleep(500)
# 显示冒号
def show_colon():
display.show(images.COLON)
# 循环显示时间
def loop():
while True:
show_time()
show_colon()
```
`images.py` 模块:
```python
from microbit import *
# 数字字体
DIGITS = [
Image("00000:"
"00000:"
"00000:"
"00000:"
"00000"),
Image("00000:"
"00000:"
"00000:"
"00000:"
"11111"),
Image("00000:"
"00000:"
"00000:"
"11111:"
"11111"),
Image("00000:"
"00000:"
"11111:"
"11111:"
"11111"),
Image("00000:"
"11111:"
"11111:"
"11111:"
"11111"),
Image("00000:"
"11111:"
"11111:"
"11111:"
"00000"),
Image("11111:"
"11111:"
"11111:"
"11111:"
"00000"),
Image("11111:"
"11111:"
"11111:"
"00000:"
"00000"),
Image("11111:"
"11111:"
"00000:"
"00000:"
"00000"),
Image("11111:"
"00000:"
"00000:"
"00000:"
"00000")
]
# 冒号图像
COLON = Image("00000:00000:00900:00000:00000")
```
在主程序中,你可以使用 `import clock` 导入 `clock.py` 模块,然后调用 `clock.loop()` 函数开始循环显示时间。这样,你就可以将时钟和图像分别放在不同的模块中,让代码更加清晰和易于维护。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)