使用Python 的指尖陀螺
先决条件: Python的海龟编程
在本文中,我们将使用Python海龟模块创建 Fidget Spinner。它是一个类似于绘图板的Python功能,它可以让我们命令一只乌龟在它上面画画!我们可以使用像turtle.forward()和turtle.right()这样的函数来移动海龟。
让我们看看逐步实现:
第 1 步:绘制指尖陀螺
在这段代码中,我们将初始化指尖陀螺的状态,顺时针和逆时针方向旋转它的角度,并制作制作指尖陀螺所需的彩色图形。
Python3
# initial state of spinner is null (stable)
state= {'turn':0 }
# Draw fidget spinner
def spin():
clear()
# Angle of fidget spinner
angle = state['turn']/10
right(angle)
# move the turtle forward by specified
# distance
forward(100)
# draw a dot with diameter 120 using colour
# red
dot(120, 'red')
# move the turtle backward by specified
# distance
back(100)
"second dot"
right(120)
forward(100)
dot(120, 'blue')
back(100)
"third dot"
right(120)
forward(100)
dot(120, 'green')
back(100)
right(120)
update()
Python3
# Animate fidget spinner
def animate():
if state['turn'] > 0:
state['turn'] -= 1
spin()
ontimer(animate, 20)
Python3
# Flick fidget spinner
def flick():
# acceleration of spinner
state['turn'] += 40
# setup window screen
setup(600, 400, 370, 0)
bgcolor("black")
tracer(False)
'''tracer brings back the fidget spinner into its initial state
after completing the rotation'''
# wing of fidget spinner
width(60)
color("orange")
# keyboard key for the rotation of spinner
onkey(flick, 'space')
listen()
animate()
done()
Python3
# import object from module turtle
from turtle import *
# initial state of spinner is null (stable)
state= {'turn':0 }
# Draw fidget spinner
def spin():
clear()
# Angle of fidget spinner
angle = state['turn']/10
# To rotate in clock wise we use right
# for Anticlockwise rotation we use left
right(angle)
# move the turtle forward by specified distance
forward(100)
# draw a dot with diameter 120 using colour red
dot(120, 'red')
# move the turtle backward by specified distance
back(100)
"second dot"
right(120)
forward(100)
dot(120, 'blue')
back(100)
"third dot"
right(120)
forward(100)
dot(120, 'green')
back(100)
right(120)
update()
# Animate fidget spinner
def animate():
if state['turn']>0:
state['turn']-=1
spin()
ontimer(animate, 20)
# Flick fidget spinner
def flick():
state['turn']+=40 #acceleration of spinner
# setup window screen
setup(600, 400, 370, 0)
bgcolor("black")
tracer(False)
# wing of fidget spinner
width(60)
color("orange")
# keyboard key for the rotation of spinner
onkey(flick,'space')
listen()
animate()
done()
输出:
第 2 步:为指尖陀螺设置动画
在这一步中,我们将调用一个函数animate() ,该函数将通过查看状态是否大于 0 来为指尖陀螺设置动画,然后从它的减少中减一并再次调用自旋函数。之后,安装一个计时器,它将在 20 毫秒后再次调用 animate函数
蟒蛇3
# Animate fidget spinner
def animate():
if state['turn'] > 0:
state['turn'] -= 1
spin()
ontimer(animate, 20)
第 3 步:移动指尖陀螺,设置窗口,并追踪微调器回到其原始位置
在这里,我们将定义轻弹函数,它将通过将指尖微调器的状态增加到 40 来移动它,我们还将设置一个窗口和它的背景颜色,我们将使用一个跟踪器,在完成它的指尖微调器后将其恢复到初始状态。旋转之后,我们将定义指尖陀螺的宽度和颜色,最后,我们将定义移动指尖陀螺的键。
蟒蛇3
# Flick fidget spinner
def flick():
# acceleration of spinner
state['turn'] += 40
# setup window screen
setup(600, 400, 370, 0)
bgcolor("black")
tracer(False)
'''tracer brings back the fidget spinner into its initial state
after completing the rotation'''
# wing of fidget spinner
width(60)
color("orange")
# keyboard key for the rotation of spinner
onkey(flick, 'space')
listen()
animate()
done()
下面是完整的实现:
蟒蛇3
# import object from module turtle
from turtle import *
# initial state of spinner is null (stable)
state= {'turn':0 }
# Draw fidget spinner
def spin():
clear()
# Angle of fidget spinner
angle = state['turn']/10
# To rotate in clock wise we use right
# for Anticlockwise rotation we use left
right(angle)
# move the turtle forward by specified distance
forward(100)
# draw a dot with diameter 120 using colour red
dot(120, 'red')
# move the turtle backward by specified distance
back(100)
"second dot"
right(120)
forward(100)
dot(120, 'blue')
back(100)
"third dot"
right(120)
forward(100)
dot(120, 'green')
back(100)
right(120)
update()
# Animate fidget spinner
def animate():
if state['turn']>0:
state['turn']-=1
spin()
ontimer(animate, 20)
# Flick fidget spinner
def flick():
state['turn']+=40 #acceleration of spinner
# setup window screen
setup(600, 400, 370, 0)
bgcolor("black")
tracer(False)
# wing of fidget spinner
width(60)
color("orange")
# keyboard key for the rotation of spinner
onkey(flick,'space')
listen()
animate()
done()
输出: