我正在努力使用python 中的海龜圖形庫制作骰子,但我被卡住了。下面是我必須制作的骰子的影像。

誰能給我一段骰子的代碼?非常感謝
uj5u.com熱心網友回復:
你可以做一個這樣的函式:
import turtle as tg
def dice(face,side=100,color='blue',width=2):
tg.color(color if face else 'white')
tg.width(width)
tg.setheading(0)
tg.pendown()
for _ in range(4):
tg.forward(side)
tg.left(90)
faces = { 0: [(1,1),(1,3),(1,5),(3,3),(5,1),(5,3),(5,5)],
1: [(3,3)],
2: [(1,1),(5,5)],
3: [(1,1),(3,3),(5,5)],
4: [(1,1),(1,5),(5,1),(5,5)],
5: [(1,1),(1,5),(3,3),(5,1),(5,5)],
6: [(1,1),(1,3),(1,5),(5,1),(5,3),(5,5)] }
x,y = tg.pos()
offset = side/15
dotSize = (side-2*offset)/7
tg.penup()
for dx,dy in faces[face]:
tg.goto(x dx*dotSize dotSize/2 offset,y dy*dotSize dotSize/2 offset)
tg.dot(dotSize*1.5,color if face else 'white')
tg.goto(x,y)
dice(n)從其左下角畫一個n點數為骰子的骰子。dice(0)通過將其涂成白色來擦除骰子。
演示:
tg.speed(0)
tg.penup()
tg.backward(175)
dice(1)
tg.forward(125)
dice(2)
tg.forward(125)
dice(3)
tg.backward(250)
tg.right(90)
tg.forward(125)
dice(4)
tg.forward(125)
dice(5)
tg.forward(125)
dice(6)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324085.html
