文章目录
- 准备
- 代码
- 效果
准备
一、根目录下放图片
代码
from turtle import *
import turtle
from random import randint
import sys
#屏幕初始化
screen = turtle.Screen()
screen.title("幸运大转盘 转转转~")
screen.setup(480,450)
screen.bgpic("转盘.png") #背景图片
screen.delay(0)
#制定点位置
list1 = ((8,30),(20,50),(0,120),(-20,50),(-8,30))
screen.addshape("myarrow",list1) #添加自定义形状
#绘制箭头
arrow = Turtle(shape = "myarrow")
arrow.color("purple") #定义箭头颜色
arrow.rt(0) #初始化箭头位置
rotateNumber = randint(50,100) #随机产生旋转次数50-100之间
angle = 45 #定义每次旋转45度
def rotate():global rotateNumber,anglescreen.onkeypress(None,"space") #按空格键启动转盘if rotateNumber>0: #rotateNumber非0时转动if rotateNumber<20:angle = rotateNumber #rotateNumber小于20旋转角度变小(减速)arrow.rt(angle) #向右旋转angle度rotateNumber = rotateNumber - 1screen.ontimer(rotate,20) #计时器(每隔20秒调用一次rotate)else: #rotateNumber为0停止转动rotateNumber = randint(50,100) #随机产生旋转次数50-100之间angle = 45 #定义每次旋转45度screen.onkeypress(rotate,"space") #按空格键启动转盘screen.onkeypress(rotate,"space")
screen.listen() #开启监听,将鼠标定位到画布
screen.mainloop()