用ChatGPT寫microbit python 程式 "跳動的愛心"

 



from microbit import *
import neopixel

# 設定NeoPixel的數量及連接的腳位
num_pixels = 25
np = neopixel.NeoPixel(pin0, num_pixels)

# 定義愛心形狀
heart = [
    "00000:00000:00000:00000:00000",
    "00000:09090:99999:99999:09990",
    "00000:99999:99999:99999:09990",
    "00009:99999:99999:99990:00900",
    "00000:09999:99999:99900:00000",
    "00000:00999:99990:99000:00000",
    "00000:00099:99900:90000:00000",
    "00000:00009:99000:00000:00000",
    "00000:00000:00000:00000:00000"
]

# 清除螢幕
display.clear()

# 愛心的顏色
heart_color = (255, 0, 0)

# 用來控制愛心跳動的變數
heartbeat = 0

# 這個迴圈會持續執行
while True:
    # 畫出愛心
    for y in range(len(heart)):
        for x in range(len(heart[y])):
            if heart[y][x] != "0":
                np[(4 - y) * 5 + x] = heart_color
            else:
                np[(4 - y) * 5 + x] = (0, 0, 0)
    np.show()

    # 愛心跳動效果
    heartbeat += 1
    if heartbeat % 10 == 0:
        heart_color = (255 - heart_color[0], 0, 0)  # 透過改變顏色來模擬跳動效果

    # 降低CPU的負荷
    sleep(50)


將程式copy到 microbit python





留言